Skip to content
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

Ignore 404 error when fetching universal login content in branding #359

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions internal/provider/resource_auth0_branding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"net/http"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
Expand Down Expand Up @@ -118,16 +119,13 @@ func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) di
if _, ok := d.GetOk("font"); ok {
result = multierror.Append(result, d.Set("font", flattenBrandingFont(branding.GetFont())))
}

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

if tenant.Flags.GetEnableCustomDomainInEmails() {
if err := setUniversalLogin(d, api); err != nil {
if _, ok := d.GetOk("universal_login"); ok {
brandingUniversalLogin, err := flattenBrandingUniversalLogin(api)
if err != nil {
return diag.FromErr(err)
}

result = multierror.Append(result, d.Set("universal_login", brandingUniversalLogin))
}

return diag.FromErr(result.ErrorOrNil())
Expand Down Expand Up @@ -225,15 +223,6 @@ func expandBrandingUniversalLogin(config cty.Value) *management.BrandingUniversa
return &universalLogin
}

func setUniversalLogin(d *schema.ResourceData, api *management.Management) error {
universalLogin, err := api.Branding.UniversalLogin()
if err != nil {
return err
}

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

func flattenBrandingColors(brandingColors *management.BrandingColors) []interface{} {
if brandingColors == nil {
return nil
Expand All @@ -246,15 +235,35 @@ func flattenBrandingColors(brandingColors *management.BrandingColors) []interfac
}
}

func flattenBrandingUniversalLogin(brandingUniversalLogin *management.BrandingUniversalLogin) []interface{} {
if brandingUniversalLogin == nil {
return nil
func flattenBrandingUniversalLogin(api *management.Management) ([]interface{}, error) {
tenant, err := api.Tenant.Read()
if err != nil {
return nil, err
}
return []interface{}{

if !tenant.GetFlags().GetEnableCustomDomainInEmails() {
return nil, nil
}

universalLogin, err := api.Branding.UniversalLogin()
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
return nil, nil
}
return nil, err
}

if universalLogin == nil {
return nil, nil
}

flattenedUniversalLogin := []interface{}{
map[string]interface{}{
"body": brandingUniversalLogin.GetBody(),
"body": universalLogin.GetBody(),
},
}

return flattenedUniversalLogin, nil
}

func flattenBrandingFont(brandingFont *management.BrandingFont) []interface{} {
Expand Down
68 changes: 64 additions & 4 deletions internal/provider/resource_auth0_branding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@ import (
"github.com/auth0/terraform-provider-auth0/internal/recorder"
)

const testAccTenantAllowsUniversalLoginCustomization = `
resource "auth0_tenant" "my_tenant" {
flags {
enable_custom_domain_in_emails = true
}
}
`

const testAccTenantDisallowsUniversalLoginCustomization = `
resource "auth0_tenant" "my_tenant" {
flags {
enable_custom_domain_in_emails = false
}
}
`

const testAccBrandingConfigCreate = `
resource "auth0_branding" "my_brand" {
depends_on = [ auth0_tenant.my_tenant ]

logo_url = "https://mycompany.org/v1/logo.png"
favicon_url = "https://mycompany.org/favicon.ico"
}
`

const testAccBrandingConfigUpdateAllFields = `
resource "auth0_branding" "my_brand" {
depends_on = [ auth0_tenant.my_tenant ]

logo_url = "https://mycompany.org/v2/logo.png"
favicon_url = "https://mycompany.org/favicon.ico"

Expand All @@ -37,6 +57,30 @@ resource "auth0_branding" "my_brand" {

const testAccBrandingConfigUpdateAgain = `
resource "auth0_branding" "my_brand" {
depends_on = [ auth0_tenant.my_tenant ]

logo_url = "https://mycompany.org/v2/logo.png"
favicon_url = "https://mycompany.org/favicon.ico"

colors {
primary = "#0059d6"
page_background = "#00FF00"
}

font {
url = "https://example.com/font/myfont.ttf"
}

universal_login {
body = "<!DOCTYPE html><html><head>{%- auth0:head -%}</head>This is getting updated but it should not be read cuz the tenant flag is disabled<body>{%- auth0:widget -%}</body></html>"
}
}
`

const testAccBrandingConfigUpdateAndAgain = `
resource "auth0_branding" "my_brand" {
depends_on = [ auth0_tenant.my_tenant ]

logo_url = "https://mycompany.org/v3/logo.png"
favicon_url = "https://mycompany.org/favicon.ico"

Expand All @@ -59,6 +103,8 @@ resource "auth0_branding" "my_brand" {

const testAccBrandingConfigReset = `
resource "auth0_branding" "my_brand" {
depends_on = [ auth0_tenant.my_tenant ]

logo_url = "https://mycompany.org/v1/logo.png"
favicon_url = "https://mycompany.org/favicon.ico"
}
Expand All @@ -71,7 +117,7 @@ func TestAccBranding(t *testing.T) {
ProviderFactories: testProviders(httpRecorder),
Steps: []resource.TestStep{
{
Config: testAccBrandingConfigCreate,
Config: testAccTenantAllowsUniversalLoginCustomization + testAccBrandingConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_branding.my_brand", "logo_url", "https://mycompany.org/v1/logo.png"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "favicon_url", "https://mycompany.org/favicon.ico"),
Expand All @@ -81,7 +127,21 @@ func TestAccBranding(t *testing.T) {
),
},
{
Config: testAccBrandingConfigUpdateAllFields,
Config: testAccTenantDisallowsUniversalLoginCustomization + testAccBrandingConfigUpdateAgain,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_branding.my_brand", "logo_url", "https://mycompany.org/v2/logo.png"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "favicon_url", "https://mycompany.org/favicon.ico"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "colors.#", "1"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "colors.0.primary", "#0059d6"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "colors.0.page_background", "#00FF00"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "font.#", "1"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "font.0.url", "https://example.com/font/myfont.ttf"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "universal_login.#", "0"),
),
ExpectNonEmptyPlan: true,
},
{
Config: testAccTenantAllowsUniversalLoginCustomization + testAccBrandingConfigUpdateAllFields,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_branding.my_brand", "logo_url", "https://mycompany.org/v2/logo.png"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "favicon_url", "https://mycompany.org/favicon.ico"),
Expand All @@ -95,7 +155,7 @@ func TestAccBranding(t *testing.T) {
),
},
{
Config: testAccBrandingConfigUpdateAgain,
Config: testAccTenantAllowsUniversalLoginCustomization + testAccBrandingConfigUpdateAndAgain,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_branding.my_brand", "logo_url", "https://mycompany.org/v3/logo.png"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "favicon_url", "https://mycompany.org/favicon.ico"),
Expand All @@ -109,7 +169,7 @@ func TestAccBranding(t *testing.T) {
),
},
{
Config: testAccBrandingConfigReset,
Config: testAccTenantAllowsUniversalLoginCustomization + testAccBrandingConfigReset,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_branding.my_brand", "logo_url", "https://mycompany.org/v1/logo.png"),
resource.TestCheckResourceAttr("auth0_branding.my_brand", "favicon_url", "https://mycompany.org/favicon.ico"),
Expand Down
Loading