github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/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 if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "DBSubnetGroupNotFoundFault" { 83 // Update state to indicate the db subnet no longer exists. 84 d.SetId("") 85 return nil 86 } 87 return err 88 } 89 90 if len(describeResp.DBSubnetGroups) != 1 || 91 *describeResp.DBSubnetGroups[0].DBSubnetGroupName != d.Id() { 92 return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups) 93 } 94 95 subnetGroup := describeResp.DBSubnetGroups[0] 96 97 d.Set("name", *subnetGroup.DBSubnetGroupName) 98 d.Set("description", *subnetGroup.DBSubnetGroupDescription) 99 100 subnets := make([]string, 0, len(subnetGroup.Subnets)) 101 for _, s := range subnetGroup.Subnets { 102 subnets = append(subnets, *s.SubnetIdentifier) 103 } 104 d.Set("subnet_ids", subnets) 105 106 return nil 107 } 108 109 func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 110 stateConf := &resource.StateChangeConf{ 111 Pending: []string{"pending"}, 112 Target: "destroyed", 113 Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta), 114 Timeout: 3 * time.Minute, 115 MinTimeout: 1 * time.Second, 116 } 117 _, err := stateConf.WaitForState() 118 return err 119 } 120 121 func resourceAwsDbSubnetGroupDeleteRefreshFunc( 122 d *schema.ResourceData, 123 meta interface{}) resource.StateRefreshFunc { 124 rdsconn := meta.(*AWSClient).rdsconn 125 126 return func() (interface{}, string, error) { 127 128 deleteOpts := rds.DeleteDBSubnetGroupMessage{ 129 DBSubnetGroupName: aws.String(d.Id()), 130 } 131 132 if err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil { 133 rdserr, ok := err.(aws.APIError) 134 if !ok { 135 return d, "error", err 136 } 137 138 if rdserr.Code != "DBSubnetGroupNotFoundFault" { 139 return d, "error", err 140 } 141 } 142 143 return d, "destroyed", nil 144 } 145 }