Skip to content

Commit

Permalink
Resource "okta_theme"
Browse files Browse the repository at this point in the history
  • Loading branch information
monde committed May 12, 2022
1 parent b5cdc5d commit 2aa4f31
Show file tree
Hide file tree
Showing 19 changed files with 657 additions and 25 deletions.
4 changes: 4 additions & 0 deletions examples/okta_theme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ information can be found in the
API documentation.

- Example [datastore.tf](./datasource.tf)
- Example [basic.tf](./basic.tf)
- Example [updated.tf](./updated.tf)
- Example [import.tf](./import.tf)
- Example [delete-images.tf](./delete-images.tf)
21 changes: 21 additions & 0 deletions examples/okta_theme/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This example is part of the test harness. The okta_theme resource state has
# already been imported via import.tf

data "okta_brands" "test" {
}

resource "okta_theme" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id

logo = "../examples/okta_theme/okta_logo.png"
favicon = "../examples/okta_theme/okta_favicon.png"
background_image = "../examples/okta_theme/okta_background_image.png"
primary_color_hex = "#1662dd"
primary_color_contrast_hex = "#ffffff"
secondary_color_hex = "#ebebed"
secondary_color_contrast_hex = "#000000"
sign_in_page_touch_point_variant = "OKTA_DEFAULT"
end_user_dashboard_touch_point_variant = "OKTA_DEFAULT"
error_page_touch_point_variant = "OKTA_DEFAULT"
email_template_touch_point_variant = "OKTA_DEFAULT"
}
22 changes: 22 additions & 0 deletions examples/okta_theme/delete-images.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This example is part of the test harness. The okta_theme resource state has
# already been imported via import.tf

data "okta_brands" "test" {
}

resource "okta_theme" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id

logo = ""
favicon = ""
background_image = ""

primary_color_hex = "#1662ff"
primary_color_contrast_hex = "#ffffff"
secondary_color_hex = "#fbfbfd"
secondary_color_contrast_hex = "#000000"
sign_in_page_touch_point_variant = "OKTA_DEFAULT"
end_user_dashboard_touch_point_variant = "OKTA_DEFAULT"
error_page_touch_point_variant = "OKTA_DEFAULT"
email_template_touch_point_variant = "OKTA_DEFAULT"
}
11 changes: 11 additions & 0 deletions examples/okta_theme/import.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "okta_brands" "test" {
}

data "okta_themes" "test" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
}

resource "okta_theme" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
theme_id = tolist(data.okta_themes.test.themes)[0].id
}
Binary file added examples/okta_theme/okta_background_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/okta_theme/okta_favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/okta_theme/okta_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions examples/okta_theme/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This example is part of the test harness. The okta_theme resource state has
# already been imported via import.tf

data "okta_brands" "test" {
}

resource "okta_theme" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id

primary_color_hex = "#1662ff"
primary_color_contrast_hex = "#ffffff"
secondary_color_hex = "#fbfbfd"
secondary_color_contrast_hex = "#000000"
sign_in_page_touch_point_variant = "OKTA_DEFAULT"
end_user_dashboard_touch_point_variant = "OKTA_DEFAULT"
error_page_touch_point_variant = "OKTA_DEFAULT"
email_template_touch_point_variant = "OKTA_DEFAULT"
}
2 changes: 1 addition & 1 deletion okta/data_source_okta_theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func dataSourceThemeRead(ctx context.Context, d *schema.ResourceData, m interfac
}

d.SetId(theme.Id)
rawMap := flattenTheme(brandID, themeID, theme)
rawMap := flattenTheme(brandID, theme)
err = setNonPrimitives(d, rawMap)
if err != nil {
return diag.Errorf("failed to set theme properties: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions okta/data_source_okta_theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestAccDataSourceOktaTheme_read(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.okta_theme.test", "id"),
resource.TestCheckResourceAttrSet("data.okta_theme.test", "logo"),
resource.TestCheckResourceAttrSet("data.okta_theme.test", "favicon"),
// resource.TestCheckResourceAttrSet("data.okta_theme.test", "background_image"), // background image is null by default, skip check
resource.TestCheckResourceAttrSet("data.okta_theme.test", "logo_url"),
resource.TestCheckResourceAttrSet("data.okta_theme.test", "favicon_url"),
// resource.TestCheckResourceAttrSet("data.okta_theme.test", "background_image_url"), // background image is null on new orgs, skip check
resource.TestCheckResourceAttrSet("data.okta_theme.test", "primary_color_hex"),
resource.TestCheckResourceAttrSet("data.okta_theme.test", "primary_color_contrast_hex"),
resource.TestCheckResourceAttrSet("data.okta_theme.test", "secondary_color_hex"),
Expand Down
2 changes: 1 addition & 1 deletion okta/data_source_okta_themes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func dataSourceThemesRead(ctx context.Context, d *schema.ResourceData, m interfa
d.SetId(fmt.Sprintf("themes-%s", brandID.(string)))
arr := make([]interface{}, len(themes))
for i, theme := range themes {
rawMap := flattenTheme("", "", theme)
rawMap := flattenTheme("", theme)
arr[i] = rawMap
}

Expand Down
1 change: 1 addition & 0 deletions okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func Provider() *schema.Provider {
securityNotificationEmails: resourceSecurityNotificationEmails(),
templateEmail: resourceTemplateEmail(),
templateSms: resourceTemplateSms(),
theme: resourceTheme(),
threatInsightSettings: resourceThreatInsightSettings(),
trustedOrigin: resourceTrustedOrigin(),
user: resourceUser(),
Expand Down
219 changes: 219 additions & 0 deletions okta/resource_okta_theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package okta

import (
"context"
"fmt"
"strings"

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

func resourceTheme() *schema.Resource {
return &schema.Resource{
CreateContext: resourceThemeCreate,
ReadContext: resourceThemeRead,
UpdateContext: resourceThemeUpdate,
DeleteContext: resourceThemeDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceThemeImportStateContext,
},
Schema: themeResourceSchema,
}
}

func resourceThemeCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
bid, ok := d.GetOk("brand_id")
if !ok {
return diag.Errorf("brand_id required to create theme")
}
brandID := bid.(string)

var themeID string
if d.Id() != "" {
themeID = d.Id()
}
if themeID == "" {
if tid, ok := d.GetOk("theme_id"); ok {
themeID = tid.(string)
}
}
if themeID == "" {
return diag.Errorf("brand_id required to create theme")
}

theme, _, err := getOktaClientFromMetadata(m).Brand.GetBrandTheme(ctx, brandID, themeID)
if err != nil {
return diag.Errorf("failed to get theme: %v", err)
}

d.SetId(theme.Id)
rawMap := flattenTheme(brandID, theme)
err = setNonPrimitives(d, rawMap)
if err != nil {
return diag.Errorf("failed to set theme properties: %v", err)
}

return nil
}

func resourceThemeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
logger(m).Info("reading theme", "id", d.Id())

bid, ok := d.GetOk("brand_id")
if !ok {
return diag.Errorf("brand_id required to import theme")
}
brandID := bid.(string)

theme, _, err := getOktaClientFromMetadata(m).Brand.GetBrandTheme(ctx, brandID, d.Id())
if err != nil {
return diag.Errorf("failed to get theme: %v", err)
}

rawMap := flattenTheme(brandID, theme)
err = setNonPrimitives(d, rawMap)
if err != nil {
return diag.Errorf("failed to set theme properties: %v", err)
}

return nil
}

func resourceThemeUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
logger(m).Info("updating theme", "id", d.Id())

bid, ok := d.GetOk("brand_id")
if !ok {
return diag.Errorf("brand_id required to update theme")
}
brandID := bid.(string)

// peform delete/upload on the logo/favicon/background_image first so any
// errors there will interrupt apply on the theme itself
if d.HasChange("logo") {
err := handleThemeLogo(ctx, d, m, brandID, d.Id())
if err != nil {
return diag.Errorf("failed to handle logo for theme: %v", err)
}
}
if d.HasChange("favicon") {
err := handleThemeFavicon(ctx, d, m, brandID, d.Id())
if err != nil {
return diag.Errorf("failed to handle favicon for theme: %v", err)
}
}
if d.HasChange("background_image") {
err := handleThemeBackgroundImage(ctx, d, m, brandID, d.Id())
if err != nil {
return diag.Errorf("failed to handle background_image for theme: %v", err)
}
}

theme := okta.Theme{}

if val, ok := d.GetOk("primary_color_hex"); ok {
theme.PrimaryColorHex = val.(string)
}

if val, ok := d.GetOk("primary_color_contrast_hex"); ok {
theme.PrimaryColorContrastHex = val.(string)
}

if val, ok := d.GetOk("secondary_color_hex"); ok {
theme.SecondaryColorHex = val.(string)
}

if val, ok := d.GetOk("secondary_color_contrast_hex"); ok {
theme.SecondaryColorContrastHex = val.(string)
}

if val, ok := d.GetOk("sign_in_page_touch_point_variant"); ok {
theme.SignInPageTouchPointVariant = val.(string)
}

if val, ok := d.GetOk("end_user_dashboard_touch_point_variant"); ok {
theme.EndUserDashboardTouchPointVariant = val.(string)
}

if val, ok := d.GetOk("error_page_touch_point_variant"); ok {
theme.ErrorPageTouchPointVariant = val.(string)
}

if val, ok := d.GetOk("email_template_touch_point_variant"); ok {
theme.EmailTemplateTouchPointVariant = val.(string)
}

themeResp, _, err := getOktaClientFromMetadata(m).Brand.UpdateBrandTheme(ctx, brandID, d.Id(), theme)
if err != nil {
return diag.Errorf("failed to update theme: %v", err)
}

rawMap := flattenTheme(brandID, themeResp)
err = setNonPrimitives(d, rawMap)
if err != nil {
return diag.Errorf("failed to set theme properties: %v", err)
}

return nil
}

func resourceThemeDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// fake delete
d.SetId("")
return nil
}

func resourceThemeImportStateContext(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid resource import specifier, expecting the following format: <brand_id>/<theme_id>")
}
brandID := parts[0]
themeID := parts[1]

theme, _, err := getOktaClientFromMetadata(m).Brand.GetBrandTheme(ctx, brandID, themeID)
if err != nil {
return nil, fmt.Errorf("failed to get theme: %v", err)
}

d.SetId(theme.Id)
rawMap := flattenTheme(brandID, theme)
err = setNonPrimitives(d, rawMap)
if err != nil {
return nil, fmt.Errorf("failed to set theme properties: %v", err)
}

return []*schema.ResourceData{d}, nil
}

func handleThemeLogo(ctx context.Context, d *schema.ResourceData, m interface{}, brandID, themeID string) error {
_, newPath := d.GetChange("logo")
if newPath == "" {
_, err := getOktaClientFromMetadata(m).Brand.DeleteBrandThemeLogo(ctx, brandID, themeID)
return err
}
_, _, err := getOktaClientFromMetadata(m).Brand.UploadBrandThemeLogo(ctx, brandID, themeID, newPath.(string))
return err
}

func handleThemeFavicon(ctx context.Context, d *schema.ResourceData, m interface{}, brandID, themeID string) error {
_, newPath := d.GetChange("favicon")
if newPath == "" {
_, err := getOktaClientFromMetadata(m).Brand.DeleteBrandThemeFavicon(ctx, brandID, themeID)
return err
}
_, _, err := getOktaClientFromMetadata(m).Brand.UploadBrandThemeFavicon(ctx, brandID, themeID, newPath.(string))
return err
}

func handleThemeBackgroundImage(ctx context.Context, d *schema.ResourceData, m interface{}, brandID, themeID string) error {
_, newPath := d.GetChange("background_image")
if newPath == "" {
_, err := getOktaClientFromMetadata(m).Brand.DeleteBrandThemeBackgroundImage(ctx, brandID, themeID)
return err
}
_, _, err := getOktaClientFromMetadata(m).Brand.UploadBrandThemeBackgroundImage(ctx, brandID, themeID, newPath.(string))
return err
}
Loading

0 comments on commit 2aa4f31

Please sign in to comment.