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/aws: Adding Tag support for DB Param Groups #4259

Merged
merged 1 commit into from
Dec 11, 2015
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
56 changes: 56 additions & 0 deletions builtin/providers/aws/resource_aws_db_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/rds"
)

Expand All @@ -24,6 +25,10 @@ func resourceAwsDbParameterGroup() *schema.Resource {
Update: resourceAwsDbParameterGroupUpdate,
Delete: resourceAwsDbParameterGroupDelete,
Schema: map[string]*schema.Schema{
"arn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -71,17 +76,21 @@ func resourceAwsDbParameterGroup() *schema.Resource {
},
Set: resourceAwsDbParameterHash,
},

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

func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))

createOpts := rds.CreateDBParameterGroupInput{
DBParameterGroupName: aws.String(d.Get("name").(string)),
DBParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: tags,
}

log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts)
Expand Down Expand Up @@ -136,6 +145,31 @@ func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) e

d.Set("parameter", flattenParameters(describeParametersResp.Parameters))

paramGroup := describeResp.DBParameterGroups[0]
arn, err := buildRDSPGARN(d, meta)
if err != nil {
name := "<empty>"
if paramGroup.DBParameterGroupName != nil && *paramGroup.DBParameterGroupName != "" {
name = *paramGroup.DBParameterGroupName
}
log.Printf("[DEBUG] Error building ARN for DB Parameter Group, not setting Tags for Param Group %s", name)
} else {
d.Set("arn", arn)
resp, err := rdsconn.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: aws.String(arn),
})

if err != nil {
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
}

var dt []*rds.Tag
if len(resp.TagList) > 0 {
dt = resp.TagList
}
d.Set("tags", tagsToMapRDS(dt))
}

return nil
}

Expand Down Expand Up @@ -177,6 +211,14 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("parameter")
}

if arn, err := buildRDSPGARN(d, meta); err == nil {
if err := setTagsRDS(rdsconn, d, arn); err != nil {
return err
} else {
d.SetPartial("tags")
}
}

d.Partial(false)

return resourceAwsDbParameterGroupRead(d, meta)
Expand Down Expand Up @@ -230,6 +272,20 @@ func resourceAwsDbParameterHash(v interface{}) int {
return hashcode.String(buf.String())
}

func buildRDSPGARN(d *schema.ResourceData, meta interface{}) (string, error) {
iamconn := meta.(*AWSClient).iamconn
region := meta.(*AWSClient).region
// An zero value GetUserInput{} defers to the currently logged in user
resp, err := iamconn.GetUser(&iam.GetUserInput{})
if err != nil {
return "", err
}
userARN := *resp.User.Arn
accountID := strings.Split(userARN, ":")[4]
arn := fmt.Sprintf("arn:aws:rds:%s:%s:pg:%s", region, accountID, d.Id())
return arn, nil
}

func validateDbParamGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
Expand Down
13 changes: 12 additions & 1 deletion builtin/providers/aws/resource_aws_db_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "tags.#", "1"),
),
},
resource.TestStep{
Expand Down Expand Up @@ -77,6 +79,8 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "tags.#", "2"),
),
},
},
Expand Down Expand Up @@ -174,7 +178,7 @@ func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
if !ok {
return err
}
if newerr.Code() != "InvalidDBParameterGroup.NotFound" {
if newerr.Code() != "DBParameterGroupNotFound" {
return err
}
}
Expand Down Expand Up @@ -262,6 +266,9 @@ resource "aws_db_parameter_group" "bar" {
name = "character_set_results"
value = "utf8"
}
tags {
foo = "bar"
}
}
`

Expand Down Expand Up @@ -290,6 +297,10 @@ resource "aws_db_parameter_group" "bar" {
name = "collation_connection"
value = "utf8_unicode_ci"
}
tags {
foo = "bar"
baz = "foo"
}
}
`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The following arguments are supported:
* `family` - (Required) The family of the DB parameter group.
* `description` - (Required) The description of the DB parameter group.
* `parameter` - (Optional) A list of DB parameters to apply.
* `tags` - (Optional) A mapping of tags to assign to the resource.

Parameter blocks support the following:

Expand All @@ -50,3 +51,4 @@ Parameter blocks support the following:
The following attributes are exported:

* `id` - The db parameter group name.
* `arn` - The ARN of the db parameter group.