From a856a9b5169e8ca5a265eedcefaf407f8ae84c74 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sat, 21 Apr 2018 18:34:20 +0900 Subject: [PATCH] Support DAX SubnetGroup --- aws/provider.go | 1 + aws/resource_aws_dax_subnet_group.go | 132 ++++++++++++++ aws/resource_aws_dax_subnet_group_test.go | 161 ++++++++++++++++++ website/aws.erb | 4 + website/docs/r/dax_subnet_group.html.markdown | 43 +++++ 5 files changed, 341 insertions(+) create mode 100644 aws/resource_aws_dax_subnet_group.go create mode 100644 aws/resource_aws_dax_subnet_group_test.go create mode 100644 website/docs/r/dax_subnet_group.html.markdown diff --git a/aws/provider.go b/aws/provider.go index 05be28cd2db1..5d10b6a93422 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/aws/resource_aws_dax_subnet_group.go b/aws/resource_aws_dax_subnet_group.go new file mode 100644 index 000000000000..9ce8128e5bc9 --- /dev/null +++ b/aws/resource_aws_dax_subnet_group.go @@ -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 +} diff --git a/aws/resource_aws_dax_subnet_group_test.go b/aws/resource_aws_dax_subnet_group_test.go new file mode 100644 index 000000000000..732ba0fbd845 --- /dev/null +++ b/aws/resource_aws_dax_subnet_group_test.go @@ -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) +} diff --git a/website/aws.erb b/website/aws.erb index 20809aecae83..70a2af0e668f 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -700,6 +700,10 @@ aws_dax_cluster + > + aws_dax_subnet_group + + diff --git a/website/docs/r/dax_subnet_group.html.markdown b/website/docs/r/dax_subnet_group.html.markdown new file mode 100644 index 000000000000..ff24a092d1dc --- /dev/null +++ b/website/docs/r/dax_subnet_group.html.markdown @@ -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 +```