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_s*) #18804

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
29 changes: 23 additions & 6 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,11 @@ func resourceAwsS3Bucket() *schema.Resource {
},
},

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

CustomizeDiff: SetTagsDiff,
}
}

Expand Down Expand Up @@ -708,8 +711,8 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn

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

// Retry due to S3 eventual consistency
_, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
Expand Down Expand Up @@ -803,6 +806,7 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {

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

input := &s3.HeadBucketInput{
Expand Down Expand Up @@ -1322,16 +1326,29 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

// Retry due to S3 eventual consistency
tags, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
tagsRaw, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return keyvaluetags.S3BucketListTags(s3conn, d.Id())
})

if err != nil {
return fmt.Errorf("error listing tags for S3 Bucket (%s): %s", d.Id(), err)
}

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

if !ok {
return fmt.Errorf("error listing tags for S3 Bucket (%s): unable to convert tags", d.Id())
}

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)
}

arn := arn.ARN{
Expand Down
39 changes: 30 additions & 9 deletions aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -32,7 +33,10 @@ func resourceAwsS3BucketObject() *schema.Resource {
Update: resourceAwsS3BucketObjectUpdate,
Delete: resourceAwsS3BucketObjectDelete,

CustomizeDiff: resourceAwsS3BucketObjectCustomizeDiff,
CustomizeDiff: customdiff.Sequence(
resourceAwsS3BucketObjectCustomizeDiff,
SetTagsDiff,
),

Schema: map[string]*schema.Schema{
"bucket": {
Expand Down Expand Up @@ -156,7 +160,8 @@ func resourceAwsS3BucketObject() *schema.Resource {
Computed: true,
},

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

"website_redirect": {
Type: schema.TypeString,
Expand Down Expand Up @@ -192,6 +197,8 @@ func resourceAwsS3BucketObject() *schema.Resource {

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

var body io.ReadSeeker

Expand Down Expand Up @@ -278,9 +285,9 @@ func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) erro
putInput.ServerSideEncryption = aws.String(s3.ServerSideEncryptionAwsKms)
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if len(tags) > 0 {
// The tag-set must be encoded as URL Query parameters.
putInput.Tagging = aws.String(keyvaluetags.New(v).IgnoreAws().UrlEncode())
putInput.Tagging = aws.String(tags.IgnoreAws().UrlEncode())
}

if v, ok := d.GetOk("website_redirect"); ok {
Expand Down Expand Up @@ -313,6 +320,7 @@ func resourceAwsS3BucketObjectCreate(d *schema.ResourceData, meta interface{}) e

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

bucket := d.Get("bucket").(string)
Expand Down Expand Up @@ -396,16 +404,29 @@ func resourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) err
}

// Retry due to S3 eventual consistency
tags, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
tagsRaw, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return keyvaluetags.S3ObjectListTags(s3conn, bucket, key)
})

if err != nil {
return fmt.Errorf("error listing tags for S3 Bucket (%s) Object (%s): %s", bucket, key, err)
}

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

if !ok {
return fmt.Errorf("error listing tags for S3 Bucket (%s) Object (%s): unable to convert tags", bucket, key)
}

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 @@ -471,8 +492,8 @@ func resourceAwsS3BucketObjectUpdate(d *schema.ResourceData, meta interface{}) e
}
}

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

if err := keyvaluetags.S3ObjectUpdateTags(conn, bucket, key, o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
Expand Down
30 changes: 25 additions & 5 deletions aws/resource_aws_s3_object_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ func resourceAwsS3ObjectCopy() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringInSlice(s3.TaggingDirective_Values(), false),
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"version_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -281,6 +282,8 @@ func resourceAwsS3ObjectCopy() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

Expand All @@ -290,6 +293,7 @@ func resourceAwsS3ObjectCopyCreate(d *schema.ResourceData, meta interface{}) err

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

bucket := d.Get("bucket").(string)
Expand Down Expand Up @@ -356,18 +360,31 @@ func resourceAwsS3ObjectCopyRead(d *schema.ResourceData, meta interface{}) error
}

