Skip to content

Commit

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

Reference: #7926

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

* resource/aws_batch_job_definition: Fix rebase issue
  • Loading branch information
bflad authored Apr 21, 2021
1 parent 254550f commit be69e9b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 35 deletions.
20 changes: 16 additions & 4 deletions aws/resource_aws_backup_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,23 @@ func resourceAwsBackupPlan() *schema.Resource {
},
"tags": tagsSchema(),
},

CustomizeDiff: SetTagsDiff,
}
}

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

input := &backup.CreateBackupPlanInput{
BackupPlan: &backup.PlanInput{
BackupPlanName: aws.String(d.Get("name").(string)),
Rules: expandBackupPlanRules(d.Get("rule").(*schema.Set)),
AdvancedBackupSettings: expandBackupPlanAdvancedBackupSettings(d.Get("advanced_backup_setting").(*schema.Set)),
},
BackupPlanTags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().BackupTags(),
BackupPlanTags: tags.IgnoreAws().BackupTags(),
}

log.Printf("[DEBUG] Creating Backup Plan: %#v", input)
Expand All @@ -182,6 +186,7 @@ func resourceAwsBackupPlanCreate(d *schema.ResourceData, meta interface{}) error

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

resp, err := conn.GetBackupPlan(&backup.GetBackupPlanInput{
Expand Down Expand Up @@ -214,10 +219,17 @@ func resourceAwsBackupPlanRead(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("error listing tags for Backup Plan (%s): %w", d.Id(), err)
}
if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
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 All @@ -241,8 +253,8 @@ func resourceAwsBackupPlanUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := keyvaluetags.BackupUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for Backup Plan (%s): %w", d.Id(), err)
}
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_backup_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func resourceAwsBackupVault() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]{1,50}$`), "must consist of lowercase letters, numbers, and hyphens."),
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -46,15 +47,19 @@ func resourceAwsBackupVault() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

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

input := &backup.CreateBackupVaultInput{
BackupVaultName: aws.String(d.Get("name").(string)),
BackupVaultTags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().BackupTags(),
BackupVaultTags: tags.IgnoreAws().BackupTags(),
}

if v, ok := d.GetOk("kms_key_arn"); ok {
Expand All @@ -73,6 +78,7 @@ func resourceAwsBackupVaultCreate(d *schema.ResourceData, meta interface{}) erro

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

input := &backup.DescribeBackupVaultInput{
Expand Down Expand Up @@ -103,8 +109,15 @@ func resourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf("error listing tags for Backup Vault (%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 All @@ -113,8 +126,8 @@ func resourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error
func resourceAwsBackupVaultUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := keyvaluetags.BackupUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for Backup Vault (%s): %s", d.Id(), err)
}
Expand Down
27 changes: 20 additions & 7 deletions aws/resource_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func resourceAwsBatchComputeEnvironment() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{batch.CEStateEnabled, batch.CEStateDisabled}, true),
Default: batch.CEStateEnabled,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Required: true,
Expand All @@ -188,11 +189,15 @@ func resourceAwsBatchComputeEnvironment() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

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

// Build the compute environment name.
var computeEnvironmentName string
Expand All @@ -218,8 +223,8 @@ func resourceAwsBatchComputeEnvironmentCreate(d *schema.ResourceData, meta inter
input.State = aws.String(v.(string))
}

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

if computeEnvironmentType == batch.CETypeManaged {
Expand Down Expand Up @@ -320,6 +325,7 @@ func resourceAwsBatchComputeEnvironmentCreate(d *schema.ResourceData, meta inter

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

computeEnvironmentName := d.Get("compute_environment_name").(string)
Expand Down Expand Up @@ -349,8 +355,15 @@ func resourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta interfa
d.Set("service_role", computeEnvironment.ServiceRole)
d.Set("state", computeEnvironment.State)

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(computeEnvironment.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.BatchKeyValueTags(computeEnvironment.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("type", computeEnvironment.Type)
Expand Down Expand Up @@ -471,8 +484,8 @@ func resourceAwsBatchComputeEnvironmentUpdate(d *schema.ResourceData, meta inter
}
}

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

if err := keyvaluetags.BatchUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_batch_job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func resourceAwsBatchJobDefinition() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"propagate_tags": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -167,11 +168,15 @@ func resourceAwsBatchJobDefinition() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

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

input := &batch.RegisterJobDefinitionInput{
Expand Down Expand Up @@ -201,8 +206,8 @@ func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{
input.RetryStrategy = expandBatchRetryStrategy(v.([]interface{})[0].(map[string]interface{}))
}

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

if v, ok := d.GetOk("timeout"); ok {
Expand All @@ -222,6 +227,7 @@ func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{

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

jobDefinition, err := finder.JobDefinitionByARN(conn, d.Id())
Expand Down Expand Up @@ -261,10 +267,17 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{})
d.Set("retry_strategy", nil)
}

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(jobDefinition.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
tags := keyvaluetags.BatchKeyValueTags(jobDefinition.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)
}

if err := d.Set("timeout", flattenBatchJobTimeout(jobDefinition.Timeout)); err != nil {
return fmt.Errorf("error setting timeout: %w", err)
}
Expand All @@ -278,8 +291,8 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{})
func resourceAwsBatchJobDefinitionUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn

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

if err := keyvaluetags.BatchUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %w", err)
Expand Down
27 changes: 20 additions & 7 deletions aws/resource_aws_batch_job_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,31 @@ func resourceAwsBatchJobQueue() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice([]string{batch.JQStateDisabled, batch.JQStateEnabled}, true),
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsBatchJobQueueCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))
input := batch.CreateJobQueueInput{
ComputeEnvironmentOrder: createComputeEnvironmentOrder(d.Get("compute_environments").([]interface{})),
JobQueueName: aws.String(d.Get("name").(string)),
Priority: aws.Int64(int64(d.Get("priority").(int))),
State: aws.String(d.Get("state").(string)),
}

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

name := d.Get("name").(string)
Expand Down Expand Up @@ -100,6 +105,7 @@ func resourceAwsBatchJobQueueCreate(d *schema.ResourceData, meta interface{}) er

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

jq, err := getJobQueue(conn, d.Id())
Expand Down Expand Up @@ -132,8 +138,15 @@ func resourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) erro
d.Set("priority", jq.Priority)
d.Set("state", jq.State)

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(jq.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.BatchKeyValueTags(jq.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 @@ -169,8 +182,8 @@ func resourceAwsBatchJobQueueUpdate(d *schema.ResourceData, meta interface{}) er
}
}

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

if err := keyvaluetags.BatchUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/backup_plan.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The following arguments are supported:
* `name` - (Required) The display name of a backup plan.
* `rule` - (Required) A rule object that specifies a scheduled task that is used to back up a selection of resources.
* `advanced_backup_setting` - (Optional) An object that specifies backup options for each resource type.
* `tags` - (Optional) Metadata that you can assign to help organize the plans you create.
* `tags` - (Optional) Metadata that you can assign to help organize the plans you create. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

### Rule Arguments
For **rule** the following attributes are supported:
Expand Down Expand Up @@ -77,6 +77,7 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the backup plan.
* `arn` - The ARN of the backup plan.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).
* `version` - Unique, randomly generated, Unicode, UTF-8 encoded string that serves as the version ID of the backup plan.

## Import
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/backup_vault.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "aws_backup_vault" "example" {
The following arguments are supported:

* `name` - (Required) Name of the backup vault to create.
* `tags` - (Optional) Metadata that you can assign to help organize the resources that you create.
* `tags` - (Optional) Metadata that you can assign to help organize the resources that you create. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `kms_key_arn` - (Optional) The server-side encryption key that is used to protect your backups.

## Attributes Reference
Expand All @@ -34,6 +34,7 @@ In addition to all arguments above, the following attributes are exported:
* `id` - The name of the vault.
* `arn` - The ARN of the vault.
* `recovery_points` - The number of recovery points that are stored in a backup vault.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

## Import

Expand Down
Loading

0 comments on commit be69e9b

Please sign in to comment.