Skip to content

Commit

Permalink
CR updates including documentation, acctest coverage, create->read flow
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Feb 11, 2021
1 parent b4983ad commit b906144
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 103 deletions.
118 changes: 72 additions & 46 deletions aws/resource_aws_ses_configuration_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,34 @@ import (
func resourceAwsSesConfigurationSet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSesConfigurationSetCreate,
Update: resourceAwsSesConfigurationSetUpdate,
Read: resourceAwsSesConfigurationSetRead,
Update: resourceAwsSesConfigurationSetUpdate,
Delete: resourceAwsSesConfigurationSetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delivery_options": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tls_policy": {
Type: schema.TypeString,
Optional: true,
Default: ses.TlsPolicyOptional,
ValidateFunc: validation.StringInSlice([]string{
ses.TlsPolicyRequire,
ses.TlsPolicyOptional,
}, false),
Type: schema.TypeString,
Optional: true,
Default: ses.TlsPolicyOptional,
ValidateFunc: validation.StringInSlice(ses.TlsPolicy_Values(), false),
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
Expand All @@ -60,34 +58,21 @@ func resourceAwsSesConfigurationSetCreate(d *schema.ResourceData, meta interface

_, err := conn.CreateConfigurationSet(createOpts)
if err != nil {
return fmt.Errorf("Error creating SES configuration set: %s", err)
return fmt.Errorf("error creating SES configuration set (%s): %w", configurationSetName, err)
}

d.SetId(configurationSetName)

return resourceAwsSesConfigurationSetUpdate(d, meta)
}

func resourceAwsSesConfigurationSetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesconn

configurationSetName := d.Get("name").(string)

updateOpts := &ses.PutConfigurationSetDeliveryOptionsInput{
ConfigurationSetName: aws.String(configurationSetName),
}

if v, ok := d.GetOk("delivery_options"); ok {
options := v.(*schema.Set).List()
delivery := options[0].(map[string]interface{})
updateOpts.DeliveryOptions = &ses.DeliveryOptions{
TlsPolicy: aws.String(delivery["tls_policy"].(string)),
if v, ok := d.GetOk("delivery_options"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input := &ses.PutConfigurationSetDeliveryOptionsInput{
ConfigurationSetName: aws.String(configurationSetName),
DeliveryOptions: expandSesConfigurationSetDeliveryOptions(v.([]interface{})),
}
}

_, err := conn.PutConfigurationSetDeliveryOptions(updateOpts)
if err != nil {
return fmt.Errorf("Error updating SES configuration set: %s", err)
_, err := conn.PutConfigurationSetDeliveryOptions(input)
if err != nil {
return fmt.Errorf("error adding SES configuration set (%s) delivery options: %w", configurationSetName, err)
}
}

return resourceAwsSesConfigurationSetRead(d, meta)
Expand All @@ -112,23 +97,33 @@ func resourceAwsSesConfigurationSetRead(d *schema.ResourceData, meta interface{}
return err
}

if response.DeliveryOptions != nil {
var deliveryOptions []map[string]interface{}
tlsPolicy := map[string]interface{}{
"tls_policy": response.DeliveryOptions.TlsPolicy,
}
deliveryOptions = append(deliveryOptions, tlsPolicy)

if err := d.Set("delivery_options", deliveryOptions); err != nil {
return fmt.Errorf("Error setting delivery_options for SES configuration set %s: %s", d.Id(), err)
}
if err := d.Set("delivery_options", flattenSesConfigurationSetDeliveryOptions(response.DeliveryOptions)); err != nil {
return fmt.Errorf("error setting delivery_options: %w", err)
}

d.Set("name", aws.StringValue(response.ConfigurationSet.Name))

return nil
}

func resourceAwsSesConfigurationSetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesconn

if d.HasChange("delivery_options") {
input := &ses.PutConfigurationSetDeliveryOptionsInput{
ConfigurationSetName: aws.String(d.Id()),
DeliveryOptions: expandSesConfigurationSetDeliveryOptions(d.Get("delivery_options").([]interface{})),
}

_, err := conn.PutConfigurationSetDeliveryOptions(input)
if err != nil {
return fmt.Errorf("error updating SES configuration set (%s) delivery options: %w", d.Id(), err)
}
}

return resourceAwsSesConfigurationSetRead(d, meta)
}

func resourceAwsSesConfigurationSetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesconn

Expand All @@ -139,3 +134,34 @@ func resourceAwsSesConfigurationSetDelete(d *schema.ResourceData, meta interface

return err
}

func expandSesConfigurationSetDeliveryOptions(l []interface{}) *ses.DeliveryOptions {
if len(l) == 0 || l[0] == nil {
return nil
}

tfMap, ok := l[0].(map[string]interface{})
if !ok {
return nil
}

options := &ses.DeliveryOptions{}

if v, ok := tfMap["tls_policy"].(string); ok && v != "" {
options.TlsPolicy = aws.String(v)
}

return options
}

func flattenSesConfigurationSetDeliveryOptions(options *ses.DeliveryOptions) []interface{} {
if options == nil {
return nil
}

m := map[string]interface{}{
"tls_policy": aws.StringValue(options.TlsPolicy),
}

return []interface{}{m}
}
Loading

0 comments on commit b906144

Please sign in to comment.