Skip to content

Commit

Permalink
Add resource for email template settings
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nel committed Jan 10, 2025
1 parent 68f035c commit 4d23e3b
Show file tree
Hide file tree
Showing 10 changed files with 946 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/resources/okta_email_template_settings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# okta_email_template_settings

Use this resource to set the [email template settings](https://developer.okta.com/docs/api/openapi/okta-management/management/tag/CustomTemplates/#tag/CustomTemplates/operation/replaceEmailSettings)
of an email template belonging to a brand in an Okta organization.

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

resource "okta_email_template_settings" "test" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "UserActivation"
recipients = "NO_USERS"
}
1 change: 1 addition & 0 deletions examples/resources/okta_email_template_settings/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import okta_email_template_settings.example <brand_id>/<template_name>
5 changes: 5 additions & 0 deletions examples/resources/okta_email_template_settings/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "okta_email_template_settings" "example" {
brand_id = "<brand id>"
template_name = "ForgotPassword"
recipients = "ADMINS_ONLY"
}
8 changes: 8 additions & 0 deletions examples/resources/okta_email_template_settings/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "okta_brands" "test" {
}

resource "okta_email_template_settings" "test" {
brand_id = tolist(data.okta_brands.test.brands)[0].id
template_name = "UserActivation"
recipients = "ADMINS_ONLY"
}
2 changes: 2 additions & 0 deletions okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
emailCustomizations = "okta_email_customizations"
emailTemplate = "okta_email_template"
emailTemplates = "okta_email_templates"
emailTemplateSettings = "okta_email_template_settings"
eventHook = "okta_event_hook"
eventHookVerification = "okta_event_hook_verification"
factor = "okta_factor"
Expand Down Expand Up @@ -282,6 +283,7 @@ func Provider() *schema.Provider {
emailDomainVerification: resourceEmailDomainVerification(),
emailSender: resourceEmailSender(),
emailSenderVerification: resourceEmailSenderVerification(),
emailTemplateSettings: resourceEmailTemplateSettings(),
eventHook: resourceEventHook(),
eventHookVerification: resourceEventHookVerification(),
factor: resourceFactor(),
Expand Down
104 changes: 104 additions & 0 deletions okta/resource_okta_email_template_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package okta

import (
"context"
"fmt"

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

func resourceEmailTemplateSettings() *schema.Resource {
return &schema.Resource{
CreateContext: resourceEmailTemplateSettingsPut,
ReadContext: resourceEmailTemplateSettingsRead,
UpdateContext: resourceEmailTemplateSettingsPut,
DeleteContext: resourceFuncNoOp,
Importer: createNestedResourceImporter([]string{"brand_id", "template_name"}),
Description: `Update settings for an email template belonging to a brand in an Okta organization.
Use this resource to get and set the [settings for an email template](https://developer.okta.com/docs/api/openapi/okta-management/management/tag/CustomTemplates/#tag/CustomTemplates/operation/getEmailSettings)
belonging to a brand in an Okta organization.`,
Schema: map[string]*schema.Schema{
"brand_id": {
Type: schema.TypeString,
Required: true,
Description: "Brand ID",
},
"template_name": {
Type: schema.TypeString,
Required: true,
Description: "Email template name - Example values: `AccountLockout`,`ADForgotPassword`,`ADForgotPasswordDenied`,`ADSelfServiceUnlock`,`ADUserActivation`,`AuthenticatorEnrolled`,`AuthenticatorReset`,`ChangeEmailConfirmation`,`EmailChallenge`,`EmailChangeConfirmation`,`EmailFactorVerification`,`ForgotPassword`,`ForgotPasswordDenied`,`IGAReviewerEndNotification`,`IGAReviewerNotification`,`IGAReviewerPendingNotification`,`IGAReviewerReassigned`,`LDAPForgotPassword`,`LDAPForgotPasswordDenied`,`LDAPSelfServiceUnlock`,`LDAPUserActivation`,`MyAccountChangeConfirmation`,`NewSignOnNotification`,`OktaVerifyActivation`,`PasswordChanged`,`PasswordResetByAdmin`,`PendingEmailChange`,`RegistrationActivation`,`RegistrationEmailVerification`,`SelfServiceUnlock`,`SelfServiceUnlockOnUnlockedAccount`,`UserActivation`",
},
"recipients": {
Type: schema.TypeString,
Required: true,
Description: "The recipients the emails of this template will be sent to - Valid values: `ALL_USERS`, `ADMINS_ONLY`, `NO_USERS`",
},
},
}
}

func resourceEmailTemplateSettingsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
ets, diagErr := etsValues("read", d)
if diagErr != nil {
return diagErr
}

emailTemplateSettings, resp, err := getOktaV3ClientFromMetadata(m).CustomizationAPI.GetEmailSettings(ctx, ets.brandID, ets.templateName).Execute()
if err := v3suppressErrorOn404(resp, err); err != nil {
return diag.Errorf("failed to get email template settings: %v", err)
}
if emailTemplateSettings == nil {
d.SetId("")
return nil
}

_ = d.Set("brand_id", ets.brandID)
_ = d.Set("template_name", ets.templateName)
_ = d.Set("recipients", emailTemplateSettings.GetRecipients())

return nil
}

func resourceEmailTemplateSettingsPut(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
ets, diagErr := etsValues("update", d)
if diagErr != nil {
return diagErr
}

es := okta.EmailSettings{}
if recipients, ok := d.GetOk("recipients"); ok {
es.Recipients = recipients.(string)
}

_, err := getOktaV3ClientFromMetadata(m).CustomizationAPI.ReplaceEmailSettings(ctx, ets.brandID, ets.templateName).EmailSettings(es).Execute()
if err != nil {
return diag.Errorf("failed to update email template settings: %v", err)
}

d.SetId(fmt.Sprintf("%s/%s", ets.brandID, ets.templateName))
return resourceEmailTemplateSettingsRead(ctx, d, m)
}

type etsHelper struct {
brandID string
templateName string
}

func etsValues(action string, d *schema.ResourceData) (*etsHelper, diag.Diagnostics) {
brandID, ok := d.GetOk("brand_id")
if !ok {
return nil, diag.Errorf("brand_id required to %s email template settings", action)
}

templateName, ok := d.GetOk("template_name")
if !ok {
return nil, diag.Errorf("template name required to %s email template settings", action)
}

return &etsHelper{
brandID: brandID.(string),
templateName: templateName.(string),
}, nil
}
37 changes: 37 additions & 0 deletions okta/resource_okta_email_template_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package okta

import (
"fmt"
"testing"

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

func TestAccResourceOktaEmailTemplateSettings(t *testing.T) {
resourceName := fmt.Sprintf("%s.test", emailTemplateSettings)
mgr := newFixtureManager("resources", emailTemplateSettings, t.Name())
config := mgr.GetFixtures("basic.tf", t)
updated := mgr.GetFixtures("updated.tf", t)
oktaResourceTest(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProviderFactories: testAccProvidersFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "recipients", "NO_USERS"),
resource.TestCheckResourceAttr(resourceName, "template_name", "UserActivation"),
),
},
{
Config: updated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "recipients", "ADMINS_ONLY"),
resource.TestCheckResourceAttr(resourceName, "template_name", "UserActivation"),
),
},
},
})
}
Loading

0 comments on commit 4d23e3b

Please sign in to comment.