Skip to content

Commit

Permalink
Refactor auth0 guardian resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jul 6, 2022
1 parent 81b2151 commit 8100199
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 60 deletions.
85 changes: 25 additions & 60 deletions auth0/resource_auth0_guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,12 @@ func createGuardian(ctx context.Context, d *schema.ResourceData, m interface{})
func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

multiFactorPolicies, err := api.Guardian.MultiFactor.Policy()
flattenedPolicy, err := flattenMultiFactorPolicy(api)
if err != nil {
return diag.FromErr(err)
}

result := multierror.Append(d.Set("policy", "never"))
if len(*multiFactorPolicies) > 0 {
result = multierror.Append(result, d.Set("policy", (*multiFactorPolicies)[0]))
}
result := multierror.Append(d.Set("policy", flattenedPolicy))

multiFactorList, err := api.Guardian.MultiFactor.List()
if err != nil {
Expand Down Expand Up @@ -347,39 +344,18 @@ func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) di
func updateGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

if err := updatePolicy(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateEmailFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateOTPFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateRecoveryCodeFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updatePhoneFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateWebAuthnRoaming(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateWebAuthnPlatform(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateDUO(d, api); err != nil {
return diag.FromErr(err)
}

if err := updatePush(d, api); err != nil {
result := multierror.Append(
updatePolicy(d, api),
updateEmailFactor(d, api),
updateOTPFactor(d, api),
updateRecoveryCodeFactor(d, api),
updatePhoneFactor(d, api),
updateWebAuthnRoaming(d, api),
updateWebAuthnPlatform(d, api),
updateDUO(d, api),
updatePush(d, api),
)
if err := result.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}

Expand All @@ -389,28 +365,17 @@ func updateGuardian(ctx context.Context, d *schema.ResourceData, m interface{})
func deleteGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

if err := api.Guardian.MultiFactor.Phone.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.Email.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.OTP.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.RecoveryCode.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.WebAuthnRoaming.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.WebAuthnPlatform.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.DUO.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.Push.Enable(false); err != nil {
result := multierror.Append(
api.Guardian.MultiFactor.Phone.Enable(false),
api.Guardian.MultiFactor.Email.Enable(false),
api.Guardian.MultiFactor.OTP.Enable(false),
api.Guardian.MultiFactor.RecoveryCode.Enable(false),
api.Guardian.MultiFactor.WebAuthnRoaming.Enable(false),
api.Guardian.MultiFactor.WebAuthnPlatform.Enable(false),
api.Guardian.MultiFactor.DUO.Enable(false),
api.Guardian.MultiFactor.Push.Enable(false),
)
if err := result.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}

Expand Down
14 changes: 14 additions & 0 deletions auth0/structure_auth0_guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func flattenMultiFactorPolicy(api *management.Management) (string, error) {
multiFactorPolicies, err := api.Guardian.MultiFactor.Policy()
if err != nil {
return "", err
}

flattenedPolicy := "never"
if len(*multiFactorPolicies) > 0 {
flattenedPolicy = (*multiFactorPolicies)[0]
}

return flattenedPolicy, nil
}

func flattenPhone(api *management.Management) ([]interface{}, error) {
phoneMessageTypes, err := api.Guardian.MultiFactor.Phone.MessageTypes()
if err != nil {
Expand Down

0 comments on commit 8100199

Please sign in to comment.