github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) { 16 var v redshift.ClusterSubnetGroup 17 rInt := acctest.RandInt() 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccRedshiftSubnetGroupConfig(rInt), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 28 resource.TestCheckResourceAttr( 29 "aws_redshift_subnet_group.foo", "subnet_ids.#", "2"), 30 resource.TestCheckResourceAttr( 31 "aws_redshift_subnet_group.foo", "description", "foo description"), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSRedshiftSubnetGroup_updateDescription(t *testing.T) { 39 var v redshift.ClusterSubnetGroup 40 rInt := acctest.RandInt() 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: testAccRedshiftSubnetGroupConfig(rInt), 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 51 resource.TestCheckResourceAttr( 52 "aws_redshift_subnet_group.foo", "description", "foo description"), 53 ), 54 }, 55 56 resource.TestStep{ 57 Config: testAccRedshiftSubnetGroup_updateDescription(rInt), 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 60 resource.TestCheckResourceAttr( 61 "aws_redshift_subnet_group.foo", "description", "foo description updated"), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAWSRedshiftSubnetGroup_updateSubnetIds(t *testing.T) { 69 var v redshift.ClusterSubnetGroup 70 rInt := acctest.RandInt() 71 72 resource.Test(t, resource.TestCase{ 73 PreCheck: func() { testAccPreCheck(t) }, 74 Providers: testAccProviders, 75 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 76 Steps: []resource.TestStep{ 77 resource.TestStep{ 78 Config: testAccRedshiftSubnetGroupConfig(rInt), 79 Check: resource.ComposeTestCheckFunc( 80 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 81 resource.TestCheckResourceAttr( 82 "aws_redshift_subnet_group.foo", "subnet_ids.#", "2"), 83 ), 84 }, 85 86 resource.TestStep{ 87 Config: testAccRedshiftSubnetGroupConfig_updateSubnetIds(rInt), 88 Check: resource.ComposeTestCheckFunc( 89 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 90 resource.TestCheckResourceAttr( 91 "aws_redshift_subnet_group.foo", "subnet_ids.#", "3"), 92 ), 93 }, 94 }, 95 }) 96 } 97 98 func TestAccAWSRedshiftSubnetGroup_tags(t *testing.T) { 99 var v redshift.ClusterSubnetGroup 100 rInt := acctest.RandInt() 101 102 resource.Test(t, resource.TestCase{ 103 PreCheck: func() { testAccPreCheck(t) }, 104 Providers: testAccProviders, 105 CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, 106 Steps: []resource.TestStep{ 107 { 108 Config: testAccRedshiftSubnetGroupConfigWithTags(rInt), 109 Check: resource.ComposeTestCheckFunc( 110 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 111 resource.TestCheckResourceAttr( 112 "aws_redshift_subnet_group.foo", "tags.%", "1"), 113 resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"), 114 ), 115 }, 116 { 117 Config: testAccRedshiftSubnetGroupConfigWithTagsUpdated(rInt), 118 Check: resource.ComposeTestCheckFunc( 119 testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v), 120 resource.TestCheckResourceAttr( 121 "aws_redshift_subnet_group.foo", "tags.%", "3"), 122 resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.environment", "production"), 123 resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"), 124 resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.foo", "bar"), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) { 132 cases := []struct { 133 Value string 134 ErrCount int 135 }{ 136 { 137 Value: "default", 138 ErrCount: 1, 139 }, 140 { 141 Value: "testing123%%", 142 ErrCount: 1, 143 }, 144 { 145 Value: "TestingSG", 146 ErrCount: 1, 147 }, 148 { 149 Value: "testing_123", 150 ErrCount: 1, 151 }, 152 { 153 Value: "testing.123", 154 ErrCount: 1, 155 }, 156 { 157 Value: randomString(256), 158 ErrCount: 1, 159 }, 160 } 161 162 for _, tc := range cases { 163 _, errors := validateRedshiftSubnetGroupName(tc.Value, "aws_redshift_subnet_group_name") 164 165 if len(errors) != tc.ErrCount { 166 t.Fatalf("Expected the Redshift Subnet Group Name to trigger a validation error") 167 } 168 } 169 } 170 171 func testAccCheckRedshiftSubnetGroupDestroy(s *terraform.State) error { 172 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 173 174 for _, rs := range s.RootModule().Resources { 175 if rs.Type != "aws_redshift_subnet_group" { 176 continue 177 } 178 179 resp, err := conn.DescribeClusterSubnetGroups( 180 &redshift.DescribeClusterSubnetGroupsInput{ 181 ClusterSubnetGroupName: aws.String(rs.Primary.ID)}) 182 if err == nil { 183 if len(resp.ClusterSubnetGroups) > 0 { 184 return fmt.Errorf("still exist.") 185 } 186 187 return nil 188 } 189 190 redshiftErr, ok := err.(awserr.Error) 191 if !ok { 192 return err 193 } 194 if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" { 195 return err 196 } 197 } 198 199 return nil 200 } 201 202 func testAccCheckRedshiftSubnetGroupExists(n string, v *redshift.ClusterSubnetGroup) resource.TestCheckFunc { 203 return func(s *terraform.State) error { 204 rs, ok := s.RootModule().Resources[n] 205 if !ok { 206 return fmt.Errorf("Not found: %s", n) 207 } 208 209 if rs.Primary.ID == "" { 210 return fmt.Errorf("No ID is set") 211 } 212 213 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 214 resp, err := conn.DescribeClusterSubnetGroups( 215 &redshift.DescribeClusterSubnetGroupsInput{ClusterSubnetGroupName: aws.String(rs.Primary.ID)}) 216 if err != nil { 217 return err 218 } 219 if len(resp.ClusterSubnetGroups) == 0 { 220 return fmt.Errorf("ClusterSubnetGroup not found") 221 } 222 223 *v = *resp.ClusterSubnetGroups[0] 224 225 return nil 226 } 227 } 228 229 func testAccRedshiftSubnetGroupConfig(rInt int) string { 230 return fmt.Sprintf(` 231 resource "aws_vpc" "foo" { 232 cidr_block = "10.1.0.0/16" 233 } 234 235 resource "aws_subnet" "foo" { 236 cidr_block = "10.1.1.0/24" 237 availability_zone = "us-west-2a" 238 vpc_id = "${aws_vpc.foo.id}" 239 tags { 240 Name = "tf-dbsubnet-test-1" 241 } 242 } 243 244 resource "aws_subnet" "bar" { 245 cidr_block = "10.1.2.0/24" 246 availability_zone = "us-west-2b" 247 vpc_id = "${aws_vpc.foo.id}" 248 tags { 249 Name = "tf-dbsubnet-test-2" 250 } 251 } 252 253 resource "aws_redshift_subnet_group" "foo" { 254 name = "foo-%d" 255 description = "foo description" 256 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 257 } 258 `, rInt) 259 } 260 261 func testAccRedshiftSubnetGroup_updateDescription(rInt int) string { 262 return fmt.Sprintf(` 263 resource "aws_vpc" "foo" { 264 cidr_block = "10.1.0.0/16" 265 } 266 267 resource "aws_subnet" "foo" { 268 cidr_block = "10.1.1.0/24" 269 availability_zone = "us-west-2a" 270 vpc_id = "${aws_vpc.foo.id}" 271 tags { 272 Name = "tf-dbsubnet-test-1" 273 } 274 } 275 276 resource "aws_subnet" "bar" { 277 cidr_block = "10.1.2.0/24" 278 availability_zone = "us-west-2b" 279 vpc_id = "${aws_vpc.foo.id}" 280 tags { 281 Name = "tf-dbsubnet-test-2" 282 } 283 } 284 285 resource "aws_redshift_subnet_group" "foo" { 286 name = "foo-%d" 287 description = "foo description updated" 288 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 289 } 290 `, rInt) 291 } 292 293 func testAccRedshiftSubnetGroupConfigWithTags(rInt int) string { 294 return fmt.Sprintf(` 295 resource "aws_vpc" "foo" { 296 cidr_block = "10.1.0.0/16" 297 } 298 299 resource "aws_subnet" "foo" { 300 cidr_block = "10.1.1.0/24" 301 availability_zone = "us-west-2a" 302 vpc_id = "${aws_vpc.foo.id}" 303 tags { 304 Name = "tf-dbsubnet-test-1" 305 } 306 } 307 308 resource "aws_subnet" "bar" { 309 cidr_block = "10.1.2.0/24" 310 availability_zone = "us-west-2b" 311 vpc_id = "${aws_vpc.foo.id}" 312 tags { 313 Name = "tf-dbsubnet-test-2" 314 } 315 } 316 317 resource "aws_redshift_subnet_group" "foo" { 318 name = "foo-%d" 319 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 320 tags { 321 Name = "tf-redshift-subnetgroup" 322 } 323 } 324 `, rInt) 325 } 326 327 func testAccRedshiftSubnetGroupConfigWithTagsUpdated(rInt int) string { 328 return fmt.Sprintf(` 329 resource "aws_vpc" "foo" { 330 cidr_block = "10.1.0.0/16" 331 } 332 333 resource "aws_subnet" "foo" { 334 cidr_block = "10.1.1.0/24" 335 availability_zone = "us-west-2a" 336 vpc_id = "${aws_vpc.foo.id}" 337 tags { 338 Name = "tf-dbsubnet-test-1" 339 } 340 } 341 342 resource "aws_subnet" "bar" { 343 cidr_block = "10.1.2.0/24" 344 availability_zone = "us-west-2b" 345 vpc_id = "${aws_vpc.foo.id}" 346 tags { 347 Name = "tf-dbsubnet-test-2" 348 } 349 } 350 351 resource "aws_redshift_subnet_group" "foo" { 352 name = "foo-%d" 353 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 354 tags { 355 Name = "tf-redshift-subnetgroup" 356 environment = "production" 357 foo = "bar" 358 } 359 } 360 `, rInt) 361 } 362 363 func testAccRedshiftSubnetGroupConfig_updateSubnetIds(rInt int) string { 364 return fmt.Sprintf(` 365 resource "aws_vpc" "foo" { 366 cidr_block = "10.1.0.0/16" 367 } 368 369 resource "aws_subnet" "foo" { 370 cidr_block = "10.1.1.0/24" 371 availability_zone = "us-west-2a" 372 vpc_id = "${aws_vpc.foo.id}" 373 tags { 374 Name = "tf-dbsubnet-test-1" 375 } 376 } 377 378 resource "aws_subnet" "bar" { 379 cidr_block = "10.1.2.0/24" 380 availability_zone = "us-west-2b" 381 vpc_id = "${aws_vpc.foo.id}" 382 tags { 383 Name = "tf-dbsubnet-test-2" 384 } 385 } 386 387 resource "aws_subnet" "foobar" { 388 cidr_block = "10.1.3.0/24" 389 availability_zone = "us-west-2c" 390 vpc_id = "${aws_vpc.foo.id}" 391 tags { 392 Name = "tf-dbsubnet-test-3" 393 } 394 } 395 396 resource "aws_redshift_subnet_group" "foo" { 397 name = "foo-%d" 398 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"] 399 } 400 `, rInt) 401 }