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