github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/builtin/providers/aws/resource_aws_db_subnet_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/aws-sdk-go/aws"
     9  	"github.com/hashicorp/aws-sdk-go/gen/rds"
    10  	"github.com/hashicorp/terraform/helper/hashcode"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsDbSubnetGroup() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsDbSubnetGroupCreate,
    18  		Read:   resourceAwsDbSubnetGroupRead,
    19  		Delete: resourceAwsDbSubnetGroupDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				ForceNew: true,
    25  				Required: true,
    26  			},
    27  
    28  			"description": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"subnet_ids": &schema.Schema{
    35  				Type:     schema.TypeSet,
    36  				Required: true,
    37  				ForceNew: true,
    38  				Elem:     &schema.Schema{Type: schema.TypeString},
    39  				Set: func(v interface{}) int {
    40  					return hashcode.String(v.(string))
    41  				},
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    48  	rdsconn := meta.(*AWSClient).rdsconn
    49  
    50  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    51  	subnetIds := make([]string, subnetIdsSet.Len())
    52  	for i, subnetId := range subnetIdsSet.List() {
    53  		subnetIds[i] = subnetId.(string)
    54  	}
    55  
    56  	createOpts := rds.CreateDBSubnetGroupMessage{
    57  		DBSubnetGroupName:        aws.String(d.Get("name").(string)),
    58  		DBSubnetGroupDescription: aws.String(d.Get("description").(string)),
    59  		SubnetIDs:                subnetIds,
    60  	}
    61  
    62  	log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts)
    63  	_, err := rdsconn.CreateDBSubnetGroup(&createOpts)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating DB Subnet Group: %s", err)
    66  	}
    67  
    68  	d.SetId(*createOpts.DBSubnetGroupName)
    69  	log.Printf("[INFO] DB Subnet Group ID: %s", d.Id())
    70  	return resourceAwsDbSubnetGroupRead(d, meta)
    71  }
    72  
    73  func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    74  	rdsconn := meta.(*AWSClient).rdsconn
    75  
    76  	describeOpts := rds.DescribeDBSubnetGroupsMessage{
    77  		DBSubnetGroupName: aws.String(d.Id()),
    78  	}
    79  
    80  	describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	if len(describeResp.DBSubnetGroups) != 1 ||
    86  		*describeResp.DBSubnetGroups[0].DBSubnetGroupName != d.Id() {
    87  		return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups)
    88  	}
    89  
    90  	subnetGroup := describeResp.DBSubnetGroups[0]
    91  
    92  	d.Set("name", *subnetGroup.DBSubnetGroupName)
    93  	d.Set("description", *subnetGroup.DBSubnetGroupDescription)
    94  
    95  	subnets := make([]string, 0, len(subnetGroup.Subnets))
    96  	for _, s := range subnetGroup.Subnets {
    97  		subnets = append(subnets, *s.SubnetIdentifier)
    98  	}
    99  	d.Set("subnet_ids", subnets)
   100  
   101  	return nil
   102  }
   103  
   104  func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   105  	stateConf := &resource.StateChangeConf{
   106  		Pending:    []string{"pending"},
   107  		Target:     "destroyed",
   108  		Refresh:    resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta),
   109  		Timeout:    3 * time.Minute,
   110  		MinTimeout: 1 * time.Second,
   111  	}
   112  	_, err := stateConf.WaitForState()
   113  	return err
   114  }
   115  
   116  func resourceAwsDbSubnetGroupDeleteRefreshFunc(
   117  	d *schema.ResourceData,
   118  	meta interface{}) resource.StateRefreshFunc {
   119  	rdsconn := meta.(*AWSClient).rdsconn
   120  
   121  	return func() (interface{}, string, error) {
   122  
   123  		deleteOpts := rds.DeleteDBSubnetGroupMessage{
   124  			DBSubnetGroupName: aws.String(d.Id()),
   125  		}
   126  
   127  		if err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil {
   128  			rdserr, ok := err.(aws.APIError)
   129  			if !ok {
   130  				return d, "error", err
   131  			}
   132  
   133  			if rdserr.Code != "DBSubnetGroupNotFoundFault" {
   134  				return d, "error", err
   135  			}
   136  		}
   137  
   138  		return d, "destroyed", nil
   139  	}
   140  }