github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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/terraform/helper/hashcode" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/helper/schema" 11 "github.com/mitchellh/goamz/rds" 12 ) 13 14 func resourceAwsDbSubnetGroup() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsDbSubnetGroupCreate, 17 Read: resourceAwsDbSubnetGroupRead, 18 Delete: resourceAwsDbSubnetGroupDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 ForceNew: true, 24 Required: true, 25 }, 26 27 "description": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 33 "subnet_ids": &schema.Schema{ 34 Type: schema.TypeSet, 35 Required: true, 36 ForceNew: true, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 Set: func(v interface{}) int { 39 return hashcode.String(v.(string)) 40 }, 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] = subnetId.(string) 53 } 54 55 createOpts := rds.CreateDBSubnetGroup{ 56 DBSubnetGroupName: d.Get("name").(string), 57 DBSubnetGroupDescription: 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.DescribeDBSubnetGroups{ 76 DBSubnetGroupName: d.Id(), 77 } 78 79 describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts) 80 if err != nil { 81 return err 82 } 83 84 if len(describeResp.DBSubnetGroups) != 1 || 85 describeResp.DBSubnetGroups[0].Name != d.Id() { 86 } 87 88 d.Set("name", describeResp.DBSubnetGroups[0].Name) 89 d.Set("description", describeResp.DBSubnetGroups[0].Description) 90 d.Set("subnet_ids", describeResp.DBSubnetGroups[0].SubnetIds) 91 92 return nil 93 } 94 95 func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 96 stateConf := &resource.StateChangeConf{ 97 Pending: []string{"pending"}, 98 Target: "destroyed", 99 Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta), 100 Timeout: 3 * time.Minute, 101 MinTimeout: 1 * time.Second, 102 } 103 _, err := stateConf.WaitForState() 104 return err 105 } 106 107 func resourceAwsDbSubnetGroupDeleteRefreshFunc( 108 d *schema.ResourceData, 109 meta interface{}) resource.StateRefreshFunc { 110 rdsconn := meta.(*AWSClient).rdsconn 111 112 return func() (interface{}, string, error) { 113 114 deleteOpts := rds.DeleteDBSubnetGroup{ 115 DBSubnetGroupName: d.Id(), 116 } 117 118 if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil { 119 rdserr, ok := err.(*rds.Error) 120 if !ok { 121 return d, "error", err 122 } 123 124 if rdserr.Code != "DBSubnetGroupNotFoundFault" { 125 return d, "error", err 126 } 127 } 128 129 return d, "destroyed", nil 130 } 131 }