2015-04-26 03:53:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-22 16:02:19 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/elasticache"
|
2016-02-04 18:20:22 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-02 18:19:23 +02:00
|
|
|
func TestAccAWSElasticacheSubnetGroup_basic(t *testing.T) {
|
|
|
|
var csg elasticache.CacheSubnetGroup
|
2016-02-04 18:20:22 +01:00
|
|
|
config := fmt.Sprintf(testAccAWSElasticacheSubnetGroupConfig, acctest.RandInt())
|
2015-06-02 18:19:23 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
|
2016-04-18 23:06:15 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elasticache_subnet_group.bar", "description", "Managed by Terraform"),
|
2015-06-02 18:19:23 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSElasticacheSubnetGroup_update(t *testing.T) {
|
|
|
|
var csg elasticache.CacheSubnetGroup
|
|
|
|
rn := "aws_elasticache_subnet_group.bar"
|
2016-02-04 18:20:22 +01:00
|
|
|
ri := acctest.RandInt()
|
2015-06-02 18:19:23 +02:00
|
|
|
preConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPre, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPost, ri)
|
|
|
|
|
2015-04-26 03:53:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-05-06 15:33:05 +02:00
|
|
|
CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy,
|
2015-04-26 03:53:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-06-02 18:19:23 +02:00
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg),
|
|
|
|
testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 1),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: postConfig,
|
2015-04-26 03:53:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-06-02 18:19:23 +02:00
|
|
|
testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg),
|
|
|
|
testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 2),
|
2015-04-26 03:53:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-06 15:33:05 +02:00
|
|
|
func testAccCheckAWSElasticacheSubnetGroupDestroy(s *terraform.State) error {
|
2015-04-26 03:53:21 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_elasticache_subnet_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{
|
|
|
|
CacheSubnetGroupName: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-12-22 16:02:19 +01:00
|
|
|
// Verify the error is what we want
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheSubnetGroupNotFoundFault" {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-26 03:53:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(res.CacheSubnetGroups) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-02 18:19:23 +02:00
|
|
|
func testAccCheckAWSElasticacheSubnetGroupExists(n string, csg *elasticache.CacheSubnetGroup) resource.TestCheckFunc {
|
2015-04-26 03:53:21 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No cache subnet group ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
|
2015-06-02 18:19:23 +02:00
|
|
|
resp, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{
|
2015-04-26 03:53:21 +02:00
|
|
|
CacheSubnetGroupName: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CacheSubnetGroup error: %v", err)
|
|
|
|
}
|
2015-06-02 18:19:23 +02:00
|
|
|
|
|
|
|
for _, c := range resp.CacheSubnetGroups {
|
|
|
|
if rs.Primary.ID == *c.CacheSubnetGroupName {
|
|
|
|
*csg = *c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if csg == nil {
|
|
|
|
return fmt.Errorf("cache subnet group not found")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSElastiCacheSubnetGroupAttrs(csg *elasticache.CacheSubnetGroup, n string, count int) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(csg.Subnets) != count {
|
|
|
|
return fmt.Errorf("Bad cache subnet count, expected: %d, got: %d", count, len(csg.Subnets))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.Attributes["description"] != *csg.CacheSubnetGroupDescription {
|
|
|
|
return fmt.Errorf("Bad cache subnet description, expected: %s, got: %s", rs.Primary.Attributes["description"], *csg.CacheSubnetGroupDescription)
|
|
|
|
}
|
|
|
|
|
2015-04-26 03:53:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 18:19:23 +02:00
|
|
|
var testAccAWSElasticacheSubnetGroupConfig = `
|
2015-04-26 03:53:21 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
2015-05-06 20:44:24 +02:00
|
|
|
cidr_block = "192.168.0.0/16"
|
2015-04-26 03:53:21 +02:00
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2015-05-06 20:44:24 +02:00
|
|
|
cidr_block = "192.168.0.0/20"
|
2015-04-26 03:53:21 +02:00
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_elasticache_subnet_group" "bar" {
|
2015-08-29 18:40:18 +02:00
|
|
|
// Including uppercase letters in this name to ensure
|
|
|
|
// that we correctly handle the fact that the API
|
|
|
|
// normalizes names to lowercase.
|
|
|
|
name = "tf-TEST-cache-subnet-%03d"
|
2015-04-26 03:53:21 +02:00
|
|
|
subnet_ids = ["${aws_subnet.foo.id}"]
|
|
|
|
}
|
2015-06-02 18:19:23 +02:00
|
|
|
`
|
|
|
|
var testAccAWSElasticacheSubnetGroupUpdateConfigPre = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-elc-sub-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_elasticache_subnet_group" "bar" {
|
|
|
|
name = "tf-test-cache-subnet-%03d"
|
|
|
|
description = "tf-test-cache-subnet-group-descr"
|
|
|
|
subnet_ids = ["${aws_subnet.foo.id}"]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAWSElasticacheSubnetGroupUpdateConfigPost = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-elc-sub-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "bar" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.0.2.0/24"
|
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
tags {
|
|
|
|
Name = "tf-test-foo-update"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_elasticache_subnet_group" "bar" {
|
|
|
|
name = "tf-test-cache-subnet-%03d"
|
|
|
|
description = "tf-test-cache-subnet-group-descr-edited"
|
|
|
|
subnet_ids = [
|
|
|
|
"${aws_subnet.foo.id}",
|
|
|
|
"${aws_subnet.bar.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|