2015-04-26 03:53:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elasticache"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-05-14 20:57:01 +02:00
|
|
|
func TestAccAWSElasticacheCluster_basic(t *testing.T) {
|
2015-04-26 03:53:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-04-28 18:30:15 +02:00
|
|
|
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
|
2015-04-26 03:53:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-04-28 18:30:15 +02:00
|
|
|
Config: testAccAWSElasticacheClusterConfig,
|
2015-04-26 03:53:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-05-06 15:33:05 +02:00
|
|
|
testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
|
2015-04-28 18:30:15 +02:00
|
|
|
testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"),
|
2015-05-13 00:05:03 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-05-14 20:57:01 +02:00
|
|
|
"aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"),
|
2015-04-26 03:53:21 +02:00
|
|
|
),
|
|
|
|
},
|
2015-04-28 18:30:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
|
2015-06-02 18:19:23 +02:00
|
|
|
var csg elasticache.CacheSubnetGroup
|
2015-04-28 18:30:15 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2015-04-26 03:53:21 +02:00
|
|
|
resource.TestStep{
|
2015-04-28 18:30:15 +02:00
|
|
|
Config: testAccAWSElasticacheClusterInVPCConfig,
|
2015-04-26 03:53:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-06-02 18:19:23 +02:00
|
|
|
testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
|
2015-04-28 18:30:15 +02:00
|
|
|
testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"),
|
2015-04-26 03:53:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error {
|
2015-04-26 03:53:21 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
2015-04-28 18:30:15 +02:00
|
|
|
if rs.Type != "aws_elasticache_cluster" {
|
2015-04-26 03:53:21 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
|
|
|
|
CacheClusterID: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(res.CacheClusters) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
func testAccCheckAWSElasticacheClusterExists(n string) 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 cluster ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
|
|
|
|
_, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
|
|
|
|
CacheClusterID: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Elasticache error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genRandInt() int {
|
|
|
|
return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
var testAccAWSElasticacheClusterConfig = fmt.Sprintf(`
|
2015-06-01 00:01:07 +02:00
|
|
|
provider "aws" {
|
2015-05-14 20:57:01 +02:00
|
|
|
region = "us-east-1"
|
|
|
|
}
|
2015-04-26 03:53:21 +02:00
|
|
|
resource "aws_security_group" "bar" {
|
|
|
|
name = "tf-test-security-group-%03d"
|
|
|
|
description = "tf-test-security-group-descr"
|
|
|
|
ingress {
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
protocol = "icmp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_elasticache_security_group" "bar" {
|
|
|
|
name = "tf-test-security-group-%03d"
|
|
|
|
description = "tf-test-security-group-descr"
|
|
|
|
security_group_names = ["${aws_security_group.bar.name}"]
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
resource "aws_elasticache_cluster" "bar" {
|
2015-04-26 03:53:21 +02:00
|
|
|
cluster_id = "tf-test-%03d"
|
|
|
|
engine = "memcached"
|
|
|
|
node_type = "cache.m1.small"
|
|
|
|
num_cache_nodes = 1
|
2015-06-01 00:01:07 +02:00
|
|
|
port = 11211
|
2015-04-26 03:53:21 +02:00
|
|
|
parameter_group_name = "default.memcached1.4"
|
|
|
|
security_group_names = ["${aws_elasticache_security_group.bar.name}"]
|
|
|
|
}
|
|
|
|
`, genRandInt(), genRandInt(), genRandInt())
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
|
2015-04-26 03:53:21 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "192.168.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "192.168.0.0/20"
|
|
|
|
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}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "bar" {
|
|
|
|
name = "tf-test-security-group-%03d"
|
|
|
|
description = "tf-test-security-group-descr"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
ingress {
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
protocol = "icmp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:30:15 +02:00
|
|
|
resource "aws_elasticache_cluster" "bar" {
|
2015-04-26 03:53:21 +02:00
|
|
|
cluster_id = "tf-test-%03d"
|
|
|
|
node_type = "cache.m1.small"
|
|
|
|
num_cache_nodes = 1
|
|
|
|
engine = "redis"
|
|
|
|
engine_version = "2.8.19"
|
|
|
|
port = 6379
|
|
|
|
subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
|
|
|
|
security_group_ids = ["${aws_security_group.bar.id}"]
|
|
|
|
parameter_group_name = "default.redis2.8"
|
|
|
|
}
|
|
|
|
`, genRandInt(), genRandInt(), genRandInt())
|