Skip to content

Commit

Permalink
Merge pull request #7090 from kterada0509/feature/add-support-docdb-c…
Browse files Browse the repository at this point in the history
…luster-parameter-group-resource

Add support docdb cluster parameter group resource
  • Loading branch information
bflad authored Jan 10, 2019
2 parents ef04295 + 940943b commit 8d937ec
Show file tree
Hide file tree
Showing 10 changed files with 1,056 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/aws/aws-sdk-go/service/directoryservice"
"github.com/aws/aws-sdk-go/service/dlm"
"github.com/aws/aws-sdk-go/service/docdb"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr"
Expand Down Expand Up @@ -265,6 +266,7 @@ type AWSClient struct {
workspacesconn *workspaces.WorkSpaces
appmeshconn *appmesh.AppMesh
transferconn *transfer.Transfer
docdbconn *docdb.DocDB
}

func (c *AWSClient) S3() *s3.S3 {
Expand Down Expand Up @@ -603,6 +605,7 @@ func (c *Config) Client() (interface{}, error) {
client.workspacesconn = workspaces.New(sess)
client.appmeshconn = appmesh.New(sess)
client.transferconn = transfer.New(sess)
client.docdbconn = docdb.New(sess)

// Workaround for https://github.com/aws/aws-sdk-go/issues/1376
client.kinesisconn.Handlers.Retry.PushBack(func(r *request.Request) {
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ func Provider() terraform.ResourceProvider {
"aws_dms_replication_instance": resourceAwsDmsReplicationInstance(),
"aws_dms_replication_subnet_group": resourceAwsDmsReplicationSubnetGroup(),
"aws_dms_replication_task": resourceAwsDmsReplicationTask(),
"aws_docdb_cluster_parameter_group": resourceAwsDocDBClusterParameterGroup(),
"aws_dx_bgp_peer": resourceAwsDxBgpPeer(),
"aws_dx_connection": resourceAwsDxConnection(),
"aws_dx_connection_association": resourceAwsDxConnectionAssociation(),
Expand Down
282 changes: 282 additions & 0 deletions aws/resource_aws_docdb_cluster_parameter_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/docdb"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

const docdbClusterParameterGroupMaxParamsBulkEdit = 20

func resourceAwsDocDBClusterParameterGroup() *schema.Resource {

return &schema.Resource{
Create: resourceAwsDocDBClusterParameterGroupCreate,
Read: resourceAwsDocDBClusterParameterGroupRead,
Update: resourceAwsDocDBClusterParameterGroupUpdate,
Delete: resourceAwsDocDBClusterParameterGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateDocDBParamGroupName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: validateDocDBParamGroupNamePrefix,
},
"family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "Managed by Terraform",
},
"parameter": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
"apply_method": {
Type: schema.TypeString,
Optional: true,
Default: docdb.ApplyMethodPendingReboot,
ValidateFunc: validation.StringInSlice([]string{
docdb.ApplyMethodImmediate,
docdb.ApplyMethodPendingReboot,
}, false),
},
},
},
},

"tags": tagsSchema(),
},
}

}

func resourceAwsDocDBClusterParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).docdbconn
tags := tagsFromMapDocDB(d.Get("tags").(map[string]interface{}))

var groupName string
if v, ok := d.GetOk("name"); ok {
groupName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
groupName = resource.PrefixedUniqueId(v.(string))
} else {
groupName = resource.UniqueId()
}

createOpts := docdb.CreateDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(groupName),
DBParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: tags,
}

log.Printf("[DEBUG] Create DocDB Cluster Parameter Group: %#v", createOpts)

resp, err := conn.CreateDBClusterParameterGroup(&createOpts)
if err != nil {
return fmt.Errorf("Error creating DocDB Cluster Parameter Group: %s", err)
}

d.SetId(aws.StringValue(createOpts.DBClusterParameterGroupName))

d.Set("arn", resp.DBClusterParameterGroup.DBClusterParameterGroupArn)

return resourceAwsDocDBClusterParameterGroupUpdate(d, meta)
}

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

describeOpts := &docdb.DescribeDBClusterParameterGroupsInput{
DBClusterParameterGroupName: aws.String(d.Id()),
}

