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

Data sources and resources for branded email customization #1089

Merged
merged 15 commits into from
May 6, 2022
Merged
11 changes: 11 additions & 0 deletions examples/okta_brand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# okta_brand

This resource represents a brand for an Okta organization. More information can
be found in the
[Brand](https://developer.okta.com/docs/reference/api/brands/#brand-object) API
documentation.

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

resource "okta_brand" "example" {
agree_to_custom_privacy_policy = true
custom_privacy_policy_url = "https://example.com/privacy-policy"
remove_powered_by_okta = false
}
6 changes: 6 additions & 0 deletions examples/okta_brand/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "okta_brands" "test" {
}

data "okta_brand" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
}
7 changes: 7 additions & 0 deletions examples/okta_brand/import.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This example
data "okta_brands" "test" {
}

resource "okta_brand" "example" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
}
8 changes: 8 additions & 0 deletions examples/okta_brand/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This example is part of the test harness. The okta_brand resource state has
# already been imported via import.tf

resource "okta_brand" "example" {
agree_to_custom_privacy_policy = true
custom_privacy_policy_url = "https://example.com/privacy-policy-updated"
remove_powered_by_okta = true
}
6 changes: 6 additions & 0 deletions examples/okta_brands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# okta_brands

This resource represents brands of an Okta organization. For more information
see the API docs for [Brands](https://developer.okta.com/docs/reference/api/brands/)

- Example [datastore.tf](./datasource.tf)
2 changes: 2 additions & 0 deletions examples/okta_brands/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "okta_brands" "test" {
}
9 changes: 9 additions & 0 deletions examples/okta_email_customization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# okta_email_customizations

Use this data source to retrieve the [email
customization](https://developer.okta.com/docs/reference/api/brands/#get-email-customization)
of an email template belonging to a brand in an Okta organization.

- Example [datastore.tf](./datasource.tf)
- Example [basic.tf](./basic.tf)
- Example [updated.tf](./updated.tf)
16 changes: 16 additions & 0 deletions examples/okta_email_customization/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data "okta_brands" "test" {
}

data "okta_email_customizations" "forgot_password" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
}

resource "okta_email_customization" "forgot_password_en_alt" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
language = "cs" # Setting the language to Czech for acceptance testing
is_default = false # A true is_default will cause an error during create if there is already a default customization. In general, use for update.
subject = "Forgot Password"
body = "Hi $$user.firstName,<br/><br/>Click this link to reset your password: $$resetPasswordLink"
}
13 changes: 13 additions & 0 deletions examples/okta_email_customization/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "okta_brands" "test" {
}

data "okta_email_customizations" "forgot_password" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
}

data "okta_email_customization" "forgot_password_en" {
customization_id = tolist(data.okta_email_customizations.forgot_password.email_customizations)[0].id
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
}
16 changes: 16 additions & 0 deletions examples/okta_email_customization/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data "okta_brands" "test" {
}

data "okta_email_customizations" "forgot_password" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
}

resource "okta_email_customization" "forgot_password_en_alt" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
language = "cs"
is_default = false
subject = "Forgot Password"
body = "Hello $$user.firstName,<br/><br/>Click this link to reset your password: $$resetPasswordLink"
}
7 changes: 7 additions & 0 deletions examples/okta_email_customizations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# okta_email_customizations

This data source represents email customizations for a brand in an Okta
organization. For more information see the API docs for [Email
Customizations](https://developer.okta.com/docs/reference/api/brands/#email-customizations)

- Example [datastore.tf](./datasource.tf)
7 changes: 7 additions & 0 deletions examples/okta_email_customizations/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "okta_brands" "test" {
}

data "okta_email_customizations" "forgot_password" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "ForgotPassword"
}
7 changes: 7 additions & 0 deletions examples/okta_email_template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# okta_email_template

This data source represents an email template for a brand in an Okta
organization. For more information see the API docs for [Email
Template](https://developer.okta.com/docs/reference/api/brands/#email-template)

- Example [datastore.tf](./datasource.tf)
7 changes: 7 additions & 0 deletions examples/okta_email_template/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "okta_brands" "test" {
}

data "okta_email_template" "forgot_password" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
name = "ForgotPassword"
}
7 changes: 7 additions & 0 deletions examples/okta_email_templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# okta_email_templates

This data source represents email templates for a brand in an Okta
organization. For more information see the API docs for [Email
Templates](https://developer.okta.com/docs/reference/api/brands/#email-templates)

- Example [datastore.tf](./datasource.tf)
6 changes: 6 additions & 0 deletions examples/okta_email_templates/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "okta_brands" "test" {
}

data "okta_email_templates" "test" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
}
116 changes: 116 additions & 0 deletions okta/brand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package okta

