Skip to content

Commit

Permalink
Resize disk in google_workbench_instance resource (GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
  • Loading branch information
bcreddy-gcp authored and vijaykanthm committed Jul 22, 2024
1 parent cdb041e commit fb945b9
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 5 deletions.
4 changes: 0 additions & 4 deletions mmv1/products/workbench/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,10 @@ properties:
name: bootDisk
default_from_api: true
description: The definition of a boot disk.
immutable: true
properties:
- !ruby/object:Api::Type::String
name: diskSizeGb
default_from_api: true
immutable: 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
Expand Down Expand Up @@ -315,13 +313,11 @@ properties:
description: Data disks attached to the VM instance. Currently supports only one data disk.
max_size: 1
default_from_api: true
immutable: true
item_type: !ruby/object:Api::Type::NestedObject
properties:
- !ruby/object:Api::Type::String
name: diskSizeGb
default_from_api: true
immutable: 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
Expand Down
45 changes: 45 additions & 0 deletions mmv1/templates/terraform/constants/workbench_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,49 @@ 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
}
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if d.HasChange("gce_setup.0.metadata") {
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 @@ -55,3 +55,10 @@ if stopInstance{
} else {
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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 @@ -448,3 +456,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)
}

0 comments on commit fb945b9

Please sign in to comment.