Skip to content

Commit bca1993

Browse files
[#11433] Update google_compute_instance description in place (#9748) (#16900)
[upstream:ce85734ca809d9b8769df5456854ea8f6b680468] Signed-off-by: Modular Magician <[email protected]>
1 parent 0368b61 commit bca1993

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

.changelog/9748.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: allow users to update `google_compute_instance` `description` without recreating VMs
3+
```

google/services/compute/resource_compute_instance.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ func ResourceComputeInstance() *schema.Resource {
562562
"description": {
563563
Type: schema.TypeString,
564564
Optional: true,
565-
ForceNew: true,
566565
Description: `A brief description of the resource.`,
567566
},
568567

@@ -1613,6 +1612,35 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
16131612
// Enable partial mode for the resource since it is possible
16141613
d.Partial(true)
16151614

1615+
if d.HasChange("description") {
1616+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
1617+
RetryFunc: func() error {
1618+
instance, err := config.NewComputeClient(userAgent).Instances.Get(project, zone, instance.Name).Do()
1619+
if err != nil {
1620+
return fmt.Errorf("Error retrieving instance: %s", err)
1621+
}
1622+
1623+
instance.Description = d.Get("description").(string)
1624+
1625+
op, err := config.NewComputeClient(userAgent).Instances.Update(project, zone, instance.Name, instance).Do()
1626+
if err != nil {
1627+
return fmt.Errorf("Error updating instance: %s", err)
1628+
}
1629+
1630+
opErr := ComputeOperationWaitTime(config, op, project, "description, updating", userAgent, d.Timeout(schema.TimeoutUpdate))
1631+
if opErr != nil {
1632+
return opErr
1633+
}
1634+
1635+
return nil
1636+
},
1637+
})
1638+
1639+
if err != nil {
1640+
return err
1641+
}
1642+
}
1643+
16161644
if d.HasChange("metadata") {
16171645
metadata, err := resourceInstanceMetadata(d)
16181646
if err != nil {

google/services/compute/resource_compute_instance_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,37 @@ func TestAccComputeInstance_resourceManagerTags(t *testing.T) {
267267
})
268268
}
269269

270+
func TestAccComputeInstance_descriptionUpdate(t *testing.T) {
271+
t.Parallel()
272+
273+
var instance compute.Instance
274+
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
275+
276+
acctest.VcrTest(t, resource.TestCase{
277+
PreCheck: func() { acctest.AccTestPreCheck(t) },
278+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
279+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
280+
Steps: []resource.TestStep{
281+
{
282+
Config: testAccComputeInstance_description(instanceName),
283+
Check: resource.ComposeTestCheckFunc(
284+
testAccCheckComputeInstanceExists(
285+
t, "google_compute_instance.foobar", &instance),
286+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "description", "old_desc"),
287+
),
288+
},
289+
{
290+
Config: testAccComputeInstance_descriptionUpdate(instanceName),
291+
Check: resource.ComposeTestCheckFunc(
292+
testAccCheckComputeInstanceExists(
293+
t, "google_compute_instance.foobar", &instance),
294+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "description", "new_desc"),
295+
),
296+
},
297+
},
298+
})
299+
}
300+
270301
func TestAccComputeInstance_IP(t *testing.T) {
271302
t.Parallel()
272303

@@ -3466,6 +3497,58 @@ resource "google_compute_instance" "foobar" {
34663497
`, instance)
34673498
}
34683499

3500+
func testAccComputeInstance_description(instance string) string {
3501+
return fmt.Sprintf(`
3502+
data "google_compute_image" "my_image" {
3503+
family = "debian-11"
3504+
project = "debian-cloud"
3505+
}
3506+
3507+
resource "google_compute_instance" "foobar" {
3508+
name = "%s"
3509+
machine_type = "e2-medium"
3510+
zone = "us-central1-a"
3511+
description = "old_desc"
3512+
3513+
boot_disk {
3514+
initialize_params {
3515+
image = data.google_compute_image.my_image.self_link
3516+
}
3517+
}
3518+
3519+
network_interface {
3520+
network = "default"
3521+
}
3522+
}
3523+
`, instance)
3524+
}
3525+
3526+
func testAccComputeInstance_descriptionUpdate(instance string) string {
3527+
return fmt.Sprintf(`
3528+
data "google_compute_image" "my_image" {
3529+
family = "debian-11"
3530+
project = "debian-cloud"
3531+
}
3532+
3533+
resource "google_compute_instance" "foobar" {
3534+
name = "%s"
3535+
machine_type = "e2-medium"
3536+
zone = "us-central1-a"
3537+
description = "new_desc"
3538+
3539+
boot_disk {
3540+
initialize_params {
3541+
image = data.google_compute_image.my_image.self_link
3542+
}
3543+
}
3544+
3545+
network_interface {
3546+
network = "default"
3547+
}
3548+
}
3549+
`, instance)
3550+
}
3551+
34693552
func testAccComputeInstance_resourceManagerTags(context map[string]interface{}) string {
34703553
return acctest.Nprintf(`
34713554
resource "google_tags_tag_key" "key" {

0 commit comments

Comments
 (0)