forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_vpc_dhcp_options.go
281 lines (236 loc) · 6.59 KB
/
resource_aws_vpc_dhcp_options.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsVpcDhcpOptions() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVpcDhcpOptionsCreate,
Read: resourceAwsVpcDhcpOptionsRead,
Update: resourceAwsVpcDhcpOptionsUpdate,
Delete: resourceAwsVpcDhcpOptionsDelete,
Schema: map[string]*schema.Schema{
"domain_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"domain_name_servers": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ntp_servers": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"netbios_node_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"netbios_name_servers": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceAwsVpcDhcpOptionsCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
setDHCPOption := func(key string) *ec2.NewDhcpConfiguration {
log.Printf("[DEBUG] Setting DHCP option %s...", key)
tfKey := strings.Replace(key, "-", "_", -1)
value, ok := d.GetOk(tfKey)
if !ok {
return nil
}
if v, ok := value.(string); ok {
return &ec2.NewDhcpConfiguration{
Key: aws.String(key),
Values: []*string{
aws.String(v),
},
}
}
if v, ok := value.([]interface{}); ok {
var s []*string
for _, attr := range v {
s = append(s, aws.String(attr.(string)))
}
return &ec2.NewDhcpConfiguration{
Key: aws.String(key),
Values: s,
}
}
return nil
}
createOpts := &ec2.CreateDhcpOptionsInput{
DhcpConfigurations: []*ec2.NewDhcpConfiguration{
setDHCPOption("domain-name"),
setDHCPOption("domain-name-servers"),
setDHCPOption("ntp-servers"),
setDHCPOption("netbios-node-type"),
setDHCPOption("netbios-name-servers"),
},
}
resp, err := conn.CreateDhcpOptions(createOpts)
if err != nil {
return fmt.Errorf("Error creating DHCP Options Set: %s", err)
}
dos := resp.DhcpOptions
d.SetId(*dos.DhcpOptionsId)
log.Printf("[INFO] DHCP Options Set ID: %s", d.Id())
// Wait for the DHCP Options to become available
log.Printf("[DEBUG] Waiting for DHCP Options (%s) to become available", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "",
Refresh: DHCPOptionsStateRefreshFunc(conn, d.Id()),
Timeout: 1 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for DHCP Options (%s) to become available: %s",
d.Id(), err)
}
return resourceAwsVpcDhcpOptionsUpdate(d, meta)
}
func resourceAwsVpcDhcpOptionsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.DescribeDhcpOptionsInput{
DhcpOptionsIds: []*string{
aws.String(d.Id()),
},
}
resp, err := conn.DescribeDhcpOptions(req)
if err != nil {
return fmt.Errorf("Error retrieving DHCP Options: %s", err)
}
if len(resp.DhcpOptions) == 0 {
return nil
}
opts := resp.DhcpOptions[0]
d.Set("tags", tagsToMap(opts.Tags))
for _, cfg := range opts.DhcpConfigurations {
tfKey := strings.Replace(*cfg.Key, "-", "_", -1)
if _, ok := d.Get(tfKey).(string); ok {
d.Set(tfKey, cfg.Values[0].Value)
} else {
values := make([]string, 0, len(cfg.Values))
for _, v := range cfg.Values {
values = append(values, *v.Value)
}
d.Set(tfKey, values)
}
}
return nil
}
func resourceAwsVpcDhcpOptionsUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
return setTags(conn, d)
}
func resourceAwsVpcDhcpOptionsDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
return resource.Retry(3*time.Minute, func() error {
log.Printf("[INFO] Deleting DHCP Options ID %s...", d.Id())
_, err := conn.DeleteDhcpOptions(&ec2.DeleteDhcpOptionsInput{
DhcpOptionsId: aws.String(d.Id()),
})
if err == nil {
return nil
}
log.Printf("[WARN] %s", err)
ec2err, ok := err.(awserr.Error)
if !ok {
return err
}
switch ec2err.Code() {
case "InvalidDhcpOptionsID.NotFound":
return nil
case "DependencyViolation":
// If it is a dependency violation, we want to disassociate
// all VPCs using the given DHCP Options ID, and retry deleting.
vpcs, err2 := findVPCsByDHCPOptionsID(conn, d.Id())
if err2 != nil {
log.Printf("[ERROR] %s", err2)
return err2
}
for _, vpc := range vpcs {
log.Printf("[INFO] Disassociating DHCP Options Set %s from VPC %s...", d.Id(), *vpc.VpcId)
if _, err := conn.AssociateDhcpOptions(&ec2.AssociateDhcpOptionsInput{
DhcpOptionsId: aws.String("default"),
VpcId: vpc.VpcId,
}); err != nil {
return err
}
}
return err //retry
default:
// Any other error, we want to quit the retry loop immediately
return resource.RetryError{Err: err}
}
return nil
})
}
func findVPCsByDHCPOptionsID(conn *ec2.EC2, id string) ([]*ec2.Vpc, error) {
req := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("dhcp-options-id"),
Values: []*string{
aws.String(id),
},
},
},
}
resp, err := conn.DescribeVpcs(req)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpcID.NotFound" {
return nil, nil
}
return nil, err
}
return resp.Vpcs, nil
}
func DHCPOptionsStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
DescribeDhcpOpts := &ec2.DescribeDhcpOptionsInput{
DhcpOptionsIds: []*string{
aws.String(id),
},
}
resp, err := conn.DescribeDhcpOptions(DescribeDhcpOpts)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidDhcpOptionsID.NotFound" {
resp = nil
} else {
log.Printf("Error on DHCPOptionsStateRefresh: %s", err)
return nil, "", err
}
}
if resp == nil {
// Sometimes AWS just has consistency issues and doesn't see
// our instance yet. Return an empty state.
return nil, "", nil
}
dos := resp.DhcpOptions[0]
return dos, "", nil
}
}