github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_db_subnet_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/iam"
    13  	"github.com/aws/aws-sdk-go/service/rds"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/helper/schema"
    16  )
    17  
    18  func resourceAwsDbSubnetGroup() *schema.Resource {
    19  	return &schema.Resource{
    20  		Create: resourceAwsDbSubnetGroupCreate,
    21  		Read:   resourceAwsDbSubnetGroupRead,
    22  		Update: resourceAwsDbSubnetGroupUpdate,
    23  		Delete: resourceAwsDbSubnetGroupDelete,
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				ForceNew: true,
    29  				Required: true,
    30  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    31  					value := v.(string)
    32  					if !regexp.MustCompile(`^[ .0-9A-Za-z-_]+$`).MatchString(value) {
    33  						errors = append(errors, fmt.Errorf(
    34  							"only alphanumeric characters, hyphens, underscores, periods, and spaces allowed in %q", k))
    35  					}
    36  					if len(value) > 255 {
    37  						errors = append(errors, fmt.Errorf(
    38  							"%q cannot be longer than 255 characters", k))
    39  					}
    40  					if regexp.MustCompile(`(?i)^default$`).MatchString(value) {
    41  						errors = append(errors, fmt.Errorf(
    42  							"%q is not allowed as %q", "Default", k))
    43  					}
    44  					return
    45  				},
    46  			},
    47  
    48  			"description": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Required: true,
    51  				ForceNew: true,
    52  			},
    53  
    54  			"subnet_ids": &schema.Schema{
    55  				Type:     schema.TypeSet,
    56  				Required: true,
    57  				Elem:     &schema.Schema{Type: schema.TypeString},
    58  				Set:      schema.HashString,
    59  			},
    60  
    61  			"tags": tagsSchema(),
    62  		},
    63  	}
    64  }
    65  
    66  func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    67  	rdsconn := meta.(*AWSClient).rdsconn
    68  	tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
    69  
    70  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    71  	subnetIds := make([]*string, subnetIdsSet.Len())
    72  	for i, subnetId := range subnetIdsSet.List() {
    73  		subnetIds[i] = aws.String(subnetId.(string))
    74  	}
    75  
    76  	createOpts := rds.CreateDBSubnetGroupInput{
    77  		DBSubnetGroupName:        aws.String(d.Get("name").(string)),
    78  		DBSubnetGroupDescription: aws.String(d.Get("description").(string)),
    79  		SubnetIds:                subnetIds,
    80  		Tags:                     tags,
    81  	}
    82  
    83  	log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts)
    84  	_, err := rdsconn.CreateDBSubnetGroup(&createOpts)
    85  	if err != nil {
    86  		return fmt.Errorf("Error creating DB Subnet Group: %s", err)
    87  	}
    88  
    89  	d.SetId(*createOpts.DBSubnetGroupName)
    90  	log.Printf("[INFO] DB Subnet Group ID: %s", d.Id())
    91  	return resourceAwsDbSubnetGroupRead(d, meta)
    92  }
    93  
    94  func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    95  	rdsconn := meta.(*AWSClient).rdsconn
    96  
    97  	describeOpts := rds.DescribeDBSubnetGroupsInput{
    98  		DBSubnetGroupName: aws.String(d.Id()),
    99  	}
   100  
   101  	describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts)
   102  	if err != nil {
   103  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DBSubnetGroupNotFoundFault" {
   104  			// Update state to indicate the db subnet no longer exists.
   105  			d.SetId("")
   106  			return nil
   107  		}
   108  		return err
   109  	}
   110  
   111  	if len(describeResp.DBSubnetGroups) == 0 {
   112  		return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
   113  	}
   114  
   115  	var subnetGroup *rds.DBSubnetGroup
   116  	for _, s := range describeResp.DBSubnetGroups {
   117  		// AWS is down casing the name provided, so we compare lower case versions
   118  		// of the names. We lower case both our name and their name in the check,
   119  		// incase they change that someday.
   120  		if strings.ToLower(d.Id()) == strings.ToLower(*s.DBSubnetGroupName) {
   121  			subnetGroup = describeResp.DBSubnetGroups[0]
   122  		}
   123  	}
   124  
   125  	if subnetGroup.DBSubnetGroupName == nil {
   126  		return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
   127  	}
   128  
   129  	d.Set("name", d.Id())
   130  	d.Set("description", *subnetGroup.DBSubnetGroupDescription)
   131  
   132  	subnets := make([]string, 0, len(subnetGroup.Subnets))
   133  	for _, s := range subnetGroup.Subnets {
   134  		subnets = append(subnets, *s.SubnetIdentifier)
   135  	}
   136  	d.Set("subnet_ids", subnets)
   137  
   138  	// list tags for resource
   139  	// set tags
   140  	conn := meta.(*AWSClient).rdsconn
   141  	arn, err := buildRDSsubgrpARN(d, meta)
   142  	if err != nil {
   143  		log.Printf("[DEBUG] Error building ARN for DB Subnet Group, not setting Tags for group %s", *subnetGroup.DBSubnetGroupName)
   144  	} else {
   145  		resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
   146  			ResourceName: aws.String(arn),
   147  		})
   148  
   149  		if err != nil {
   150  			log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn)
   151  		}
   152  
   153  		var dt []*rds.Tag
   154  		if len(resp.TagList) > 0 {
   155  			dt = resp.TagList
   156  		}
   157  		d.Set("tags", tagsToMapRDS(dt))
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func resourceAwsDbSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   164  	conn := meta.(*AWSClient).rdsconn
   165  	if d.HasChange("subnet_ids") {
   166  		_, n := d.GetChange("subnet_ids")
   167  		if n == nil {
   168  			n = new(schema.Set)
   169  		}
   170  		ns := n.(*schema.Set)
   171  
   172  		var sIds []*string
   173  		for _, s := range ns.List() {
   174  			sIds = append(sIds, aws.String(s.(string)))
   175  		}
   176  
   177  		_, err := conn.ModifyDBSubnetGroup(&rds.ModifyDBSubnetGroupInput{
   178  			DBSubnetGroupName: aws.String(d.Id()),
   179  			SubnetIds:         sIds,
   180  		})
   181  
   182  		if err != nil {
   183  			return err
   184  		}
   185  	}
   186  
   187  	if arn, err := buildRDSsubgrpARN(d, meta); err == nil {
   188  		if err := setTagsRDS(conn, d, arn); err != nil {
   189  			return err
   190  		} else {
   191  			d.SetPartial("tags")
   192  		}
   193  	}
   194  
   195  	return resourceAwsDbSubnetGroupRead(d, meta)
   196  }
   197  
   198  func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   199  	stateConf := &resource.StateChangeConf{
   200  		Pending:    []string{"pending"},
   201  		Target:     "destroyed",
   202  		Refresh:    resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta),
   203  		Timeout:    3 * time.Minute,
   204  		MinTimeout: 1 * time.Second,
   205  	}
   206  	_, err := stateConf.WaitForState()
   207  	return err
   208  }
   209  
   210  func resourceAwsDbSubnetGroupDeleteRefreshFunc(
   211  	d *schema.ResourceData,
   212  	meta interface{}) resource.StateRefreshFunc {
   213  	rdsconn := meta.(*AWSClient).rdsconn
   214  
   215  	return func() (interface{}, string, error) {
   216  
   217  		deleteOpts := rds.DeleteDBSubnetGroupInput{
   218  			DBSubnetGroupName: aws.String(d.Id()),
   219  		}
   220  
   221  		if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil {
   222  			rdserr, ok := err.(awserr.Error)
   223  			if !ok {
   224  				return d, "error", err
   225  			}
   226  
   227  			if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
   228  				return d, "error", err
   229  			}
   230  		}
   231  
   232  		return d, "destroyed", nil
   233  	}
   234  }
   235  
   236  func buildRDSsubgrpARN(d *schema.ResourceData, meta interface{}) (string, error) {
   237  	iamconn := meta.(*AWSClient).iamconn
   238  	region := meta.(*AWSClient).region
   239  	// An zero value GetUserInput{} defers to the currently logged in user
   240  	resp, err := iamconn.GetUser(&iam.GetUserInput{})
   241  	if err != nil {
   242  		return "", err
   243  	}
   244  	userARN := *resp.User.Arn
   245  	accountID := strings.Split(userARN, ":")[4]
   246  	arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountID, d.Id())
   247  	return arn, nil
   248  }