github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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/service/rds" 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: schema.HashString, 40 }, 41 }, 42 } 43 } 44 45 func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error { 46 rdsconn := meta.(*AWSClient).rdsconn 47 48 subnetIdsSet := d.Get("subnet_ids").(*schema.Set) 49 subnetIds := make([]*string, subnetIdsSet.Len()) 50 for i, subnetId := range subnetIdsSet.List() { 51 subnetIds[i] = aws.String(subnetId.(string)) 52 } 53 54 createOpts := rds.CreateDBSubnetGroupInput{ 55 DBSubnetGroupName: aws.String(d.Get("name").(string)), 56 DBSubnetGroupDescription: aws.String(d.Get("description").(string)), 57 SubnetIDs: subnetIds, 58 } 59 60 log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts) 61 _, err := rdsconn.CreateDBSubnetGroup(&createOpts) 62 if err != nil { 63 return fmt.Errorf("Error creating DB Subnet Group: %s", err) 64 } 65 66 d.SetId(*createOpts.DBSubnetGroupName) 67 log.Printf("[INFO] DB Subnet Group ID: %s", d.Id()) 68 return resourceAwsDbSubnetGroupRead(d, meta) 69 } 70 71 func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { 72 rdsconn := meta.(*AWSClient).rdsconn 73 74 describeOpts := rds.DescribeDBSubnetGroupsInput{ 75 DBSubnetGroupName: aws.String(d.Id()), 76 } 77 78 describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts) 79 if err != nil { 80 if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "DBSubnetGroupNotFoundFault" { 81 // Update state to indicate the db subnet no longer exists. 82 d.SetId("") 83 return nil 84 } 85 return err 86 } 87 88 if len(describeResp.DBSubnetGroups) == 0 { 89 return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups) 90 } 91 92 var subnetGroup *rds.DBSubnetGroup 93 for _, s := range describeResp.DBSubnetGroups { 94 // AWS is down casing the name provided, so we compare lower case versions 95 // of the names. We lower case both our name and their name in the check, 96 // incase they change that someday. 97 if strings.ToLower(d.Id()) == strings.ToLower(*s.DBSubnetGroupName) { 98 subnetGroup = describeResp.DBSubnetGroups[0] 99 } 100 } 101 102 if subnetGroup.DBSubnetGroupName == nil { 103 return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups) 104 } 105 106 d.Set("name", d.Id()) 107 d.Set("description", *subnetGroup.DBSubnetGroupDescription) 108 109 subnets := make([]string, 0, len(subnetGroup.Subnets)) 110 for _, s := range subnetGroup.Subnets { 111 subnets = append(subnets, *s.SubnetIdentifier) 112 } 113 d.Set("subnet_ids", subnets) 114 115 return nil 116 } 117 118 func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 119 stateConf := &resource.StateChangeConf{ 120 Pending: []string{"pending"}, 121 Target: "destroyed", 122 Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta), 123 Timeout: 3 * time.Minute, 124 MinTimeout: 1 * time.Second, 125 } 126 _, err := stateConf.WaitForState() 127 return err 128 } 129 130 func resourceAwsDbSubnetGroupDeleteRefreshFunc( 131 d *schema.ResourceData, 132 meta interface{}) resource.StateRefreshFunc { 133 rdsconn := meta.(*AWSClient).rdsconn 134 135 return func() (interface{}, string, error) { 136 137 deleteOpts := rds.DeleteDBSubnetGroupInput{ 138 DBSubnetGroupName: aws.String(d.Id()), 139 } 140 141 if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil { 142 rdserr, ok := err.(aws.APIError) 143 if !ok { 144 return d, "error", err 145 } 146 147 if rdserr.Code != "DBSubnetGroupNotFoundFault" { 148 return d, "error", err 149 } 150 } 151 152 return d, "destroyed", nil 153 } 154 }