Skip to content

Commit

Permalink
provider: Support default tags (resources aws_c*) (#18728)
Browse files Browse the repository at this point in the history
* provider: Support default tags (resources aws_c*)

Reference: #7926

* resource/aws_cloudwatch_composite_alarm: Add missing meta

* docs/provider: Update tagging documentation (resources aws_c*)

* resource/aws_cognito_user_pool: Ensure updates include default tags

Output from acceptance testing:

```
--- PASS: TestAccAWSCognitoUserPool_basic (32.72s)
--- PASS: TestAccAWSCognitoUserPool_disappears (23.07s)
--- PASS: TestAccAWSCognitoUserPool_MfaConfiguration_SmsConfiguration (90.95s)
--- PASS: TestAccAWSCognitoUserPool_MfaConfiguration_SmsConfigurationAndSoftwareTokenMfaConfiguration (81.96s)
--- PASS: TestAccAWSCognitoUserPool_MfaConfiguration_SmsConfigurationToSoftwareTokenMfaConfiguration (52.93s)
--- PASS: TestAccAWSCognitoUserPool_MfaConfiguration_SoftwareTokenMfaConfiguration (46.95s)
--- PASS: TestAccAWSCognitoUserPool_MfaConfiguration_SoftwareTokenMfaConfigurationToSmsConfiguration (47.99s)
--- PASS: TestAccAWSCognitoUserPool_recovery (49.48s)
--- PASS: TestAccAWSCognitoUserPool_schemaAttributes (52.07s)
--- PASS: TestAccAWSCognitoUserPool_schemaAttributesModified (32.71s)
--- PASS: TestAccAWSCognitoUserPool_schemaAttributesRemoved (32.71s)
--- PASS: TestAccAWSCognitoUserPool_SmsAuthenticationMessage (45.81s)
--- PASS: TestAccAWSCognitoUserPool_SmsConfiguration (80.39s)
--- PASS: TestAccAWSCognitoUserPool_SmsConfiguration_ExternalId (75.83s)
--- PASS: TestAccAWSCognitoUserPool_SmsConfiguration_SnsCallerArn (73.17s)
--- PASS: TestAccAWSCognitoUserPool_SmsVerificationMessage (53.22s)
--- PASS: TestAccAWSCognitoUserPool_update (86.20s)
--- PASS: TestAccAWSCognitoUserPool_withAdminCreateUserConfiguration (43.93s)
--- PASS: TestAccAWSCognitoUserPool_withAdminCreateUserConfigurationAndPasswordPolicy (24.82s)
--- PASS: TestAccAWSCognitoUserPool_withAdvancedSecurityMode (69.48s)
--- PASS: TestAccAWSCognitoUserPool_withAliasAttributes (52.96s)
--- PASS: TestAccAWSCognitoUserPool_withDeviceConfiguration (43.98s)
--- PASS: TestAccAWSCognitoUserPool_withEmailConfiguration (28.09s)
--- PASS: TestAccAWSCognitoUserPool_withEmailVerificationMessage (44.74s)
--- PASS: TestAccAWSCognitoUserPool_withLambdaConfig (79.27s)
--- PASS: TestAccAWSCognitoUserPool_withPasswordPolicy (53.72s)
--- PASS: TestAccAWSCognitoUserPool_withTags (70.01s)
--- PASS: TestAccAWSCognitoUserPool_withUsernameConfiguration (53.49s)
--- PASS: TestAccAWSCognitoUserPool_withVerificationMessageTemplate (53.61s)
--- SKIP: TestAccAWSCognitoUserPool_withEmailConfigurationSource (0.00s)
```

* provider: Add missing default tags handling
  • Loading branch information
bflad authored Apr 21, 2021
1 parent 83a19e7 commit be1b29d
Show file tree
Hide file tree
Showing 52 changed files with 567 additions and 200 deletions.
25 changes: 19 additions & 6 deletions aws/resource_aws_cloud9_environment_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,24 @@ func resourceAwsCloud9EnvironmentEc2() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsCloud9EnvironmentEc2Create(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

params := &cloud9.CreateEnvironmentEC2Input{
InstanceType: aws.String(d.Get("instance_type").(string)),
Name: aws.String(d.Get("name").(string)),
ClientRequestToken: aws.String(resource.UniqueId()),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().Cloud9Tags(),
Tags: tags.IgnoreAws().Cloud9Tags(),
}

if v, ok := d.GetOk("automatic_stop_time_minutes"); ok {
Expand Down Expand Up @@ -149,6 +154,7 @@ func resourceAwsCloud9EnvironmentEc2Create(d *schema.ResourceData, meta interfac

func resourceAwsCloud9EnvironmentEc2Read(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

log.Printf("[INFO] Reading Cloud9 Environment EC2 %s", d.Id())
Expand Down Expand Up @@ -184,8 +190,15 @@ func resourceAwsCloud9EnvironmentEc2Read(d *schema.ResourceData, meta interface{
return fmt.Errorf("error listing tags for Cloud9 EC2 Environment (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

log.Printf("[DEBUG] Received Cloud9 Environment EC2: %s", env)
Expand All @@ -211,8 +224,8 @@ func resourceAwsCloud9EnvironmentEc2Update(d *schema.ResourceData, meta interfac

log.Printf("[DEBUG] Cloud9 Environment EC2 updated: %s", out)

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
arn := d.Get("arn").(string)

if err := keyvaluetags.Cloud9UpdateTags(conn, arn, o, n); err != nil {
Expand Down
29 changes: 22 additions & 7 deletions aws/resource_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ func resourceAwsCloudFormationStack() *schema.Resource {
Optional: true,
ForceNew: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"iam_role_arn": {
Type: schema.TypeString,
Optional: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsCloudFormationStackCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

requestToken := resource.UniqueId()
input := cloudformation.CreateStackInput{
Expand Down Expand Up @@ -160,8 +165,8 @@ func resourceAwsCloudFormationStackCreate(d *schema.ResourceData, meta interface
if v, ok := d.GetOk("policy_url"); ok {
input.StackPolicyURL = aws.String(v.(string))
}
if v, ok := d.GetOk("tags"); ok {
input.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CloudformationTags()
if len(tags) > 0 {
input.Tags = tags.IgnoreAws().CloudformationTags()
}
if v, ok := d.GetOk("timeout_in_minutes"); ok {
m := int64(v.(int))
Expand Down Expand Up @@ -198,6 +203,7 @@ func resourceAwsCloudFormationStackCreate(d *schema.ResourceData, meta interface

func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &cloudformation.DescribeStacksInput{
Expand Down Expand Up @@ -269,8 +275,15 @@ func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}
return err
}

if err := d.Set("tags", keyvaluetags.CloudformationKeyValueTags(stack.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.CloudformationKeyValueTags(stack.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

err = d.Set("outputs", flattenCloudFormationOutputs(stack.Outputs))
Expand All @@ -290,6 +303,8 @@ func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}

func resourceAwsCloudFormationStackUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

requestToken := resource.UniqueId()
input := &cloudformation.UpdateStackInput{
Expand Down Expand Up @@ -323,8 +338,8 @@ func resourceAwsCloudFormationStackUpdate(d *schema.ResourceData, meta interface
input.Parameters = expandCloudFormationParameters(v.(map[string]interface{}))
}

if v, ok := d.GetOk("tags"); ok {
input.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CloudformationTags()
if len(tags) > 0 {
input.Tags = tags.IgnoreAws().CloudformationTags()
}

if d.HasChange("policy_body") {
Expand Down
29 changes: 22 additions & 7 deletions aws/resource_aws_cloudformation_stack_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func resourceAwsCloudFormationStackSet() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"template_body": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -122,11 +123,15 @@ func resourceAwsCloudFormationStackSet() *schema.Resource {
ConflictsWith: []string{"template_body"},
},
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsCloudFormationStackSetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))
name := d.Get("name").(string)

input := &cloudformation.CreateStackSetInput{
Expand Down Expand Up @@ -162,8 +167,8 @@ func resourceAwsCloudFormationStackSetCreate(d *schema.ResourceData, meta interf
input.PermissionModel = aws.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok {
input.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CloudformationTags()
if len(tags) > 0 {
input.Tags = tags.IgnoreAws().CloudformationTags()
}

if v, ok := d.GetOk("template_body"); ok {
Expand All @@ -188,6 +193,7 @@ func resourceAwsCloudFormationStackSetCreate(d *schema.ResourceData, meta interf

func resourceAwsCloudFormationStackSetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &cloudformation.DescribeStackSetInput{
Expand Down Expand Up @@ -235,8 +241,15 @@ func resourceAwsCloudFormationStackSetRead(d *schema.ResourceData, meta interfac

d.Set("stack_set_id", stackSet.StackSetId)

if err := d.Set("tags", keyvaluetags.CloudformationKeyValueTags(stackSet.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.CloudformationKeyValueTags(stackSet.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

d.Set("template_body", stackSet.TemplateBody)
Expand All @@ -246,6 +259,8 @@ func resourceAwsCloudFormationStackSetRead(d *schema.ResourceData, meta interfac

func resourceAwsCloudFormationStackSetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

input := &cloudformation.UpdateStackSetInput{
OperationId: aws.String(resource.UniqueId()),
Expand Down Expand Up @@ -278,8 +293,8 @@ func resourceAwsCloudFormationStackSetUpdate(d *schema.ResourceData, meta interf
input.PermissionModel = aws.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok {
input.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CloudformationTags()
if len(tags) > 0 {
input.Tags = tags.IgnoreAws().CloudformationTags()
}

if v, ok := d.GetOk("template_url"); ok {
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,18 +724,23 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Default: false,
},

"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsCloudFrontDistributionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudfrontconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

params := &cloudfront.CreateDistributionWithTagsInput{
DistributionConfigWithTags: &cloudfront.DistributionConfigWithTags{
DistributionConfig: expandDistributionConfig(d),
Tags: &cloudfront.Tags{Items: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CloudfrontTags()},
Tags: &cloudfront.Tags{Items: tags.IgnoreAws().CloudfrontTags()},
},
}

Expand Down Expand Up @@ -781,6 +786,7 @@ func resourceAwsCloudFrontDistributionCreate(d *schema.ResourceData, meta interf

func resourceAwsCloudFrontDistributionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudfrontconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

params := &cloudfront.GetDistributionInput{
Expand Down Expand Up @@ -822,8 +828,15 @@ func resourceAwsCloudFrontDistributionRead(d *schema.ResourceData, meta interfac
if err != nil {
return fmt.Errorf("error listing tags for CloudFront Distribution (%s): %s", d.Id(), err)
}
if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

return nil
Expand Down Expand Up @@ -870,8 +883,8 @@ func resourceAwsCloudFrontDistributionUpdate(d *schema.ResourceData, meta interf
}
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := keyvaluetags.CloudfrontUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for CloudFront Distribution (%s): %s", d.Id(), err)
}
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_cloudhsm_v2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,26 @@ func resourceAwsCloudHsmV2Cluster() *schema.Resource {
Computed: true,
},

"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsCloudHsmV2ClusterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudhsmv2conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

input := &cloudhsmv2.CreateClusterInput{
HsmType: aws.String(d.Get("hsm_type").(string)),
SubnetIds: expandStringSet(d.Get("subnet_ids").(*schema.Set)),
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
input.TagList = keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().Cloudhsmv2Tags()
input.TagList = tags.IgnoreAws().Cloudhsmv2Tags()
}

if v, ok := d.GetOk("source_backup_identifier"); ok {
Expand Down Expand Up @@ -149,6 +154,7 @@ func resourceAwsCloudHsmV2ClusterCreate(d *schema.ResourceData, meta interface{}

func resourceAwsCloudHsmV2ClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudhsmv2conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

cluster, err := finder.Cluster(conn, d.Id())
Expand Down Expand Up @@ -197,8 +203,15 @@ func resourceAwsCloudHsmV2ClusterRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error saving Subnet IDs to state for CloudHSMv2 Cluster (%s): %s", d.Id(), err)
}

if err := d.Set("tags", keyvaluetags.Cloudhsmv2KeyValueTags(cluster.TagList).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.Cloudhsmv2KeyValueTags(cluster.TagList).IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

return nil
Expand All @@ -207,8 +220,8 @@ func resourceAwsCloudHsmV2ClusterRead(d *schema.ResourceData, meta interface{})
func resourceAwsCloudHsmV2ClusterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudhsmv2conn

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := keyvaluetags.Cloudhsmv2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
Expand Down
Loading

0 comments on commit be1b29d

Please sign in to comment.