Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #438 from nekootoko3/fix-read-branding
Browse files Browse the repository at this point in the history
Fix reading auth0_branding when enable_custom_domain_in_emails is true
  • Loading branch information
sergiught authored Jan 20, 2022
2 parents b0b1c5f + 99b263f commit fff6b91
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.26.1

BUG FIXES:

* resource/auth0_branding: Fix reading auth0_branding when enable_custom_domain_in_emails flag is true ([#438](https://github.com/alexkappa/terraform-provider-auth0/pull/438))

## 0.26.0

ENHANCEMENTS:
Expand Down
74 changes: 42 additions & 32 deletions auth0/resource_auth0_branding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (

func newBranding() *schema.Resource {
return &schema.Resource{

Create: createBranding,
Read: readBranding,
Update: updateBranding,
Delete: deleteBranding,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"colors": {
Type: schema.TypeList,
Expand Down Expand Up @@ -89,8 +87,7 @@ func createBranding(d *schema.ResourceData, m interface{}) error {

func readBranding(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
b, err := api.Branding.Read()

branding, err := api.Branding.Read()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -101,56 +98,53 @@ func readBranding(d *schema.ResourceData, m interface{}) error {
return err
}

d.Set("favicon_url", b.FaviconURL)
d.Set("logo_url", b.LogoURL)
d.Set("colors", flattenBrandingColors(b.Colors))
d.Set("font", flattenBrandingFont(b.Font))
d.Set("favicon_url", branding.FaviconURL)
d.Set("logo_url", branding.LogoURL)
d.Set("colors", flattenBrandingColors(branding.Colors))
d.Set("font", flattenBrandingFont(branding.Font))

t, err := api.Tenant.Read()
tenant, err := api.Tenant.Read()
if err != nil {
return err
}

if t.Flags.EnableCustomDomainInEmails != nil && *t.Flags.EnableCustomDomainInEmails {
ul, err := api.Branding.UniversalLogin()
if err != nil {
if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails {
if err := setUniversalLogin(d, m); err != nil {
d.SetId("")
return err
}

d.Set("universal_login", flattenBrandingUniversalLogin(ul))
}

return nil
}

func updateBranding(d *schema.ResourceData, m interface{}) error {
b := buildBranding(d)
ul := buildBrandingUniversalLogin(d)
api := m.(*management.Management)
err := api.Branding.Update(b)
if err != nil {

branding := buildBranding(d)
if err := api.Branding.Update(branding); err != nil {
return err
}

if ul.GetBody() != "" {
err = api.Branding.SetUniversalLogin(ul)
if err != nil {
universalLogin := buildBrandingUniversalLogin(d)
if universalLogin.GetBody() != "" {
if err := api.Branding.SetUniversalLogin(universalLogin); err != nil {
return err
}
}

return readBranding(d, m)
}

func deleteBranding(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
t, err := api.Tenant.Read()
tenant, err := api.Tenant.Read()
if err != nil {
return err
}

if t.Flags.EnableCustomDomainInEmails != nil && *t.Flags.EnableCustomDomainInEmails {
err = api.Branding.DeleteUniversalLogin()
if err != nil {
if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails {
if err = api.Branding.DeleteUniversalLogin(); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
Expand All @@ -164,35 +158,51 @@ func deleteBranding(d *schema.ResourceData, m interface{}) error {
}

func buildBranding(d *schema.ResourceData) *management.Branding {
b := &management.Branding{
branding := &management.Branding{
FaviconURL: String(d, "favicon_url"),
LogoURL: String(d, "logo_url"),
}

List(d, "colors").Elem(func(d ResourceData) {
b.Colors = &management.BrandingColors{
branding.Colors = &management.BrandingColors{
PageBackground: String(d, "page_background"),
Primary: String(d, "primary"),
}
})

List(d, "font").Elem(func(d ResourceData) {
b.Font = &management.BrandingFont{
branding.Font = &management.BrandingFont{
URL: String(d, "url"),
}
})

return b
return branding
}

func buildBrandingUniversalLogin(d *schema.ResourceData) *management.BrandingUniversalLogin {
b := &management.BrandingUniversalLogin{}
universalLogin := &management.BrandingUniversalLogin{}

List(d, "universal_login").Elem(func(d ResourceData) {
b.Body = String(d, "body")
universalLogin.Body = String(d, "body")
})

return b
return universalLogin
}

func setUniversalLogin(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
universalLogin, err := api.Branding.UniversalLogin()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
return nil
}
}
return err
}

d.Set("universal_login", flattenBrandingUniversalLogin(universalLogin))
return nil
}

func flattenBrandingColors(brandingColors *management.BrandingColors) []interface{} {
Expand Down

0 comments on commit fff6b91

Please sign in to comment.