// Retry due to S3 eventual consistency
tags, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
tagsRaw, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return keyvaluetags.S3ObjectListTags(s3conn, bucket, key)
})

if err != nil {
return fmt.Errorf("error listing tags for S3 Bucket (%s) Object (%s): %w", bucket, key, err)
}

if err := d.Set("tags", tags.(keyvaluetags.KeyValueTags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
tags, ok := tagsRaw.(keyvaluetags.KeyValueTags)

if !ok {
return fmt.Errorf("error listing tags for S3 Bucket (%s) Object (%s): unable to convert tags", bucket, key)
}

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 @@ -417,6 +434,7 @@ func resourceAwsS3ObjectCopyUpdate(d *schema.ResourceData, meta interface{}) err
"storage_class",
"tagging_directive",
"tags",
"tags_all",
"website_redirect",
}
if d.HasChanges(args...) {
Expand Down Expand Up @@ -451,6 +469,8 @@ func resourceAwsS3ObjectCopyDelete(d *schema.ResourceData, meta interface{}) err

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

input := &s3.CopyObjectInput{
Bucket: aws.String(d.Get("bucket").(string)),
Expand Down Expand Up @@ -592,9 +612,9 @@ func resourceAwsS3ObjectCopyDoCopy(d *schema.ResourceData, meta interface{}) err
input.TaggingDirective = aws.String(v.(string))
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if len(tags) > 0 {
// The tag-set must be encoded as URL Query parameters.
input.Tagging = aws.String(keyvaluetags.New(v).IgnoreAws().UrlEncode())
input.Tagging = aws.String(tags.IgnoreAws().UrlEncode())
}

if v, ok := d.GetOk("website_redirect"); ok {
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_s3control_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ func resourceAwsS3ControlBucket() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: SetTagsDiff,
}
}

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

bucket := d.Get("bucket").(string)

Expand All @@ -91,8 +96,8 @@ func resourceAwsS3ControlBucketCreate(d *schema.ResourceData, meta interface{})

d.SetId(aws.StringValue(output.BucketArn))

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.S3controlBucketUpdateTags(conn, d.Id(), nil, v); err != nil {
if len(tags) > 0 {
if err := keyvaluetags.S3controlBucketUpdateTags(conn, d.Id(), nil, tags); err != nil {
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("error adding S3 Control Bucket (%s) tags: %w", d.Id(), err)
}
}
Expand All @@ -102,6 +107,7 @@ func resourceAwsS3ControlBucketCreate(d *schema.ResourceData, meta interface{})

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

parsedArn, err := arn.Parse(d.Id())
Expand Down Expand Up @@ -160,18 +166,25 @@ func resourceAwsS3ControlBucketRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("error listing tags for S3 Control Bucket (%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
}

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

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

if err := keyvaluetags.S3controlBucketUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating S3 Control Bucket (%s) tags: %w", d.Id(), err)
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_sagemaker_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,23 @@ func resourceAwsSagemakerApp() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"user_profile_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
},

CustomizeDiff: SetTagsDiff,
}
}

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

input := &sagemaker.CreateAppInput{
AppName: aws.String(d.Get("app_name").(string)),
Expand All @@ -93,8 +98,8 @@ func resourceAwsSagemakerAppCreate(d *schema.ResourceData, meta interface{}) err
UserProfileName: aws.String(d.Get("user_profile_name").(string)),
}

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

if v, ok := d.GetOk("resource_spec"); ok {
Expand Down Expand Up @@ -124,6 +129,7 @@ func resourceAwsSagemakerAppCreate(d *schema.ResourceData, meta interface{}) err

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

domainID, userProfileName, appType, appName, err := decodeSagemakerAppID(d.Id())
Expand Down Expand Up @@ -164,18 +170,25 @@ func resourceAwsSagemakerAppRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error listing tags for SageMaker App (%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
}

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

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

if err := keyvaluetags.SagemakerUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating SageMaker App (%s) tags: %w", d.Id(), err)
Expand Down
Loading