From 0ab69350e44db6fc442b510b02b49d50300e9e1d Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Fri, 27 Sep 2024 15:00:07 -0500 Subject: [PATCH 1/3] Convert from Ruby to Go change update method of auto_delete field --- .../compute/resource_compute_instance.go.tmpl | 14 +++- .../resource_compute_instance_test.go.tmpl | 70 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl index d380348ce911..ef3f4c58ae6d 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl @@ -208,7 +208,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.`, }, @@ -2411,6 +2410,19 @@ 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 errwrap.Wrapf("Error setting auto_delete : {{"{{"}}err{{"}}"}}", err) + } + opErr := ComputeOperationWaitTime(config, op, project, "changing auto_delete", userAgent, d.Timeout(schema.TimeoutUpdate)) + if opErr != nil { + return opErr + } + log.Printf("[DEBUG] Successfully changed auto_delete on disk %s", instance.Disks[0].DeviceName) + } + // 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/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl index fc2e195448b7..a035157b826a 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl @@ -3476,6 +3476,50 @@ 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"), + ), + }, + }, + }) +} + {{ if ne $.TargetVersionName `ga` -}} const errorDeleteAccessConfigWithSecPolicy = "Cannot delete an access config with a security policy set. Please remove the security policy first" @@ -10729,3 +10773,29 @@ 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) +} \ No newline at end of file From b309f148678eaa6a251a5658b64d7d6e3e284c0f Mon Sep 17 00:00:00 2001 From: karolgorc Date: Fri, 18 Oct 2024 08:22:19 +0200 Subject: [PATCH 2/3] Update mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl Co-authored-by: Riley Karson --- .../terraform/services/compute/resource_compute_instance.go.tmpl | 1 - 1 file changed, 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl index ef3f4c58ae6d..9dfc6cb2c6e0 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl @@ -2420,7 +2420,6 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err if opErr != nil { return opErr } - log.Printf("[DEBUG] Successfully changed auto_delete on disk %s", instance.Disks[0].DeviceName) } // d.HasChange("service_account") is oversensitive: see https://github.com/hashicorp/terraform/issues/17411 From 44a622a4d9ce32693bb6fdff97d33c7e341a4d9b Mon Sep 17 00:00:00 2001 From: Karol Gorczyk Date: Fri, 18 Oct 2024 09:50:43 +0200 Subject: [PATCH 3/3] change wrapf to errorf --- .../services/compute/resource_compute_instance.go.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl index 9dfc6cb2c6e0..fc3e147a92b5 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl @@ -2414,7 +2414,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err 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 errwrap.Wrapf("Error setting auto_delete : {{"{{"}}err{{"}}"}}", err) + 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 {