github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 tags { 234 Name = "testAccRedshiftSubnetGroupConfig" 235 } 236 } 237 238 resource "aws_subnet" "foo" { 239 cidr_block = "10.1.1.0/24" 240 availability_zone = "us-west-2a" 241 vpc_id = "${aws_vpc.foo.id}" 242 tags { 243 Name = "tf-dbsubnet-test-1" 244 } 245 } 246 247 resource "aws_subnet" "bar" { 248 cidr_block = "10.1.2.0/24" 249 availability_zone = "us-west-2b" 250 vpc_id = "${aws_vpc.foo.id}" 251 tags { 252 Name = "tf-dbsubnet-test-2" 253 } 254 } 255 256 resource "aws_redshift_subnet_group" "foo" { 257 name = "foo-%d" 258 description = "foo description" 259 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 260 } 261 `, rInt) 262 } 263 264 func testAccRedshiftSubnetGroup_updateDescription(rInt int) string { 265 return fmt.Sprintf(` 266 resource "aws_vpc" "foo" { 267 cidr_block = "10.1.0.0/16" 268 tags { 269 Name = "testAccRedshiftSubnetGroup_updateDescription" 270 } 271 } 272 273 resource "aws_subnet" "foo" { 274 cidr_block = "10.1.1.0/24" 275 availability_zone = "us-west-2a" 276 vpc_id = "${aws_vpc.foo.id}" 277 tags { 278 Name = "tf-dbsubnet-test-1" 279 } 280 } 281 282 resource "aws_subnet" "bar" { 283 cidr_block = "10.1.2.0/24" 284 availability_zone = "us-west-2b" 285 vpc_id = "${aws_vpc.foo.id}" 286 tags { 287 Name = "tf-dbsubnet-test-2" 288 } 289 } 290 291 resource "aws_redshift_subnet_group" "foo" { 292 name = "foo-%d" 293 description = "foo description updated" 294 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 295 } 296 `, rInt) 297 } 298 299 func testAccRedshiftSubnetGroupConfigWithTags(rInt int) string { 300 return fmt.Sprintf(` 301 resource "aws_vpc" "foo" { 302 cidr_block = "10.1.0.0/16" 303 tags { 304 Name = "testAccRedshiftSubnetGroupConfigWithTags" 305 } 306 } 307 308 resource "aws_subnet" "foo" { 309 cidr_block = "10.1.1.0/24" 310 availability_zone = "us-west-2a" 311 vpc_id = "${aws_vpc.foo.id}" 312 tags { 313 Name = "tf-dbsubnet-test-1" 314 } 315 } 316 317 resource "aws_subnet" "bar" { 318 cidr_block = "10.1.2.0/24" 319 availability_zone = "us-west-2b" 320 vpc_id = "${aws_vpc.foo.id}" 321 tags { 322 Name = "tf-dbsubnet-test-2" 323 } 324 } 325 326 resource "aws_redshift_subnet_group" "foo" { 327 name = "foo-%d" 328 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 329 tags { 330 Name = "tf-redshift-subnetgroup" 331 } 332 } 333 `, rInt) 334 } 335 336 func testAccRedshiftSubnetGroupConfigWithTagsUpdated(rInt int) string { 337 return fmt.Sprintf(` 338 resource "aws_vpc" "foo" { 339 cidr_block = "10.1.0.0/16" 340 tags { 341 Name = "testAccRedshiftSubnetGroupConfigWithTags" 342 } 343 } 344 345 resource "aws_subnet" "foo" { 346 cidr_block = "10.1.1.0/24" 347 availability_zone = "us-west-2a" 348 vpc_id = "${aws_vpc.foo.id}" 349 tags { 350 Name = "tf-dbsubnet-test-1" 351 } 352 } 353 354 resource "aws_subnet" "bar" { 355 cidr_block = "10.1.2.0/24" 356 availability_zone = "us-west-2b" 357 vpc_id = "${aws_vpc.foo.id}" 358 tags { 359 Name = "tf-dbsubnet-test-2" 360 } 361 } 362 363 resource "aws_redshift_subnet_group" "foo" { 364 name = "foo-%d" 365 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 366 tags { 367 Name = "tf-redshift-subnetgroup" 368 environment = "production" 369 foo = "bar" 370 } 371 } 372 `, rInt) 373 } 374 375 func testAccRedshiftSubnetGroupConfig_updateSubnetIds(rInt int) string { 376 return fmt.Sprintf(` 377 resource "aws_vpc" "foo" { 378 cidr_block = "10.1.0.0/16" 379 tags { 380 Name = "testAccRedshiftSubnetGroupConfig_updateSubnetIds" 381 } 382 } 383 384 resource "aws_subnet" "foo" { 385 cidr_block = "10.1.1.0/24" 386 availability_zone = "us-west-2a" 387 vpc_id = "${aws_vpc.foo.id}" 388 tags { 389 Name = "tf-dbsubnet-test-1" 390 } 391 } 392 393 resource "aws_subnet" "bar" { 394 cidr_block = "10.1.2.0/24" 395 availability_zone = "us-west-2b" 396 vpc_id = "${aws_vpc.foo.id}" 397 tags { 398 Name = "tf-dbsubnet-test-2" 399 } 400 } 401 402 resource "aws_subnet" "foobar" { 403 cidr_block = "10.1.3.0/24" 404 availability_zone = "us-west-2c" 405 vpc_id = "${aws_vpc.foo.id}" 406 tags { 407 Name = "tf-dbsubnet-test-3" 408 } 409 } 410 411 resource "aws_redshift_subnet_group" "foo" { 412 name = "foo-%d" 413 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"] 414 } 415 `, rInt) 416 }