Skip to content

Commit

Permalink
Merge branch 'atsushi-ishibashi-dax_pg'
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Apr 23, 2018
2 parents b5f08ea + e2d8cf7 commit 610a99c
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func Provider() terraform.ResourceProvider {
"aws_codepipeline": resourceAwsCodePipeline(),
"aws_customer_gateway": resourceAwsCustomerGateway(),
"aws_dax_cluster": resourceAwsDaxCluster(),
"aws_dax_parameter_group": resourceAwsDaxParameterGroup(),
"aws_dax_subnet_group": resourceAwsDaxSubnetGroup(),
"aws_db_event_subscription": resourceAwsDbEventSubscription(),
"aws_db_instance": resourceAwsDbInstance(),
Expand Down
191 changes: 191 additions & 0 deletions aws/resource_aws_dax_parameter_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
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 resourceAwsDaxParameterGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDaxParameterGroupCreate,
Read: resourceAwsDaxParameterGroupRead,
Update: resourceAwsDaxParameterGroupUpdate,
Delete: resourceAwsDaxParameterGroupDelete,

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,
ForceNew: true,
},
"parameters": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}

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

input := &dax.CreateParameterGroupInput{
ParameterGroupName: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

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

d.SetId(d.Get("name").(string))

if len(d.Get("parameters").(*schema.Set).List()) > 0 {
return resourceAwsDaxParameterGroupUpdate(d, meta)
}
return resourceAwsDaxParameterGroupRead(d, meta)
}

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

resp, err := conn.DescribeParameterGroups(&dax.DescribeParameterGroupsInput{
ParameterGroupNames: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, dax.ErrCodeParameterGroupNotFoundFault, "") {
log.Printf("[WARN] DAX ParameterGroup %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

if len(resp.ParameterGroups) == 0 {
log.Printf("[WARN] DAX ParameterGroup %q not found, removing from state", d.Id())
d.SetId("")
return nil
}

pg := resp.ParameterGroups[0]

paramresp, err := conn.DescribeParameters(&dax.DescribeParametersInput{
ParameterGroupName: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, dax.ErrCodeParameterGroupNotFoundFault, "") {
log.Printf("[WARN] DAX ParameterGroup %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

d.Set("name", pg.ParameterGroupName)
desc := pg.Description
// default description is " "
if desc != nil && *desc == " " {
*desc = ""
}
d.Set("description", desc)
d.Set("parameters", flattenDaxParameterGroupParameters(paramresp.Parameters))
return nil
}

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

input := &dax.UpdateParameterGroupInput{
ParameterGroupName: aws.String(d.Id()),
}

if d.HasChange("parameters") {
input.ParameterNameValues = expandDaxParameterGroupParameterNameValue(
d.Get("parameters").(*schema.Set).List(),
)
}

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

return resourceAwsDaxParameterGroupRead(d, meta)
}

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

input := &dax.DeleteParameterGroupInput{
ParameterGroupName: aws.String(d.Id()),
}

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

return nil
}

func expandDaxParameterGroupParameterNameValue(config []interface{}) []*dax.ParameterNameValue {
if len(config) == 0 {
return nil
}
results := make([]*dax.ParameterNameValue, 0, len(config))
for _, raw := range config {
m := raw.(map[string]interface{})
pnv := &dax.ParameterNameValue{
ParameterName: aws.String(m["name"].(string)),
ParameterValue: aws.String(m["value"].(string)),
}
results = append(results, pnv)
}
return results
}

func flattenDaxParameterGroupParameters(params []*dax.Parameter) []map[string]interface{} {
if len(params) == 0 {
return nil
}
results := make([]map[string]interface{}, 0)
for _, p := range params {
m := map[string]interface{}{
"name": aws.StringValue(p.ParameterName),
"value": aws.StringValue(p.ParameterValue),
}
results = append(results, m)
}
return results
}
124 changes: 124 additions & 0 deletions aws/resource_aws_dax_parameter_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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 TestAccAwsDaxParameterGroup_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDaxParameterGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccDaxParameterGroupConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDaxParameterGroupExists("aws_dax_parameter_group.test"),
resource.TestCheckResourceAttr("aws_dax_parameter_group.test", "parameters.#", "2"),
),
},
{
Config: testAccDaxParameterGroupConfig_parameters(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDaxParameterGroupExists("aws_dax_parameter_group.test"),
resource.TestCheckResourceAttr("aws_dax_parameter_group.test", "parameters.#", "2"),
),
},
},
})
}

func TestAccAwsDaxParameterGroup_import(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_dax_parameter_group.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDaxParameterGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDaxParameterGroupConfig(rName),
},

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

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

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

_, err := conn.DescribeParameterGroups(&dax.DescribeParameterGroupsInput{
ParameterGroupNames: []*string{aws.String(rs.Primary.ID)},
})
if err != nil {
if isAWSErr(err, dax.ErrCodeParameterGroupNotFoundFault, "") {
return nil
}
return err
}
}
return nil
}

func testAccCheckAwsDaxParameterGroupExists(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.DescribeParameterGroups(&dax.DescribeParameterGroupsInput{
ParameterGroupNames: []*string{aws.String(rs.Primary.ID)},
})
if err != nil {
return err
}

return nil
}
}

func testAccDaxParameterGroupConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_dax_parameter_group" "test" {
name = "%s"
}
`, rName)
}

func testAccDaxParameterGroupConfig_parameters(rName string) string {
return fmt.Sprintf(`
resource "aws_dax_parameter_group" "test" {
name = "%s"
parameters {
name = "query-ttl-millis"
value = "100000"
}
parameters {
name = "record-ttl-millis"
value = "100000"
}
}
`, rName)
}
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,10 @@
<a href="/docs/providers/aws/r/dax_cluster.html">aws_dax_cluster</a>
</li>

<li<%= sidebar_current("docs-aws-resource-dax-parameter-group") %>>
<a href="/docs/providers/aws/r/dax_parameter_group.html">aws_dax_parameter_group</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>
Expand Down
58 changes: 58 additions & 0 deletions website/docs/r/dax_parameter_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
layout: "aws"
page_title: "AWS: aws_dax_parameter_group"
sidebar_current: "docs-aws-resource-dax-parameter-group"
description: |-
Provides an DAX Parameter Group resource.
---

# aws_dax_parameter_group

Provides a DAX Parameter Group resource.

## Example Usage

```hcl
resource "aws_dax_parameter_group" "example" {
name = "example"
parameters {
name = "query-ttl-millis"
value = "100000"
}
parameters {
name = "record-ttl-millis"
value = "100000"
}
}
```

## Argument Reference

The following arguments are supported:

* `name` – (Required) The name of the parameter group.

* `description` - (Optional, ForceNew) A description of the parameter group.

* `parameters` – (Optional) The parameters of the parameter group.

## parameters

`parameters` supports the following:

* `name` - (Required) The name of the parameter.
* `value` - (Required) The value for the parameter.

## Attributes Reference

The following additional attributes are exported:

* `id` - The name of the parameter group.

## Import

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

```
$ terraform import aws_dax_parameter_group.example my_dax_pg
```

0 comments on commit 610a99c

Please sign in to comment.