Merge pull request #4259 from stack72/aws-db_parameter_group-tags

provider/aws: Adding Tag support for DB Param Groups
This commit is contained in:
Clint 2015-12-11 08:58:26 -06:00
commit 8b21f284b4
3 changed files with 70 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/rds"
) )
@ -24,6 +25,10 @@ func resourceAwsDbParameterGroup() *schema.Resource {
Update: resourceAwsDbParameterGroupUpdate, Update: resourceAwsDbParameterGroupUpdate,
Delete: resourceAwsDbParameterGroupDelete, Delete: resourceAwsDbParameterGroupDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"arn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{ "name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
ForceNew: true, ForceNew: true,
@ -71,17 +76,21 @@ func resourceAwsDbParameterGroup() *schema.Resource {
}, },
Set: resourceAwsDbParameterHash, Set: resourceAwsDbParameterHash,
}, },
"tags": tagsSchema(),
}, },
} }
} }
func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn rdsconn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
createOpts := rds.CreateDBParameterGroupInput{ createOpts := rds.CreateDBParameterGroupInput{
DBParameterGroupName: aws.String(d.Get("name").(string)), DBParameterGroupName: aws.String(d.Get("name").(string)),
DBParameterGroupFamily: aws.String(d.Get("family").(string)), DBParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)), Description: aws.String(d.Get("description").(string)),
Tags: tags,
} }
log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts) log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts)
@ -136,6 +145,31 @@ func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) e
d.Set("parameter", flattenParameters(describeParametersResp.Parameters)) 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 return nil
} }
@ -177,6 +211,14 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{})
d.SetPartial("parameter") 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) d.Partial(false)
return resourceAwsDbParameterGroupRead(d, meta) return resourceAwsDbParameterGroupRead(d, meta)
@ -230,6 +272,20 @@ func resourceAwsDbParameterHash(v interface{}) int {
return hashcode.String(buf.String()) 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) { func validateDbParamGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) { if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {

View File

@ -44,6 +44,8 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"), "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "tags.#", "1"),
), ),
}, },
resource.TestStep{ resource.TestStep{
@ -77,6 +79,8 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"), "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "tags.#", "2"),
), ),
}, },
}, },
@ -174,7 +178,7 @@ func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
if !ok { if !ok {
return err return err
} }
if newerr.Code() != "InvalidDBParameterGroup.NotFound" { if newerr.Code() != "DBParameterGroupNotFound" {
return err return err
} }
} }
@ -262,6 +266,9 @@ resource "aws_db_parameter_group" "bar" {
name = "character_set_results" name = "character_set_results"
value = "utf8" value = "utf8"
} }
tags {
foo = "bar"
}
} }
` `
@ -290,6 +297,10 @@ resource "aws_db_parameter_group" "bar" {
name = "collation_connection" name = "collation_connection"
value = "utf8_unicode_ci" value = "utf8_unicode_ci"
} }
tags {
foo = "bar"
baz = "foo"
}
} }
` `

View File

@ -36,6 +36,7 @@ The following arguments are supported:
* `family` - (Required) The family of the DB parameter group. * `family` - (Required) The family of the DB parameter group.
* `description` - (Required) The description of the DB parameter group. * `description` - (Required) The description of the DB parameter group.
* `parameter` - (Optional) A list of DB parameters to apply. * `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: Parameter blocks support the following:
@ -50,3 +51,4 @@ Parameter blocks support the following:
The following attributes are exported: The following attributes are exported:
* `id` - The db parameter group name. * `id` - The db parameter group name.
* `arn` - The ARN of the db parameter group.