Skip to content

Commit e0c0ba2

Browse files
karolgorcc2thornrileykarson
authored andcommitted
Change update method for boot_disk.auto_delete on google_compute_instance (GoogleCloudPlatform#11742)
Co-authored-by: Cameron Thornton <[email protected]> Co-authored-by: Riley Karson <[email protected]>
1 parent f5dfeaa commit e0c0ba2

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func ResourceComputeInstance() *schema.Resource {
218218
Optional: true,
219219
AtLeastOneOf: bootDiskKeys,
220220
Default: true,
221-
ForceNew: true,
222221
Description: `Whether the disk will be auto-deleted when the instance is deleted.`,
223222
},
224223

@@ -2502,6 +2501,18 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
25022501
}
25032502
}
25042503

2504+
// if any other boot_disk fields will be added here this should be wrapped in if d.HasChange("boot_disk")
2505+
if d.HasChange("boot_disk.0.auto_delete") {
2506+
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()
2507+
if err != nil {
2508+
return fmt.Errorf("Error changing auto_delete: %s", err)
2509+
}
2510+
opErr := ComputeOperationWaitTime(config, op, project, "changing auto_delete", userAgent, d.Timeout(schema.TimeoutUpdate))
2511+
if opErr != nil {
2512+
return opErr
2513+
}
2514+
}
2515+
25052516
// d.HasChange("service_account") is oversensitive: see https://github.com/hashicorp/terraform/issues/17411
25062517
// Until that's fixed, manually check whether there is a change.
25072518
o, n := d.GetChange("service_account")

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

+81-12
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,49 @@ func TestAccComputeInstance_proactiveAttributionLabel(t *testing.T) {
36643664
})
36653665
}
36663666

3667+
func TestAccComputeInstance_autoDeleteUpdate(t *testing.T) {
3668+
t.Parallel()
3669+
3670+
var instance compute.Instance
3671+
context_1 := map[string]interface{}{
3672+
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
3673+
"auto_delete": "true",
3674+
}
3675+
context_2 := map[string]interface{}{
3676+
"instance_name": context_1["instance_name"],
3677+
"auto_delete": "false",
3678+
}
3679+
3680+
acctest.VcrTest(t, resource.TestCase{
3681+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3682+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3683+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
3684+
Steps: []resource.TestStep{
3685+
{
3686+
Config: testAccComputeInstance_autoDeleteUpdate(context_1),
3687+
Check: resource.ComposeTestCheckFunc(
3688+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
3689+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "true"),
3690+
),
3691+
},
3692+
{
3693+
Config: testAccComputeInstance_autoDeleteUpdate(context_2),
3694+
Check: resource.ComposeTestCheckFunc(
3695+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
3696+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "false"),
3697+
),
3698+
},
3699+
{
3700+
Config: testAccComputeInstance_autoDeleteUpdate(context_1),
3701+
Check: resource.ComposeTestCheckFunc(
3702+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
3703+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.auto_delete", "true"),
3704+
),
3705+
},
3706+
},
3707+
})
3708+
}
3709+
36673710
func TestAccComputeInstance_keyRevocationActionType(t *testing.T) {
36683711
t.Parallel()
36693712

@@ -11117,29 +11160,55 @@ resource "google_compute_instance" "foobar" {
1111711160
`, diskName, instanceName, machineType, zone, bootDiskInterface, allowStoppingForUpdate)
1111811161
}
1111911162

11163+
func testAccComputeInstance_autoDeleteUpdate(context map[string]interface{}) string {
11164+
return acctest.Nprintf(`
11165+
data "google_compute_image" "my_image" {
11166+
family = "debian-11"
11167+
project = "debian-cloud"
11168+
}
11169+
11170+
resource "google_compute_instance" "foobar" {
11171+
name = "%{instance_name}"
11172+
machine_type = "n1-standard-1"
11173+
zone = "us-central1-a"
11174+
11175+
boot_disk {
11176+
auto_delete = %{auto_delete}
11177+
initialize_params {
11178+
image = data.google_compute_image.my_image.self_link
11179+
}
11180+
}
11181+
11182+
network_interface {
11183+
network = "default"
11184+
}
11185+
}
11186+
`, context)
11187+
}
11188+
1112011189
func testAccComputeInstance_keyRevocationActionType(context map[string]interface{}) string {
1112111190
return acctest.Nprintf(`
1112211191
data "google_compute_image" "my_image" {
11123-
family = "debian-11"
11124-
project = "debian-cloud"
11192+
family = "debian-11"
11193+
project = "debian-cloud"
1112511194
}
1112611195

1112711196
resource "google_compute_instance" "foobar" {
11128-
name = "%{instance_name}"
11129-
machine_type = "e2-medium"
11130-
zone = "us-central1-a"
11197+
name = "%{instance_name}"
11198+
machine_type = "e2-medium"
11199+
zone = "us-central1-a"
1113111200

11132-
boot_disk {
11201+
boot_disk {
1113311202
initialize_params {
11134-
image = data.google_compute_image.my_image.self_link
11203+
image = data.google_compute_image.my_image.self_link
11204+
}
1113511205
}
11136-
}
1113711206

11138-
network_interface {
11139-
network = "default"
11140-
}
11207+
network_interface {
11208+
network = "default"
11209+
}
1114111210

11142-
key_revocation_action_type = %{key_revocation_action_type}
11211+
key_revocation_action_type = %{key_revocation_action_type}
1114311212
}
1114411213
`, context)
1114511214
}

0 commit comments

Comments
 (0)