Merge pull request #9504 from hashicorp/aws-redshift-sng-tags

provider/aws: Add tagging support to aws_redshift_subnet_group
This commit is contained in:
Paul Stack 2016-10-25 11:48:16 +01:00 committed by GitHub
commit a65dc539ac
3 changed files with 136 additions and 5 deletions

View File

@ -24,25 +24,27 @@ func resourceAwsRedshiftSubnetGroup() *schema.Resource {
}, },
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
ForceNew: true, ForceNew: true,
Required: true, Required: true,
ValidateFunc: validateRedshiftSubnetGroupName, ValidateFunc: validateRedshiftSubnetGroupName,
}, },
"description": &schema.Schema{ "description": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Default: "Managed by Terraform", Default: "Managed by Terraform",
}, },
"subnet_ids": &schema.Schema{ "subnet_ids": {
Type: schema.TypeSet, Type: schema.TypeSet,
Required: true, Required: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString, Set: schema.HashString,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -55,11 +57,13 @@ func resourceAwsRedshiftSubnetGroupCreate(d *schema.ResourceData, meta interface
for i, subnetId := range subnetIdsSet.List() { for i, subnetId := range subnetIdsSet.List() {
subnetIds[i] = aws.String(subnetId.(string)) subnetIds[i] = aws.String(subnetId.(string))
} }
tags := tagsFromMapRedshift(d.Get("tags").(map[string]interface{}))
createOpts := redshift.CreateClusterSubnetGroupInput{ createOpts := redshift.CreateClusterSubnetGroupInput{
ClusterSubnetGroupName: aws.String(d.Get("name").(string)), ClusterSubnetGroupName: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)), Description: aws.String(d.Get("description").(string)),
SubnetIds: subnetIds, SubnetIds: subnetIds,
Tags: tags,
} }
log.Printf("[DEBUG] Create Redshift Subnet Group: %#v", createOpts) log.Printf("[DEBUG] Create Redshift Subnet Group: %#v", createOpts)
@ -97,12 +101,25 @@ func resourceAwsRedshiftSubnetGroupRead(d *schema.ResourceData, meta interface{}
d.Set("name", d.Id()) d.Set("name", d.Id())
d.Set("description", describeResp.ClusterSubnetGroups[0].Description) d.Set("description", describeResp.ClusterSubnetGroups[0].Description)
d.Set("subnet_ids", subnetIdsToSlice(describeResp.ClusterSubnetGroups[0].Subnets)) d.Set("subnet_ids", subnetIdsToSlice(describeResp.ClusterSubnetGroups[0].Subnets))
if err := d.Set("tags", tagsToMapRedshift(describeResp.ClusterSubnetGroups[0].Tags)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Redshift Subnet Group Tags: %#v", err)
}
return nil return nil
} }
func resourceAwsRedshiftSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsRedshiftSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn conn := meta.(*AWSClient).redshiftconn
arn, tagErr := buildRedshiftSubnetGroupARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region)
if tagErr != nil {
return fmt.Errorf("Error building ARN for Redshift Subnet Group, not updating Tags for Subnet Group %s", d.Id())
} else {
if tagErr := setTagsRedshift(conn, d, arn); tagErr != nil {
return tagErr
}
}
if d.HasChange("subnet_ids") || d.HasChange("description") { if d.HasChange("subnet_ids") || d.HasChange("description") {
_, n := d.GetChange("subnet_ids") _, n := d.GetChange("subnet_ids")
if n == nil { if n == nil {
@ -189,3 +206,15 @@ func validateRedshiftSubnetGroupName(v interface{}, k string) (ws []string, erro
} }
return return
} }
func buildRedshiftSubnetGroupARN(identifier, partition, accountid, region string) (string, error) {
if partition == "" {
return "", fmt.Errorf("Unable to construct Subnet Group ARN because of missing AWS partition")
}
if accountid == "" {
return "", fmt.Errorf("Unable to construct Subnet Group ARN because of missing AWS Account ID")
}
arn := fmt.Sprintf("arn:%s:redshift:%s:%s:subnetgroup:%s", partition, region, accountid, identifier)
return arn, nil
}

View File

@ -26,7 +26,7 @@ func TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_redshift_subnet_group.foo", "subnet_ids.#", "2"), "aws_redshift_subnet_group.foo", "subnet_ids.#", "2"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_redshift_subnet_group.foo", "description", "Managed by Terraform"), "aws_redshift_subnet_group.foo", "description", "foo description"),
), ),
}, },
}, },
@ -91,6 +91,38 @@ func TestAccAWSRedshiftSubnetGroup_updateSubnetIds(t *testing.T) {
}) })
} }
func TestAccAWSRedshiftSubnetGroup_tags(t *testing.T) {
var v redshift.ClusterSubnetGroup
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccRedshiftSubnetGroupConfigWithTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
resource.TestCheckResourceAttr(
"aws_redshift_subnet_group.foo", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"),
),
},
{
Config: testAccRedshiftSubnetGroupConfigWithTagsUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
resource.TestCheckResourceAttr(
"aws_redshift_subnet_group.foo", "tags.%", "3"),
resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.environment", "production"),
resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"),
resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.foo", "bar"),
),
},
},
})
}
func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) { func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) {
cases := []struct { cases := []struct {
Value string Value string
@ -249,6 +281,72 @@ resource "aws_redshift_subnet_group" "foo" {
} }
` `
const testAccRedshiftSubnetGroupConfigWithTags = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-1"
}
}
resource "aws_subnet" "bar" {
cidr_block = "10.1.2.0/24"
availability_zone = "us-west-2b"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-2"
}
}
resource "aws_redshift_subnet_group" "foo" {
name = "foo"
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
tags {
Name = "tf-redshift-subnetgroup"
}
}
`
const testAccRedshiftSubnetGroupConfigWithTagsUpdated = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-1"
}
}
resource "aws_subnet" "bar" {
cidr_block = "10.1.2.0/24"
availability_zone = "us-west-2b"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-2"
}
}
resource "aws_redshift_subnet_group" "foo" {
name = "foo"
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
tags {
Name = "tf-redshift-subnetgroup"
environment = "production"
foo = "bar"
}
}
`
const testAccRedshiftSubnetGroupConfig_updateSubnetIds = ` const testAccRedshiftSubnetGroupConfig_updateSubnetIds = `
resource "aws_vpc" "foo" { resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16" cidr_block = "10.1.0.0/16"

View File

@ -38,6 +38,9 @@ resource "aws_subnet" "bar" {
resource "aws_redshift_subnet_group" "foo" { resource "aws_redshift_subnet_group" "foo" {
name = "foo" name = "foo"
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
tags {
environment = "Production"
}
} }
``` ```
@ -47,7 +50,8 @@ The following arguments are supported:
* `name` - (Required) The name of the Redshift Subnet group. * `name` - (Required) The name of the Redshift Subnet group.
* `description` - (Optional) The description of the Redshift Subnet group. Defaults to "Managed by Terraform". * `description` - (Optional) The description of the Redshift Subnet group. Defaults to "Managed by Terraform".
* `subnet_ids` - (Optional) An array of VPC subnet IDs.. * `subnet_ids` - (Optional) An array of VPC subnet IDs.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference ## Attributes Reference