Skip to content

Commit

Permalink
Merge pull request hashicorp#18736 from hashicorp/f-dtags-e6
Browse files Browse the repository at this point in the history
provider: Support default tags (resources aws_e* 6)
  • Loading branch information
anGie44 authored Apr 20, 2021
2 parents f107406 + 81bb8ce commit 93d578a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
37 changes: 22 additions & 15 deletions aws/resource_aws_elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func resourceAwsElb() *schema.Resource {
State: schema.ImportStatePassthrough,
},

CustomizeDiff: SetTagsDiff,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -246,14 +248,16 @@ func resourceAwsElb() *schema.Resource {
Computed: true,
},

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

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

// Expand the "listener" set to aws-sdk-go compat []*elb.Listener
listeners, err := expandListeners(d.Get("listener").(*schema.Set).List())
Expand All @@ -273,16 +277,14 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
d.Set("name", elbName)
}

tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().ElbTags()

// Provision the elb
elbOpts := &elb.CreateLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Listeners: listeners,
}

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

if _, ok := d.GetOk("internal"); ok {
Expand Down Expand Up @@ -328,15 +330,12 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(elbName)
log.Printf("[INFO] ELB ID: %s", d.Id())

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

return resourceAwsElbUpdate(d, meta)
}

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

elbName := d.Id()
Expand Down Expand Up @@ -369,11 +368,11 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
}

return flattenAwsELbResource(d, meta.(*AWSClient).ec2conn, elbconn, describeResp.LoadBalancerDescriptions[0], ignoreTagsConfig)
return flattenAwsELbResource(d, meta.(*AWSClient).ec2conn, elbconn, describeResp.LoadBalancerDescriptions[0], ignoreTagsConfig, defaultTagsConfig)
}

// flattenAwsELbResource takes a *elbv2.LoadBalancer and populates all respective resource fields.
func flattenAwsELbResource(d *schema.ResourceData, ec2conn *ec2.EC2, elbconn *elb.ELB, lb *elb.LoadBalancerDescription, ignoreTagsConfig *keyvaluetags.IgnoreConfig) error {
func flattenAwsELbResource(d *schema.ResourceData, ec2conn *ec2.EC2, elbconn *elb.ELB, lb *elb.LoadBalancerDescription, ignoreTagsConfig *keyvaluetags.IgnoreConfig, defaultTagsConfig *keyvaluetags.DefaultConfig) error {
describeAttrsOpts := &elb.DescribeLoadBalancerAttributesInput{
LoadBalancerName: aws.String(d.Id()),
}
Expand All @@ -397,6 +396,7 @@ func flattenAwsELbResource(d *schema.ResourceData, ec2conn *ec2.EC2, elbconn *el
d.Set("instances", flattenInstances(lb.Instances))
d.Set("listener", flattenListeners(lb.ListenerDescriptions))
d.Set("security_groups", flattenStringList(lb.SecurityGroups))

if lb.SourceSecurityGroup != nil {
group := lb.SourceSecurityGroup.GroupName
if lb.SourceSecurityGroup.OwnerAlias != nil && *lb.SourceSecurityGroup.OwnerAlias != "" {
Expand Down Expand Up @@ -454,8 +454,15 @@ func flattenAwsELbResource(d *schema.ResourceData, ec2conn *ec2.EC2, elbconn *el
return fmt.Errorf("error listing tags for ELB (%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)
}

// There's only one health check, so save that to state as we
Expand Down Expand Up @@ -765,8 +772,8 @@ func resourceAwsElbUpdate(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.ElbUpdateTags(elbconn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating ELB(%s) tags: %s", d.Id(), err)
Expand Down
25 changes: 19 additions & 6 deletions aws/resource_aws_emr_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func resourceAwsEMRCluster() *schema.Resource {
State: schema.ImportStatePassthrough,
},

CustomizeDiff: SetTagsDiff,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -446,7 +448,8 @@ func resourceAwsEMRCluster() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"configurations": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -698,6 +701,8 @@ func InstanceFleetConfigSchema() *schema.Resource {

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

log.Printf("[DEBUG] Creating EMR cluster")
applications := d.Get("applications").(*schema.Set).List()
Expand Down Expand Up @@ -840,7 +845,7 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
ReleaseLabel: aws.String(d.Get("release_label").(string)),
ServiceRole: aws.String(d.Get("service_role").(string)),
VisibleToAllUsers: aws.Bool(d.Get("visible_to_all_users").(bool)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EmrTags(),
Tags: tags.IgnoreAws().EmrTags(),
}

if v, ok := d.GetOk("additional_info"); ok {
Expand Down Expand Up @@ -985,6 +990,7 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error

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

req := &emr.DescribeClusterInput{
Expand Down Expand Up @@ -1070,8 +1076,15 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
}
}

if err := d.Set("tags", keyvaluetags.EmrKeyValueTags(cluster.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error settings tags: %s", err)
tags := keyvaluetags.EmrKeyValueTags(cluster.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("name", cluster.Name)
Expand Down Expand Up @@ -1320,8 +1333,8 @@ func resourceAwsEMRClusterUpdate(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.EmrUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating EMR Cluster (%s) tags: %s", d.Id(), err)
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/elb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The following arguments are supported:
* `idle_timeout` - (Optional) The time in seconds that the connection is allowed to be idle. Default: `60`
* `connection_draining` - (Optional) Boolean to enable connection draining. Default: `false`
* `connection_draining_timeout` - (Optional) The time in seconds to allow for connections to drain. Default: `300`
* `tags` - (Optional) A map of tags to assign to the resource.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

Exactly one of `availability_zones` or `subnets` must be specified: this
determines if the ELB exists in a VPC or in EC2-classic.
Expand Down Expand Up @@ -146,6 +146,7 @@ In addition to all arguments above, the following attributes are exported:
part of your inbound rules for your load balancer's back-end application
instances. Only available on ELBs launched in a VPC.
* `zone_id` - The canonical hosted zone ID of the ELB (to be used in a Route 53 Alias record)
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block).

## Import

Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/emr_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ EOF
* `autoscaling_role` - (Optional) An IAM role for automatic scaling policies. The IAM role provides permissions that the automatic scaling feature requires to launch and terminate EC2 instances in an instance group.
* `step` - (Optional) List of steps to run when creating the cluster. Defined below. It is highly recommended to utilize the [lifecycle configuration block](https://www.terraform.io/docs/configuration/meta-arguments/lifecycle.html) with `ignore_changes` if other steps are being managed outside of Terraform. This argument is processed in [attribute-as-blocks mode](https://www.terraform.io/docs/configuration/attr-as-blocks.html).
* `step_concurrency_level` - (Optional) The number of steps that can be executed concurrently. You can specify a maximum of 256 steps. Only valid for EMR clusters with `release_label` 5.28.0 or greater. (default is 1)
* `tags` - (Optional) list of tags to apply to the EMR Cluster
* `tags` - (Optional) list of tags to apply to the EMR Cluster. If configured with a provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

## core_instance_group Configuration Block

Expand Down Expand Up @@ -536,7 +536,7 @@ In addition to all arguments above, the following attributes are exported:
* `configurations` - The list of Configurations supplied to the EMR cluster.
* `service_role` - The IAM role that will be assumed by the Amazon EMR service to access AWS resources on your behalf.
* `visible_to_all_users` - Indicates whether the job flow is visible to all IAM users of the AWS account associated with the job flow.
* `tags` - The list of tags associated with a cluster.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block).

## Example bootable config

Expand Down

0 comments on commit 93d578a

Please sign in to comment.