Skip to content

Commit

Permalink
resource/aws_dlm_lifecycle_policy: Add tags argument and arn attribute (
Browse files Browse the repository at this point in the history
#10864)

* internal/keyvaluetags: Fix dlm service tagging for resource tags

The service did not previously support resource tagging (slice based Tag types were used for other structures not relevant to the purpose of this package). Here we generate all functionality related to DLM resource tagging including listing and updating.

Updated via:

```
make gen
```

* resource/aws_dlm_lifecycle_policy: Add tags argument and arn attribute

Reference: https://github.com/terraform-providers/terraform-provider-aws/blob/master/.github/CONTRIBUTING.md#adding-resource-tagging-support

Output from acceptance testing:

```
--- PASS: TestAccAWSDlmLifecyclePolicy_Basic (14.81s)
--- PASS: TestAccAWSDlmLifecyclePolicy_Full (22.48s)
--- PASS: TestAccAWSDlmLifecyclePolicy_Tags (32.00s)
```
  • Loading branch information
bflad authored Nov 15, 2019
1 parent f885529 commit 3bf9a83
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 35 deletions.
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var serviceNames = []string{
"dax",
"devicefarm",
"directoryservice",
"dlm",
"docdb",
"dynamodb",
"ecr",
Expand Down
2 changes: 1 addition & 1 deletion aws/internal/keyvaluetags/generators/servicetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var sliceServiceNames = []string{
"devicefarm",
"directconnect",
"directoryservice",
"dlm",
"docdb",
"dynamodb",
"ec2",
Expand Down Expand Up @@ -103,6 +102,7 @@ var mapServiceNames = []string{
"codecommit",
"cognitoidentity",
"cognitoidentityprovider",
"dlm",
"eks",
"glacier",
"glue",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/updatetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var serviceNames = []string{
"devicefarm",
"directconnect",
"directoryservice",
"dlm",
"docdb",
"dynamodb",
"ec2",
Expand Down
2 changes: 1 addition & 1 deletion aws/internal/keyvaluetags/key_value_tags.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:generate go run generators/listtags/main.go
//go:generate go run generators/servicetags/main.go
//go:generate go run generators/listtags/main.go
//go:generate go run generators/updatetags/main.go

package keyvaluetags
Expand Down
18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/aws/aws-sdk-go/service/directoryservice"
"github.com/aws/aws-sdk-go/service/dlm"
"github.com/aws/aws-sdk-go/service/docdb"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -155,6 +156,8 @@ func ServiceClientType(serviceName string) string {
funcType = reflect.TypeOf(directconnect.New)
case "directoryservice":
funcType = reflect.TypeOf(directoryservice.New)
case "dlm":
funcType = reflect.TypeOf(dlm.New)
case "docdb":
funcType = reflect.TypeOf(docdb.New)
case "dynamodb":
Expand Down
38 changes: 10 additions & 28 deletions aws/internal/keyvaluetags/service_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions aws/internal/keyvaluetags/update_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 33 additions & 4 deletions aws/resource_aws_dlm_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/service/dlm"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsDlmLifecyclePolicy() *schema.Resource {
Expand All @@ -22,6 +23,10 @@ func resourceAwsDlmLifecyclePolicy() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -130,6 +135,7 @@ func resourceAwsDlmLifecyclePolicy() *schema.Resource {
dlm.SettablePolicyStateValuesEnabled,
}, false),
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -144,6 +150,10 @@ func resourceAwsDlmLifecyclePolicyCreate(d *schema.ResourceData, meta interface{
State: aws.String(d.Get("state").(string)),
}

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

log.Printf("[INFO] Creating DLM lifecycle policy: %s", input)
out, err := conn.CreateLifecyclePolicy(&input)
if err != nil {
Expand Down Expand Up @@ -173,13 +183,18 @@ func resourceAwsDlmLifecyclePolicyRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error reading DLM Lifecycle Policy (%s): %s", d.Id(), err)
}

d.Set("arn", out.Policy.PolicyArn)
d.Set("description", out.Policy.Description)
d.Set("execution_role_arn", out.Policy.ExecutionRoleArn)
d.Set("state", out.Policy.State)
if err := d.Set("policy_details", flattenDlmPolicyDetails(out.Policy.PolicyDetails)); err != nil {
return fmt.Errorf("error setting policy details %s", err)
}

if err := d.Set("tags", keyvaluetags.DlmKeyValueTags(out.Policy.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

Expand All @@ -189,24 +204,38 @@ func resourceAwsDlmLifecyclePolicyUpdate(d *schema.ResourceData, meta interface{
input := dlm.UpdateLifecyclePolicyInput{
PolicyId: aws.String(d.Id()),
}
updateLifecyclePolicy := false

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
updateLifecyclePolicy = true
}
if d.HasChange("execution_role_arn") {
input.ExecutionRoleArn = aws.String(d.Get("execution_role_arn").(string))
updateLifecyclePolicy = true
}
if d.HasChange("state") {
input.State = aws.String(d.Get("state").(string))
updateLifecyclePolicy = true
}
if d.HasChange("policy_details") {
input.PolicyDetails = expandDlmPolicyDetails(d.Get("policy_details").([]interface{}))
updateLifecyclePolicy = true
}

log.Printf("[INFO] Updating lifecycle policy %s", d.Id())
_, err := conn.UpdateLifecyclePolicy(&input)
if err != nil {
return fmt.Errorf("error updating DLM Lifecycle Policy (%s): %s", d.Id(), err)
if updateLifecyclePolicy {
log.Printf("[INFO] Updating lifecycle policy %s", d.Id())
_, err := conn.UpdateLifecyclePolicy(&input)
if err != nil {
return fmt.Errorf("error updating DLM Lifecycle Policy (%s): %s", d.Id(), err)
}
}

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

return resourceAwsDlmLifecyclePolicyRead(d, meta)
Expand Down
Loading

0 comments on commit 3bf9a83

Please sign in to comment.