github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_aws_db_subnet_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/awslabs/aws-sdk-go/aws"
    10  	"github.com/awslabs/aws-sdk-go/aws/awserr"
    11  	"github.com/awslabs/aws-sdk-go/service/rds"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsDbSubnetGroup() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsDbSubnetGroupCreate,
    19  		Read:   resourceAwsDbSubnetGroupRead,
    20  		Delete: resourceAwsDbSubnetGroupDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				ForceNew: true,
    26  				Required: true,
    27  			},
    28  
    29  			"description": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"subnet_ids": &schema.Schema{
    36  				Type:     schema.TypeSet,
    37  				Required: true,
    38  				ForceNew: true,
    39  				Elem:     &schema.Schema{Type: schema.TypeString},
    40  				Set:      schema.HashString,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    47  	rdsconn := meta.(*AWSClient).rdsconn
    48  
    49  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    50  	subnetIds := make([]*string, subnetIdsSet.Len())
    51  	for i, subnetId := range subnetIdsSet.List() {
    52  		subnetIds[i] = aws.String(subnetId.(string))
    53  	}
    54  
    55  	createOpts := rds.CreateDBSubnetGroupInput{
    56  		DBSubnetGroupName:        aws.String(d.Get("name").(string)),
    57  		DBSubnetGroupDescription: aws.String(d.Get("description").(string)),
    58  		SubnetIDs:                subnetIds,
    59  	}
    60  
    61  	log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts)
    62  	_, err := rdsconn.CreateDBSubnetGroup(&createOpts)
    63  	if err != nil {
    64  		return fmt.Errorf("Error creating DB Subnet Group: %s", err)
    65  	}
    66  
    67  	d.SetId(*createOpts.DBSubnetGroupName)
    68  	log.Printf("[INFO] DB Subnet Group ID: %s", d.Id())
    69  	return resourceAwsDbSubnetGroupRead(d, meta)
    70  }
    71  
    72  func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    73  	rdsconn := meta.(*AWSClient).rdsconn
    74  
    75  	describeOpts := rds.DescribeDBSubnetGroupsInput{
    76  		DBSubnetGroupName: aws.String(d.Id()),
    77  	}
    78  
    79  	describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts)
    80  	if err != nil {
    81  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DBSubnetGroupNotFoundFault" {
    82  			// Update state to indicate the db subnet no longer exists.
    83  			d.SetId("")
    84  			return nil
    85  		}
    86  		return err
    87  	}
    88  
    89  	if len(describeResp.DBSubnetGroups) == 0 {
    90  		return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
    91  	}
    92  
    93  	var subnetGroup *rds.DBSubnetGroup
    94  	for _, s := range describeResp.DBSubnetGroups {
    95  		// AWS is down casing the name provided, so we compare lower case versions
    96  		// of the names. We lower case both our name and their name in the check,
    97  		// incase they change that someday.
    98  		if strings.ToLower(d.Id()) == strings.ToLower(*s.DBSubnetGroupName) {
    99  			subnetGroup = describeResp.DBSubnetGroups[0]
   100  		}
   101  	}
   102  
   103  	if subnetGroup.DBSubnetGroupName == nil {
   104  		return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
   105  	}
   106  
   107  	d.Set("name", d.Id())
   108  	d.Set("description", *subnetGroup.DBSubnetGroupDescription)
   109  
   110  	subnets := make([]string, 0, len(subnetGroup.Subnets))
   111  	for _, s := range subnetGroup.Subnets {
   112  		subnets = append(subnets, *s.SubnetIdentifier)
   113  	}
   114  	d.Set("subnet_ids", subnets)
   115  
   116  	return nil
   117  }
   118  
   119  func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   120  	stateConf := &resource.StateChangeConf{
   121  		Pending:    []string{"pending"},
   122  		Target:     "destroyed",
   123  		Refresh:    resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta),
   124  		Timeout:    3 * time.Minute,
   125  		MinTimeout: 1 * time.Second,
   126  	}
   127  	_, err := stateConf.WaitForState()
   128  	return err
   129  }
   130  
   131  func resourceAwsDbSubnetGroupDeleteRefreshFunc(
   132  	d *schema.ResourceData,
   133  	meta interface{}) resource.StateRefreshFunc {
   134  	rdsconn := meta.(*AWSClient).rdsconn
   135  
   136  	return func() (interface{}, string, error) {
   137  
   138  		deleteOpts := rds.DeleteDBSubnetGroupInput{
   139  			DBSubnetGroupName: aws.String(d.Id()),
   140  		}
   141  
   142  		if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil {
   143  			rdserr, ok := err.(awserr.Error)
   144  			if !ok {
   145  				return d, "error", err
   146  			}
   147  
   148  			if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
   149  				return d, "error", err
   150  			}
   151  		}
   152  
   153  		return d, "destroyed", nil
   154  	}
   155  }