/
resource_compute_instance_group_manager.go
1066 lines (932 loc) · 37.9 KB
/
resource_compute_instance_group_manager.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package google
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"google.golang.org/api/compute/v1"
)
func resourceComputeInstanceGroupManager() *schema.Resource {
return &schema.Resource{
Create: resourceComputeInstanceGroupManagerCreate,
Read: resourceComputeInstanceGroupManagerRead,
Update: resourceComputeInstanceGroupManagerUpdate,
Delete: resourceComputeInstanceGroupManagerDelete,
Importer: &schema.ResourceImporter{
State: resourceInstanceGroupManagerStateImporter,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Minute),
Update: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(15 * time.Minute),
},
Schema: map[string]*schema.Schema{
"base_instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The base instance name to use for instances in this group. The value must be a valid RFC1035 name. Supported characters are lowercase letters, numbers, and hyphens (-). Instances are named by appending a hyphen and a random four-character string to the base instance name.`,
},
"version": {
Type: schema.TypeList,
Required: true,
Description: `Application versions managed by this instance group. Each version deals with a specific instance template, allowing canary release scenarios.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Version name.`,
},
"instance_template": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: compareSelfLinkRelativePaths,
Description: `The full URL to an instance template from which all new instances of this version will be created.`,
},
"target_size": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `The number of instances calculated as a fixed number or a percentage depending on the settings.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fixed": {
Type: schema.TypeInt,
Optional: true,
Description: `The number of instances which are managed for this version. Conflicts with percent.`,
},
"percent": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 100),
Description: `The number of instances (calculated as percentage) which are managed for this version. Conflicts with fixed. Note that when using percent, rounding will be in favor of explicitly set target_size values; a managed instance group with 2 instances and 2 versions, one of which has a target_size.percent of 60 will create 2 instances of that version.`,
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the instance group manager. Must be 1-63 characters long and comply with RFC1035. Supported characters include lowercase letters, numbers, and hyphens.`,
},
"zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The zone that instances in this group should be created in.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `An optional textual description of the instance group manager.`,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: `The fingerprint of the instance group manager.`,
},
"instance_group": {
Type: schema.TypeString,
Computed: true,
Description: `The full URL of the instance group created by the manager.`,
},
"named_port": {
Type: schema.TypeSet,
Optional: true,
Description: `The named port configuration.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of the port.`,
},
"port": {
Type: schema.TypeInt,
Required: true,
Description: `The port number.`,
},
},
},
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
Description: `The URL of the created resource.`,
},
"target_pools": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: selfLinkRelativePathHash,
Description: `The full URL of all target pools to which new instances in the group are added. Updating the target pools attribute does not affect existing instances.`,
},
"target_size": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. Defaults to 0.`,
},
"auto_healing_policies": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `The autohealing policies for this managed instance group. You can specify only one value.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"health_check": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: compareSelfLinkRelativePaths,
Description: `The health check resource that signals autohealing.`,
},
"initial_delay_sec": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 3600),
Description: `The number of seconds that the managed instance group waits before it applies autohealing policies to new instances or recently recreated instances. Between 0 and 3600.`,
},
},
},
},
"update_policy": {
Computed: true,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `The update policy for this managed instance group.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"minimal_action": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"RESTART", "REPLACE"}, false),
Description: `Minimal action to be taken on an instance. You can specify either RESTART to restart existing instances or REPLACE to delete and create new instances from the target template. If you specify a RESTART, the Updater will attempt to perform that action only. However, if the Updater determines that the minimal action you specify is not enough to perform the update, it might perform a more disruptive action.`,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"OPPORTUNISTIC", "PROACTIVE"}, false),
Description: `The type of update process. You can specify either PROACTIVE so that the instance group manager proactively executes actions in order to bring instances to their target versions or OPPORTUNISTIC so that no action is proactively executed but the update will be performed as part of other actions (for example, resizes or recreateInstances calls).`,
},
"max_surge_fixed": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ConflictsWith: []string{"update_policy.0.max_surge_percent"},
Description: `The maximum number of instances that can be created above the specified targetSize during the update process. Conflicts with max_surge_percent. If neither is set, defaults to 1`,
},
"max_surge_percent": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"update_policy.0.max_surge_fixed"},
ValidateFunc: validation.IntBetween(0, 100),
Description: `The maximum number of instances(calculated as percentage) that can be created above the specified targetSize during the update process. Conflicts with max_surge_fixed.`,
},
"max_unavailable_fixed": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ConflictsWith: []string{"update_policy.0.max_unavailable_percent"},
Description: `The maximum number of instances that can be unavailable during the update process. Conflicts with max_unavailable_percent. If neither is set, defaults to 1.`,
},
"max_unavailable_percent": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"update_policy.0.max_unavailable_fixed"},
ValidateFunc: validation.IntBetween(0, 100),
Description: `The maximum number of instances(calculated as percentage) that can be unavailable during the update process. Conflicts with max_unavailable_fixed.`,
},
"replacement_method": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"RECREATE", "SUBSTITUTE", ""}, false),
DiffSuppressFunc: emptyOrDefaultStringSuppress("SUBSTITUTE"),
Description: `The instance replacement method for managed instance groups. Valid values are: "RECREATE", "SUBSTITUTE". If SUBSTITUTE (default), the group replaces VM instances with new instances that have randomly generated names. If RECREATE, instance names are preserved. You must also set max_unavailable_fixed or max_unavailable_percent to be greater than 0.`,
},
},
},
},
"wait_for_instances": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: `Whether to wait for all instances to be created/updated before returning. Note that if this is set to true and the operation does not succeed, Terraform will continue trying until it times out.`,
},
"wait_for_instances_status": {
Type: schema.TypeString,
Optional: true,
Default: "STABLE",
ValidateFunc: validation.StringInSlice([]string{"STABLE", "UPDATED"}, false),
Description: `When used with wait_for_instances specifies the status to wait for. When STABLE is specified this resource will wait until the instances are stable before returning. When UPDATED is set, it will wait for the version target to be reached and any per instance configs to be effective as well as all instances to be stable before returning.`,
},
"stateful_disk": {
Type: schema.TypeSet,
Optional: true,
Description: `Disks created on the instances that will be preserved on instance delete, update, etc.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Required: true,
Description: `The device name of the disk to be attached.`,
},
"delete_rule": {
Type: schema.TypeString,
Default: "NEVER",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"NEVER", "ON_PERMANENT_INSTANCE_DELETION"}, true),
Description: `A value that prescribes what should happen to the stateful disk when the VM instance is deleted. The available options are NEVER and ON_PERMANENT_INSTANCE_DELETION. NEVER - detach the disk when the VM is deleted, but do not delete the disk. ON_PERMANENT_INSTANCE_DELETION will delete the stateful disk when the VM is permanently deleted from the instance group. The default is NEVER.`,
},
},
},
},
"operation": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeList,
Computed: true,
Description: `The status of this managed instance group.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_stable": {
Type: schema.TypeBool,
Computed: true,
Description: `A bit indicating whether the managed instance group is in a stable state. A stable state means that: none of the instances in the managed instance group is currently undergoing any type of change (for example, creation, restart, or deletion); no future changes are scheduled for instances in the managed instance group; and the managed instance group itself is not being modified.`,
},
"version_target": {
Type: schema.TypeList,
Computed: true,
Description: `A status of consistency of Instances' versions with their target version specified by version field on Instance Group Manager.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_reached": {
Type: schema.TypeBool,
Computed: true,
Description: `A bit indicating whether version target has been reached in this managed instance group, i.e. all instances are in their target version. Instances' target version are specified by version field on Instance Group Manager.`,
},
},
},
},
"stateful": {
Type: schema.TypeList,
Computed: true,
Description: `Stateful status of the given Instance Group Manager.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"has_stateful_config": {
Type: schema.TypeBool,
Computed: true,
Description: `A bit indicating whether the managed instance group has stateful configuration, that is, if you have configured any items in a stateful policy or in per-instance configs. The group might report that it has no stateful config even when there is still some preserved state on a managed instance, for example, if you have deleted all PICs but not yet applied those deletions.`,
},
"per_instance_configs": {
Type: schema.TypeList,
Computed: true,
Description: `Status of per-instance configs on the instance.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"all_effective": {
Type: schema.TypeBool,
Computed: true,
Description: `A bit indicating if all of the group's per-instance configs (listed in the output of a listPerInstanceConfigs API call) have status EFFECTIVE or there are no per-instance-configs.`,
},
},
},
},
},
},
},
},
},
},
},
UseJSONNumber: true,
}
}
func getNamedPorts(nps []interface{}) []*compute.NamedPort {
namedPorts := make([]*compute.NamedPort, 0, len(nps))
for _, v := range nps {
np := v.(map[string]interface{})
namedPorts = append(namedPorts, &compute.NamedPort{
Name: np["name"].(string),
Port: int64(np["port"].(int)),
})
}
return namedPorts
}
func getNamedPortsBeta(nps []interface{}) []*compute.NamedPort {
namedPorts := make([]*compute.NamedPort, 0, len(nps))
for _, v := range nps {
np := v.(map[string]interface{})
namedPorts = append(namedPorts, &compute.NamedPort{
Name: np["name"].(string),
Port: int64(np["port"].(int)),
})
}
return namedPorts
}
func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
// Build the parameter
manager := &compute.InstanceGroupManager{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
BaseInstanceName: d.Get("base_instance_name").(string),
TargetSize: int64(d.Get("target_size").(int)),
NamedPorts: getNamedPortsBeta(d.Get("named_port").(*schema.Set).List()),
TargetPools: convertStringSet(d.Get("target_pools").(*schema.Set)),
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
UpdatePolicy: expandUpdatePolicy(d.Get("update_policy").([]interface{})),
StatefulPolicy: expandStatefulPolicy(d.Get("stateful_disk").(*schema.Set).List()),
// Force send TargetSize to allow a value of 0.
ForceSendFields: []string{"TargetSize"},
}
log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager)
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Insert(
project, zone, manager).Do()
if err != nil {
return fmt.Errorf("Error creating InstanceGroupManager: %s", err)
}
// It probably maybe worked, so store the ID now
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{name}}")
if err != nil {
return err
}
d.SetId(id)
// Wait for the operation to complete
err = computeOperationWaitTime(config, op, project, "Creating InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutCreate))
if err != nil {
// Check if the create operation failed because Terraform was prematurely terminated. If it was we can persist the
// operation id to state so that a subsequent refresh of this resource will wait until the operation has terminated
// before attempting to Read the state of the manager. This allows a graceful resumption of a Create that was killed
// by the upstream Terraform process exiting early such as a sigterm.
select {
case <-config.context.Done():
log.Printf("[DEBUG] Persisting %s so this operation can be resumed \n", op.Name)
if err := d.Set("operation", op.Name); err != nil {
return fmt.Errorf("Error setting operation: %s", err)
}
return nil
default:
// leaving default case to ensure this is non blocking
}
return err
}
if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}
return resourceComputeInstanceGroupManagerRead(d, meta)
}
func flattenNamedPortsBeta(namedPorts []*compute.NamedPort) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(namedPorts))
for _, namedPort := range namedPorts {
namedPortMap := make(map[string]interface{})
namedPortMap["name"] = namedPort.Name
namedPortMap["port"] = namedPort.Port
result = append(result, namedPortMap)
}
return result
}
func flattenVersions(versions []*compute.InstanceGroupManagerVersion) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(versions))
for _, version := range versions {
versionMap := make(map[string]interface{})
versionMap["name"] = version.Name
versionMap["instance_template"] = ConvertSelfLinkToV1(version.InstanceTemplate)
versionMap["target_size"] = flattenFixedOrPercent(version.TargetSize)
result = append(result, versionMap)
}
return result
}
func flattenFixedOrPercent(fixedOrPercent *compute.FixedOrPercent) []map[string]interface{} {
result := make(map[string]interface{})
if value := fixedOrPercent.Percent; value > 0 {
result["percent"] = value
} else if value := fixedOrPercent.Fixed; value > 0 {
result["fixed"] = fixedOrPercent.Fixed
} else {
return []map[string]interface{}{}
}
return []map[string]interface{}{result}
}
func getManager(d *schema.ResourceData, meta interface{}) (*compute.InstanceGroupManager, error) {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return nil, err
}
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return nil, err
}
zone, _ := getZone(d, config)
name := d.Get("name").(string)
manager, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Get(project, zone, name).Do()
if err != nil {
return nil, handleNotFoundError(err, d, fmt.Sprintf("Instance Group Manager %q", name))
}
if manager == nil {
log.Printf("[WARN] Removing Instance Group Manager %q because it's gone", d.Get("name").(string))
// The resource doesn't exist anymore
d.SetId("")
return nil, nil
}
return manager, nil
}
func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
operation := d.Get("operation").(string)
if operation != "" {
log.Printf("[DEBUG] in progress operation detected at %v, attempting to resume", operation)
zone, _ := getZone(d, config)
op := &compute.Operation{
Name: operation,
Zone: zone,
}
if err := d.Set("operation", op.Name); err != nil {
return fmt.Errorf("Error setting operation: %s", err)
}
err = computeOperationWaitTime(config, op, project, "Creating InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutCreate))
if err != nil {
// remove from state to allow refresh to finish
log.Printf("[DEBUG] Resumed operation returned an error, removing from state: %s", err)
d.SetId("")
return nil
}
}
manager, err := getManager(d, meta)
if err != nil {
return err
}
if manager == nil {
log.Printf("[WARN] Instance Group Manager %q not found, removing from state.", d.Id())
d.SetId("")
return nil
}
if err := d.Set("base_instance_name", manager.BaseInstanceName); err != nil {
return fmt.Errorf("Error setting base_instance_name: %s", err)
}
if err := d.Set("name", manager.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("zone", GetResourceNameFromSelfLink(manager.Zone)); err != nil {
return fmt.Errorf("Error setting zone: %s", err)
}
if err := d.Set("description", manager.Description); err != nil {
return fmt.Errorf("Error setting description: %s", err)
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("target_size", manager.TargetSize); err != nil {
return fmt.Errorf("Error setting target_size: %s", err)
}
if err = d.Set("target_pools", mapStringArr(manager.TargetPools, ConvertSelfLinkToV1)); err != nil {
return fmt.Errorf("Error setting target_pools in state: %s", err.Error())
}
if err = d.Set("named_port", flattenNamedPortsBeta(manager.NamedPorts)); err != nil {
return fmt.Errorf("Error setting named_port in state: %s", err.Error())
}
if err = d.Set("stateful_disk", flattenStatefulPolicy(manager.StatefulPolicy)); err != nil {
return fmt.Errorf("Error setting stateful_disk in state: %s", err.Error())
}
if err := d.Set("fingerprint", manager.Fingerprint); err != nil {
return fmt.Errorf("Error setting fingerprint: %s", err)
}
if err := d.Set("instance_group", ConvertSelfLinkToV1(manager.InstanceGroup)); err != nil {
return fmt.Errorf("Error setting instance_group: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(manager.SelfLink)); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
if err = d.Set("auto_healing_policies", flattenAutoHealingPolicies(manager.AutoHealingPolicies)); err != nil {
return fmt.Errorf("Error setting auto_healing_policies in state: %s", err.Error())
}
if err := d.Set("version", flattenVersions(manager.Versions)); err != nil {
return err
}
if err = d.Set("update_policy", flattenUpdatePolicy(manager.UpdatePolicy)); err != nil {
return fmt.Errorf("Error setting update_policy in state: %s", err.Error())
}
if err = d.Set("status", flattenStatus(manager.Status)); err != nil {
return fmt.Errorf("Error setting status in state: %s", err.Error())
}
// If unset in state set to default value
if d.Get("wait_for_instances_status").(string) == "" {
if err := d.Set("wait_for_instances_status", "STABLE"); err != nil {
return fmt.Errorf("Error setting wait_for_instances_status in state: %s", err.Error())
}
}
return nil
}
func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
zone, err := getZone(d, config)
if err != nil {
return err
}
updatedManager := &compute.InstanceGroupManager{
Fingerprint: d.Get("fingerprint").(string),
}
var change bool
if d.HasChange("description") {
updatedManager.Description = d.Get("description").(string)
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "Description")
change = true
}
if d.HasChange("target_pools") {
updatedManager.TargetPools = convertStringSet(d.Get("target_pools").(*schema.Set))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetPools")
change = true
}
if d.HasChange("auto_healing_policies") {
updatedManager.AutoHealingPolicies = expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{}))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "AutoHealingPolicies")
change = true
}
if d.HasChange("version") {
updatedManager.Versions = expandVersions(d.Get("version").([]interface{}))
change = true
}
if d.HasChange("update_policy") {
updatedManager.UpdatePolicy = expandUpdatePolicy(d.Get("update_policy").([]interface{}))
change = true
}
if d.HasChange("stateful_disk") {
updatedManager.StatefulPolicy = expandStatefulPolicy(d.Get("stateful_disk").(*schema.Set).List())
change = true
}
if change {
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Patch(project, zone, d.Get("name").(string), updatedManager).Do()
if err != nil {
return fmt.Errorf("Error updating managed group instances: %s", err)
}
err = computeOperationWaitTime(config, op, project, "Updating managed group instances", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
// named ports can't be updated through PATCH
// so we call the update method on the instance group, instead of the igm
if d.HasChange("named_port") {
d.Partial(true)
// Build the parameters for a "SetNamedPorts" request:
namedPorts := getNamedPortsBeta(d.Get("named_port").(*schema.Set).List())
setNamedPorts := &compute.InstanceGroupsSetNamedPortsRequest{
NamedPorts: namedPorts,
}
// Make the request:
op, err := config.NewComputeClient(userAgent).InstanceGroups.SetNamedPorts(
project, zone, d.Get("name").(string), setNamedPorts).Do()
if err != nil {
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
}
// Wait for the operation to complete:
err = computeOperationWaitTime(config, op, project, "Updating InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
// target_size should be updated through resize
if d.HasChange("target_size") {
d.Partial(true)
targetSize := int64(d.Get("target_size").(int))
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Resize(
project, zone, d.Get("name").(string), targetSize).Do()
if err != nil {
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
}
// Wait for the operation to complete
err = computeOperationWaitTime(config, op, project, "Updating InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
d.Partial(false)
if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}
return resourceComputeInstanceGroupManagerRead(d, meta)
}
func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
zone, _ := getZone(d, config)
name := d.Get("name").(string)
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Delete(project, zone, name).Do()
attempt := 0
for err != nil && attempt < 20 {
attempt++
time.Sleep(2000 * time.Millisecond)
op, err = config.NewComputeClient(userAgent).InstanceGroupManagers.Delete(project, zone, name).Do()
}
if err != nil {
return fmt.Errorf("Error deleting instance group manager: %s", err)
}
currentSize := int64(d.Get("target_size").(int))
// Wait for the operation to complete
err = computeOperationWaitTime(config, op, project, "Deleting InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutDelete))
for err != nil && currentSize > 0 {
if !strings.Contains(err.Error(), "timeout") {
return err
}
instanceGroup, igErr := config.NewComputeClient(userAgent).InstanceGroups.Get(
project, zone, name).Do()
if igErr != nil {
return fmt.Errorf("Error getting instance group size: %s", err)
}
instanceGroupSize := instanceGroup.Size
if instanceGroupSize >= currentSize {
return fmt.Errorf("Error, instance group isn't shrinking during delete")
}
log.Printf("[INFO] timeout occurred, but instance group is shrinking (%d < %d)", instanceGroupSize, currentSize)
currentSize = instanceGroupSize
err = computeOperationWaitTime(config, op, project, "Deleting InstanceGroupManager", userAgent, d.Timeout(schema.TimeoutDelete))
}
d.SetId("")
return nil
}
func computeIGMWaitForInstanceStatus(d *schema.ResourceData, meta interface{}) error {
waitForUpdates := d.Get("wait_for_instances_status").(string) == "UPDATED"
conf := resource.StateChangeConf{
Pending: []string{"creating", "error", "updating per instance configs", "reaching version target"},
Target: []string{"created"},
Refresh: waitForInstancesRefreshFunc(getManager, waitForUpdates, d, meta),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err := conf.WaitForState()
if err != nil {
return err
}
return nil
}
func expandAutoHealingPolicies(configured []interface{}) []*compute.InstanceGroupManagerAutoHealingPolicy {
autoHealingPolicies := make([]*compute.InstanceGroupManagerAutoHealingPolicy, 0, len(configured))
for _, raw := range configured {
data := raw.(map[string]interface{})
autoHealingPolicy := compute.InstanceGroupManagerAutoHealingPolicy{
HealthCheck: data["health_check"].(string),
InitialDelaySec: int64(data["initial_delay_sec"].(int)),
}
autoHealingPolicies = append(autoHealingPolicies, &autoHealingPolicy)
}
return autoHealingPolicies
}
func expandStatefulPolicy(configured []interface{}) *compute.StatefulPolicy {
disks := make(map[string]compute.StatefulPolicyPreservedStateDiskDevice)
for _, raw := range configured {
data := raw.(map[string]interface{})
disk := compute.StatefulPolicyPreservedStateDiskDevice{
AutoDelete: data["delete_rule"].(string),
}
disks[data["device_name"].(string)] = disk
}
if len(disks) > 0 {
return &compute.StatefulPolicy{PreservedState: &compute.StatefulPolicyPreservedState{Disks: disks}}
}
return nil
}
func expandVersions(configured []interface{}) []*compute.InstanceGroupManagerVersion {
versions := make([]*compute.InstanceGroupManagerVersion, 0, len(configured))
for _, raw := range configured {
data := raw.(map[string]interface{})
version := compute.InstanceGroupManagerVersion{
Name: data["name"].(string),
InstanceTemplate: data["instance_template"].(string),
TargetSize: expandFixedOrPercent(data["target_size"].([]interface{})),
}
versions = append(versions, &version)
}
return versions
}
func expandFixedOrPercent(configured []interface{}) *compute.FixedOrPercent {
fixedOrPercent := &compute.FixedOrPercent{}
for _, raw := range configured {
if raw != nil {
data := raw.(map[string]interface{})
if percent := data["percent"]; percent.(int) > 0 {
fixedOrPercent.Percent = int64(percent.(int))
} else {
fixedOrPercent.Fixed = int64(data["fixed"].(int))
fixedOrPercent.ForceSendFields = []string{"Fixed"}
}
}
}
return fixedOrPercent
}
func expandUpdatePolicy(configured []interface{}) *compute.InstanceGroupManagerUpdatePolicy {
updatePolicy := &compute.InstanceGroupManagerUpdatePolicy{}
for _, raw := range configured {
data := raw.(map[string]interface{})
updatePolicy.MinimalAction = data["minimal_action"].(string)
updatePolicy.Type = data["type"].(string)
updatePolicy.ReplacementMethod = data["replacement_method"].(string)
// percent and fixed values are conflicting
// when the percent values are set, the fixed values will be ignored
if v := data["max_surge_percent"]; v.(int) > 0 {
updatePolicy.MaxSurge = &compute.FixedOrPercent{
Percent: int64(v.(int)),
NullFields: []string{"Fixed"},
}
} else {
updatePolicy.MaxSurge = &compute.FixedOrPercent{
Fixed: int64(data["max_surge_fixed"].(int)),
// allow setting this value to 0
ForceSendFields: []string{"Fixed"},
NullFields: []string{"Percent"},
}
}
if v := data["max_unavailable_percent"]; v.(int) > 0 {
updatePolicy.MaxUnavailable = &compute.FixedOrPercent{
Percent: int64(v.(int)),
NullFields: []string{"Fixed"},
}
} else {
updatePolicy.MaxUnavailable = &compute.FixedOrPercent{
Fixed: int64(data["max_unavailable_fixed"].(int)),
// allow setting this value to 0
ForceSendFields: []string{"Fixed"},
NullFields: []string{"Percent"},
}
}
}
return updatePolicy
}
func flattenAutoHealingPolicies(autoHealingPolicies []*compute.InstanceGroupManagerAutoHealingPolicy) []map[string]interface{} {
autoHealingPoliciesSchema := make([]map[string]interface{}, 0, len(autoHealingPolicies))
for _, autoHealingPolicy := range autoHealingPolicies {
data := map[string]interface{}{
"health_check": autoHealingPolicy.HealthCheck,
"initial_delay_sec": autoHealingPolicy.InitialDelaySec,
}
autoHealingPoliciesSchema = append(autoHealingPoliciesSchema, data)
}
return autoHealingPoliciesSchema
}
func flattenStatefulPolicy(statefulPolicy *compute.StatefulPolicy) []map[string]interface{} {
if statefulPolicy == nil || statefulPolicy.PreservedState == nil || statefulPolicy.PreservedState.Disks == nil {
return make([]map[string]interface{}, 0, 0)
}
result := make([]map[string]interface{}, 0, len(statefulPolicy.PreservedState.Disks))
for deviceName, disk := range statefulPolicy.PreservedState.Disks {
data := map[string]interface{}{
"device_name": deviceName,
"delete_rule": disk.AutoDelete,
}
result = append(result, data)
}
return result
}
func flattenUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdatePolicy) []map[string]interface{} {
results := []map[string]interface{}{}
if updatePolicy != nil {
up := map[string]interface{}{}
if updatePolicy.MaxSurge != nil {
up["max_surge_fixed"] = updatePolicy.MaxSurge.Fixed
up["max_surge_percent"] = updatePolicy.MaxSurge.Percent
} else {
up["max_surge_fixed"] = 0
up["max_surge_percent"] = 0
}
if updatePolicy.MaxUnavailable != nil {
up["max_unavailable_fixed"] = updatePolicy.MaxUnavailable.Fixed
up["max_unavailable_percent"] = updatePolicy.MaxUnavailable.Percent
} else {
up["max_unavailable_fixed"] = 0
up["max_unavailable_percent"] = 0
}
up["minimal_action"] = updatePolicy.MinimalAction
up["type"] = updatePolicy.Type