|
8 | 8 | "strings"
|
9 | 9 |
|
10 | 10 | "github.com/hashicorp/errwrap"
|
| 11 | + "github.com/hashicorp/terraform/helper/customdiff" |
11 | 12 | "github.com/hashicorp/terraform/helper/schema"
|
12 | 13 | "github.com/hashicorp/terraform/helper/validation"
|
13 | 14 | "github.com/mitchellh/hashstructure"
|
@@ -551,6 +552,9 @@ func resourceComputeInstance() *schema.Resource {
|
551 | 552 | Deprecated: "Use timeouts block instead.",
|
552 | 553 | },
|
553 | 554 | },
|
| 555 | + CustomizeDiff: customdiff.All( |
| 556 | + suppressEmptyGuestAcceleratorDiff, |
| 557 | + ), |
554 | 558 | }
|
555 | 559 | }
|
556 | 560 |
|
@@ -1219,6 +1223,46 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([
|
1219 | 1223 | return guestAccelerators, nil
|
1220 | 1224 | }
|
1221 | 1225 |
|
| 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 | + |
1222 | 1266 | func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
1223 | 1267 | config := meta.(*Config)
|
1224 | 1268 |
|
|
0 commit comments