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

NewResource/dax_subnet_group: Support DAX SubnetGroup #4302

Merged
merged 1 commit into from
Apr 23, 2018
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func Provider() terraform.ResourceProvider {
"aws_codepipeline": resourceAwsCodePipeline(),
"aws_customer_gateway": resourceAwsCustomerGateway(),
"aws_dax_cluster": resourceAwsDaxCluster(),
"aws_dax_subnet_group": resourceAwsDaxSubnetGroup(),
"aws_db_event_subscription": resourceAwsDbEventSubscription(),
"aws_db_instance": resourceAwsDbInstance(),
"aws_db_option_group": resourceAwsDbOptionGroup(),
Expand Down
132 changes: 132 additions & 0 deletions aws/resource_aws_dax_subnet_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package aws

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dax"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsDaxSubnetGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDaxSubnetGroupCreate,
Read: resourceAwsDaxSubnetGroupRead,
Update: resourceAwsDaxSubnetGroupUpdate,
Delete: resourceAwsDaxSubnetGroupDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"subnet_ids": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsDaxSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).daxconn

input := &dax.CreateSubnetGroupInput{
SubnetGroupName: aws.String(d.Get("name").(string)),
SubnetIds: expandStringSet(d.Get("subnet_ids").(*schema.Set)),
}
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

_, err := conn.CreateSubnetGroup(input)
if err != nil {
return err
}

d.SetId(d.Get("name").(string))
return resourceAwsDaxSubnetGroupRead(d, meta)
}

func resourceAwsDaxSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).daxconn

resp, err := conn.DescribeSubnetGroups(&dax.DescribeSubnetGroupsInput{
SubnetGroupNames: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, dax.ErrCodeSubnetGroupNotFoundFault, "") {
log.Printf("[WARN] DAX SubnetGroup %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
sg := resp.SubnetGroups[0]

d.Set("name", sg.SubnetGroupName)
d.Set("description", sg.Description)
subnetIDs := make([]*string, 0, len(sg.Subnets))
for _, v := range sg.Subnets {
subnetIDs = append(subnetIDs, v.SubnetIdentifier)
}
d.Set("subnet_ids", flattenStringList(subnetIDs))
d.Set("vpc_id", sg.VpcId)
return nil
}

func resourceAwsDaxSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).daxconn

input := &dax.UpdateSubnetGroupInput{
SubnetGroupName: aws.String(d.Id()),
}

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}

if d.HasChange("subnet_ids") {
input.SubnetIds = expandStringSet(d.Get("subnet_ids").(*schema.Set))
}

_, err := conn.UpdateSubnetGroup(input)
if err != nil {
return err
}

return resourceAwsDaxSubnetGroupRead(d, meta)
}

func resourceAwsDaxSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).daxconn

input := &dax.DeleteSubnetGroupInput{
SubnetGroupName: aws.String(d.Id()),
}

_, err := conn.DeleteSubnetGroup(input)
if err != nil {
if isAWSErr(err, dax.ErrCodeSubnetGroupNotFoundFault, "") {
return nil
}
return err
}

return nil
}
161 changes: 161 additions & 0 deletions aws/resource_aws_dax_subnet_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dax"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAwsDaxSubnetGroup_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDaxSubnetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccDaxSubnetGroupConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDaxSubnetGroupExists("aws_dax_subnet_group.test"),
resource.TestCheckResourceAttr("aws_dax_subnet_group.test", "subnet_ids.#", "2"),
resource.TestCheckResourceAttrSet("aws_dax_subnet_group.test", "vpc_id"),
),
},
{
Config: testAccDaxSubnetGroupConfig_update(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDaxSubnetGroupExists("aws_dax_subnet_group.test"),
resource.TestCheckResourceAttr("aws_dax_subnet_group.test", "description", "update"),
resource.TestCheckResourceAttr("aws_dax_subnet_group.test", "subnet_ids.#", "3"),
resource.TestCheckResourceAttrSet("aws_dax_subnet_group.test", "vpc_id"),
),
},
},
})
}

func TestAccAwsDaxSubnetGroup_import(t *testing.T) {
resourceName := "aws_dax_subnet_group.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDaxSubnetGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDaxSubnetGroupConfig(acctest.RandString(5)),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAwsDaxSubnetGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).daxconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_dax_subnet_group" {
continue
}

_, err := conn.DescribeSubnetGroups(&dax.DescribeSubnetGroupsInput{
SubnetGroupNames: []*string{aws.String(rs.Primary.ID)},
})
if err != nil {
if isAWSErr(err, dax.ErrCodeSubnetGroupNotFoundFault, "") {
return nil
}
return err
}
}
return nil
}

func testAccCheckAwsDaxSubnetGroupExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

conn := testAccProvider.Meta().(*AWSClient).daxconn

_, err := conn.DescribeSubnetGroups(&dax.DescribeSubnetGroupsInput{
SubnetGroupNames: []*string{aws.String(rs.Primary.ID)},
})
if err != nil {
return err
}

return nil
}
}

func testAccDaxSubnetGroupConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "test1" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_subnet" "test2" {
cidr_block = "10.0.2.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_dax_subnet_group" "test" {
name = "%s"
subnet_ids = [
"${aws_subnet.test1.id}",
"${aws_subnet.test2.id}",
]
}
`, rName)
}

func testAccDaxSubnetGroupConfig_update(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "test1" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_subnet" "test2" {
cidr_block = "10.0.2.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_subnet" "test3" {
cidr_block = "10.0.3.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_dax_subnet_group" "test" {
name = "%s"
description = "update"
subnet_ids = [
"${aws_subnet.test1.id}",
"${aws_subnet.test2.id}",
"${aws_subnet.test3.id}",
]
}
`, rName)
}
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@
<a href="/docs/providers/aws/r/dax_cluster.html">aws_dax_cluster</a>
</li>

<li<%= sidebar_current("docs-aws-resource-dax-subnet-group") %>>
<a href="/docs/providers/aws/r/dax_subnet_group.html">aws_dax_subnet_group</a>
</li>

</ul>
</li>

Expand Down
43 changes: 43 additions & 0 deletions website/docs/r/dax_subnet_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: "aws"
page_title: "AWS: aws_dax_subnet_group"
sidebar_current: "docs-aws-resource-dax-subnet-group"
description: |-
Provides an DAX Subnet Group resource.
---

# aws_dax_subnet_group

Provides a DAX Subnet Group resource.

## Example Usage

```hcl
resource "aws_dax_subnet_group" "example" {
name = "example"
subnet_ids = ["${aws_subnet.example1.id}", "${aws_subnet.example2.id}"]
}
```

## Argument Reference

The following arguments are supported:

* `name` – (Required) The name of the subnet group.
* `description` - (Optional) A description of the subnet group.
* `subnet_ids` – (Required) A list of VPC subnet IDs for the subnet group.

## Attributes Reference

The following additional attributes are exported:

* `id` - The name of the subnet group.
* `vpc_id` – VPC ID of the subnet group.

## Import

DAX Subnet Group can be imported using the `name`, e.g.

```
$ terraform import aws_dax_subnet_group.example my_dax_sg
```