From 67c1971e63b9bfcff152b8979ad9a5ee9c91e0c7 Mon Sep 17 00:00:00 2001 From: stack72 Date: Fri, 11 Dec 2015 12:07:50 +0000 Subject: [PATCH] Adding support to DB Parameter Group for Tags --- .../aws/resource_aws_db_parameter_group.go | 56 +++++++++++++++++++ .../resource_aws_db_parameter_group_test.go | 13 ++++- .../aws/r/db_parameter_group.html.markdown | 2 + 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group.go b/builtin/providers/aws/resource_aws_db_parameter_group.go index b4f07e43d..0513be0b5 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group.go @@ -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" ) @@ -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, @@ -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) @@ -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 := "" + 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 } @@ -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) @@ -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) { diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index d0042df23..c2a8b9538 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -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{ @@ -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"), ), }, }, @@ -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 } } @@ -262,6 +266,9 @@ resource "aws_db_parameter_group" "bar" { name = "character_set_results" value = "utf8" } + tags { + foo = "bar" + } } ` @@ -290,6 +297,10 @@ resource "aws_db_parameter_group" "bar" { name = "collation_connection" value = "utf8_unicode_ci" } + tags { + foo = "bar" + baz = "foo" + } } ` diff --git a/website/source/docs/providers/aws/r/db_parameter_group.html.markdown b/website/source/docs/providers/aws/r/db_parameter_group.html.markdown index 41e2f7b86..8fa5b3b6c 100644 --- a/website/source/docs/providers/aws/r/db_parameter_group.html.markdown +++ b/website/source/docs/providers/aws/r/db_parameter_group.html.markdown @@ -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: @@ -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.