Skip to content

Commit

Permalink
r/aws_elasticache_security_group: add import support (hashicorp#2277)
Browse files Browse the repository at this point in the history
* r/aws_elasticache_security_group: add import support

* r/aws_elasticache_security_group: hashicorp#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
  • Loading branch information
bflad authored and mdlavin committed Dec 9, 2017
1 parent f44b675 commit 0d823c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
13 changes: 11 additions & 2 deletions aws/resource_aws_elasticache_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -86,15 +89,15 @@ 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)
if err != nil {
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
Expand All @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions aws/resource_aws_elasticache_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/elasticache_security_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 0d823c4

Please sign in to comment.