Skip to content

Commit 264cfbd

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

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

google/resource_compute_instance.go

+44
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"
@@ -551,6 +552,9 @@ func resourceComputeInstance() *schema.Resource {
551552
Deprecated: "Use timeouts block instead.",
552553
},
553554
},
555+
CustomizeDiff: customdiff.All(
556+
suppressEmptyGuestAcceleratorDiff,
557+
),
554558
}
555559
}
556560

@@ -1219,6 +1223,46 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([
12191223
return guestAccelerators, nil
12201224
}
12211225

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

0 commit comments

Comments
 (0)