Skip to content

Commit 93381e9

Browse files
Resize disk in google_workbench_instance resource (#10972) (#7566)
[upstream:ade0a1ec36b97ef2853044110fea0cdd4bec6383] Signed-off-by: Modular Magician <[email protected]>
1 parent bc926a1 commit 93381e9

File tree

3 files changed

+232
-5
lines changed

3 files changed

+232
-5
lines changed

.changelog/10972.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
workbench: added update support to `gce_setup.boot_disk` and `gce_setup.data_disks` fields in `google_workbench_instance` resource
3+
```

google-beta/services/workbench/resource_workbench_instance.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,50 @@ func waitForWorkbenchOperation(config *transport_tpg.Config, d *schema.ResourceD
227227
return nil
228228
}
229229

230+
func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.ResourceData, project string, userAgent string, isBoot bool) error {
231+
diskObj := make(map[string]interface{})
232+
var sizeString string
233+
var diskKey string
234+
if isBoot {
235+
sizeString = "gce_setup.0.boot_disk.0.disk_size_gb"
236+
diskKey = "bootDisk"
237+
} else {
238+
sizeString = "gce_setup.0.data_disks.0.disk_size_gb"
239+
diskKey = "dataDisk"
240+
}
241+
disk := make(map[string]interface{})
242+
disk["diskSizeGb"] = d.Get(sizeString)
243+
diskObj[diskKey] = disk
244+
245+
resizeUrl, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:resizeDisk")
246+
if err != nil {
247+
return err
248+
}
249+
250+
dRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
251+
Config: config,
252+
Method: "POST",
253+
RawURL: resizeUrl,
254+
UserAgent: userAgent,
255+
Body: diskObj,
256+
Timeout: d.Timeout(schema.TimeoutUpdate),
257+
})
258+
259+
if err != nil {
260+
return fmt.Errorf("Error resizing disk: %s", err)
261+
}
262+
263+
var opRes map[string]interface{}
264+
err = WorkbenchOperationWaitTimeWithResponse(
265+
config, dRes, &opRes, project, "Resizing disk", userAgent,
266+
d.Timeout(schema.TimeoutUpdate))
267+
if err != nil {
268+
return fmt.Errorf("Error resizing disk: %s", err)
269+
}
270+
271+
return nil
272+
}
273+
230274
func ResourceWorkbenchInstance() *schema.Resource {
231275
return &schema.Resource{
232276
Create: resourceWorkbenchInstanceCreate,
@@ -303,7 +347,6 @@ Currently supports only one accelerator configuration.`,
303347
Type: schema.TypeList,
304348
Computed: true,
305349
Optional: true,
306-
ForceNew: true,
307350
Description: `The definition of a boot disk.`,
308351
MaxItems: 1,
309352
Elem: &schema.Resource{
@@ -321,7 +364,6 @@ data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
321364
Type: schema.TypeString,
322365
Computed: true,
323366
Optional: true,
324-
ForceNew: true,
325367
Description: `Optional. The size of the boot disk in GB attached to this instance,
326368
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to the
327369
recommended value of 150GB.`,
@@ -372,7 +414,6 @@ For example: gcr.io/{project_id}/{imageName}`,
372414
Type: schema.TypeList,
373415
Computed: true,
374416
Optional: true,
375-
ForceNew: true,
376417
Description: `Data disks attached to the VM instance. Currently supports only one data disk.`,
377418
MaxItems: 1,
378419
Elem: &schema.Resource{
@@ -390,7 +431,6 @@ and data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
390431
Type: schema.TypeString,
391432
Computed: true,
392433
Optional: true,
393-
ForceNew: true,
394434
Description: `Optional. The size of the disk in GB attached to this VM instance,
395435
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to
396436
100.`,
@@ -1032,7 +1072,7 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e
10321072
if d.HasChange("effective_labels") {
10331073
newUpdateMask = append(newUpdateMask, "labels")
10341074
}
1035-
1075+
updateMask = newUpdateMask
10361076
// Overwrite the previously set mask.
10371077
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(newUpdateMask, ",")})
10381078
if err != nil {
@@ -1061,6 +1101,13 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e
10611101
log.Printf("[DEBUG] Workbench Instance %q need not be stopped for the update.", name)
10621102
}
10631103

1104+
if d.HasChange("gce_setup.0.boot_disk.0.disk_size_gb") {
1105+
resizeWorkbenchInstanceDisk(config, d, project, userAgent, true)
1106+
}
1107+
if d.HasChange("gce_setup.0.data_disks.0.disk_size_gb") {
1108+
resizeWorkbenchInstanceDisk(config, d, project, userAgent, false)
1109+
}
1110+
10641111
// err == nil indicates that the billing_project value was found
10651112
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
10661113
billingProject = bp

google-beta/services/workbench/resource_workbench_instance_test.go

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ resource "google_workbench_instance" "instance" {
8080
enable_integrity_monitoring = false
8181
}
8282
83+
boot_disk {
84+
disk_size_gb = 310
85+
}
86+
87+
data_disks {
88+
disk_size_gb = 330
89+
}
90+
8391
metadata = {
8492
terraform = "true"
8593
}
@@ -449,3 +457,172 @@ resource "google_workbench_instance" "instance" {
449457
}
450458
`, context)
451459
}
460+
461+
func TestAccWorkbenchInstance_updateBootDisk(t *testing.T) {
462+
t.Parallel()
463+
464+
context := map[string]interface{}{
465+
"random_suffix": acctest.RandString(t, 10),
466+
}
467+
468+
acctest.VcrTest(t, resource.TestCase{
469+
PreCheck: func() { acctest.AccTestPreCheck(t) },
470+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
471+
Steps: []resource.TestStep{
472+
{
473+
Config: testAccWorkbenchInstance_basic(context),
474+
Check: resource.ComposeTestCheckFunc(
475+
resource.TestCheckResourceAttr(
476+
"google_workbench_instance.instance", "state", "ACTIVE"),
477+
),
478+
},
479+
{
480+
ResourceName: "google_workbench_instance.instance",
481+
ImportState: true,
482+
ImportStateVerify: true,
483+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
484+
},
485+
{
486+
Config: testAccWorkbenchInstance_updateBootDisk(context),
487+
Check: resource.ComposeTestCheckFunc(
488+
resource.TestCheckResourceAttr(
489+
"google_workbench_instance.instance", "state", "ACTIVE"),
490+
),
491+
},
492+
{
493+
ResourceName: "google_workbench_instance.instance",
494+
ImportState: true,
495+
ImportStateVerify: true,
496+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
497+
},
498+
},
499+
})
500+
}
501+
502+
func TestAccWorkbenchInstance_updateDataDisk(t *testing.T) {
503+
t.Parallel()
504+
505+
context := map[string]interface{}{
506+
"random_suffix": acctest.RandString(t, 10),
507+
}
508+
509+
acctest.VcrTest(t, resource.TestCase{
510+
PreCheck: func() { acctest.AccTestPreCheck(t) },
511+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
512+
Steps: []resource.TestStep{
513+
{
514+
Config: testAccWorkbenchInstance_basic(context),
515+
Check: resource.ComposeTestCheckFunc(
516+
resource.TestCheckResourceAttr(
517+
"google_workbench_instance.instance", "state", "ACTIVE"),
518+
),
519+
},
520+
{
521+
ResourceName: "google_workbench_instance.instance",
522+
ImportState: true,
523+
ImportStateVerify: true,
524+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
525+
},
526+
{
527+
Config: testAccWorkbenchInstance_updateDataDisk(context),
528+
Check: resource.ComposeTestCheckFunc(
529+
resource.TestCheckResourceAttr(
530+
"google_workbench_instance.instance", "state", "ACTIVE"),
531+
),
532+
},
533+
{
534+
ResourceName: "google_workbench_instance.instance",
535+
ImportState: true,
536+
ImportStateVerify: true,
537+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
538+
},
539+
},
540+
})
541+
}
542+
543+
func TestAccWorkbenchInstance_updateBothDisks(t *testing.T) {
544+
t.Parallel()
545+
546+
context := map[string]interface{}{
547+
"random_suffix": acctest.RandString(t, 10),
548+
}
549+
550+
acctest.VcrTest(t, resource.TestCase{
551+
PreCheck: func() { acctest.AccTestPreCheck(t) },
552+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
553+
Steps: []resource.TestStep{
554+
{
555+
Config: testAccWorkbenchInstance_basic(context),
556+
Check: resource.ComposeTestCheckFunc(
557+
resource.TestCheckResourceAttr(
558+
"google_workbench_instance.instance", "state", "ACTIVE"),
559+
),
560+
},
561+
{
562+
ResourceName: "google_workbench_instance.instance",
563+
ImportState: true,
564+
ImportStateVerify: true,
565+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
566+
},
567+
{
568+
Config: testAccWorkbenchInstance_updateBothDisks(context),
569+
Check: resource.ComposeTestCheckFunc(
570+
resource.TestCheckResourceAttr(
571+
"google_workbench_instance.instance", "state", "ACTIVE"),
572+
),
573+
},
574+
{
575+
ResourceName: "google_workbench_instance.instance",
576+
ImportState: true,
577+
ImportStateVerify: true,
578+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
579+
},
580+
},
581+
})
582+
}
583+
584+
func testAccWorkbenchInstance_updateBootDisk(context map[string]interface{}) string {
585+
return acctest.Nprintf(`
586+
resource "google_workbench_instance" "instance" {
587+
name = "tf-test-workbench-instance%{random_suffix}"
588+
location = "us-central1-a"
589+
gce_setup {
590+
boot_disk {
591+
disk_size_gb = 310
592+
}
593+
}
594+
}
595+
`, context)
596+
}
597+
598+
func testAccWorkbenchInstance_updateDataDisk(context map[string]interface{}) string {
599+
return acctest.Nprintf(`
600+
resource "google_workbench_instance" "instance" {
601+
name = "tf-test-workbench-instance%{random_suffix}"
602+
location = "us-central1-a"
603+
gce_setup {
604+
data_disks {
605+
disk_size_gb = 330
606+
}
607+
}
608+
}
609+
`, context)
610+
}
611+
612+
func testAccWorkbenchInstance_updateBothDisks(context map[string]interface{}) string {
613+
return acctest.Nprintf(`
614+
resource "google_workbench_instance" "instance" {
615+
name = "tf-test-workbench-instance%{random_suffix}"
616+
location = "us-central1-a"
617+
gce_setup {
618+
boot_disk {
619+
disk_size_gb = 310
620+
}
621+
622+
data_disks {
623+
disk_size_gb = 330
624+
}
625+
}
626+
}
627+
`, context)
628+
}

0 commit comments

Comments
 (0)