From 520f96e84b8b4c85efd29d97c9a3713cae0199bc Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 29 Sep 2016 22:24:52 +0100 Subject: [PATCH] provider/aws: Support Import of AWS elasticache_replication_groups Fixes #9094 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSElasticacheReplicationGroup_importBasic' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/30 00:09:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSElasticacheReplicationGroup_importBasic -timeout 120m === RUN TestAccAWSElasticacheReplicationGroup_importBasic --- PASS: TestAccAWSElasticacheReplicationGroup_importBasic (756.38s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws756.398s ``` --- ..._aws_elasticache_replication_group_test.go | 37 +++++++++++++++++++ ...ource_aws_elasticache_replication_group.go | 26 ++++++++++--- ...lasticache_replication_group.html.markdown | 10 ++++- 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 builtin/providers/aws/import_aws_elasticache_replication_group_test.go diff --git a/builtin/providers/aws/import_aws_elasticache_replication_group_test.go b/builtin/providers/aws/import_aws_elasticache_replication_group_test.go new file mode 100644 index 000000000..372eff849 --- /dev/null +++ b/builtin/providers/aws/import_aws_elasticache_replication_group_test.go @@ -0,0 +1,37 @@ +package aws + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSElasticacheReplicationGroup_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) + + name := acctest.RandString(10) + + resourceName := "aws_elasticache_replication_group.bar" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSElasticacheReplicationGroupConfig(name), + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"apply_immediately"}, //not in the API + }, + }, + }) +} diff --git a/builtin/providers/aws/resource_aws_elasticache_replication_group.go b/builtin/providers/aws/resource_aws_elasticache_replication_group.go index bcee8cf03..2ae8b1bdb 100644 --- a/builtin/providers/aws/resource_aws_elasticache_replication_group.go +++ b/builtin/providers/aws/resource_aws_elasticache_replication_group.go @@ -57,6 +57,9 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource { Read: resourceAwsElasticacheReplicationGroupRead, Update: resourceAwsElasticacheReplicationGroupUpdate, Delete: resourceAwsElasticacheReplicationGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: resourceSchema, } @@ -192,7 +195,17 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int return nil } - d.Set("automatic_failover_enabled", rgp.AutomaticFailover) + if rgp.AutomaticFailover != nil { + switch strings.ToLower(*rgp.AutomaticFailover) { + case "disabled", "disabling": + d.Set("automatic_failover_enabled", false) + case "enabled", "enabling": + d.Set("automatic_failover_enabled", true) + default: + log.Printf("Unknown AutomaticFailover state %s", *rgp.AutomaticFailover) + } + } + d.Set("replication_group_description", rgp.Description) d.Set("number_cache_clusters", len(rgp.MemberClusters)) d.Set("replication_group_id", rgp.ReplicationGroupId) @@ -217,15 +230,16 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int d.Set("engine", c.Engine) d.Set("engine_version", c.EngineVersion) 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) - + d.Set("port", rgp.NodeGroups[0].PrimaryEndpoint.Port) d.Set("primary_endpoint_address", rgp.NodeGroups[0].PrimaryEndpoint.Address) - } return nil diff --git a/website/source/docs/providers/aws/r/elasticache_replication_group.html.markdown b/website/source/docs/providers/aws/r/elasticache_replication_group.html.markdown index a19ef7b0e..2de0e3099 100644 --- a/website/source/docs/providers/aws/r/elasticache_replication_group.html.markdown +++ b/website/source/docs/providers/aws/r/elasticache_replication_group.html.markdown @@ -68,4 +68,12 @@ Please note that setting a `snapshot_retention_limit` is not supported on cache. The following attributes are exported: * `id` - The ID of the ElastiCache Replication Group -* `primary_endpoint_address` - The address of the endpoint for the primary node in the replication group \ No newline at end of file +* `primary_endpoint_address` - The address of the endpoint for the primary node in the replication group + +## Import + +ElastiCache Replication Groups can be imported using the `replication_group_id`, e.g. + +``` +$ terraform import aws_elasticache_replication_group.my_replication_group replication-group-1 +``` \ No newline at end of file