From e0fcd2283e35bb5b00a7d162c2eeed48c3667775 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 4 Dec 2024 15:06:28 +0000 Subject: [PATCH] Change update method for `boot_disk.auto_delete` on `google_compute_instance` (#11742) Co-authored-by: Cameron Thornton Co-authored-by: Riley Karson [upstream:7ce8ed7654523056e56701a7dcfb1cc54a58a445] Signed-off-by: Modular Magician --- .changelog/11742.txt | 3 + .../compute/resource_compute_instance.go | 13 ++- .../compute/resource_compute_instance_test.go | 93 ++++++++++++++++--- 3 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 .changelog/11742.txt diff --git a/.changelog/11742.txt b/.changelog/11742.txt new file mode 100644 index 00000000000..b3ad6a154d9 --- /dev/null +++ b/.changelog/11742.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: `google_compute_instance` is no longer recreated when changing `boot_disk.auto_delete` +``` \ No newline at end of file diff --git a/google/services/compute/resource_compute_instance.go b/google/services/compute/resource_compute_instance.go index 2805560f262..807558cf8ce 100644 --- a/google/services/compute/resource_compute_instance.go +++ b/google/services/compute/resource_compute_instance.go @@ -212,7 +212,6 @@ func ResourceComputeInstance() *schema.Resource { Optional: true, AtLeastOneOf: bootDiskKeys, Default: true, - ForceNew: true, Description: `Whether the disk will be auto-deleted when the instance is deleted.`, }, @@ -2338,6 +2337,18 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err } } + // if any other boot_disk fields will be added here this should be wrapped in if d.HasChange("boot_disk") + if d.HasChange("boot_disk.0.auto_delete") { + op, err := config.NewComputeClient(userAgent).Instances.SetDiskAutoDelete(project, zone, instance.Name, d.Get("boot_disk.0.auto_delete").(bool), d.Get("boot_disk.0.device_name").(string)).Do() + if err != nil { + return fmt.Errorf("Error changing auto_delete: %s", err) + } + opErr := ComputeOperationWaitTime(config, op, project, "changing auto_delete", userAgent, d.Timeout(schema.TimeoutUpdate)) + if opErr != nil { + return opErr + } + } + // d.HasChange("service_account") is oversensitive: see https://github.com/hashicorp/terraform/issues/17411 // Until that's fixed, manually check whether there is a change. o, n := d.GetChange("service_account") diff --git a/google/services/compute/resource_compute_instance_test.go b/google/services/compute/resource_compute_instance_test.go index bcc6d3e6bbe..202a96fe006 100644 --- a/google/services/compute/resource_compute_instance_test.go +++ b/google/services/compute/resource_compute_instance_test.go @@ -3469,6 +3469,49 @@ func TestAccComputeInstance_proactiveAttributionLabel(t *testing.T) { }) } +func TestAccComputeInstance_autoDeleteUpdate(t *testing.T) { + t.Parallel() + + var instance compute.Instance + context_1 := map[string]interface{}{ + "instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)), + "auto_delete": "true", + } + context_2 := map[string]interface{}{ + "instance_name": context_1["instance_name"], + "auto_delete": "false", + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeInstance_autoDeleteUpdate(context_1), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance), + resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "true"), + ), + }, + { + Config: testAccComputeInstance_autoDeleteUpdate(context_2), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance), + resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "false"), + ), + }, + { + Config: testAccComputeInstance_autoDeleteUpdate(context_1), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance), + resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "true"), + ), + }, + }, + }) +} + func TestAccComputeInstance_keyRevocationActionType(t *testing.T) { t.Parallel() @@ -9355,29 +9398,55 @@ resource "google_compute_instance" "foobar" { `, diskName, instanceName, machineType, zone, bootDiskInterface, allowStoppingForUpdate) } +func testAccComputeInstance_autoDeleteUpdate(context map[string]interface{}) string { + return acctest.Nprintf(` +data "google_compute_image" "my_image" { + family = "debian-11" + project = "debian-cloud" +} + +resource "google_compute_instance" "foobar" { + name = "%{instance_name}" + machine_type = "n1-standard-1" + zone = "us-central1-a" + + boot_disk { + auto_delete = %{auto_delete} + initialize_params { + image = data.google_compute_image.my_image.self_link + } + } + + network_interface { + network = "default" + } +} +`, context) +} + func testAccComputeInstance_keyRevocationActionType(context map[string]interface{}) string { return acctest.Nprintf(` data "google_compute_image" "my_image" { - family = "debian-11" - project = "debian-cloud" + family = "debian-11" + project = "debian-cloud" } resource "google_compute_instance" "foobar" { - name = "%{instance_name}" - machine_type = "e2-medium" - zone = "us-central1-a" + name = "%{instance_name}" + machine_type = "e2-medium" + zone = "us-central1-a" - boot_disk { + boot_disk { initialize_params { - image = data.google_compute_image.my_image.self_link + image = data.google_compute_image.my_image.self_link + } } - } - network_interface { - network = "default" - } + network_interface { + network = "default" + } - key_revocation_action_type = %{key_revocation_action_type} + key_revocation_action_type = %{key_revocation_action_type} } `, context) }