2014-09-12 17:59:31 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-06-25 11:57:21 +02:00
|
|
|
"regexp"
|
2015-04-03 16:19:20 +02:00
|
|
|
"strings"
|
2014-09-12 17:59:31 +02:00
|
|
|
"time"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-09-02 09:24:34 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/rds"
|
2014-09-12 17:59:31 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsDbSubnetGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsDbSubnetGroupCreate,
|
|
|
|
Read: resourceAwsDbSubnetGroupRead,
|
2015-08-24 23:52:30 +02:00
|
|
|
Update: resourceAwsDbSubnetGroupUpdate,
|
2014-09-12 17:59:31 +02:00
|
|
|
Delete: resourceAwsDbSubnetGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-12-11 13:39:15 +01:00
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-09-12 17:59:31 +02:00
|
|
|
"name": &schema.Schema{
|
2015-12-16 16:00:36 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateSubnetGroupName,
|
2014-09-12 17:59:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"subnet_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-09-12 17:59:31 +02:00
|
|
|
},
|
2015-09-01 17:21:49 +02:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2014-09-12 17:59:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2015-09-01 17:21:49 +02:00
|
|
|
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
|
2014-09-12 17:59:31 +02:00
|
|
|
|
|
|
|
subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
|
2015-04-15 22:02:52 +02:00
|
|
|
subnetIds := make([]*string, subnetIdsSet.Len())
|
2014-09-12 17:59:31 +02:00
|
|
|
for i, subnetId := range subnetIdsSet.List() {
|
2015-04-15 22:02:52 +02:00
|
|
|
subnetIds[i] = aws.String(subnetId.(string))
|
2014-09-12 17:59:31 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
createOpts := rds.CreateDBSubnetGroupInput{
|
2015-02-26 23:32:34 +01:00
|
|
|
DBSubnetGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
DBSubnetGroupDescription: aws.String(d.Get("description").(string)),
|
2015-08-17 20:27:16 +02:00
|
|
|
SubnetIds: subnetIds,
|
2015-09-01 17:21:49 +02:00
|
|
|
Tags: tags,
|
2014-09-12 17:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts)
|
|
|
|
_, err := rdsconn.CreateDBSubnetGroup(&createOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating DB Subnet Group: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-26 23:32:34 +01:00
|
|
|
d.SetId(*createOpts.DBSubnetGroupName)
|
2014-09-12 17:59:31 +02:00
|
|
|
log.Printf("[INFO] DB Subnet Group ID: %s", d.Id())
|
|
|
|
return resourceAwsDbSubnetGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2014-09-12 17:59:31 +02:00
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
describeOpts := rds.DescribeDBSubnetGroupsInput{
|
2015-02-26 23:32:34 +01:00
|
|
|
DBSubnetGroupName: aws.String(d.Id()),
|
2014-09-12 17:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts)
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DBSubnetGroupNotFoundFault" {
|
2015-03-19 22:45:07 +01:00
|
|
|
// Update state to indicate the db subnet no longer exists.
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-12 17:59:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:19:20 +02:00
|
|
|
if len(describeResp.DBSubnetGroups) == 0 {
|
2015-02-26 23:32:34 +01:00
|
|
|
return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
|
2014-09-12 17:59:31 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
var subnetGroup *rds.DBSubnetGroup
|
2015-04-03 16:19:20 +02:00
|
|
|
for _, s := range describeResp.DBSubnetGroups {
|
|
|
|
// AWS is down casing the name provided, so we compare lower case versions
|
|
|
|
// of the names. We lower case both our name and their name in the check,
|
|
|
|
// incase they change that someday.
|
|
|
|
if strings.ToLower(d.Id()) == strings.ToLower(*s.DBSubnetGroupName) {
|
|
|
|
subnetGroup = describeResp.DBSubnetGroups[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if subnetGroup.DBSubnetGroupName == nil {
|
|
|
|
return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
|
|
|
|
}
|
2015-02-26 23:32:34 +01:00
|
|
|
|
2015-12-16 16:00:36 +01:00
|
|
|
d.Set("name", subnetGroup.DBSubnetGroupName)
|
|
|
|
d.Set("description", subnetGroup.DBSubnetGroupDescription)
|
2015-02-26 23:32:34 +01:00
|
|
|
|
|
|
|
subnets := make([]string, 0, len(subnetGroup.Subnets))
|
|
|
|
for _, s := range subnetGroup.Subnets {
|
|
|
|
subnets = append(subnets, *s.SubnetIdentifier)
|
|
|
|
}
|
|
|
|
d.Set("subnet_ids", subnets)
|
2014-09-12 17:59:31 +02:00
|
|
|
|
2015-09-01 17:21:49 +02:00
|
|
|
// list tags for resource
|
|
|
|
// set tags
|
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2015-09-02 09:24:34 +02:00
|
|
|
arn, err := buildRDSsubgrpARN(d, meta)
|
2015-09-01 17:21:49 +02:00
|
|
|
if err != nil {
|
2015-09-01 17:38:51 +02:00
|
|
|
log.Printf("[DEBUG] Error building ARN for DB Subnet Group, not setting Tags for group %s", *subnetGroup.DBSubnetGroupName)
|
2015-09-01 17:21:49 +02:00
|
|
|
} else {
|
2015-12-11 13:39:15 +01:00
|
|
|
d.Set("arn", arn)
|
2015-09-01 17:21:49 +02:00
|
|
|
resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
|
|
|
|
ResourceName: aws.String(arn),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dt []*rds.Tag
|
|
|
|
if len(resp.TagList) > 0 {
|
|
|
|
dt = resp.TagList
|
|
|
|
}
|
|
|
|
d.Set("tags", tagsToMapRDS(dt))
|
|
|
|
}
|
|
|
|
|
2014-09-12 17:59:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 23:52:30 +02:00
|
|
|
func resourceAwsDbSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2016-03-31 01:37:26 +02:00
|
|
|
if d.HasChange("subnet_ids") || d.HasChange("description") {
|
2015-08-24 23:52:30 +02:00
|
|
|
_, n := d.GetChange("subnet_ids")
|
|
|
|
if n == nil {
|
|
|
|
n = new(schema.Set)
|
|
|
|
}
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
var sIds []*string
|
|
|
|
for _, s := range ns.List() {
|
|
|
|
sIds = append(sIds, aws.String(s.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.ModifyDBSubnetGroup(&rds.ModifyDBSubnetGroupInput{
|
2016-03-31 00:36:42 +02:00
|
|
|
DBSubnetGroupName: aws.String(d.Id()),
|
|
|
|
DBSubnetGroupDescription: aws.String(d.Get("description").(string)),
|
|
|
|
SubnetIds: sIds,
|
2015-08-24 23:52:30 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-01 17:38:51 +02:00
|
|
|
|
2015-09-02 09:24:34 +02:00
|
|
|
if arn, err := buildRDSsubgrpARN(d, meta); err == nil {
|
2015-09-01 17:38:51 +02:00
|
|
|
if err := setTagsRDS(conn, d, arn); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 23:52:30 +02:00
|
|
|
return resourceAwsDbSubnetGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"destroyed"},
|
2014-11-21 17:58:34 +01:00
|
|
|
Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta),
|
|
|
|
Timeout: 3 * time.Minute,
|
|
|
|
MinTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbSubnetGroupDeleteRefreshFunc(
|
2014-09-12 17:59:31 +02:00
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) resource.StateRefreshFunc {
|
2014-11-21 17:58:34 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2014-09-12 17:59:31 +02:00
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
deleteOpts := rds.DeleteDBSubnetGroupInput{
|
2015-02-26 23:32:34 +01:00
|
|
|
DBSubnetGroupName: aws.String(d.Id()),
|
2014-09-12 17:59:31 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
rdserr, ok := err.(awserr.Error)
|
2014-09-12 17:59:31 +02:00
|
|
|
if !ok {
|
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
|
2015-05-20 13:21:23 +02:00
|
|
|
if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
|
2014-09-12 17:59:31 +02:00
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, "destroyed", nil
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 09:24:34 +02:00
|
|
|
|
|
|
|
func buildRDSsubgrpARN(d *schema.ResourceData, meta interface{}) (string, error) {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
region := meta.(*AWSClient).region
|
|
|
|
// An zero value GetUserInput{} defers to the currently logged in user
|
|
|
|
resp, err := iamconn.GetUser(&iam.GetUserInput{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
userARN := *resp.User.Arn
|
|
|
|
accountID := strings.Split(userARN, ":")[4]
|
|
|
|
arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountID, d.Id())
|
|
|
|
return arn, nil
|
|
|
|
}
|
2015-12-16 16:00:36 +01:00
|
|
|
|
|
|
|
func validateSubnetGroupName(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !regexp.MustCompile(`^[ .0-9a-z-_]+$`).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
2016-01-29 00:20:50 +01:00
|
|
|
"only lowercase alphanumeric characters, hyphens, underscores, periods, and spaces allowed in %q", k))
|
2015-12-16 16:00:36 +01:00
|
|
|
}
|
|
|
|
if len(value) > 255 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 255 characters", k))
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(`(?i)^default$`).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q is not allowed as %q", "Default", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|