diff --git a/internal/service/sesv2/configuration_set.go b/internal/service/sesv2/configuration_set.go index 0e31bf0b541..1a9be61fe56 100644 --- a/internal/service/sesv2/configuration_set.go +++ b/internal/service/sesv2/configuration_set.go @@ -5,7 +5,6 @@ package sesv2 import ( "context" - "errors" "fmt" "log" "time" @@ -22,6 +21,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/create" "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" + "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -197,8 +197,9 @@ func resourceConfigurationSetCreate(ctx context.Context, d *schema.ResourceData, var diags diag.Diagnostics conn := meta.(*conns.AWSClient).SESV2Client(ctx) + name := d.Get("configuration_set_name").(string) input := &sesv2.CreateConfigurationSetInput{ - ConfigurationSetName: aws.String(d.Get("configuration_set_name").(string)), + ConfigurationSetName: aws.String(name), Tags: getTagsIn(ctx), } @@ -236,16 +237,13 @@ func resourceConfigurationSetCreate(ctx context.Context, d *schema.ResourceData, input.VdmOptions = expandVDMOptions(v.([]interface{})[0].(map[string]interface{})) } - out, err := conn.CreateConfigurationSet(ctx, input) - if err != nil { - return create.AppendDiagError(diags, names.SESV2, create.ErrActionCreating, resNameConfigurationSet, d.Get("configuration_set_name").(string), err) - } + _, err := conn.CreateConfigurationSet(ctx, input) - if out == nil { - return create.AppendDiagError(diags, names.SESV2, create.ErrActionCreating, resNameConfigurationSet, d.Get("configuration_set_name").(string), errors.New("empty output")) + if err != nil { + return create.AppendDiagError(diags, names.SESV2, create.ErrActionCreating, resNameConfigurationSet, name, err) } - d.SetId(d.Get("configuration_set_name").(string)) + d.SetId(name) return append(diags, resourceConfigurationSetRead(ctx, d, meta)...) } @@ -254,7 +252,7 @@ func resourceConfigurationSetRead(ctx context.Context, d *schema.ResourceData, m var diags diag.Diagnostics conn := meta.(*conns.AWSClient).SESV2Client(ctx) - out, err := findConfigurationSetByID(ctx, conn, d.Id()) + output, err := findConfigurationSetByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] SESV2 ConfigurationSet (%s) not found, removing from state", d.Id()) @@ -266,51 +264,45 @@ func resourceConfigurationSetRead(ctx context.Context, d *schema.ResourceData, m return create.AppendDiagError(diags, names.SESV2, create.ErrActionReading, resNameConfigurationSet, d.Id(), err) } - d.Set(names.AttrARN, configurationSetARN(ctx, meta.(*conns.AWSClient), aws.ToString(out.ConfigurationSetName))) - d.Set("configuration_set_name", out.ConfigurationSetName) - - if out.DeliveryOptions != nil { - if err := d.Set("delivery_options", []interface{}{flattenDeliveryOptions(out.DeliveryOptions)}); err != nil { + d.Set(names.AttrARN, configurationSetARN(ctx, meta.(*conns.AWSClient), aws.ToString(output.ConfigurationSetName))) + d.Set("configuration_set_name", output.ConfigurationSetName) + if output.DeliveryOptions != nil { + if err := d.Set("delivery_options", []interface{}{flattenDeliveryOptions(output.DeliveryOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { d.Set("delivery_options", nil) } - - if out.ReputationOptions != nil { - if err := d.Set("reputation_options", []interface{}{flattenReputationOptions(out.ReputationOptions)}); err != nil { + if output.ReputationOptions != nil { + if err := d.Set("reputation_options", []interface{}{flattenReputationOptions(output.ReputationOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { d.Set("reputation_options", nil) } - - if out.SendingOptions != nil { - if err := d.Set("sending_options", []interface{}{flattenSendingOptions(out.SendingOptions)}); err != nil { + if output.SendingOptions != nil { + if err := d.Set("sending_options", []interface{}{flattenSendingOptions(output.SendingOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { d.Set("sending_options", nil) } - - if out.SuppressionOptions != nil { - if err := d.Set("suppression_options", []interface{}{flattenSuppressionOptions(out.SuppressionOptions)}); err != nil { + if output.SuppressionOptions != nil { + if err := d.Set("suppression_options", []interface{}{flattenSuppressionOptions(output.SuppressionOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { d.Set("suppression_options", nil) } - - if out.TrackingOptions != nil { - if err := d.Set("tracking_options", []interface{}{flattenTrackingOptions(out.TrackingOptions)}); err != nil { + if output.TrackingOptions != nil { + if err := d.Set("tracking_options", []interface{}{flattenTrackingOptions(output.TrackingOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { d.Set("tracking_options", nil) } - - if out.VdmOptions != nil { - if err := d.Set("vdm_options", []interface{}{flattenVDMOptions(out.VdmOptions)}); err != nil { + if output.VdmOptions != nil { + if err := d.Set("vdm_options", []interface{}{flattenVDMOptions(output.VdmOptions)}); err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionSetting, resNameConfigurationSet, d.Id(), err) } } else { @@ -325,7 +317,7 @@ func resourceConfigurationSetUpdate(ctx context.Context, d *schema.ResourceData, conn := meta.(*conns.AWSClient).SESV2Client(ctx) if d.HasChanges("delivery_options") { - in := &sesv2.PutConfigurationSetDeliveryOptionsInput{ + input := &sesv2.PutConfigurationSetDeliveryOptionsInput{ ConfigurationSetName: aws.String(d.Id()), } @@ -333,27 +325,27 @@ func resourceConfigurationSetUpdate(ctx context.Context, d *schema.ResourceData, tfMap := v.([]interface{})[0].(map[string]interface{}) if v, ok := tfMap["max_delivery_seconds"].(int); ok { - in.MaxDeliverySeconds = aws.Int64(int64(v)) + input.MaxDeliverySeconds = aws.Int64(int64(v)) } if v, ok := tfMap["sending_pool_name"].(string); ok && v != "" { - in.SendingPoolName = aws.String(v) + input.SendingPoolName = aws.String(v) } if v, ok := tfMap["tls_policy"].(string); ok && v != "" { - in.TlsPolicy = types.TlsPolicy(v) + input.TlsPolicy = types.TlsPolicy(v) } } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet DeliveryOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetDeliveryOptions(ctx, in) + _, err := conn.PutConfigurationSetDeliveryOptions(ctx, input) + if err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } } if d.HasChanges("reputation_options") { - in := &sesv2.PutConfigurationSetReputationOptionsInput{ + input := &sesv2.PutConfigurationSetReputationOptionsInput{ ConfigurationSetName: aws.String(d.Id()), } @@ -361,39 +353,39 @@ func resourceConfigurationSetUpdate(ctx context.Context, d *schema.ResourceData, tfMap := v.([]interface{})[0].(map[string]interface{}) if v, ok := tfMap["reputation_metrics_enabled"].(bool); ok { - in.ReputationMetricsEnabled = v + input.ReputationMetricsEnabled = v } } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet ReputationOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetReputationOptions(ctx, in) + _, err := conn.PutConfigurationSetReputationOptions(ctx, input) + if err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } } if d.HasChanges("sending_options") { + input := &sesv2.PutConfigurationSetSendingOptionsInput{ + ConfigurationSetName: aws.String(d.Id()), + } + if v, ok := d.GetOk("sending_options"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { tfMap := v.([]interface{})[0].(map[string]interface{}) - in := &sesv2.PutConfigurationSetSendingOptionsInput{ - ConfigurationSetName: aws.String(d.Id()), - } - if v, ok := tfMap["sending_enabled"].(bool); ok { - in.SendingEnabled = v + input.SendingEnabled = v } + } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet SendingOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetSendingOptions(ctx, in) - if err != nil { - return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) - } + _, err := conn.PutConfigurationSetSendingOptions(ctx, input) + + if err != nil { + return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } } if d.HasChanges("suppression_options") { - in := &sesv2.PutConfigurationSetSuppressionOptionsInput{ + input := &sesv2.PutConfigurationSetSuppressionOptionsInput{ ConfigurationSetName: aws.String(d.Id()), } @@ -401,19 +393,19 @@ func resourceConfigurationSetUpdate(ctx context.Context, d *schema.ResourceData, tfMap := v.([]interface{})[0].(map[string]interface{}) if v, ok := tfMap["suppressed_reasons"].([]interface{}); ok && len(v) > 0 { - in.SuppressedReasons = expandSuppressedReasons(v) + input.SuppressedReasons = flex.ExpandStringyValueList[types.SuppressionListReason](v) } } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet SuppressionOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetSuppressionOptions(ctx, in) + _, err := conn.PutConfigurationSetSuppressionOptions(ctx, input) + if err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } } if d.HasChanges("tracking_options") { - in := &sesv2.PutConfigurationSetTrackingOptionsInput{ + input := &sesv2.PutConfigurationSetTrackingOptionsInput{ ConfigurationSetName: aws.String(d.Id()), } @@ -421,32 +413,32 @@ func resourceConfigurationSetUpdate(ctx context.Context, d *schema.ResourceData, tfMap := v.([]interface{})[0].(map[string]interface{}) if v, ok := tfMap["custom_redirect_domain"].(string); ok && v != "" { - in.CustomRedirectDomain = aws.String(v) + input.CustomRedirectDomain = aws.String(v) } if v, ok := tfMap["https_policy"].(string); ok && v != "" { - in.HttpsPolicy = types.HttpsPolicy(v) + input.HttpsPolicy = types.HttpsPolicy(v) } } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet TrackingOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetTrackingOptions(ctx, in) + _, err := conn.PutConfigurationSetTrackingOptions(ctx, input) + if err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } } if d.HasChanges("vdm_options") { - in := &sesv2.PutConfigurationSetVdmOptionsInput{ + input := &sesv2.PutConfigurationSetVdmOptionsInput{ ConfigurationSetName: aws.String(d.Id()), } if v, ok := d.GetOk("vdm_options"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { - in.VdmOptions = expandVDMOptions(v.([]interface{})[0].(map[string]interface{})) + input.VdmOptions = expandVDMOptions(v.([]interface{})[0].(map[string]interface{})) } - log.Printf("[DEBUG] Updating SESV2 ConfigurationSet VdmOptions (%s): %#v", d.Id(), in) - _, err := conn.PutConfigurationSetVdmOptions(ctx, in) + _, err := conn.PutConfigurationSetVdmOptions(ctx, input) + if err != nil { return create.AppendDiagError(diags, names.SESV2, create.ErrActionUpdating, resNameConfigurationSet, d.Id(), err) } @@ -509,19 +501,19 @@ func flattenDeliveryOptions(apiObject *types.DeliveryOptions) map[string]interfa return nil } - m := map[string]interface{}{} + tfMap := map[string]interface{}{ + "tls_policy": apiObject.TlsPolicy, + } if v := apiObject.MaxDeliverySeconds; v != nil { - m["max_delivery_seconds"] = aws.ToInt64(v) + tfMap["max_delivery_seconds"] = aws.ToInt64(v) } if v := apiObject.SendingPoolName; v != nil { - m["sending_pool_name"] = aws.ToString(v) + tfMap["sending_pool_name"] = aws.ToString(v) } - m["tls_policy"] = string(apiObject.TlsPolicy) - - return m + return tfMap } func flattenReputationOptions(apiObject *types.ReputationOptions) map[string]interface{} { @@ -529,15 +521,15 @@ func flattenReputationOptions(apiObject *types.ReputationOptions) map[string]int return nil } - m := map[string]interface{}{ + tfMap := map[string]interface{}{ "reputation_metrics_enabled": apiObject.ReputationMetricsEnabled, } if v := apiObject.LastFreshStart; v != nil { - m["last_fresh_start"] = v.Format(time.RFC3339) + tfMap["last_fresh_start"] = v.Format(time.RFC3339) } - return m + return tfMap } func flattenSendingOptions(apiObject *types.SendingOptions) map[string]interface{} { @@ -545,11 +537,11 @@ func flattenSendingOptions(apiObject *types.SendingOptions) map[string]interface return nil } - m := map[string]interface{}{ + tfMap := map[string]interface{}{ "sending_enabled": apiObject.SendingEnabled, } - return m + return tfMap } func flattenSuppressionOptions(apiObject *types.SuppressionOptions) map[string]interface{} { @@ -557,23 +549,13 @@ func flattenSuppressionOptions(apiObject *types.SuppressionOptions) map[string]i return nil } - m := map[string]interface{}{} + tfMap := map[string]interface{}{} if v := apiObject.SuppressedReasons; v != nil { - m["suppressed_reasons"] = flattenSuppressedReasons(apiObject.SuppressedReasons) - } - - return m -} - -func flattenSuppressedReasons(apiObject []types.SuppressionListReason) []string { - var out []string - - for _, v := range apiObject { - out = append(out, string(v)) + tfMap["suppressed_reasons"] = apiObject.SuppressedReasons } - return out + return tfMap } func flattenTrackingOptions(apiObject *types.TrackingOptions) map[string]interface{} { @@ -581,15 +563,15 @@ func flattenTrackingOptions(apiObject *types.TrackingOptions) map[string]interfa return nil } - m := map[string]interface{}{} + tfMap := map[string]interface{}{ + "https_policy": apiObject.HttpsPolicy, + } if v := apiObject.CustomRedirectDomain; v != nil { - m["custom_redirect_domain"] = aws.ToString(v) + tfMap["custom_redirect_domain"] = aws.ToString(v) } - m["https_policy"] = string(apiObject.HttpsPolicy) - - return m + return tfMap } func flattenVDMOptions(apiObject *types.VdmOptions) map[string]interface{} { @@ -597,17 +579,17 @@ func flattenVDMOptions(apiObject *types.VdmOptions) map[string]interface{} { return nil } - m := map[string]interface{}{} + tfMap := map[string]interface{}{} if v := apiObject.DashboardOptions; v != nil { - m["dashboard_options"] = []interface{}{flattenDashboardOptions(v)} + tfMap["dashboard_options"] = []interface{}{flattenDashboardOptions(v)} } if v := apiObject.GuardianOptions; v != nil { - m["guardian_options"] = []interface{}{flattenGuardianOptions(v)} + tfMap["guardian_options"] = []interface{}{flattenGuardianOptions(v)} } - return m + return tfMap } func flattenDashboardOptions(apiObject *types.DashboardOptions) map[string]interface{} { @@ -615,11 +597,11 @@ func flattenDashboardOptions(apiObject *types.DashboardOptions) map[string]inter return nil } - m := map[string]interface{}{ - "engagement_metrics": string(apiObject.EngagementMetrics), + tfMap := map[string]interface{}{ + "engagement_metrics": apiObject.EngagementMetrics, } - return m + return tfMap } func flattenGuardianOptions(apiObject *types.GuardianOptions) map[string]interface{} { @@ -627,11 +609,11 @@ func flattenGuardianOptions(apiObject *types.GuardianOptions) map[string]interfa return nil } - m := map[string]interface{}{ - "optimized_shared_delivery": string(apiObject.OptimizedSharedDelivery), + tfMap := map[string]interface{}{ + "optimized_shared_delivery": apiObject.OptimizedSharedDelivery, } - return m + return tfMap } func expandDeliveryOptions(tfMap map[string]interface{}) *types.DeliveryOptions { @@ -639,21 +621,21 @@ func expandDeliveryOptions(tfMap map[string]interface{}) *types.DeliveryOptions return nil } - a := &types.DeliveryOptions{} + apiObject := &types.DeliveryOptions{} if v, ok := tfMap["max_delivery_seconds"].(int); ok { - a.MaxDeliverySeconds = aws.Int64(int64(v)) + apiObject.MaxDeliverySeconds = aws.Int64(int64(v)) } if v, ok := tfMap["sending_pool_name"].(string); ok && v != "" { - a.SendingPoolName = aws.String(v) + apiObject.SendingPoolName = aws.String(v) } if v, ok := tfMap["tls_policy"].(string); ok && v != "" { - a.TlsPolicy = types.TlsPolicy(v) + apiObject.TlsPolicy = types.TlsPolicy(v) } - return a + return apiObject } func expandReputationOptions(tfMap map[string]interface{}) *types.ReputationOptions { @@ -661,13 +643,13 @@ func expandReputationOptions(tfMap map[string]interface{}) *types.ReputationOpti return nil } - a := &types.ReputationOptions{} + apiObject := &types.ReputationOptions{} if v, ok := tfMap["reputation_metrics_enabled"].(bool); ok { - a.ReputationMetricsEnabled = v + apiObject.ReputationMetricsEnabled = v } - return a + return apiObject } func expandSendingOptions(tfMap map[string]interface{}) *types.SendingOptions { @@ -675,13 +657,13 @@ func expandSendingOptions(tfMap map[string]interface{}) *types.SendingOptions { return nil } - a := &types.SendingOptions{} + apiObject := &types.SendingOptions{} if v, ok := tfMap["sending_enabled"].(bool); ok { - a.SendingEnabled = v + apiObject.SendingEnabled = v } - return a + return apiObject } func expandSuppressionOptions(tfMap map[string]interface{}) *types.SuppressionOptions { @@ -693,7 +675,7 @@ func expandSuppressionOptions(tfMap map[string]interface{}) *types.SuppressionOp if v, ok := tfMap["suppressed_reasons"].([]interface{}); ok { if len(v) > 0 { - apiObject.SuppressedReasons = expandSuppressedReasons(v) + apiObject.SuppressedReasons = flex.ExpandStringyValueList[types.SuppressionListReason](v) } else { apiObject.SuppressedReasons = make([]types.SuppressionListReason, 0) } @@ -702,34 +684,22 @@ func expandSuppressionOptions(tfMap map[string]interface{}) *types.SuppressionOp return apiObject } -func expandSuppressedReasons(tfList []interface{}) []types.SuppressionListReason { - var out []types.SuppressionListReason - - for _, v := range tfList { - if v, ok := v.(string); ok && v != "" { - out = append(out, types.SuppressionListReason(v)) - } - } - - return out -} - func expandTrackingOptions(tfMap map[string]interface{}) *types.TrackingOptions { if tfMap == nil { return nil } - a := &types.TrackingOptions{} + apiObject := &types.TrackingOptions{} if v, ok := tfMap["custom_redirect_domain"].(string); ok && v != "" { - a.CustomRedirectDomain = aws.String(v) + apiObject.CustomRedirectDomain = aws.String(v) } if v, ok := tfMap["https_policy"].(string); ok && v != "" { - a.HttpsPolicy = types.HttpsPolicy(v) + apiObject.HttpsPolicy = types.HttpsPolicy(v) } - return a + return apiObject } func expandVDMOptions(tfMap map[string]interface{}) *types.VdmOptions { @@ -737,17 +707,17 @@ func expandVDMOptions(tfMap map[string]interface{}) *types.VdmOptions { return nil } - a := &types.VdmOptions{} + apiObject := &types.VdmOptions{} if v, ok := tfMap["dashboard_options"].([]interface{}); ok && len(v) > 0 && v[0] != nil { - a.DashboardOptions = expandDashboardOptions(v[0].(map[string]interface{})) + apiObject.DashboardOptions = expandDashboardOptions(v[0].(map[string]interface{})) } if v, ok := tfMap["guardian_options"].([]interface{}); ok && len(v) > 0 && v[0] != nil { - a.GuardianOptions = expandGuardianOptions(v[0].(map[string]interface{})) + apiObject.GuardianOptions = expandGuardianOptions(v[0].(map[string]interface{})) } - return a + return apiObject } func expandDashboardOptions(tfMap map[string]interface{}) *types.DashboardOptions { @@ -755,13 +725,13 @@ func expandDashboardOptions(tfMap map[string]interface{}) *types.DashboardOption return nil } - a := &types.DashboardOptions{} + apiObject := &types.DashboardOptions{} if v, ok := tfMap["engagement_metrics"].(string); ok && v != "" { - a.EngagementMetrics = types.FeatureStatus(v) + apiObject.EngagementMetrics = types.FeatureStatus(v) } - return a + return apiObject } func expandGuardianOptions(tfMap map[string]interface{}) *types.GuardianOptions { @@ -769,13 +739,13 @@ func expandGuardianOptions(tfMap map[string]interface{}) *types.GuardianOptions return nil } - a := &types.GuardianOptions{} + apiObject := &types.GuardianOptions{} if v, ok := tfMap["optimized_shared_delivery"].(string); ok && v != "" { - a.OptimizedSharedDelivery = types.FeatureStatus(v) + apiObject.OptimizedSharedDelivery = types.FeatureStatus(v) } - return a + return apiObject } func configurationSetARN(ctx context.Context, c *conns.AWSClient, configurationSetName string) string {