Skip to content

Commit

Permalink
Merge pull request hashicorp#18724 from hashicorp/f-default-tags-l
Browse files Browse the repository at this point in the history
provider: Support default tags (resources aws_l*)
  • Loading branch information
anGie44 authored Apr 20, 2021
2 parents 153d480 + ee8dac2 commit f107406
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 45 deletions.
24 changes: 18 additions & 6 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,14 @@ func resourceAwsLambdaFunction() *schema.Resource {
Optional: true,
ValidateFunc: validateArn,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: customdiff.Sequence(
checkHandlerRuntimeForZipFunction,
updateComputedAttributesOnPublish,
SetTagsDiff,
),
}
}
Expand Down Expand Up @@ -373,6 +375,8 @@ func hasConfigChanges(d resourceDiffer) bool {
// CreateFunction in the API / SDK
func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

functionName := d.Get("function_name").(string)
reservedConcurrentExecutions := d.Get("reserved_concurrent_executions").(int)
Expand Down Expand Up @@ -512,8 +516,8 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
params.KMSKeyArn = aws.String(v.(string))
}

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

err := resource.Retry(waiter.LambdaFunctionCreateTimeout, func() *resource.RetryError { // nosem: helper-schema-resource-Retry-without-TimeoutError-check
Expand Down Expand Up @@ -624,6 +628,7 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
// GetFunction in the API / SDK
func resourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

params := &lambda.GetFunctionInput{
Expand Down Expand Up @@ -657,9 +662,16 @@ func resourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) err
// Tagging operations are permitted on Lambda functions only.
// Tags on aliases and versions are not supported.
if !qualifierExistance {
if err := d.Set("tags", keyvaluetags.LambdaKeyValueTags(getFunctionOutput.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
tags := keyvaluetags.LambdaKeyValueTags(getFunctionOutput.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)
}
}

// getFunctionOutput.Code.Location is a pre-signed URL pointing at the zip
Expand Down Expand Up @@ -931,8 +943,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
}

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

if err := keyvaluetags.LambdaUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating Lambda Function (%s) tags: %w", arn, err)
Expand Down
25 changes: 18 additions & 7 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"hibernation_options": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -642,12 +642,15 @@ func resourceAwsLaunchTemplate() *schema.Resource {
}
return false
}),
SetTagsDiff,
),
}
}

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

var ltName string
if v, ok := d.GetOk("name"); ok {
Expand All @@ -667,7 +670,7 @@ func resourceAwsLaunchTemplateCreate(d *schema.ResourceData, meta interface{}) e
ClientToken: aws.String(resource.UniqueId()),
LaunchTemplateName: aws.String(ltName),
LaunchTemplateData: launchTemplateData,
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeLaunchTemplate),
TagSpecifications: ec2TagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeLaunchTemplate),
}

