forked from hashicorp/terraform
/
resource_aws_db_subnet_group.go
131 lines (107 loc) · 3.24 KB
/
resource_aws_db_subnet_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package aws
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/rds"
)
func resourceAwsDbSubnetGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDbSubnetGroupCreate,
Read: resourceAwsDbSubnetGroupRead,
Delete: resourceAwsDbSubnetGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_ids": &schema.Schema{
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
},
}
}
func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
subnetIds := make([]string, subnetIdsSet.Len())
for i, subnetId := range subnetIdsSet.List() {
subnetIds[i] = subnetId.(string)
}
createOpts := rds.CreateDBSubnetGroup{
DBSubnetGroupName: d.Get("name").(string),
DBSubnetGroupDescription: d.Get("description").(string),
SubnetIds: subnetIds,
}
log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts)
_, err := rdsconn.CreateDBSubnetGroup(&createOpts)
if err != nil {
return fmt.Errorf("Error creating DB Subnet Group: %s", err)
}
d.SetId(createOpts.DBSubnetGroupName)
log.Printf("[INFO] DB Subnet Group ID: %s", d.Id())
return resourceAwsDbSubnetGroupRead(d, meta)
}
func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
describeOpts := rds.DescribeDBSubnetGroups{
DBSubnetGroupName: d.Id(),
}
describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts)
if err != nil {
return err
}
if len(describeResp.DBSubnetGroups) != 1 ||
describeResp.DBSubnetGroups[0].Name != d.Id() {
}
d.Set("name", describeResp.DBSubnetGroups[0].Name)
d.Set("description", describeResp.DBSubnetGroups[0].Description)
d.Set("subnet_ids", describeResp.DBSubnetGroups[0].SubnetIds)
return nil
}
func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "destroyed",
Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta),
Timeout: 3 * time.Minute,
MinTimeout: 1 * time.Second,
}
_, err := stateConf.WaitForState()
return err
}
func resourceAwsDbSubnetGroupDeleteRefreshFunc(
d *schema.ResourceData,
meta interface{}) resource.StateRefreshFunc {
rdsconn := meta.(*AWSClient).rdsconn
return func() (interface{}, string, error) {
deleteOpts := rds.DeleteDBSubnetGroup{
DBSubnetGroupName: d.Id(),
}
if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil {
rdserr, ok := err.(*rds.Error)
if !ok {
return d, "error", err
}
if rdserr.Code != "DBSubnetGroupNotFoundFault" {
return d, "error", err
}
}
return d, "destroyed", nil
}
}