Skip to content

Commit

Permalink
Simplify management of branding themes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Feb 22, 2023
1 parent c1817e8 commit e5e7363
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 342 deletions.
21 changes: 2 additions & 19 deletions internal/auth0/branding/data_source_theme.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package branding

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
Expand All @@ -12,26 +9,12 @@ import (
// NewThemeDataSource will return a new auth0_branding_theme data source.
func NewThemeDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: readBrandingThemeForDataSource,
ReadContext: readBrandingTheme,
Description: "Use this data source to access information about the tenant's branding theme settings.",
Schema: themeDataSourceSchema(),
}
}

func themeDataSourceSchema() map[string]*schema.Schema {
dataSourceSchema := internalSchema.TransformResourceToDataSource(NewThemeResource().Schema)

dataSourceSchema["branding_theme_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The ID of the branding theme.",
}

return dataSourceSchema
}

func readBrandingThemeForDataSource(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
brandingThemeID := data.Get("branding_theme_id").(string)
data.SetId(brandingThemeID)
return readBrandingTheme(ctx, data, meta)
return internalSchema.TransformResourceToDataSource(NewThemeResource().Schema)
}
2 changes: 1 addition & 1 deletion internal/auth0/branding/data_source_theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ resource "auth0_branding_theme" "my_theme" {
}
data "auth0_branding_theme" "test" {
branding_theme_id = auth0_branding_theme.my_theme.id
depends_on = [ auth0_branding_theme.my_theme ]
}
`

Expand Down
17 changes: 11 additions & 6 deletions internal/auth0/branding/resource_theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ func NewThemeResource() *schema.Resource {
func createBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)

if existingBrandingTheme, err := api.BrandingTheme.Default(); err == nil {
data.SetId(existingBrandingTheme.GetID())
return updateBrandingTheme(ctx, data, meta)
}

brandingTheme := expandBrandingTheme(data)
if err := api.BrandingTheme.Create(&brandingTheme); err != nil {
return diag.FromErr(err)
Expand All @@ -427,18 +432,18 @@ func createBrandingTheme(ctx context.Context, data *schema.ResourceData, meta in
func readBrandingTheme(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)

brandingTheme, err := api.BrandingTheme.Read(data.Id())
brandingTheme, err := api.BrandingTheme.Default()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}

return diag.FromErr(err)
}

data.SetId(brandingTheme.GetID())

result := multierror.Append(
data.Set("display_name", brandingTheme.GetDisplayName()),
data.Set("borders", flattenBrandingThemeBorders(brandingTheme.Borders)),
Expand Down
Loading

0 comments on commit e5e7363

Please sign in to comment.