forked from hashicorp/terraform-provider-google
/
resource_compute_instance_group.go
499 lines (425 loc) · 14.7 KB
/
resource_compute_instance_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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package google
import (
"fmt"
"log"
"reflect"
"sort"
"strings"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceComputeInstanceGroup() *schema.Resource {
return &schema.Resource{
Create: resourceComputeInstanceGroupCreate,
Read: resourceComputeInstanceGroupRead,
Update: resourceComputeInstanceGroupUpdate,
Delete: resourceComputeInstanceGroupDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeInstanceGroupImportState,
},
SchemaVersion: 2,
MigrateState: resourceComputeInstanceGroupMigrateState,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"instances": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"named_port": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"port": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"network": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
},
CustomizeDiff: customDiffInstanceGroupInstancesField,
}
}
func customDiffInstanceGroupInstancesField(diff *schema.ResourceDiff, meta interface{}) error {
// This deals with an interesting problem that deserves some attention.
// When an instance is destroyed and recreated, its membership in
// instance groups disappears. However, its recreated `self_link` will
// be the same as the `self_link` from before the destroy/recreate.
// Therefore, if some instances which are set in the `instances` field
// are destroyed and recreated, although in *reality* there is a diff
// between the GCP state and the desired state, Terraform cannot *see*
// the diff without running a full Read cycle. There's no Read implicit
// in the `Apply` stage, so we need to trick Terraform into calling
// Update when things like this happen.
// This function will be called in 3 different states.
// 1) it will be called on a new resource which hasn't been created yet.
// We shouldn't do anything interesting in that case.
// 2) it will be called on an updated resource during "plan" time - that is,
// before anything has actually been done. In that case, we need to show
// a diff on the resource if there's a chance that any of the instances
// will be destroyed and recreated. Fortunately, in this case, the
// upstream logic will show that there is a diff. This will be a
// "Computed" diff - as if we had called diff.SetComputed("instances"),
// and that's a good response.
// 3) it will be called on an updated resource at "apply" time - that is,
// right when we're about to do something with this resource. That
// is designed to check whether there really is a diff on the Computed
// field "instances". Here, we have to get tricky. We need to show
// a diff, and it can't be a Computed diff (`apply` skips `Update` if
// the only diffs are Computed). It also can't be a ForceNew diff,
// because Terraform crashes if there's a ForceNew diff at apply time
// after not seeing one at plan time. We're in a pickle - the Terraform
// state matches our desired state, but is *wrong*. We add a fake item
// to the "instances" set, so that Terraform sees a diff between the
// state and the desired state.
oldI, newI := diff.GetChange("instances")
oldInstanceSet := oldI.(*schema.Set)
newInstanceSet := newI.(*schema.Set)
oldInstances := convertStringArr(oldInstanceSet.List())
newInstances := convertStringArr(newInstanceSet.List())
log.Printf("[DEBUG] InstanceGroup CustomizeDiff old: %#v, new: %#v", oldInstances, newInstances)
var memberUrls []string
config := meta.(*Config)
// We can't use getProject() or getZone(), because we only have a schema.ResourceDiff,
// not a schema.ResourceData. We'll have to emulate them like this.
project := diff.Get("project").(string)
if project == "" {
project = config.Project
}
zone := diff.Get("zone").(string)
if zone == "" {
project = config.Zone
}
// We need to see what instances are present in the instance group. There are a few
// possible results.
// 1) The instance group doesn't exist. We don't change the diff in this case -
// if the instance is being created, that's the right thing to do.
// 2) The instance group exists, and the GCP state matches the terraform state. In this
// case, we should do nothing.
// 3) The instance group exists, and the GCP state does not match the terraform state.
// In this case, we add the string "FORCE_UPDATE" to the list of instances, to convince
// Terraform to execute an update even though there's no diff between the terraform
// state and the desired state.
members, err := config.clientCompute.InstanceGroups.ListInstances(
project, zone, diff.Get("name").(string), &compute.InstanceGroupsListInstancesRequest{
InstanceState: "ALL",
}).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// This is where we'll end up at either plan time or apply time on first creation.
return nil
} else {
// Any other errors return them
return fmt.Errorf("Error reading InstanceGroup Members: %s", err)
}
}
for _, member := range members.Items {
memberUrls = append(memberUrls, member.Instance)
}
sort.Strings(memberUrls)
sort.Strings(oldInstances)
sort.Strings(newInstances)
log.Printf("[DEBUG] InstanceGroup members: %#v. OldInstances: %#v", memberUrls, oldInstances)
if !reflect.DeepEqual(memberUrls, oldInstances) && reflect.DeepEqual(newInstances, oldInstances) {
// This is where we'll end up at apply-time only if an instance is
// somehow removed from the set of instances between refresh and update.
newInstancesList := append(newInstances, "FORCE_UPDATE")
diff.SetNew("instances", newInstancesList)
}
// This is where we'll end up if the GCP state matches the Terraform state.
return nil
}
func getInstanceReferences(instanceUrls []string) (refs []*compute.InstanceReference) {
for _, v := range instanceUrls {
refs = append(refs, &compute.InstanceReference{
Instance: v,
})
}
return refs
}
func validInstanceURLs(instanceUrls []string) bool {
for _, v := range instanceUrls {
if !strings.HasPrefix(v, "https://www.googleapis.com/compute/v1/") {
return false
}
}
return true
}
func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
// Build the parameter
instanceGroup := &compute.InstanceGroup{
Name: name,
}
// Set optional fields
if v, ok := d.GetOk("description"); ok {
instanceGroup.Description = v.(string)
}
if v, ok := d.GetOk("named_port"); ok {
instanceGroup.NamedPorts = getNamedPorts(v.([]interface{}))
}
if v, ok := d.GetOk("network"); ok {
instanceGroup.Network = v.(string)
}
log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup)
op, err := config.clientCompute.InstanceGroups.Insert(
project, zone, instanceGroup).Do()
if err != nil {
return fmt.Errorf("Error creating InstanceGroup: %s", err)
}
// It probably maybe worked, so store the ID now
d.SetId(fmt.Sprintf("%s/%s", zone, name))
// Wait for the operation to complete
err = computeOperationWait(config.clientCompute, op, project, "Creating InstanceGroup")
if err != nil {
d.SetId("")
return err
}
if v, ok := d.GetOk("instances"); ok {
instanceUrls := convertStringArr(v.(*schema.Set).List())
if !validInstanceURLs(instanceUrls) {
return fmt.Errorf("Error invalid instance URLs: %v", instanceUrls)
}
addInstanceReq := &compute.InstanceGroupsAddInstancesRequest{
Instances: getInstanceReferences(instanceUrls),
}
log.Printf("[DEBUG] InstanceGroup add instances request: %#v", addInstanceReq)
op, err := config.clientCompute.InstanceGroups.AddInstances(
project, zone, name, addInstanceReq).Do()
if err != nil {
return fmt.Errorf("Error adding instances to InstanceGroup: %s", err)
}
// Wait for the operation to complete
err = computeOperationWait(config.clientCompute, op, project, "Adding instances to InstanceGroup")
if err != nil {
return err
}
}
return resourceComputeInstanceGroupRead(d, meta)
}
func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
// retrieve instance group
instanceGroup, err := config.clientCompute.InstanceGroups.Get(
project, zone, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Instance Group %q", name))
}
// retrieve instance group members
var memberUrls []string
members, err := config.clientCompute.InstanceGroups.ListInstances(
project, zone, name, &compute.InstanceGroupsListInstancesRequest{
InstanceState: "ALL",
}).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't have any instances
d.Set("instances", nil)
} else {
// any other errors return them
return fmt.Errorf("Error reading InstanceGroup Members: %s", err)
}
} else {
for _, member := range members.Items {
memberUrls = append(memberUrls, member.Instance)
}
log.Printf("[DEBUG] InstanceGroup members: %v", memberUrls)
d.Set("instances", memberUrls)
}
d.Set("named_port", flattenNamedPorts(instanceGroup.NamedPorts))
d.Set("description", instanceGroup.Description)
// Set computed fields
d.Set("network", instanceGroup.Network)
d.Set("size", instanceGroup.Size)
d.Set("project", project)
d.Set("zone", zone)
d.Set("self_link", instanceGroup.SelfLink)
return nil
}
func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
err := resourceComputeInstanceGroupRead(d, meta)
if err != nil {
return err
}
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
d.Partial(true)
if d.HasChange("instances") {
// to-do check for no instances
_, to_ := d.GetChange("instances")
// We need to get the current state from d directly because
// it is likely to have been changed by the Read() above.
from_ := d.Get("instances")
to_.(*schema.Set).Remove("FORCE_UPDATE")
from := convertStringArr(from_.(*schema.Set).List())
to := convertStringArr(to_.(*schema.Set).List())
if !validInstanceURLs(from) {
return fmt.Errorf("Error invalid instance URLs: %v", from)
}
if !validInstanceURLs(to) {
return fmt.Errorf("Error invalid instance URLs: %v", to)
}
add, remove := calcAddRemove(from, to)
if len(remove) > 0 {
removeReq := &compute.InstanceGroupsRemoveInstancesRequest{
Instances: getInstanceReferences(remove),
}
log.Printf("[DEBUG] InstanceGroup remove instances request: %#v", removeReq)
removeOp, err := config.clientCompute.InstanceGroups.RemoveInstances(
project, zone, name, removeReq).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Instances already removed from InstanceGroup: %s", remove)
} else {
return fmt.Errorf("Error removing instances from InstanceGroup: %s", err)
}
} else {
// Wait for the operation to complete
err = computeOperationWait(config.clientCompute, removeOp, project, "Updating InstanceGroup")
if err != nil {
return err
}
}
}
if len(add) > 0 {
addReq := &compute.InstanceGroupsAddInstancesRequest{
Instances: getInstanceReferences(add),
}
log.Printf("[DEBUG] InstanceGroup adding instances request: %#v", addReq)
addOp, err := config.clientCompute.InstanceGroups.AddInstances(
project, zone, name, addReq).Do()
if err != nil {
return fmt.Errorf("Error adding instances from InstanceGroup: %s", err)
}
// Wait for the operation to complete
err = computeOperationWait(config.clientCompute, addOp, project, "Updating InstanceGroup")
if err != nil {
return err
}
}
d.SetPartial("instances")
}
if d.HasChange("named_port") {
// Important to fetch via GetChange, because the above Read() will
// have reset the value retrieved via Get() to its current value.
_, namedPorts_ := d.GetChange("named_port")
namedPorts := getNamedPorts(namedPorts_.([]interface{}))
namedPortsReq := &compute.InstanceGroupsSetNamedPortsRequest{
NamedPorts: namedPorts,
}
log.Printf("[DEBUG] InstanceGroup updating named ports request: %#v", namedPortsReq)
op, err := config.clientCompute.InstanceGroups.SetNamedPorts(
project, zone, name, namedPortsReq).Do()
if err != nil {
return fmt.Errorf("Error updating named ports for InstanceGroup: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Updating InstanceGroup")
if err != nil {
return err
}
d.SetPartial("named_port")
}
d.Partial(false)
return resourceComputeInstanceGroupRead(d, meta)
}
func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
op, err := config.clientCompute.InstanceGroups.Delete(project, zone, name).Do()
if err != nil {
return fmt.Errorf("Error deleting InstanceGroup: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Deleting InstanceGroup")
if err != nil {
return err
}
d.SetId("")
return nil
}
func resourceComputeInstanceGroupImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid compute instance group specifier. Expecting {zone}/{name}")
}
d.Set("zone", parts[0])
d.Set("name", parts[1])
return []*schema.ResourceData{d}, nil
}