From eff11efff3cd66551f7a37479d0c53f33c81b505 Mon Sep 17 00:00:00 2001 From: AMeng Date: Fri, 19 Aug 2016 11:10:57 -0600 Subject: [PATCH 1/3] provider/aws: Support Import aws_elasticache_cluster --- .../import_aws_elasticache_cluster_test.go | 28 +++++++++++++++++++ .../aws/resource_aws_elasticache_cluster.go | 3 ++ .../source/docs/import/importability.html.md | 2 +- .../aws/r/elasticache_cluster.html.markdown | 9 ++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/aws/import_aws_elasticache_cluster_test.go diff --git a/builtin/providers/aws/import_aws_elasticache_cluster_test.go b/builtin/providers/aws/import_aws_elasticache_cluster_test.go new file mode 100644 index 000000000..c0da43fca --- /dev/null +++ b/builtin/providers/aws/import_aws_elasticache_cluster_test.go @@ -0,0 +1,28 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSElasticacheCluster_importBasic(t *testing.T) { + resourceName := "aws_elasticache_cluster.bar" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSElasticacheClusterConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/aws/resource_aws_elasticache_cluster.go b/builtin/providers/aws/resource_aws_elasticache_cluster.go index d24201457..794a7f882 100644 --- a/builtin/providers/aws/resource_aws_elasticache_cluster.go +++ b/builtin/providers/aws/resource_aws_elasticache_cluster.go @@ -200,6 +200,9 @@ func resourceAwsElasticacheCluster() *schema.Resource { Read: resourceAwsElasticacheClusterRead, Update: resourceAwsElasticacheClusterUpdate, Delete: resourceAwsElasticacheClusterDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: resourceSchema, } diff --git a/website/source/docs/import/importability.html.md b/website/source/docs/import/importability.html.md index f0a297370..d08fb6b67 100644 --- a/website/source/docs/import/importability.html.md +++ b/website/source/docs/import/importability.html.md @@ -48,6 +48,7 @@ To make a resource importable, please see the * aws_eip * aws_elastic_beanstalk_application * aws_elastic_beanstalk_environment +* aws_elasticache_cluster * aws_elasticache_parameter_group * aws_elasticache_subnet_group * aws_elb @@ -149,4 +150,3 @@ To make a resource importable, please see the * triton_key * triton_machine * triton_vlan - diff --git a/website/source/docs/providers/aws/r/elasticache_cluster.html.markdown b/website/source/docs/providers/aws/r/elasticache_cluster.html.markdown index 9f24185b8..04bd84fb3 100644 --- a/website/source/docs/providers/aws/r/elasticache_cluster.html.markdown +++ b/website/source/docs/providers/aws/r/elasticache_cluster.html.markdown @@ -119,3 +119,12 @@ The following attributes are exported: [1]: https://docs.aws.amazon.com/AmazonElastiCache/latest/APIReference/API_ModifyCacheCluster.html [2]: https://docs.aws.amazon.com/fr_fr/AmazonElastiCache/latest/UserGuide/Clusters.Modify.html + + +## Import + +ElastiCache Clusters can be imported using the `cluster_id`, e.g. + +``` +$ terraform import aws_elasticache_cluster.my_cluster my_cluster +``` From 01cfeb806213d8d4e0530687990dd2e6735a4c03 Mon Sep 17 00:00:00 2001 From: AMeng Date: Fri, 19 Aug 2016 12:32:38 -0600 Subject: [PATCH 2/3] provider/aws: Set AWS region in elasticache cluster import test --- builtin/providers/aws/import_aws_elasticache_cluster_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/providers/aws/import_aws_elasticache_cluster_test.go b/builtin/providers/aws/import_aws_elasticache_cluster_test.go index c0da43fca..f1e951bc1 100644 --- a/builtin/providers/aws/import_aws_elasticache_cluster_test.go +++ b/builtin/providers/aws/import_aws_elasticache_cluster_test.go @@ -1,12 +1,17 @@ package aws import ( + "os" "testing" "github.com/hashicorp/terraform/helper/resource" ) func TestAccAWSElasticacheCluster_importBasic(t *testing.T) { + oldvar := os.Getenv("AWS_DEFAULT_REGION") + os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + defer os.Setenv("AWS_DEFAULT_REGION", oldvar) + resourceName := "aws_elasticache_cluster.bar" resource.Test(t, resource.TestCase{ From b02a5c47ec342422e2c9c8f68bc8febf2dab32e9 Mon Sep 17 00:00:00 2001 From: stack72 Date: Fri, 23 Sep 2016 11:06:19 +0100 Subject: [PATCH 3/3] provider/aws: Support Import of aws_elasticache_cluster Initial tests were failing as follows: ``` === RUN TestAccAWSElasticacheCluster_importBasic --- FAIL: TestAccAWSElasticacheCluster_importBasic (362.66s) testing.go:265: Step 1 error: ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected. (map[string]string) { } (map[string]string) (len=2) { (string) (len=20) "parameter_group_name": (string) (len=20) "default.memcached1.4", (string) (len=22) "security_group_names.#": (string) (len=1) "0" } FAIL exit status 1 ``` The import of ElastiCache clusters helped to point out 3 things: 1. Currently, we were trying to set the parameter_group_name as follows: ``` d.Set("parameter_group_name", c.CacheParameterGroup) ``` Unfortunately, c.CacheParameterGroup is a struct not a string. This was causing the test import failure. So this had to be replaced as follows: ``` if c.CacheParameterGroup != nil { d.Set("parameter_group_name", c.CacheParameterGroup.CacheParameterGroupName) } ``` 2. We were trying to set the security_group_names as follows: ``` d.Set("security_group_names", c.CacheSecurityGroups) ``` The CacheSecurityGroups was actually a []* so had to be changed to work as follows: ``` if len(c.CacheSecurityGroups) > 0 { d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(c.CacheSecurityGroups)) } ``` 3. We were trying to set the security_group_ids as follows: ``` d.Set("security_group_ids", c.SecurityGroups) ``` This is another []* and needs to be changed as follows: ``` if len(c.SecurityGroups) > 0 { d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(c.SecurityGroups)) } ``` This then allows the import test to pass as expected: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSElasticacheCluster_importBasic' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/23 10:59:01 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSElasticacheCluster_importBasic -timeout 120m === RUN TestAccAWSElasticacheCluster_importBasic --- PASS: TestAccAWSElasticacheCluster_importBasic (351.96s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 351.981s ``` As a final test, I ran the basic ElastiCache cluster creation to make sure all passed as expected: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSElasticacheCluster_basic' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/23 11:05:51 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSElasticacheCluster_basic -timeout 120m === RUN TestAccAWSElasticacheCluster_basic --- PASS: TestAccAWSElasticacheCluster_basic (809.25s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 809.267s ``` --- .../import_aws_elasticache_cluster_test.go | 5 ++++- .../aws/resource_aws_elasticache_cluster.go | 8 +++++--- .../resource_aws_elasticache_cluster_test.go | 17 ++++++++++++++++ builtin/providers/aws/structure.go | 20 +++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/import_aws_elasticache_cluster_test.go b/builtin/providers/aws/import_aws_elasticache_cluster_test.go index f1e951bc1..6128ddf95 100644 --- a/builtin/providers/aws/import_aws_elasticache_cluster_test.go +++ b/builtin/providers/aws/import_aws_elasticache_cluster_test.go @@ -4,6 +4,7 @@ import ( "os" "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) @@ -12,6 +13,8 @@ func TestAccAWSElasticacheCluster_importBasic(t *testing.T) { os.Setenv("AWS_DEFAULT_REGION", "us-east-1") defer os.Setenv("AWS_DEFAULT_REGION", oldvar) + name := acctest.RandString(10) + resourceName := "aws_elasticache_cluster.bar" resource.Test(t, resource.TestCase{ @@ -20,7 +23,7 @@ func TestAccAWSElasticacheCluster_importBasic(t *testing.T) { CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSElasticacheClusterConfig, + Config: testAccAWSElasticacheClusterConfigBasic(name), }, resource.TestStep{ diff --git a/builtin/providers/aws/resource_aws_elasticache_cluster.go b/builtin/providers/aws/resource_aws_elasticache_cluster.go index 831424020..9b30963c4 100644 --- a/builtin/providers/aws/resource_aws_elasticache_cluster.go +++ b/builtin/providers/aws/resource_aws_elasticache_cluster.go @@ -364,9 +364,11 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) } d.Set("subnet_group_name", c.CacheSubnetGroupName) - d.Set("security_group_names", c.CacheSecurityGroups) - d.Set("security_group_ids", c.SecurityGroups) - d.Set("parameter_group_name", c.CacheParameterGroup) + d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(c.CacheSecurityGroups)) + d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(c.SecurityGroups)) + if c.CacheParameterGroup != nil { + d.Set("parameter_group_name", c.CacheParameterGroup.CacheParameterGroupName) + } d.Set("maintenance_window", c.PreferredMaintenanceWindow) d.Set("snapshot_window", c.SnapshotWindow) d.Set("snapshot_retention_limit", c.SnapshotRetentionLimit) diff --git a/builtin/providers/aws/resource_aws_elasticache_cluster_test.go b/builtin/providers/aws/resource_aws_elasticache_cluster_test.go index fe3d87691..cc9083862 100644 --- a/builtin/providers/aws/resource_aws_elasticache_cluster_test.go +++ b/builtin/providers/aws/resource_aws_elasticache_cluster_test.go @@ -219,6 +219,23 @@ func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheClust } } +func testAccAWSElasticacheClusterConfigBasic(clusterId string) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} + +resource "aws_elasticache_cluster" "bar" { + cluster_id = "tf-%s" + engine = "memcached" + node_type = "cache.m1.small" + num_cache_nodes = 1 + port = 11211 + parameter_group_name = "default.memcached1.4" +} +`, clusterId) +} + var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` provider "aws" { region = "us-east-1" diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 925abf8f4..a18f3d4b2 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -758,6 +758,26 @@ func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} return att } +func flattenElastiCacheSecurityGroupNames(securityGroups []*elasticache.CacheSecurityGroupMembership) []string { + result := make([]string, 0, len(securityGroups)) + for _, sg := range securityGroups { + if sg.CacheSecurityGroupName != nil { + result = append(result, *sg.CacheSecurityGroupName) + } + } + return result +} + +func flattenElastiCacheSecurityGroupIds(securityGroups []*elasticache.SecurityGroupMembership) []string { + result := make([]string, 0, len(securityGroups)) + for _, sg := range securityGroups { + if sg.SecurityGroupId != nil { + result = append(result, *sg.SecurityGroupId) + } + } + return result +} + // Flattens step adjustments into a list of map[string]interface. func flattenStepAdjustments(adjustments []*autoscaling.StepAdjustment) []map[string]interface{} { result := make([]map[string]interface{}, 0, len(adjustments))