if v, ok := d.GetOk("description"); ok {
Expand All @@ -690,6 +693,7 @@ func resourceAwsLaunchTemplateCreate(d *schema.ResourceData, meta interface{}) e

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

log.Printf("[DEBUG] Reading launch template %s", d.Id())
Expand Down Expand Up @@ -731,8 +735,15 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err
d.Set("name", lt.LaunchTemplateName)
d.Set("latest_version", lt.LatestVersionNumber)
d.Set("default_version", lt.DefaultVersionNumber)
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(lt.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.Ec2KeyValueTags(lt.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 Expand Up @@ -895,8 +906,8 @@ func resourceAwsLaunchTemplateUpdate(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.Ec2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
Expand Down
28 changes: 21 additions & 7 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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 @@ -30,7 +31,10 @@ func resourceAwsLb() *schema.Resource {
Update: resourceAwsLbUpdate,
Delete: resourceAwsLbDelete,
// Subnets are ForceNew for Network Load Balancers
CustomizeDiff: customizeDiffNLBSubnets,
CustomizeDiff: customdiff.Sequence(
customizeDiffNLBSubnets,
SetTagsDiff,
),
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -244,7 +248,8 @@ func resourceAwsLb() *schema.Resource {
Computed: true,
},

"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},
}
}
Expand All @@ -257,7 +262,8 @@ func suppressIfLBType(t string) schema.SchemaDiffSuppressFunc {

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

var name string
if v, ok := d.GetOk("name"); ok {
Expand All @@ -275,7 +281,7 @@ func resourceAwsLbCreate(d *schema.ResourceData, meta interface{}) error {
}

if len(tags) > 0 {
elbOpts.Tags = tags
elbOpts.Tags = tags.IgnoreAws().Elbv2Tags()
}

if _, ok := d.GetOk("internal"); ok {
Expand Down Expand Up @@ -376,8 +382,8 @@ func resourceAwsLbRead(d *schema.ResourceData, meta interface{}) error {
func resourceAwsLbUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elbv2conn

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

err := resource.Retry(waiter.LoadBalancerTagPropagationTimeout, func() *resource.RetryError {
err := keyvaluetags.Elbv2UpdateTags(conn, d.Id(), o, n)
Expand Down Expand Up @@ -731,6 +737,7 @@ func lbSuffixFromARN(arn *string) string {
// flattenAwsLbResource takes a *elbv2.LoadBalancer and populates all respective resource fields.
func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.LoadBalancer) error {
conn := meta.(*AWSClient).elbv2conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

d.Set("arn", lb.LoadBalancerArn)
Expand Down Expand Up @@ -759,10 +766,17 @@ func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.Lo
return fmt.Errorf("error listing tags for (%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)
}

attributesResp, err := conn.DescribeLoadBalancerAttributes(&elbv2.DescribeLoadBalancerAttributesInput{
LoadBalancerArn: aws.String(d.Id()),
})
Expand Down
23 changes: 18 additions & 5 deletions aws/resource_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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 @@ -24,7 +25,10 @@ import (
func resourceAwsLbTargetGroup() *schema.Resource {
return &schema.Resource{
// NLBs have restrictions on them at this time
CustomizeDiff: resourceAwsLbTargetGroupCustomizeDiff,
CustomizeDiff: customdiff.Sequence(
resourceAwsLbTargetGroupCustomizeDiff,
SetTagsDiff,
),

Create: resourceAwsLbTargetGroupCreate,
Read: resourceAwsLbTargetGroupRead,
Expand Down Expand Up @@ -262,7 +266,8 @@ func resourceAwsLbTargetGroup() *schema.Resource {
elbv2.TargetTypeEnumLambda,
}, false),
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -426,8 +431,8 @@ func resourceAwsLbTargetGroupRead(d *schema.ResourceData, meta interface{}) erro
func resourceAwsLbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elbv2conn

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

err := resource.Retry(waiter.LoadBalancerTagPropagationTimeout, func() *resource.RetryError {
err := keyvaluetags.Elbv2UpdateTags(conn, d.Id(), o, n)
Expand Down Expand Up @@ -692,6 +697,7 @@ func lbTargetGroupSuffixFromARN(arn *string) string {
// flattenAwsLbTargetGroupResource takes a *elbv2.TargetGroup and populates all respective resource fields.
func flattenAwsLbTargetGroupResource(d *schema.ResourceData, meta interface{}, targetGroup *elbv2.TargetGroup) error {
conn := meta.(*AWSClient).elbv2conn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

d.Set("arn", targetGroup.TargetGroupArn)
Expand Down Expand Up @@ -774,10 +780,17 @@ func flattenAwsLbTargetGroupResource(d *schema.ResourceData, meta interface{}, t
return fmt.Errorf("error listing tags for LB Target Group (%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 Down
27 changes: 20 additions & 7 deletions aws/resource_aws_licensemanager_license_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ func resourceAwsLicenseManagerLicenseConfiguration() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},

CustomizeDiff: SetTagsDiff,
}
}

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

opts := &licensemanager.CreateLicenseConfigurationInput{
LicenseCountingType: aws.String(d.Get("license_counting_type").(string)),
Expand All @@ -97,8 +102,8 @@ func resourceAwsLicenseManagerLicenseConfigurationCreate(d *schema.ResourceData,
opts.LicenseRules = expandStringList(v.([]interface{}))
}

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

log.Printf("[DEBUG] License Manager license configuration: %s", opts)
Expand All @@ -113,6 +118,7 @@ func resourceAwsLicenseManagerLicenseConfigurationCreate(d *schema.ResourceData,

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

resp, err := conn.GetLicenseConfiguration(&licensemanager.GetLicenseConfigurationInput{
Expand All @@ -139,8 +145,15 @@ func resourceAwsLicenseManagerLicenseConfigurationRead(d *schema.ResourceData, m
d.Set("name", resp.Name)
d.Set("owner_account_id", resp.OwnerAccountId)

if err := d.Set("tags", keyvaluetags.LicensemanagerKeyValueTags(resp.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
tags := keyvaluetags.LicensemanagerKeyValueTags(resp.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 @@ -149,8 +162,8 @@ func resourceAwsLicenseManagerLicenseConfigurationRead(d *schema.ResourceData, m
func resourceAwsLicenseManagerLicenseConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn

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

if err := keyvaluetags.LicensemanagerUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating License Manager License Configuration (%s) tags: %s", d.Id(), err)
Expand Down
Loading

0 comments on commit f107406

Please sign in to comment.