@@ -7,12 +7,14 @@ import (
7
7
"time"
8
8
9
9
"github.com/hashicorp/terraform/helper/schema"
10
+ "github.com/hashicorp/terraform/helper/validation"
10
11
11
12
computeBeta "google.golang.org/api/compute/v0.beta"
12
13
"google.golang.org/api/compute/v1"
13
14
)
14
15
15
16
var InstanceGroupManagerBaseApiVersion = v1
17
+ var InstanceGroupManagerVersionedFeatures = []Feature {Feature {Version : v0beta , Item : "auto_healing_policies" }}
16
18
17
19
func resourceComputeInstanceGroupManager () * schema.Resource {
18
20
return & schema.Resource {
@@ -116,6 +118,27 @@ func resourceComputeInstanceGroupManager() *schema.Resource {
116
118
Computed : true ,
117
119
Optional : true ,
118
120
},
121
+
122
+ "auto_healing_policies" : & schema.Schema {
123
+ Type : schema .TypeList ,
124
+ Optional : true ,
125
+ MaxItems : 1 ,
126
+ Elem : & schema.Resource {
127
+ Schema : map [string ]* schema.Schema {
128
+ "health_check" : & schema.Schema {
129
+ Type : schema .TypeString ,
130
+ Required : true ,
131
+ DiffSuppressFunc : compareSelfLinkRelativePaths ,
132
+ },
133
+
134
+ "initial_delay_sec" : & schema.Schema {
135
+ Type : schema .TypeInt ,
136
+ Required : true ,
137
+ ValidateFunc : validation .IntBetween (0 , 3600 ),
138
+ },
139
+ },
140
+ },
141
+ },
119
142
},
120
143
}
121
144
}
@@ -147,7 +170,7 @@ func getNamedPortsBeta(nps []interface{}) []*computeBeta.NamedPort {
147
170
}
148
171
149
172
func resourceComputeInstanceGroupManagerCreate (d * schema.ResourceData , meta interface {}) error {
150
- computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , [] Feature {} )
173
+ computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , InstanceGroupManagerVersionedFeatures )
151
174
config := meta .(* Config )
152
175
153
176
project , err := getProject (d , config )
@@ -192,6 +215,10 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
192
215
return fmt .Errorf ("Update strategy must be \" NONE\" or \" RESTART\" " )
193
216
}
194
217
218
+ if v , ok := d .GetOk ("auto_healing_policies" ); ok {
219
+ manager .AutoHealingPolicies = expandAutoHealingPolicies (v .([]interface {}))
220
+ }
221
+
195
222
log .Printf ("[DEBUG] InstanceGroupManager insert request: %#v" , manager )
196
223
var op interface {}
197
224
switch computeApiVersion {
@@ -246,7 +273,7 @@ func flattenNamedPortsBeta(namedPorts []*computeBeta.NamedPort) []map[string]int
246
273
}
247
274
248
275
func resourceComputeInstanceGroupManagerRead (d * schema.ResourceData , meta interface {}) error {
249
- computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , [] Feature {} )
276
+ computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , InstanceGroupManagerVersionedFeatures )
250
277
config := meta .(* Config )
251
278
252
279
project , err := getProject (d , config )
@@ -354,12 +381,13 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
354
381
update_strategy = "RESTART"
355
382
}
356
383
d .Set ("update_strategy" , update_strategy .(string ))
384
+ d .Set ("auto_healing_policies" , flattenAutoHealingPolicies (manager .AutoHealingPolicies ))
357
385
358
386
return nil
359
387
}
360
388
361
389
func resourceComputeInstanceGroupManagerUpdate (d * schema.ResourceData , meta interface {}) error {
362
- computeApiVersion := getComputeApiVersionUpdate (d , InstanceGroupManagerBaseApiVersion , [] Feature {} , []Feature {})
390
+ computeApiVersion := getComputeApiVersionUpdate (d , InstanceGroupManagerBaseApiVersion , InstanceGroupManagerVersionedFeatures , []Feature {})
363
391
config := meta .(* Config )
364
392
365
393
project , err := getProject (d , config )
@@ -604,13 +632,36 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
604
632
d .SetPartial ("target_size" )
605
633
}
606
634
635
+ // We will always be in v0beta inside this conditional
636
+ if d .HasChange ("auto_healing_policies" ) {
637
+ setAutoHealingPoliciesRequest := & computeBeta.InstanceGroupManagersSetAutoHealingRequest {}
638
+ if v , ok := d .GetOk ("auto_healing_policies" ); ok {
639
+ setAutoHealingPoliciesRequest .AutoHealingPolicies = expandAutoHealingPolicies (v .([]interface {}))
640
+ }
641
+
642
+ op , err := config .clientComputeBeta .InstanceGroupManagers .SetAutoHealingPolicies (
643
+ project , d .Get ("zone" ).(string ), d .Id (), setAutoHealingPoliciesRequest ).Do ()
644
+
645
+ if err != nil {
646
+ return fmt .Errorf ("Error updating AutoHealingPolicies: %s" , err )
647
+ }
648
+
649
+ // Wait for the operation to complete
650
+ err = computeSharedOperationWaitZone (config , op , project , d .Get ("zone" ).(string ), "Updating AutoHealingPolicies" )
651
+ if err != nil {
652
+ return err
653
+ }
654
+
655
+ d .SetPartial ("auto_healing_policies" )
656
+ }
657
+
607
658
d .Partial (false )
608
659
609
660
return resourceComputeInstanceGroupManagerRead (d , meta )
610
661
}
611
662
612
663
func resourceComputeInstanceGroupManagerDelete (d * schema.ResourceData , meta interface {}) error {
613
- computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , [] Feature {} )
664
+ computeApiVersion := getComputeApiVersion (d , InstanceGroupManagerBaseApiVersion , InstanceGroupManagerVersionedFeatures )
614
665
config := meta .(* Config )
615
666
616
667
project , err := getProject (d , config )
@@ -686,3 +737,30 @@ func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta inte
686
737
d .SetId ("" )
687
738
return nil
688
739
}
740
+
741
+ func expandAutoHealingPolicies (configured []interface {}) []* computeBeta.InstanceGroupManagerAutoHealingPolicy {
742
+ autoHealingPolicies := make ([]* computeBeta.InstanceGroupManagerAutoHealingPolicy , 0 , len (configured ))
743
+ for _ , raw := range configured {
744
+ data := raw .(map [string ]interface {})
745
+ autoHealingPolicy := computeBeta.InstanceGroupManagerAutoHealingPolicy {
746
+ HealthCheck : data ["health_check" ].(string ),
747
+ InitialDelaySec : int64 (data ["initial_delay_sec" ].(int )),
748
+ }
749
+
750
+ autoHealingPolicies = append (autoHealingPolicies , & autoHealingPolicy )
751
+ }
752
+ return autoHealingPolicies
753
+ }
754
+
755
+ func flattenAutoHealingPolicies (autoHealingPolicies []* computeBeta.InstanceGroupManagerAutoHealingPolicy ) []map [string ]interface {} {
756
+ autoHealingPoliciesSchema := make ([]map [string ]interface {}, 0 , len (autoHealingPolicies ))
757
+ for _ , autoHealingPolicy := range autoHealingPolicies {
758
+ data := map [string ]interface {}{
759
+ "health_check" : autoHealingPolicy .HealthCheck ,
760
+ "initial_delay_sec" : autoHealingPolicy .InitialDelaySec ,
761
+ }
762
+
763
+ autoHealingPoliciesSchema = append (autoHealingPoliciesSchema , data )
764
+ }
765
+ return autoHealingPoliciesSchema
766
+ }
0 commit comments