Skip to content

Commit

Permalink
Add settings to auth0_email
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 4, 2022
1 parent ea5ab2a commit e80c159
Show file tree
Hide file tree
Showing 3 changed files with 716 additions and 50 deletions.
135 changes: 135 additions & 0 deletions internal/provider/resource_auth0_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,64 @@ func newEmail() *schema.Resource {
},
},
},
"settings": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: "Specific email provider settings.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"message": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Message settings for the `mandrill` or `ses` email provider.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"view_content_link": {
Type: schema.TypeBool,
Optional: true,
Description: "Setting for the `mandrill` email provider. " +
"Set to `true` to see the content of individual emails sent to users.",
},
"configuration_set_name": {
Type: schema.TypeString,
Optional: true,
Description: "Setting for the `ses` email provider. " +
"The name of the configuration set to apply to the sent emails.",
},
},
},
},
"headers": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Headers settings for the `smtp` email provider.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"x_mc_view_content_link": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(
[]string{"true", "false"},
false,
),
Description: "Disable or enable the default View Content Link " +
"for sensitive emails.",
},
"x_ses_configuration_set": {
Type: schema.TypeString,
Optional: true,
Description: "SES Configuration set to include when sending emails.",
},
},
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -163,6 +221,7 @@ func readEmail(ctx context.Context, d *schema.ResourceData, m interface{}) diag.
d.Set("enabled", email.GetEnabled()),
d.Set("default_from_address", email.GetDefaultFromAddress()),
d.Set("credentials", flattenEmailProviderCredentials(d, email)),
d.Set("settings", flattenEmailProviderSettings(email)),
)

return diag.FromErr(result.ErrorOrNil())
Expand Down Expand Up @@ -231,6 +290,18 @@ func expandEmailProviderMandrill(config cty.Value, emailProvider *management.Ema
}
return stop
})

config.GetAttr("settings").ForEachElement(func(_ cty.Value, settings cty.Value) (stop bool) {
settings.GetAttr("message").ForEachElement(func(_ cty.Value, message cty.Value) (stop bool) {
emailProvider.Settings = &management.EmailProviderSettingsMandrill{
Message: &management.EmailProviderSettingsMandrillMessage{
ViewContentLink: value.Bool(message.GetAttr("view_content_link")),
},
}
return stop
})
return stop
})
}

func expandEmailProviderSES(config cty.Value, emailProvider *management.EmailProvider) {
Expand All @@ -242,6 +313,18 @@ func expandEmailProviderSES(config cty.Value, emailProvider *management.EmailPro
}
return stop
})

config.GetAttr("settings").ForEachElement(func(_ cty.Value, settings cty.Value) (stop bool) {
settings.GetAttr("message").ForEachElement(func(_ cty.Value, message cty.Value) (stop bool) {
emailProvider.Settings = &management.EmailProviderSettingsSES{
Message: &management.EmailProviderSettingsSESMessage{
ConfigurationSetName: value.String(message.GetAttr("configuration_set_name")),
},
}
return stop
})
return stop
})
}

func expandEmailProviderSendGrid(config cty.Value, emailProvider *management.EmailProvider) {
Expand Down Expand Up @@ -284,6 +367,19 @@ func expandEmailProviderSmtp(config cty.Value, emailProvider *management.EmailPr
}
return stop
})

config.GetAttr("settings").ForEachElement(func(_ cty.Value, settings cty.Value) (stop bool) {
settings.GetAttr("headers").ForEachElement(func(_ cty.Value, headers cty.Value) (stop bool) {
emailProvider.Settings = management.EmailProviderSettingsSMTP{
Headers: &management.EmailProviderSettingsSMTPHeaders{
XMCViewContentLink: value.String(headers.GetAttr("x_mc_view_content_link")),
XSESConfigurationSet: value.String(headers.GetAttr("x_ses_configuration_set")),
},
}
return stop
})
return stop
})
}

