Skip to content

Commit

Permalink
Add support of storage pool on boot disk. (#11230) (#18817)
Browse files Browse the repository at this point in the history
[upstream:a91e512e76282855fce81ad9622d0d1b73d816e9]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jul 23, 2024
1 parent c7165ac commit b4f0138
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11230.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `storage_pool` under `boot_disk.initialize_params` to `google_compute_instance` resource
```
15 changes: 15 additions & 0 deletions google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var (
"boot_disk.0.initialize_params.0.provisioned_iops",
"boot_disk.0.initialize_params.0.provisioned_throughput",
"boot_disk.0.initialize_params.0.enable_confidential_compute",
"boot_disk.0.initialize_params.0.storage_pool",
}

schedulingKeys = []string{
Expand Down Expand Up @@ -289,6 +290,15 @@ func ResourceComputeInstance() *schema.Resource {
ForceNew: true,
Description: `A flag to enable confidential compute mode on boot disk`,
},

"storage_pool": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: initializeParamsKeys,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `The URL of the storage pool in which the new disk is created`,
},
},
},
},
Expand Down Expand Up @@ -2823,6 +2833,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
if _, ok := d.GetOk("boot_disk.0.initialize_params.0.resource_manager_tags"); ok {
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.resource_manager_tags")
}

if v, ok := d.GetOk("boot_disk.0.initialize_params.0.storage_pool"); ok {
disk.InitializeParams.StoragePool = v.(string)
}
}

if v, ok := d.GetOk("boot_disk.0.mode"); ok {
Expand Down Expand Up @@ -2865,6 +2879,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
"provisioned_iops": diskDetails.ProvisionedIops,
"provisioned_throughput": diskDetails.ProvisionedThroughput,
"enable_confidential_compute": diskDetails.EnableConfidentialCompute,
"storage_pool": tpgresource.GetResourceNameFromSelfLink(diskDetails.StoragePool),
}}
}

Expand Down
99 changes: 99 additions & 0 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package compute_test

import (
"fmt"
"net/http"
"reflect"
"regexp"
"sort"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/hashicorp/terraform-provider-google/google/envvar"
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

"google.golang.org/api/compute/v1"
)
Expand Down Expand Up @@ -8044,3 +8046,100 @@ resource "google_compute_instance" "foobar" {
}
`, suffix, region, suffix, instance, region, stack_type)
}

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

instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(t, 10))
storagePoolName := fmt.Sprintf("tf-test-storage-pool-%s", acctest.RandString(t, 10))
storagePoolUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePools/%s", envvar.GetTestProjectFromEnv(), envvar.GetTestZoneFromEnv(), storagePoolName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
PreConfig: setupTestingStoragePool_HyperdiskBalanced(t, storagePoolName),
Config: testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolUrl, envvar.GetTestZoneFromEnv()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.initialize_params.0.storage_pool", storagePoolName),
),
},
{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})

cleanupTestingStoragePool(t, storagePoolName)
}

func setupTestingStoragePool_HyperdiskBalanced(t *testing.T, storagePoolName string) func() {
return func() {
config := acctest.GoogleProviderConfig(t)
headers := make(http.Header)
project := envvar.GetTestProjectFromEnv()
zone := envvar.GetTestZoneFromEnv()
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, project, zone)
storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/hyperdisk-balanced", project, zone)
defaultTimeout := 20 * time.Minute
obj := make(map[string]interface{})
obj["name"] = storagePoolName
obj["poolProvisionedCapacityGb"] = 10240
obj["poolProvisionedIops"] = 10000
obj["poolProvisionedThroughput"] = 1024
obj["storagePoolType"] = storagePoolTypeUrl
obj["capacityProvisioningType"] = "ADVANCED"

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
Body: obj,
Timeout: defaultTimeout,
Headers: headers,
})
if err != nil {
t.Errorf("Error creating StoragePool: %s", err)
}

err = tpgcompute.ComputeOperationWaitTime(config, res, project, "Creating StoragePool", config.UserAgent, defaultTimeout)
if err != nil {
t.Errorf("Error waiting to create StoragePool: %s", err)
}
}
}

func testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolUrl, zone string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "ubuntu-2204-lts"
project = "ubuntu-os-cloud"
}
data "google_project" "project" {}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type= "h3-standard-88"
zone = "%s"
boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
type = "hyperdisk-balanced"
size = 500
storage_pool = "%s"
}
}
network_interface {
network = "default"
}
}
`, instanceName, zone, storagePoolUrl)
}
5 changes: 5 additions & 0 deletions website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ is desired, you will need to modify your state file manually using
* `enable_confidential_compute` - (Optional) Whether this disk is using confidential compute mode.
Note: Only supported on hyperdisk skus, disk_encryption_key is required when setting to true.

* `storage_pool` - (Optional) The URL of the storage pool in which the new disk is created.
For example:
* https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/storagePools/{storagePool}
* /projects/{project}/zones/{zone}/storagePools/{storagePool}

<a name="nested_scratch_disk"></a>The `scratch_disk` block supports:

* `interface` - (Required) The disk interface to use for attaching this disk; either SCSI or NVME.
Expand Down

0 comments on commit b4f0138

Please sign in to comment.