Skip to content

Commit 03d0687

Browse files
authored
Allow updating guest accelerator count to 0 on compute_instance (#15410)
1 parent 5eb5d67 commit 03d0687

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,12 +3376,23 @@ func suppressEmptyGuestAcceleratorDiff(_ context.Context, d *schema.ResourceDiff
33763376
return nil
33773377
}
33783378

3379-
firstAccel, ok := new[0].(map[string]interface{})
3379+
// Check if old had a non-zero count
3380+
if len(old) > 0 {
3381+
if apiAccel, ok := old[0].(map[string]interface{}); ok {
3382+
if oldCount, ok := apiAccel["count"].(int); ok && oldCount != 0 {
3383+
// Old count wasn't 0, so don't clear the diff
3384+
return nil
3385+
}
3386+
}
3387+
}
3388+
3389+
// Check new accelerator configuration
3390+
configAccel, ok := new[0].(map[string]interface{})
33803391
if !ok {
33813392
return fmt.Errorf("Unable to type assert guest accelerator")
33823393
}
33833394

3384-
if firstAccel["count"].(int) == 0 {
3395+
if configAccel["count"].(int) == 0 {
33853396
if err := d.Clear("guest_accelerator"); err != nil {
33863397
return err
33873398
}

mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,10 +2504,16 @@ func TestAccComputeInstance_guestAccelerator(t *testing.T) {
25042504
testAccCheckComputeInstanceHasGuestAccelerator(&instance, "nvidia-tesla-t4", 1),
25052505
),
25062506
},
2507+
{
2508+
Config: testAccComputeInstance_guestAccelerator(instanceName, 0),
2509+
Check: resource.ComposeTestCheckFunc(
2510+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
2511+
testAccCheckComputeInstanceHasGuestAccelerator(&instance, "nvidia-tesla-t4", 0),
2512+
),
2513+
},
25072514
computeInstanceImportStep("us-east1-d", instanceName, []string{"metadata.baz", "metadata.foo"}),
25082515
},
25092516
})
2510-
25112517
}
25122518

25132519
func TestAccComputeInstance_guestAcceleratorSkip(t *testing.T) {
@@ -5841,6 +5847,9 @@ func testAccCheckComputeInstanceHasMultiNic(instance *compute.Instance) resource
58415847
func testAccCheckComputeInstanceHasGuestAccelerator(instance *compute.Instance, acceleratorType string, acceleratorCount int64) resource.TestCheckFunc {
58425848
return func(s *terraform.State) error {
58435849
if len(instance.GuestAccelerators) != 1 {
5850+
if int(acceleratorCount) == 0 && len(instance.GuestAccelerators) == 0 {
5851+
return nil
5852+
}
58445853
return fmt.Errorf("Expected only one guest accelerator")
58455854
}
58465855

0 commit comments

Comments
 (0)