Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_elasticache_security_group: add import support #2277

Merged
merged 2 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```