From 787064d391a8d6333c29d205502d08c32afb8437 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 14 Nov 2017 03:49:42 -0500 Subject: [PATCH 1/2] r/aws_elasticache_security_group: add import support --- ...resource_aws_elasticache_security_group.go | 6 ++++++ ...rce_aws_elasticache_security_group_test.go | 19 +++++++++++++++++++ .../elasticache_security_group.html.markdown | 8 ++++++++ 3 files changed, 33 insertions(+) diff --git a/aws/resource_aws_elasticache_security_group.go b/aws/resource_aws_elasticache_security_group.go index 07676e5135a..b0f67fc1535 100644 --- a/aws/resource_aws_elasticache_security_group.go +++ b/aws/resource_aws_elasticache_security_group.go @@ -17,6 +17,12 @@ func resourceAwsElasticacheSecurityGroup() *schema.Resource { Create: resourceAwsElasticacheSecurityGroupCreate, Read: resourceAwsElasticacheSecurityGroupRead, Delete: resourceAwsElasticacheSecurityGroupDelete, + Importer: &schema.ResourceImporter{ + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + d.Set("name", d.Id()) + return []*schema.ResourceData{d}, nil + }, + }, Schema: map[string]*schema.Schema{ "description": &schema.Schema{ diff --git a/aws/resource_aws_elasticache_security_group_test.go b/aws/resource_aws_elasticache_security_group_test.go index 2c9c93b865c..0258570b40a 100644 --- a/aws/resource_aws_elasticache_security_group_test.go +++ b/aws/resource_aws_elasticache_security_group_test.go @@ -30,6 +30,25 @@ func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) { }) } +func TestAccAWSElasticacheSecurityGroup_Import(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSElasticacheSecurityGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSElasticacheSecurityGroupConfig, + }, + + resource.TestStep{ + ResourceName: "aws_elasticache_security_group.bar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckAWSElasticacheSecurityGroupDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elasticacheconn diff --git a/website/docs/r/elasticache_security_group.html.markdown b/website/docs/r/elasticache_security_group.html.markdown index 08b0c2d2dc4..d3116cb729b 100644 --- a/website/docs/r/elasticache_security_group.html.markdown +++ b/website/docs/r/elasticache_security_group.html.markdown @@ -45,3 +45,11 @@ The following attributes are exported: * `description` * `name` * `security_group_names` + +## Import + +ElastiCache Security Groups can be imported by name, e.g. + +``` +$ terraform import aws_elasticache_security_group.my_ec_security_group ec-security-group-1 +``` From 6d7715acc0a0d1b5a1e1b5b5c5e0331867a27ae3 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 27 Nov 2017 21:45:53 -0500 Subject: [PATCH 2/2] r/aws_elasticache_security_group: #2277 review updates * Use d.Id() instead of d.Get("name") on read, which allows using schema.ImportStatePassthrough * d.Set("security_group_names") on read * Set AWS_DEFAULT_REGION to us-east-1 on import testing --- aws/resource_aws_elasticache_security_group.go | 15 +++++++++------ ...esource_aws_elasticache_security_group_test.go | 6 ++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_elasticache_security_group.go b/aws/resource_aws_elasticache_security_group.go index b0f67fc1535..1f282f9a6b3 100644 --- a/aws/resource_aws_elasticache_security_group.go +++ b/aws/resource_aws_elasticache_security_group.go @@ -18,10 +18,7 @@ func resourceAwsElasticacheSecurityGroup() *schema.Resource { Read: resourceAwsElasticacheSecurityGroupRead, Delete: resourceAwsElasticacheSecurityGroupDelete, Importer: &schema.ResourceImporter{ - State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - d.Set("name", d.Id()) - return []*schema.ResourceData{d}, nil - }, + State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ @@ -92,7 +89,7 @@ func resourceAwsElasticacheSecurityGroupCreate(d *schema.ResourceData, meta inte func resourceAwsElasticacheSecurityGroupRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elasticacheconn req := &elasticache.DescribeCacheSecurityGroupsInput{ - CacheSecurityGroupName: aws.String(d.Get("name").(string)), + CacheSecurityGroupName: aws.String(d.Id()), } res, err := conn.DescribeCacheSecurityGroups(req) @@ -100,7 +97,7 @@ func resourceAwsElasticacheSecurityGroupRead(d *schema.ResourceData, meta interf return err } if len(res.CacheSecurityGroups) == 0 { - return fmt.Errorf("Error missing %v", d.Get("name")) + return fmt.Errorf("Error missing %v", d.Id()) } var group *elasticache.CacheSecurityGroup @@ -117,6 +114,12 @@ func resourceAwsElasticacheSecurityGroupRead(d *schema.ResourceData, meta interf d.Set("name", group.CacheSecurityGroupName) d.Set("description", group.Description) + sgNames := make([]string, 0, len(group.EC2SecurityGroups)) + for _, sg := range group.EC2SecurityGroups { + sgNames = append(sgNames, *sg.EC2SecurityGroupName) + } + d.Set("security_group_names", sgNames) + return nil } diff --git a/aws/resource_aws_elasticache_security_group_test.go b/aws/resource_aws_elasticache_security_group_test.go index 0258570b40a..553e9fada73 100644 --- a/aws/resource_aws_elasticache_security_group_test.go +++ b/aws/resource_aws_elasticache_security_group_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "os" "testing" "github.com/aws/aws-sdk-go/aws" @@ -31,6 +32,11 @@ func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) { } func TestAccAWSElasticacheSecurityGroup_Import(t *testing.T) { + // Use EC2-Classic enabled us-east-1 for testing + oldRegion := os.Getenv("AWS_DEFAULT_REGION") + os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + defer os.Setenv("AWS_DEFAULT_REGION", oldRegion) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders,