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

service/opsworks: add default tags support to opsworks_*_layer resources #19090

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
81 changes: 46 additions & 35 deletions aws/opsworks_layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
}

if lt.CustomShortName {
Expand Down Expand Up @@ -247,34 +248,31 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource {

return &schema.Resource{
Read: func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

return lt.Read(d, client, ignoreTagsConfig)
return lt.Read(d, meta)
},
Create: func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
return lt.Create(d, client, meta)
return lt.Create(d, meta)
},
Update: func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

return lt.Update(d, client, ignoreTagsConfig)
return lt.Update(d, meta)
},
Delete: func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).opsworksconn
return lt.Delete(d, client)
return lt.Delete(d, meta)
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: resourceSchema,

CustomizeDiff: SetTagsDiff,
}
}

func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWorks, ignoreTagsConfig *keyvaluetags.IgnoreConfig) error {
func (lt *opsworksLayerType) Read(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).opsworksconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

req := &opsworks.DescribeLayersInput{
LayerIds: []*string{
Expand All @@ -284,7 +282,7 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo

log.Printf("[DEBUG] Reading OpsWorks layer: %s", d.Id())

resp, err := client.DescribeLayers(req)
resp, err := conn.DescribeLayers(req)
if err != nil {
if isAWSErr(err, opsworks.ErrCodeResourceNotFoundException, "") {
d.SetId("")
Expand Down Expand Up @@ -334,7 +332,7 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo
aws.String(d.Id()),
},
}
loadBalancers, err := client.DescribeElasticLoadBalancers(ebsRequest)
loadBalancers, err := conn.DescribeElasticLoadBalancers(ebsRequest)
if err != nil {
return err
}
Expand All @@ -350,21 +348,30 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo

arn := aws.StringValue(layer.Arn)
d.Set("arn", arn)
tags, err := keyvaluetags.OpsworksListTags(client, arn)
tags, err := keyvaluetags.OpsworksListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for Opsworks Layer (%s): %s", arn, 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 (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.OpsWorks, meta interface{}) error {
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
func (lt *opsworksLayerType) Create(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).opsworksconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

attributes, err := lt.AttributeMap(d)
if err != nil {
Expand Down Expand Up @@ -398,7 +405,7 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops

log.Printf("[DEBUG] Creating OpsWorks layer: %s", d.Id())

resp, err := client.CreateLayer(req)
resp, err := conn.CreateLayer(req)
if err != nil {
return err
}
Expand All @@ -409,7 +416,7 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops
loadBalancer := aws.String(d.Get("elastic_load_balancer").(string))
if loadBalancer != nil && *loadBalancer != "" {
log.Printf("[DEBUG] Attaching load balancer: %s", *loadBalancer)
_, err := client.AttachElasticLoadBalancer(&opsworks.AttachElasticLoadBalancerInput{
_, err := conn.AttachElasticLoadBalancer(&opsworks.AttachElasticLoadBalancerInput{
ElasticLoadBalancerName: loadBalancer,
LayerId: &layerId,
})
Expand All @@ -426,16 +433,18 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops
Resource: fmt.Sprintf("layer/%s", d.Id()),
}.String()

if v, ok := d.GetOk("tags"); ok {
if err := keyvaluetags.OpsworksUpdateTags(client, arn, nil, v.(map[string]interface{})); err != nil {
if len(tags) > 0 {
if err := keyvaluetags.OpsworksUpdateTags(conn, arn, nil, tags); err != nil {
return fmt.Errorf("error updating Opsworks stack (%s) tags: %s", arn, err)
}
}

return lt.Read(d, client, ignoreTagsConfig)
return lt.Read(d, meta)
}

func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.OpsWorks, ignoreTagsConfig *keyvaluetags.IgnoreConfig) error {
func (lt *opsworksLayerType) Update(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).opsworksconn

attributes, err := lt.AttributeMap(d)
if err != nil {
return err
Expand Down Expand Up @@ -475,7 +484,7 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops

if loadBalancerOld != nil && *loadBalancerOld != "" {
log.Printf("[DEBUG] Dettaching load balancer: %s", *loadBalancerOld)
_, err := client.DetachElasticLoadBalancer(&opsworks.DetachElasticLoadBalancerInput{
_, err := conn.DetachElasticLoadBalancer(&opsworks.DetachElasticLoadBalancerInput{
ElasticLoadBalancerName: loadBalancerOld,
LayerId: aws.String(d.Id()),
})
Expand All @@ -486,7 +495,7 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops

if loadBalancerNew != nil && *loadBalancerNew != "" {
log.Printf("[DEBUG] Attaching load balancer: %s", *loadBalancerNew)
_, err := client.AttachElasticLoadBalancer(&opsworks.AttachElasticLoadBalancerInput{
_, err := conn.AttachElasticLoadBalancer(&opsworks.AttachElasticLoadBalancerInput{
ElasticLoadBalancerName: loadBalancerNew,
LayerId: aws.String(d.Id()),
})
Expand All @@ -496,31 +505,33 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops
}
}

_, err = client.UpdateLayer(req)
_, err = conn.UpdateLayer(req)
if err != nil {
return err
}

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

arn := d.Get("arn").(string)
if err := keyvaluetags.OpsworksUpdateTags(client, arn, o, n); err != nil {
if err := keyvaluetags.OpsworksUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating Opsworks Layer (%s) tags: %s", arn, err)
}
}

return lt.Read(d, client, ignoreTagsConfig)
return lt.Read(d, meta)
}

func (lt *opsworksLayerType) Delete(d *schema.ResourceData, client *opsworks.OpsWorks) error {
func (lt *opsworksLayerType) Delete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).opsworksconn

req := &opsworks.DeleteLayerInput{
LayerId: aws.String(d.Id()),
}

log.Printf("[DEBUG] Deleting OpsWorks layer: %s", d.Id())

_, err := client.DeleteLayer(req)
_, err := conn.DeleteLayer(req)
return err
}

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_custom_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand Down Expand Up @@ -68,6 +68,7 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_ganglia_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -68,3 +68,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_haproxy_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -71,3 +71,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_java_app_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -69,3 +69,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_memcached_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -65,3 +65,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_mysql_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -69,3 +69,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_nodejs_app_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -65,3 +65,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_php_app_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -64,6 +64,7 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_rails_app_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The following arguments are supported:
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `custom_json` - (Optional) Custom JSON attributes to apply to the layer.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -70,3 +70,4 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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).
3 changes: 2 additions & 1 deletion website/docs/r/opsworks_static_web_layer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The following arguments are supported:
* `system_packages` - (Optional) Names of a set of system packages to install on the layer's instances.
* `use_ebs_optimized_instances` - (Optional) Whether to use EBS-optimized instances.
* `ebs_volume` - (Optional) `ebs_volume` blocks, as described below, will each create an EBS volume and connect it to the layer's instances.
* `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](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

The following extra optional arguments, all lists of Chef recipe names, allow
custom Chef recipes to be applied to layer instances at the five different
Expand All @@ -63,6 +63,7 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The id of the layer.
* `arn` - The Amazon Resource Name(ARN) of the layer.
* `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