github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_elasticache_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/elasticache" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSElasticacheSubnetGroup_basic(t *testing.T) { 16 var csg elasticache.CacheSubnetGroup 17 config := fmt.Sprintf(testAccAWSElasticacheSubnetGroupConfig, acctest.RandInt()) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: config, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccAWSElasticacheSubnetGroup_update(t *testing.T) { 35 var csg elasticache.CacheSubnetGroup 36 rn := "aws_elasticache_subnet_group.bar" 37 ri := acctest.RandInt() 38 preConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPre, ri) 39 postConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPost, ri) 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: preConfig, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg), 50 testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 1), 51 ), 52 }, 53 54 resource.TestStep{ 55 Config: postConfig, 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg), 58 testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 2), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func testAccCheckAWSElasticacheSubnetGroupDestroy(s *terraform.State) error { 66 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 67 68 for _, rs := range s.RootModule().Resources { 69 if rs.Type != "aws_elasticache_subnet_group" { 70 continue 71 } 72 res, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{ 73 CacheSubnetGroupName: aws.String(rs.Primary.ID), 74 }) 75 if err != nil { 76 // Verify the error is what we want 77 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheSubnetGroupNotFoundFault" { 78 continue 79 } 80 return err 81 } 82 if len(res.CacheSubnetGroups) > 0 { 83 return fmt.Errorf("still exist.") 84 } 85 } 86 return nil 87 } 88 89 func testAccCheckAWSElasticacheSubnetGroupExists(n string, csg *elasticache.CacheSubnetGroup) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 rs, ok := s.RootModule().Resources[n] 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No cache subnet group ID is set") 98 } 99 100 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 101 resp, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{ 102 CacheSubnetGroupName: aws.String(rs.Primary.ID), 103 }) 104 if err != nil { 105 return fmt.Errorf("CacheSubnetGroup error: %v", err) 106 } 107 108 for _, c := range resp.CacheSubnetGroups { 109 if rs.Primary.ID == *c.CacheSubnetGroupName { 110 *csg = *c 111 } 112 } 113 114 if csg == nil { 115 return fmt.Errorf("cache subnet group not found") 116 } 117 return nil 118 } 119 } 120 121 func testAccCheckAWSElastiCacheSubnetGroupAttrs(csg *elasticache.CacheSubnetGroup, n string, count int) resource.TestCheckFunc { 122 return func(s *terraform.State) error { 123 124 rs, ok := s.RootModule().Resources[n] 125 if !ok { 126 return fmt.Errorf("Not found: %s", n) 127 } 128 129 if len(csg.Subnets) != count { 130 return fmt.Errorf("Bad cache subnet count, expected: %d, got: %d", count, len(csg.Subnets)) 131 } 132 133 if rs.Primary.Attributes["description"] != *csg.CacheSubnetGroupDescription { 134 return fmt.Errorf("Bad cache subnet description, expected: %s, got: %s", rs.Primary.Attributes["description"], *csg.CacheSubnetGroupDescription) 135 } 136 137 return nil 138 } 139 } 140 141 var testAccAWSElasticacheSubnetGroupConfig = ` 142 resource "aws_vpc" "foo" { 143 cidr_block = "192.168.0.0/16" 144 tags { 145 Name = "tf-test" 146 } 147 } 148 149 resource "aws_subnet" "foo" { 150 vpc_id = "${aws_vpc.foo.id}" 151 cidr_block = "192.168.0.0/20" 152 availability_zone = "us-west-2a" 153 tags { 154 Name = "tf-test" 155 } 156 } 157 158 resource "aws_elasticache_subnet_group" "bar" { 159 // Including uppercase letters in this name to ensure 160 // that we correctly handle the fact that the API 161 // normalizes names to lowercase. 162 name = "tf-TEST-cache-subnet-%03d" 163 description = "tf-test-cache-subnet-group-descr" 164 subnet_ids = ["${aws_subnet.foo.id}"] 165 } 166 ` 167 var testAccAWSElasticacheSubnetGroupUpdateConfigPre = ` 168 resource "aws_vpc" "foo" { 169 cidr_block = "10.0.0.0/16" 170 tags { 171 Name = "tf-elc-sub-test" 172 } 173 } 174 175 resource "aws_subnet" "foo" { 176 vpc_id = "${aws_vpc.foo.id}" 177 cidr_block = "10.0.1.0/24" 178 availability_zone = "us-west-2a" 179 tags { 180 Name = "tf-test" 181 } 182 } 183 184 resource "aws_elasticache_subnet_group" "bar" { 185 name = "tf-test-cache-subnet-%03d" 186 description = "tf-test-cache-subnet-group-descr" 187 subnet_ids = ["${aws_subnet.foo.id}"] 188 } 189 ` 190 191 var testAccAWSElasticacheSubnetGroupUpdateConfigPost = ` 192 resource "aws_vpc" "foo" { 193 cidr_block = "10.0.0.0/16" 194 tags { 195 Name = "tf-elc-sub-test" 196 } 197 } 198 199 resource "aws_subnet" "foo" { 200 vpc_id = "${aws_vpc.foo.id}" 201 cidr_block = "10.0.1.0/24" 202 availability_zone = "us-west-2a" 203 tags { 204 Name = "tf-test" 205 } 206 } 207 208 resource "aws_subnet" "bar" { 209 vpc_id = "${aws_vpc.foo.id}" 210 cidr_block = "10.0.2.0/24" 211 availability_zone = "us-west-2a" 212 tags { 213 Name = "tf-test-foo-update" 214 } 215 } 216 217 resource "aws_elasticache_subnet_group" "bar" { 218 name = "tf-test-cache-subnet-%03d" 219 description = "tf-test-cache-subnet-group-descr-edited" 220 subnet_ids = [ 221 "${aws_subnet.foo.id}", 222 "${aws_subnet.bar.id}", 223 ] 224 } 225 `