-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DXCDT-482: Fix deleting the universal login template within auth0_branding #695
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,17 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
|
||
"github.com/auth0/terraform-provider-auth0/internal/config" | ||
"github.com/auth0/terraform-provider-auth0/internal/value" | ||
) | ||
|
||
var errNoCustomDomain = fmt.Errorf( | ||
"managing the Universal Login body through the 'auth0_branding' resource requires at least one custom domain " + | ||
"to be configured for the tenant.\n\nUse the 'auth0_custom_domain' resource to set one up", | ||
) | ||
|
||
// NewResource will return a new auth0_branding resource. | ||
func NewResource() *schema.Resource { | ||
return &schema.Resource{ | ||
|
@@ -32,6 +38,7 @@ func NewResource() *schema.Resource { | |
"colors": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Description: "Configuration settings for colors for branding.", | ||
Elem: &schema.Resource{ | ||
|
@@ -66,6 +73,7 @@ func NewResource() *schema.Resource { | |
"font": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marking this now as computed in order to not require any custom logic within the read func that only reads the font details if this is present within the config as it goes against best practices. |
||
MaxItems: 1, | ||
Description: "Configuration settings to customize the font.", | ||
Elem: &schema.Resource{ | ||
|
@@ -87,10 +95,10 @@ func NewResource() *schema.Resource { | |
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"body": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "The body of login pages.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marking this as required to prevent instances where the |
||
ValidateFunc: validation.StringIsNotEmpty, | ||
Description: "The html template for the New Universal Login Experience.", | ||
}, | ||
}, | ||
}, | ||
|
@@ -115,18 +123,12 @@ func readBranding(_ context.Context, d *schema.ResourceData, m interface{}) diag | |
result := multierror.Append( | ||
d.Set("favicon_url", branding.GetFaviconURL()), | ||
d.Set("logo_url", branding.GetLogoURL()), | ||
d.Set("colors", flattenBrandingColors(branding.GetColors())), | ||
d.Set("font", flattenBrandingFont(branding.GetFont())), | ||
d.Set("universal_login", nil), | ||
) | ||
if _, ok := d.GetOk("colors"); ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a very weird custom logic we had within the read func that was only assigning the API data if the config blocks were present. |
||
result = multierror.Append(result, d.Set("colors", flattenBrandingColors(branding.GetColors()))) | ||
} | ||
if _, ok := d.GetOk("font"); ok { | ||
result = multierror.Append(result, d.Set("font", flattenBrandingFont(branding.GetFont()))) | ||
} | ||
if _, ok := d.GetOk("universal_login"); ok { | ||
if err := checkForCustomDomains(api); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := checkForCustomDomains(api); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're only fetching template data if there's a custom domain set. |
||
brandingUniversalLogin, err := flattenBrandingUniversalLogin(api) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
|
@@ -147,6 +149,19 @@ func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{}) | |
} | ||
} | ||
|
||
oldUL, newUL := d.GetChange("universal_login") | ||
oldUniversalLogin := oldUL.([]interface{}) | ||
newUniversalLogin := newUL.([]interface{}) | ||
|
||
// This indicates that a removal of the block happened, and we need to delete the template. | ||
if len(newUniversalLogin) == 0 && len(oldUniversalLogin) != 0 { | ||
if err := api.Branding.DeleteUniversalLogin(); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return readBranding(ctx, d, m) | ||
} | ||
Comment on lines
+152
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the part that actually fixes the GitHub issue around the deletion of the template. |
||
|
||
if universalLogin := expandBrandingUniversalLogin(d.GetRawConfig()); universalLogin.GetBody() != "" { | ||
if err := checkForCustomDomains(api); err != nil { | ||
return diag.FromErr(err) | ||
|
@@ -160,44 +175,31 @@ func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{}) | |
return readBranding(ctx, d, m) | ||
} | ||
|
||
func deleteBranding(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
func deleteBranding(_ context.Context, _ *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
api := m.(*config.Config).GetAPI() | ||
|
||
if _, ok := d.GetOk("universal_login"); !ok { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err := checkForCustomDomains(api); err != nil { | ||
d.SetId("") | ||
return diag.Diagnostics{ | ||
{ | ||
Severity: diag.Warning, | ||
Summary: "No custom domains configured", | ||
Detail: "Failed to properly destroy the 'auth0_branding' resource " + | ||
"because no custom domains are available on the tenant.", | ||
AttributePath: cty.Path{cty.GetAttrStep{Name: "universal_login"}}, | ||
}, | ||
if err == errNoCustomDomain { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The delete func was also improved to directly allow the deletion of the resource if there are no custom domains set. |
||
return nil | ||
} | ||
|
||
return diag.FromErr(err) | ||
} | ||
|
||
if err := api.Branding.DeleteUniversalLogin(); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func expandBranding(config cty.Value) *management.Branding { | ||
branding := &management.Branding{ | ||
return &management.Branding{ | ||
FaviconURL: value.String(config.GetAttr("favicon_url")), | ||
LogoURL: value.String(config.GetAttr("logo_url")), | ||
Colors: expandBrandingColors(config.GetAttr("colors")), | ||
Font: expandBrandingFont(config.GetAttr("font")), | ||
} | ||
|
||
return branding | ||
} | ||
|
||
func expandBrandingColors(config cty.Value) *management.BrandingColors { | ||
|
@@ -267,10 +269,6 @@ func flattenBrandingUniversalLogin(api *management.Management) ([]interface{}, e | |
return nil, err | ||
} | ||
|
||
if universalLogin == nil { | ||
return nil, nil | ||
} | ||
Comment on lines
-270
to
-272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was unnecessary. |
||
|
||
flattenedUniversalLogin := []interface{}{ | ||
map[string]interface{}{ | ||
"body": universalLogin.GetBody(), | ||
|
@@ -284,6 +282,7 @@ func flattenBrandingFont(brandingFont *management.BrandingFont) []interface{} { | |
if brandingFont == nil { | ||
return nil | ||
} | ||
|
||
return []interface{}{ | ||
map[string]interface{}{ | ||
"url": brandingFont.GetURL(), | ||
|
@@ -298,11 +297,7 @@ func checkForCustomDomains(api *management.Management) error { | |
} | ||
|
||
if len(customDomains) < 1 { | ||
return fmt.Errorf( | ||
"managing the universal login body through the 'auth0_branding' resource requires at least " + | ||
"one custom domain to be configured for the tenant.\n\n" + | ||
"Use the 'auth0_custom_domain' resource to set one up.", | ||
) | ||
return errNoCustomDomain | ||
} | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking this now as computed in order to not require any custom logic within the read func that only reads the colors details if this is present within the config as it goes against best practices.