Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: Support default tags (resources aws_b*) #18715

Merged
merged 4 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
"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
27 changes: 20 additions & 7 deletions aws/resource_aws_batch_job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func resourceAwsBatchJobDefinition() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"timeout": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -104,11 +105,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 All @@ -132,8 +137,8 @@ func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{
input.RetryStrategy = expandJobDefinitionRetryStrategy(v.([]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 @@ -151,6 +156,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

arn := d.Get("arn").(string)
Expand Down Expand Up @@ -182,8 +188,15 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting retry_strategy: %s", err)
}

if err := d.Set("tags", keyvaluetags.BatchKeyValueTags(job.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.BatchKeyValueTags(job.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(job.Timeout)); err != nil {
Expand All @@ -198,8 +211,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.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", 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