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

Commit

Permalink
Refactor branding resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 20, 2022
1 parent 8e39fec commit 48a2fd1
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 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,7 +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 @@ -100,18 +98,18 @@ 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 {
if err := assignUniversalLogin(d, m); err != nil {
if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails {
if err := setUniversalLogin(d, m); err != nil {
d.SetId("")
return err
}
Expand All @@ -121,33 +119,32 @@ func readBranding(d *schema.ResourceData, m interface{}) error {
}

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 @@ -161,40 +158,40 @@ 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 assignUniversalLogin(d *schema.ResourceData, m interface{}) error {
func setUniversalLogin(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
ul, err := api.Branding.UniversalLogin()
universalLogin, err := api.Branding.UniversalLogin()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -204,7 +201,7 @@ func assignUniversalLogin(d *schema.ResourceData, m interface{}) error {
return err
}

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

Expand Down

0 comments on commit 48a2fd1

Please sign in to comment.