github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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 Update: nil, 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 p := meta.(*ResourceProvider) 49 rdsconn := p.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] = subnetId.(string) 55 } 56 57 createOpts := rds.CreateDBSubnetGroup{ 58 DBSubnetGroupName: d.Get("name").(string), 59 DBSubnetGroupDescription: 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 resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 75 stateConf := &resource.StateChangeConf{ 76 Pending: []string{"pending"}, 77 Target: "destroyed", 78 Refresh: resourceDbSubnetGroupDeleteRefreshFunc(d, meta), 79 Timeout: 3 * time.Minute, 80 MinTimeout: 1 * time.Second, 81 } 82 _, err := stateConf.WaitForState() 83 return err 84 } 85 86 func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { 87 p := meta.(*ResourceProvider) 88 rdsconn := p.rdsconn 89 90 describeOpts := rds.DescribeDBSubnetGroups{ 91 DBSubnetGroupName: d.Id(), 92 } 93 94 describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts) 95 if err != nil { 96 return err 97 } 98 99 if len(describeResp.DBSubnetGroups) != 1 || 100 describeResp.DBSubnetGroups[0].Name != d.Id() { 101 } 102 103 d.Set("name", describeResp.DBSubnetGroups[0].Name) 104 d.Set("description", describeResp.DBSubnetGroups[0].Description) 105 d.Set("subnet_ids", describeResp.DBSubnetGroups[0].SubnetIds) 106 107 return nil 108 } 109 110 func resourceDbSubnetGroupDeleteRefreshFunc( 111 d *schema.ResourceData, 112 meta interface{}) resource.StateRefreshFunc { 113 p := meta.(*ResourceProvider) 114 rdsconn := p.rdsconn 115 116 return func() (interface{}, string, error) { 117 118 deleteOpts := rds.DeleteDBSubnetGroup{ 119 DBSubnetGroupName: d.Id(), 120 } 121 122 if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil { 123 rdserr, ok := err.(*rds.Error) 124 if !ok { 125 return d, "error", err 126 } 127 128 if rdserr.Code != "DBSubnetGroupNotFoundFault" { 129 return d, "error", err 130 } 131 } 132 133 return d, "destroyed", nil 134 } 135 }