From 0d823c4c81b4e3530992a18aa1ff67d96ac7212d Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 8 Dec 2017 06:59:39 -0500 Subject: [PATCH] r/aws_elasticache_security_group: add import support (#2277) * r/aws_elasticache_security_group: add import support * 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 --- ...resource_aws_elasticache_security_group.go | 13 ++++++++-- ...rce_aws_elasticache_security_group_test.go | 25 +++++++++++++++++++ .../elasticache_security_group.html.markdown | 8 ++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_elasticache_security_group.go b/aws/resource_aws_elasticache_security_group.go index 07676e5135a..1f282f9a6b3 100644 --- a/aws/resource_aws_elasticache_security_group.go +++ b/aws/resource_aws_elasticache_security_group.go @@ -17,6 +17,9 @@ func resourceAwsElasticacheSecurityGroup() *schema.Resource { Create: resourceAwsElasticacheSecurityGroupCreate, Read: resourceAwsElasticacheSecurityGroupRead, Delete: resourceAwsElasticacheSecurityGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "description": &schema.Schema{ @@ -86,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) @@ -94,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 @@ -111,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 2c9c93b865c..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" @@ -30,6 +31,30 @@ 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, + 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 +```