import (
"encoding/json"

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

var brandResourceSchema = map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Brand ID",
},
"brand_id": {
Type: schema.TypeString,
Optional: true,
Description: "Brand ID - Note: Okta API for brands only reads and updates therefore the okta_brand resource needs to act as a quasi data source. Do this by setting brand_id.",
},
"agree_to_custom_privacy_policy": {
Type: schema.TypeBool,
Optional: true,
Description: "Consent for updating the custom privacy policy URL.",
RequiredWith: []string{"custom_privacy_policy_url"},
},
"custom_privacy_policy_url": {
Type: schema.TypeString,
Optional: true,
Description: "Custom privacy policy URL",
DiffSuppressFunc: suppressDuringCreate,
},
"links": {
Type: schema.TypeString,
Computed: true,
Description: "Link relations for this object - JSON HAL - Discoverable resources related to the brand",
},
"remove_powered_by_okta": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: `Removes "Powered by Okta" from the Okta-hosted sign-in page and "© 2021 Okta, Inc." from the Okta End-User Dashboard`,
DiffSuppressFunc: suppressDuringCreate,
},
}

func suppressDuringCreate(k, old, new string, d *schema.ResourceData) bool {
// If brand_id has changed assume this is create and treat the properties as readers not caring about what would otherwise apear to be drift.
if d.HasChange("brand_id") {
return true
}
return old == new
}

var brandDataSourceSchema = map[string]*schema.Schema{
"brand_id": {
Type: schema.TypeString,
Required: true,
Description: "Brand ID",
},
"custom_privacy_policy_url": {
Type: schema.TypeString,
Computed: true,
Description: "Custom privacy policy URL",
},
"links": {
Type: schema.TypeString,
Computed: true,
Description: "Link relations for this object - JSON HAL - Discoverable resources related to the brand",
},
"remove_powered_by_okta": {
Type: schema.TypeBool,
Computed: true,
Description: `Removes "Powered by Okta" from the Okta-hosted sign-in page and "© 2021 Okta, Inc." from the Okta End-User Dashboard`,
},
}

var brandsDataSourceSchema = map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Brand ID",
},
"custom_privacy_policy_url": {
Type: schema.TypeString,
Computed: true,
Description: "Custom privacy policy URL",
},
"links": {
Type: schema.TypeString,
Computed: true,
Description: "Link relations for this object - JSON HAL - Discoverable resources related to the brand",
},
"remove_powered_by_okta": {
Type: schema.TypeBool,
Computed: true,
Description: `Removes "Powered by Okta" from the Okta-hosted sign-in page and "© 2021 Okta, Inc." from the Okta End-User Dashboard`,
},
}

func flattenBrand(brand *okta.Brand) map[string]interface{} {
attrs := map[string]interface{}{}
// NOTE: explicitly set defaults on brand
attrs["custom_privacy_policy_url"] = ""
if brand.CustomPrivacyPolicyUrl != "" {
attrs["custom_privacy_policy_url"] = brand.CustomPrivacyPolicyUrl
}
links, _ := json.Marshal(brand.Links)
attrs["links"] = string(links)
attrs["remove_powered_by_okta"] = false
if brand.RemovePoweredByOkta != nil {
attrs["remove_powered_by_okta"] = *brand.RemovePoweredByOkta
}

return attrs
}
37 changes: 37 additions & 0 deletions okta/data_source_okta_brand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package okta

import (
"context"

"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 dataSourceBrand() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBrandRead,
Schema: brandDataSourceSchema,
}
}

func dataSourceBrandRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var brand *okta.Brand
var err error
brandID, ok := d.GetOk("brand_id")
if ok {
logger(m).Info("reading brand by ID", "id", brandID.(string))
brand, _, err = getOktaClientFromMetadata(m).Brand.GetBrand(ctx, brandID.(string))
if err != nil {
return diag.Errorf("failed to get brand: %v", err)
}
}
d.SetId(brand.Id)
rawMap := flattenBrand(brand)
err = setNonPrimitives(d, rawMap)
if err != nil {
return diag.Errorf("failed to set brand's properties: %v", err)
}

return nil
}
29 changes: 29 additions & 0 deletions okta/data_source_okta_brand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package okta

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceOktaBrand_read(t *testing.T) {
ri := acctest.RandInt()
mgr := newFixtureManager(brand)
config := mgr.GetFixtures("datasource.tf", ri, t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
Steps: []resource.TestStep{
{
Config: config,
Destroy: false,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.okta_brand.example", "brand_id"),
resource.TestCheckResourceAttrSet("data.okta_brand.example", "links"),
),
},
},
})
}
45 changes: 45 additions & 0 deletions okta/data_source_okta_brands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package okta

import (
"context"

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

func dataSourceBrands() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBrandsRead,
Schema: map[string]*schema.Schema{
"brands": {
Type: schema.TypeSet,
Computed: true,
Description: "List of `okta_brand` belonging to the organization",
Elem: &schema.Resource{
Schema: brandsDataSourceSchema,
},
},
},
}
}

func dataSourceBrandsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
brands, _, err := getOktaClientFromMetadata(m).Brand.ListBrands(ctx)
if err != nil {
return diag.Errorf("failed to list brands: %v", err)
}

d.SetId("brands")
arr := make([]interface{}, len(brands))
for i, brand := range brands {
rawMap := flattenBrand(brand)
rawMap["id"] = brand.Id
arr[i] = rawMap
}
brandDataSource := &schema.Resource{
Schema: brandResourceSchema,
}
_ = d.Set("brands", schema.NewSet(schema.HashResource(brandDataSource), arr))

return nil
}
Loading