Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize disk in google_workbench_instance resource #18482

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/10972.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workbench: added update support to `gce_setup.boot_disk` and `gce_setup.data_disks` fields in `google_workbench_instance` resource
```
57 changes: 52 additions & 5 deletions google/services/workbench/resource_workbench_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,50 @@ func waitForWorkbenchOperation(config *transport_tpg.Config, d *schema.ResourceD
return nil
}

func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.ResourceData, project string, userAgent string, isBoot bool) error {
diskObj := make(map[string]interface{})
var sizeString string
var diskKey string
if isBoot {
sizeString = "gce_setup.0.boot_disk.0.disk_size_gb"
diskKey = "bootDisk"
} else {
sizeString = "gce_setup.0.data_disks.0.disk_size_gb"
diskKey = "dataDisk"
}
disk := make(map[string]interface{})
disk["diskSizeGb"] = d.Get(sizeString)
diskObj[diskKey] = disk

resizeUrl, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:resizeDisk")
if err != nil {
return err
}

dRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
RawURL: resizeUrl,
UserAgent: userAgent,
Body: diskObj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})

if err != nil {
return fmt.Errorf("Error resizing disk: %s", err)
}

var opRes map[string]interface{}
err = WorkbenchOperationWaitTimeWithResponse(
config, dRes, &opRes, project, "Resizing disk", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error resizing disk: %s", err)
}

return nil
}

func ResourceWorkbenchInstance() *schema.Resource {
return &schema.Resource{
Create: resourceWorkbenchInstanceCreate,
Expand Down Expand Up @@ -303,7 +347,6 @@ Currently supports only one accelerator configuration.`,
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `The definition of a boot disk.`,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -321,7 +364,6 @@ data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. The size of the boot disk in GB attached to this instance,
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to the
recommended value of 150GB.`,
Expand Down Expand Up @@ -372,7 +414,6 @@ For example: gcr.io/{project_id}/{imageName}`,
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Data disks attached to the VM instance. Currently supports only one data disk.`,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -390,7 +431,6 @@ and data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. The size of the disk in GB attached to this VM instance,
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to
100.`,
Expand Down Expand Up @@ -1032,7 +1072,7 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e
if d.HasChange("effective_labels") {
newUpdateMask = append(newUpdateMask, "labels")
}

updateMask = newUpdateMask
// Overwrite the previously set mask.
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(newUpdateMask, ",")})
if err != nil {
Expand Down Expand Up @@ -1061,6 +1101,13 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] Workbench Instance %q need not be stopped for the update.", name)
}

if d.HasChange("gce_setup.0.boot_disk.0.disk_size_gb") {
resizeWorkbenchInstanceDisk(config, d, project, userAgent, true)
}
if d.HasChange("gce_setup.0.data_disks.0.disk_size_gb") {
resizeWorkbenchInstanceDisk(config, d, project, userAgent, false)
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
Expand Down
177 changes: 177 additions & 0 deletions google/services/workbench/resource_workbench_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ resource "google_workbench_instance" "instance" {
enable_integrity_monitoring = false
}

boot_disk {
disk_size_gb = 310
}

data_disks {
disk_size_gb = 330
}

metadata = {
terraform = "true"
}
Expand Down Expand Up @@ -449,3 +457,172 @@ resource "google_workbench_instance" "instance" {
}
`, context)
}

func TestAccWorkbenchInstance_updateBootDisk(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccWorkbenchInstance_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
{
Config: testAccWorkbenchInstance_updateBootDisk(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
},
})
}

func TestAccWorkbenchInstance_updateDataDisk(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccWorkbenchInstance_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
{
Config: testAccWorkbenchInstance_updateDataDisk(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
},
})
}

func TestAccWorkbenchInstance_updateBothDisks(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccWorkbenchInstance_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
{
Config: testAccWorkbenchInstance_updateBothDisks(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
},
},
})
}

func testAccWorkbenchInstance_updateBootDisk(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"
gce_setup {
boot_disk {
disk_size_gb = 310
}
}
}
`, context)
}

func testAccWorkbenchInstance_updateDataDisk(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"
gce_setup {
data_disks {
disk_size_gb = 330
}
}
}
`, context)
}

func testAccWorkbenchInstance_updateBothDisks(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"
gce_setup {
boot_disk {
disk_size_gb = 310
}

data_disks {
disk_size_gb = 330
}
}
}
`, context)
}