Skip to content

Commit fee7d03

Browse files
committed
attempt to clear guest accelerator diff
1 parent 30ec4cf commit fee7d03

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

google/resource_compute_instance.go

+45
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/hashicorp/errwrap"
11+
"github.com/hashicorp/terraform/helper/customdiff"
1112
"github.com/hashicorp/terraform/helper/schema"
1213
"github.com/hashicorp/terraform/helper/validation"
1314
"github.com/mitchellh/hashstructure"
@@ -491,6 +492,7 @@ func resourceComputeInstance() *schema.Resource {
491492
"guest_accelerator": &schema.Schema{
492493
Type: schema.TypeList,
493494
Optional: true,
495+
Computed: true,
494496
ForceNew: true,
495497
Elem: &schema.Resource{
496498
Schema: map[string]*schema.Schema{
@@ -551,6 +553,9 @@ func resourceComputeInstance() *schema.Resource {
551553
Deprecated: "Use timeouts block instead.",
552554
},
553555
},
556+
CustomizeDiff: customdiff.All(
557+
suppressEmptyGuestAcceleratorDiff,
558+
),
554559
}
555560
}
556561

@@ -1219,6 +1224,46 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([
12191224
return guestAccelerators, nil
12201225
}
12211226

1227+
// suppressEmptyGuestAcceleratorDiff is used to work around perpetual diff
1228+
// issues when a count of `0` guest accelerators is desired. This may occur when
1229+
// guest_accelerator support is controlled via a module variable. E.g.:
1230+
//
1231+
// guest_accelerators {
1232+
// count = "${var.enable_gpu ? var.gpu_count : 0}"
1233+
// ...
1234+
// }
1235+
// After reconciling the desired and actual state, we would otherwise see a
1236+
// perpetual resembling:
1237+
// [] != [{"count":0, "type": "nvidia-tesla-k80"}]
1238+
func suppressEmptyGuestAcceleratorDiff(d *schema.ResourceDiff, meta interface{}) error {
1239+
oldi, newi := d.GetChange("guest_accelerator")
1240+
1241+
old, ok := oldi.([]interface{})
1242+
if !ok {
1243+
return fmt.Errorf("Expected old guest accelerator diff to be a slice")
1244+
}
1245+
1246+
new, ok := newi.([]interface{})
1247+
if !ok {
1248+
return fmt.Errorf("Expected new guest accelerator diff to be a slice")
1249+
}
1250+
1251+
if len(old) != 0 && len(new) != 1 {
1252+
return nil
1253+
}
1254+
1255+
firstAccel, ok := new[0].(map[string]interface{})
1256+
if !ok {
1257+
return fmt.Errorf("Unable to type assert guest accelerator")
1258+
}
1259+
1260+
if firstAccel["count"].(int) == 0 {
1261+
d.Clear("guest_accelerator")
1262+
}
1263+
1264+
return nil
1265+
}
1266+
12221267
func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error {
12231268
config := meta.(*Config)
12241269

0 commit comments

Comments
 (0)