func flattenEmailProviderCredentials(d *schema.ResourceData, emailProvider *management.EmailProvider) []interface{} {
Expand Down Expand Up @@ -329,3 +425,42 @@ func flattenEmailProviderCredentials(d *schema.ResourceData, emailProvider *mana

return []interface{}{credentials}
}

func flattenEmailProviderSettings(emailProvider *management.EmailProvider) []interface{} {
if emailProvider.Settings == nil {
return nil
}

var settings interface{}
switch settingsType := emailProvider.Settings.(type) {
case *management.EmailProviderSettingsMandrill:
settings = map[string]interface{}{
"message": []map[string]interface{}{
{
"view_content_link": settingsType.GetMessage().GetViewContentLink(),
},
},
}
case *management.EmailProviderSettingsSES:
settings = map[string]interface{}{
"message": []map[string]interface{}{
{
"configuration_set_name": settingsType.GetMessage().GetConfigurationSetName(),
},
},
}
case *management.EmailProviderSettingsSMTP:
settings = map[string]interface{}{
"headers": []map[string]interface{}{
{
"x_mc_view_content_link": settingsType.GetHeaders().GetXMCViewContentLink(),
"x_ses_configuration_set": settingsType.GetHeaders().GetXSESConfigurationSet(),
},
},
}
default:
settings = nil
}

return []interface{}{settings}
}
99 changes: 99 additions & 0 deletions internal/provider/resource_auth0_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ resource "auth0_email" "my_email_provider" {
}
`

const testAccUpdateSESEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "ses"
enabled = true
default_from_address = "[email protected]"
credentials {
access_key_id = "AKIAXXXXXXXXXXXXXXXX"
secret_access_key = "7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
region = "us-east-1"
}
settings {
message {
configuration_set_name = "example"
}
}
}
`

const testAccCreateMandrillEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "mandrill"
Expand All @@ -49,6 +67,22 @@ resource "auth0_email" "my_email_provider" {
}
`

const testAccUpdateMandrillEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "mandrill"
enabled = true
default_from_address = "[email protected]"
credentials {
api_key = "7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
settings {
message {
view_content_link = true
}
}
}
`

const testAccCreateSmtpEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "smtp"
Expand All @@ -63,6 +97,26 @@ resource "auth0_email" "my_email_provider" {
}
`

const testAccUpdateSmtpEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "smtp"
enabled = true
default_from_address = "[email protected]"
credentials {
smtp_host = "example.com"
smtp_port = 984
smtp_user = "bob"
smtp_pass = "secret"
}
settings {
headers {
x_mc_view_content_link = "true"
x_ses_configuration_set = "example"
}
}
}
`

const testAccCreateMailgunEmailProvider = `
resource "auth0_email" "my_email_provider" {
name = "mailgun"
Expand Down Expand Up @@ -133,6 +187,21 @@ func TestAccEmail(t *testing.T) {
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.region", "us-east-1"),
),
},
{
Config: testAccUpdateSESEmailProvider,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "name", "ses"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "enabled", "true"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "default_from_address", "[email protected]"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.access_key_id", "AKIAXXXXXXXXXXXXXXXX"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.secret_access_key", "7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.region", "us-east-1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.message.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.message.0.configuration_set_name", "example"),
),
},
{
Config: testAccCreateMandrillEmailProvider,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -143,6 +212,19 @@ func TestAccEmail(t *testing.T) {
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.api_key", "7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
),
},
{
Config: testAccUpdateMandrillEmailProvider,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "name", "mandrill"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "enabled", "true"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "default_from_address", "[email protected]"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.api_key", "7e8c2148xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.message.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.message.0.view_content_link", "true"),
),
},
{
Config: testAccCreateSmtpEmailProvider,
Check: resource.ComposeTestCheckFunc(
Expand All @@ -156,6 +238,23 @@ func TestAccEmail(t *testing.T) {
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.smtp_pass", "secret"),
),
},
{
Config: testAccUpdateSmtpEmailProvider,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "name", "smtp"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "enabled", "true"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "default_from_address", "[email protected]"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.smtp_host", "example.com"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.smtp_port", "984"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.smtp_user", "bob"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "credentials.0.smtp_pass", "secret"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.headers.#", "1"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.headers.0.x_mc_view_content_link", "true"),
resource.TestCheckResourceAttr("auth0_email.my_email_provider", "settings.0.headers.0.x_ses_configuration_set", "example"),
),
},
{
Config: testAccCreateMailgunEmailProvider,
Check: resource.ComposeTestCheckFunc(
Expand Down
Loading

0 comments on commit e80c159

Please sign in to comment.