describeResp, err := conn.DescribeDBClusterParameterGroups(describeOpts)
if err != nil {
if isAWSErr(err, docdb.ErrCodeDBParameterGroupNotFoundFault, "") {
log.Printf("[WARN] DocDB Cluster Parameter Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading DocDB Cluster Parameter Group (%s): %s", d.Id(), err)
}

if len(describeResp.DBClusterParameterGroups) != 1 ||
*describeResp.DBClusterParameterGroups[0].DBClusterParameterGroupName != d.Id() {
return fmt.Errorf("Unable to find Cluster Parameter Group: %#v", describeResp.DBClusterParameterGroups)
}

arn := aws.StringValue(describeResp.DBClusterParameterGroups[0].DBClusterParameterGroupArn)
d.Set("arn", arn)
d.Set("description", describeResp.DBClusterParameterGroups[0].Description)
d.Set("family", describeResp.DBClusterParameterGroups[0].DBParameterGroupFamily)
d.Set("name", describeResp.DBClusterParameterGroups[0].DBClusterParameterGroupName)

describeParametersOpts := &docdb.DescribeDBClusterParametersInput{
DBClusterParameterGroupName: aws.String(d.Id()),
Source: aws.String("user"),
}

describeParametersResp, err := conn.DescribeDBClusterParameters(describeParametersOpts)
if err != nil {
return fmt.Errorf("error reading DocDB Cluster Parameter Group (%s) parameters: %s", d.Id(), err)
}

if err := d.Set("parameter", flattenDocDBParameters(describeParametersResp.Parameters)); err != nil {
return fmt.Errorf("error setting docdb cluster parameter: %s", err)
}

resp, err := conn.ListTagsForResource(&docdb.ListTagsForResourceInput{
ResourceName: aws.String(arn),
})

if err != nil {
return fmt.Errorf("error listing tags for DocDB Cluster Parameter Group (%s): %s", d.Id(), err)
}

if err := d.Set("tags", tagsToMapDocDB(resp.TagList)); err != nil {
return fmt.Errorf("Error setting docdb parameter group tags: %s", err)
}

return nil
}

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

d.Partial(true)

if d.HasChange("parameter") {
o, n := d.GetChange("parameter")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}

os := o.(*schema.Set)
ns := n.(*schema.Set)

parameters, err := expandDocDBParameters(ns.Difference(os).List())
if err != nil {
return err
}
if len(parameters) > 0 {
// We can only modify 20 parameters at a time, so walk them until
// we've got them all.
for parameters != nil {
var paramsToModify []*docdb.Parameter
if len(parameters) <= docdbClusterParameterGroupMaxParamsBulkEdit {
paramsToModify, parameters = parameters[:], nil
} else {
paramsToModify, parameters = parameters[:docdbClusterParameterGroupMaxParamsBulkEdit], parameters[docdbClusterParameterGroupMaxParamsBulkEdit:]
}
parameterGroupName := d.Id()
modifyOpts := docdb.ModifyDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(parameterGroupName),
Parameters: paramsToModify,
}

log.Printf("[DEBUG] Modify DocDB Cluster Parameter Group: %#v", modifyOpts)
_, err := conn.ModifyDBClusterParameterGroup(&modifyOpts)
if err != nil {
if isAWSErr(err, docdb.ErrCodeDBParameterGroupNotFoundFault, "") {
log.Printf("[WARN] DocDB Cluster Parameter Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error modifying DocDB Cluster Parameter Group: %s", err)
}
}
d.SetPartial("parameter")
}
}

if err := setTagsDocDB(conn, d); err != nil {
return err
}
d.SetPartial("tags")

d.Partial(false)

return resourceAwsDocDBClusterParameterGroupRead(d, meta)
}

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

deleteOpts := &docdb.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(d.Id()),
}

_, err := conn.DeleteDBClusterParameterGroup(deleteOpts)
if err != nil {
if isAWSErr(err, docdb.ErrCodeDBParameterGroupNotFoundFault, "") {
return nil
}
return err
}

return waitForDocDBClusterParameterGroupDeletion(conn, d.Id())
}

func waitForDocDBClusterParameterGroupDeletion(conn *docdb.DocDB, name string) error {
params := &docdb.DescribeDBClusterParameterGroupsInput{
DBClusterParameterGroupName: aws.String(name),
}

return resource.Retry(10*time.Minute, func() *resource.RetryError {
_, err := conn.DescribeDBClusterParameterGroups(params)

if isAWSErr(err, docdb.ErrCodeDBParameterGroupNotFoundFault, "") {
return nil
}

if err != nil {
return resource.NonRetryableError(err)
}

return resource.RetryableError(fmt.Errorf("DocDB Parameter Group (%s) still exists", name))
})
}
Loading

0 comments on commit 8d937ec

Please sign in to comment.