github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_redshift_subnet_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/redshift" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) { 15 var v redshift.ClusterSubnetGroup 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccRedshiftSubnetGroupConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 26 resource.TestCheckResourceAttr( 27 "aws_redshift_subnet_group.foo", "subnet_ids.#", "2"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccAWSRedshiftSubnetGroup_updateSubnetIds(t *testing.T) { 35 var v redshift.ClusterSubnetGroup 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccRedshiftSubnetGroupConfig, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 46 resource.TestCheckResourceAttr( 47 "aws_redshift_subnet_group.foo", "subnet_ids.#", "2"), 48 ), 49 }, 50 51 resource.TestStep{ 52 Config: testAccRedshiftSubnetGroupConfig_updateSubnetIds, 53 Check: resource.ComposeTestCheckFunc( 54 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 55 resource.TestCheckResourceAttr( 56 "aws_redshift_subnet_group.foo", "subnet_ids.#", "3"), 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) { 64 cases := []struct { 65 Value string 66 ErrCount int 67 }{ 68 { 69 Value: "default", 70 ErrCount: 1, 71 }, 72 { 73 Value: "testing123%%", 74 ErrCount: 1, 75 }, 76 { 77 Value: "TestingSG", 78 ErrCount: 1, 79 }, 80 { 81 Value: randomString(256), 82 ErrCount: 1, 83 }, 84 } 85 86 for _, tc := range cases { 87 _, errors := validateRedshiftSubnetGroupName(tc.Value, "aws_redshift_subnet_group_name") 88 89 if len(errors) != tc.ErrCount { 90 t.Fatalf("Expected the Redshift Subnet Group Name to trigger a validation error") 91 } 92 } 93 } 94 95 func testAccCheckRedshiftSubnetGroupDestroy(s *terraform.State) error { 96 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 97 98 for _, rs := range s.RootModule().Resources { 99 if rs.Type != "aws_redshift_subnet_group" { 100 continue 101 } 102 103 resp, err := conn.DescribeClusterSubnetGroups( 104 &redshift.DescribeClusterSubnetGroupsInput{ 105 ClusterSubnetGroupName: aws.String(rs.Primary.ID)}) 106 if err == nil { 107 if len(resp.ClusterSubnetGroups) > 0 { 108 return fmt.Errorf("still exist.") 109 } 110 111 return nil 112 } 113 114 redshiftErr, ok := err.(awserr.Error) 115 if !ok { 116 return err 117 } 118 if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" { 119 return err 120 } 121 } 122 123 return nil 124 } 125 126 func testAccCheckRedshiftSubnetGroupExists(n string, v *redshift.ClusterSubnetGroup) resource.TestCheckFunc { 127 return func(s *terraform.State) error { 128 rs, ok := s.RootModule().Resources[n] 129 if !ok { 130 return fmt.Errorf("Not found: %s", n) 131 } 132 133 if rs.Primary.ID == "" { 134 return fmt.Errorf("No ID is set") 135 } 136 137 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 138 resp, err := conn.DescribeClusterSubnetGroups( 139 &redshift.DescribeClusterSubnetGroupsInput{ClusterSubnetGroupName: aws.String(rs.Primary.ID)}) 140 if err != nil { 141 return err 142 } 143 if len(resp.ClusterSubnetGroups) == 0 { 144 return fmt.Errorf("ClusterSubnetGroup not found") 145 } 146 147 *v = *resp.ClusterSubnetGroups[0] 148 149 return nil 150 } 151 } 152 153 const testAccRedshiftSubnetGroupConfig = ` 154 resource "aws_vpc" "foo" { 155 cidr_block = "10.1.0.0/16" 156 } 157 158 resource "aws_subnet" "foo" { 159 cidr_block = "10.1.1.0/24" 160 availability_zone = "us-west-2a" 161 vpc_id = "${aws_vpc.foo.id}" 162 tags { 163 Name = "tf-dbsubnet-test-1" 164 } 165 } 166 167 resource "aws_subnet" "bar" { 168 cidr_block = "10.1.2.0/24" 169 availability_zone = "us-west-2b" 170 vpc_id = "${aws_vpc.foo.id}" 171 tags { 172 Name = "tf-dbsubnet-test-2" 173 } 174 } 175 176 resource "aws_redshift_subnet_group" "foo" { 177 name = "foo" 178 description = "foo description" 179 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 180 } 181 ` 182 183 const testAccRedshiftSubnetGroupConfig_updateSubnetIds = ` 184 resource "aws_vpc" "foo" { 185 cidr_block = "10.1.0.0/16" 186 } 187 188 resource "aws_subnet" "foo" { 189 cidr_block = "10.1.1.0/24" 190 availability_zone = "us-west-2a" 191 vpc_id = "${aws_vpc.foo.id}" 192 tags { 193 Name = "tf-dbsubnet-test-1" 194 } 195 } 196 197 resource "aws_subnet" "bar" { 198 cidr_block = "10.1.2.0/24" 199 availability_zone = "us-west-2b" 200 vpc_id = "${aws_vpc.foo.id}" 201 tags { 202 Name = "tf-dbsubnet-test-2" 203 } 204 } 205 206 resource "aws_subnet" "foobar" { 207 cidr_block = "10.1.3.0/24" 208 availability_zone = "us-west-2c" 209 vpc_id = "${aws_vpc.foo.id}" 210 tags { 211 Name = "tf-dbsubnet-test-3" 212 } 213 } 214 215 resource "aws_redshift_subnet_group" "foo" { 216 name = "foo" 217 description = "foo description" 218 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"] 219 } 220 `