-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'atsushi-ishibashi-dax_pg'
- Loading branch information
Showing
5 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |