Skip to content

Commit

Permalink
fix: error checking on resource read
Browse files Browse the repository at this point in the history
  • Loading branch information
MCBrandenburg committed Sep 18, 2020
1 parent fc0e73c commit f9c3249
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 150 deletions.
3 changes: 1 addition & 2 deletions fusionauth/resource_fusionauth_application_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func readApplication(data *schema.ResourceData, i interface{}) error {
if err != nil {
return err
}
buildResourceDataFromApplication(resp.Application, data)

return nil
return buildResourceDataFromApplication(resp.Application, data)
}

func updateApplication(data *schema.ResourceData, i interface{}) error {
Expand Down
79 changes: 62 additions & 17 deletions fusionauth/resource_fusionauth_application_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fusionauth

import (
"fmt"

"github.com/FusionAuth/go-client/pkg/fusionauth"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -117,11 +119,15 @@ func buildRequireable(key string, data *schema.ResourceData) fusionauth.Requirab
}
}

func buildResourceDataFromApplication(a fusionauth.Application, data *schema.ResourceData) {
_ = data.Set("tenant_id", a.TenantId)
_ = data.Set("authentication_token_configuration_enabled", a.AuthenticationTokenConfiguration.Enabled)
func buildResourceDataFromApplication(a fusionauth.Application, data *schema.ResourceData) error {
if err := data.Set("tenant_id", a.TenantId); err != nil {
return fmt.Errorf("application.tenant_id: %s", err.Error())
}
if err := data.Set("authentication_token_configuration_enabled", a.AuthenticationTokenConfiguration.Enabled); err != nil {
return fmt.Errorf("application.authentication_token_configuration_enabled: %s", err.Error())
}

_ = data.Set("clean_speak_configuration", []map[string]interface{}{
err := data.Set("clean_speak_configuration", []map[string]interface{}{
{
"application_ids": a.CleanSpeakConfiguration.ApplicationIds,
"username_moderation": []map[string]interface{}{
Expand All @@ -132,10 +138,15 @@ func buildResourceDataFromApplication(a fusionauth.Application, data *schema.Res
},
},
})
if err != nil {
return fmt.Errorf("application.clean_speak_configuration: %s", err.Error())
}

_ = data.Set("data", a.Data)
if err := data.Set("data", a.Data); err != nil {
return fmt.Errorf("application.data: %s", err.Error())
}

_ = data.Set("jwt_configuration", []map[string]interface{}{
err = data.Set("jwt_configuration", []map[string]interface{}{
{
"enabled": a.JwtConfiguration.Enabled,
"access_token_id": a.JwtConfiguration.AccessTokenKeyId,
Expand All @@ -144,26 +155,37 @@ func buildResourceDataFromApplication(a fusionauth.Application, data *schema.Res
"ttl_seconds": a.JwtConfiguration.TimeToLiveInSeconds,
},
})
if err != nil {
return fmt.Errorf("application.jwt_configuration: %s", err.Error())
}

_ = data.Set("lambda_configuration", []map[string]interface{}{
err = data.Set("lambda_configuration", []map[string]interface{}{
{
"access_token_populate_id": a.LambdaConfiguration.AccessTokenPopulateId,
"id_token_populate_id": a.LambdaConfiguration.IdTokenPopulateId,
"samlv2_populate_id": a.LambdaConfiguration.Samlv2PopulateId,
},
})
if err != nil {
return fmt.Errorf("application.lambda_configuration: %s", err.Error())
}

_ = data.Set("login_configuration", []map[string]interface{}{
err = data.Set("login_configuration", []map[string]interface{}{
{
"allow_token_refresh": a.LoginConfiguration.AllowTokenRefresh,
"generate_refresh_tokens": a.LoginConfiguration.GenerateRefreshTokens,
"require_authentication": a.LoginConfiguration.RequireAuthentication,
},
})
if err != nil {
return fmt.Errorf("application.login_configuration: %s", err.Error())
}

_ = data.Set("name", a.Name)
if err := data.Set("name", a.Name); err != nil {
return fmt.Errorf("application.name: %s", err.Error())
}

_ = data.Set("oauth_configuration", []map[string]interface{}{
err = data.Set("oauth_configuration", []map[string]interface{}{
{
"authorized_origin_urls": a.OauthConfiguration.AuthorizedOriginURLs,
"authorized_redirect_urls": a.OauthConfiguration.AuthorizedRedirectURLs,
Expand All @@ -176,10 +198,15 @@ func buildResourceDataFromApplication(a fusionauth.Application, data *schema.Res
"enabled_grants": a.OauthConfiguration.EnabledGrants,
},
})
if err != nil {
return fmt.Errorf("application.oauth_configuration: %s", err.Error())
}

_ = data.Set("passwordless_configuration_enabled", a.PasswordlessConfiguration.Enabled)
if err := data.Set("passwordless_configuration_enabled", a.PasswordlessConfiguration.Enabled); err != nil {
return fmt.Errorf("application.passwordless_configuration_enabled: %s", err.Error())
}

_ = data.Set("registration_configuration", []map[string]interface{}{
err = data.Set("registration_configuration", []map[string]interface{}{
{
"enabled": a.RegistrationConfiguration.Enabled,
"birth_date": []map[string]interface{}{
Expand Down Expand Up @@ -224,15 +251,21 @@ func buildResourceDataFromApplication(a fusionauth.Application, data *schema.Res
"form_id": a.RegistrationConfiguration.FormId,
},
})
if err != nil {
return fmt.Errorf("application.registration_configuration: %s", err.Error())
}

_ = data.Set("registration_delete_policy", []map[string]interface{}{
err = data.Set("registration_delete_policy", []map[string]interface{}{
{
"unverified_enabled": a.RegistrationDeletePolicy.Unverified.Enabled,
"unverified_number_of_days_to_retain": a.RegistrationDeletePolicy.Unverified.NumberOfDaysToRetain,
},
})
if err != nil {
return fmt.Errorf("application.registration_delete_policy: %s", err.Error())
}

_ = data.Set("samlv2_configuration", []map[string]interface{}{
err = data.Set("samlv2_configuration", []map[string]interface{}{
{
"enabled": a.Samlv2Configuration.Enabled,
"audience": a.Samlv2Configuration.Audience,
Expand All @@ -244,16 +277,28 @@ func buildResourceDataFromApplication(a fusionauth.Application, data *schema.Res
"xml_signature_canonicalization_method": a.Samlv2Configuration.XmlSignatureC14nMethod,
},
})
if err != nil {
return fmt.Errorf("application.samlv2_configuration: %s", err.Error())
}

_ = data.Set("verification_email_template_id", a.VerificationEmailTemplateId)
_ = data.Set("verify_registration", a.VerifyRegistration)
if err := data.Set("verification_email_template_id", a.VerificationEmailTemplateId); err != nil {
return fmt.Errorf("application.verification_email_template_id: %s", err.Error())
}
if err := data.Set("verify_registration", a.VerifyRegistration); err != nil {
return fmt.Errorf("application.verify_registration: %s", err.Error())
}

_ = data.Set("email_configuration", []map[string]interface{}{
err = data.Set("email_configuration", []map[string]interface{}{
{
"email_verification_template_id": a.EmailConfiguration.EmailVerificationEmailTemplateId,
"forgot_password_template_id": a.EmailConfiguration.ForgotPasswordEmailTemplateId,
"passwordless_email_template_id": a.EmailConfiguration.PasswordlessEmailTemplateId,
"set_password_email_template_id": a.EmailConfiguration.SetPasswordEmailTemplateId,
},
})
if err != nil {
return fmt.Errorf("application.email_configuration: %s", err.Error())
}

return nil
}
40 changes: 30 additions & 10 deletions fusionauth/resource_fusionauth_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,36 @@ func readEmail(data *schema.ResourceData, i interface{}) error {
}

t := resp.EmailTemplate
_ = data.Set("default_from_name", t.DefaultFromName)
_ = data.Set("default_html_template", t.DefaultHtmlTemplate)
_ = data.Set("default_subject", t.DefaultSubject)
_ = data.Set("default_text_template", t.DefaultTextTemplate)
_ = data.Set("from_email", t.FromEmail)
_ = data.Set("localized_from_names", t.LocalizedFromNames)
_ = data.Set("localized_html_templates", t.LocalizedHtmlTemplates)
_ = data.Set("localized_subjects", t.LocalizedSubjects)
_ = data.Set("localized_text_templates", t.LocalizedTextTemplates)
_ = data.Set("name", t.Name)
if err := data.Set("default_from_name", t.DefaultFromName); err != nil {
return fmt.Errorf("email.default_from_name: %s", err.Error())
}
if err := data.Set("default_html_template", t.DefaultHtmlTemplate); err != nil {
return fmt.Errorf("email.default_html_template: %s", err.Error())
}
if err := data.Set("default_subject", t.DefaultSubject); err != nil {
return fmt.Errorf("email.default_subject: %s", err.Error())
}
if err := data.Set("default_text_template", t.DefaultTextTemplate); err != nil {
return fmt.Errorf("email.default_text_template: %s", err.Error())
}
if err := data.Set("from_email", t.FromEmail); err != nil {
return fmt.Errorf("email.from_email: %s", err.Error())
}
if err := data.Set("localized_from_names", t.LocalizedFromNames); err != nil {
return fmt.Errorf("email.localized_from_names: %s", err.Error())
}
if err := data.Set("localized_html_templates", t.LocalizedHtmlTemplates); err != nil {
return fmt.Errorf("email.localized_html_templates: %s", err.Error())
}
if err := data.Set("localized_subjects", t.LocalizedSubjects); err != nil {
return fmt.Errorf("email.localized_subjects: %s", err.Error())
}
if err := data.Set("localized_text_templates", t.LocalizedTextTemplates); err != nil {
return fmt.Errorf("email.localized_text_templates: %s", err.Error())
}
if err := data.Set("name", t.Name); err != nil {
return fmt.Errorf("email.name: %s", err.Error())
}

return nil
}
Expand Down
16 changes: 12 additions & 4 deletions fusionauth/resource_fusionauth_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ func readGroup(data *schema.ResourceData, i interface{}) error {
}

t := resp.Group
_ = data.Set("name", t.Name)
_ = data.Set("tenant_id", t.TenantId)
_ = data.Set("data", t.Data)
if err := data.Set("name", t.Name); err != nil {
return fmt.Errorf("group.name: %s", err.Error())
}
if err := data.Set("tenant_id", t.TenantId); err != nil {
return fmt.Errorf("group.tenant_id: %s", err.Error())
}
if err := data.Set("data", t.Data); err != nil {
return fmt.Errorf("group.data: %s", err.Error())
}

var s []string

Expand All @@ -104,7 +110,9 @@ func readGroup(data *schema.ResourceData, i interface{}) error {
s = append(s, t.Roles[i][j].Id)
}
}
_ = data.Set("role_ids", s)
if err := data.Set("role_ids", s); err != nil {
return fmt.Errorf("group.role_ids: %s", err.Error())
}
return nil
}

Expand Down
39 changes: 28 additions & 11 deletions fusionauth/resource_fusionauth_idp_google.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fusionauth

import (
"encoding/json"
"fmt"

"github.com/FusionAuth/go-client/pkg/fusionauth"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -200,18 +201,31 @@ func readIDPGoogle(data *schema.ResourceData, i interface{}) error {
var ipb GoogleIdentityProviderBody
_ = json.Unmarshal(b, &ipb)

buildResourceFromIDPGoogle(ipb.IdentityProvider, data)
return nil
return buildResourceFromIDPGoogle(ipb.IdentityProvider, data)
}

func buildResourceFromIDPGoogle(o fusionauth.GoogleIdentityProvider, data *schema.ResourceData) {
_ = data.Set("button_text", o.ButtonText)
_ = data.Set("debug", o.Debug)
_ = data.Set("enabled", o.Enabled)
_ = data.Set("lambda_reconcile_id", o.LambdaConfiguration.ReconcileId)
_ = data.Set("client_id", o.ClientId)
_ = data.Set("client_secret", o.ClientSecret)
_ = data.Set("scope", o.Scope)
func buildResourceFromIDPGoogle(o fusionauth.GoogleIdentityProvider, data *schema.ResourceData) error {
if err := data.Set("button_text", o.ButtonText); err != nil {
return fmt.Errorf("idpGoogle.button_text: %s", err.Error())
}
if err := data.Set("debug", o.Debug); err != nil {
return fmt.Errorf("idpGoogle.debug: %s", err.Error())
}
if err := data.Set("enabled", o.Enabled); err != nil {
return fmt.Errorf("idpGoogle.enabled: %s", err.Error())
}
if err := data.Set("lambda_reconcile_id", o.LambdaConfiguration.ReconcileId); err != nil {
return fmt.Errorf("idpGoogle.lambda_reconcile_id: %s", err.Error())
}
if err := data.Set("client_id", o.ClientId); err != nil {
return fmt.Errorf("idpGoogle.client_id: %s", err.Error())
}
if err := data.Set("client_secret", o.ClientSecret); err != nil {
return fmt.Errorf("idpGoogle.client_secret: %s", err.Error())
}
if err := data.Set("scope", o.Scope); err != nil {
return fmt.Errorf("idpGoogle.scope: %s", err.Error())
}

// Since this is coming down as an interface and would end up being map[string]interface{}
// with one of the values being map[string]interface{}
Expand All @@ -232,7 +246,10 @@ func buildResourceFromIDPGoogle(o fusionauth.GoogleIdentityProvider, data *schem
"scope": v.Scope,
})
}
_ = data.Set("application_configuration", ac)
if err := data.Set("application_configuration", ac); err != nil {
return fmt.Errorf("idpGoogle.application_configuration: %s", err.Error())
}
return nil
}

func updateIDPGoogle(data *schema.ResourceData, i interface{}) error {
Expand Down
Loading

0 comments on commit f9c3249

Please sign in to comment.