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