Skip to content

Commit

Permalink
Add support for specifying storage pool when creating a disk.
Browse files Browse the repository at this point in the history
  • Loading branch information
sizzle0121 committed May 24, 2024
1 parent 302f6b3 commit 1f2ef3c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mmv1/products/compute/Disk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,14 @@ properties:
resource: 'License'
imports: 'selfLink'
custom_expand: 'templates/terraform/custom_expand/array_resourceref_with_validation.go.erb'
- !ruby/object:Api::Type::String
name: 'storagePool'
required: false
immutable: true
description: |
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}
diff_suppress_func: 'tpgresource.CompareResourceNames'
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package compute_test

import (
"fmt"
"net/http"
"os"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

<% if version == "ga" -%>
"google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -1516,3 +1519,104 @@ resource "google_compute_disk" "foobar" {
`, add, strategy, diskName)
}

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

config := acctest.GoogleProviderConfig(t)
storagePoolName := fmt.Sprintf("tf-test-storage-pool-%s", acctest.RandString(t, 10))
storagePoolUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePools/%s", config.Project, config.Zone, storagePoolName)
diskName := fmt.Sprintf("tf-test-disk-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
PreConfig: setupTestingStoragePool(t, config, storagePoolName),
Config: testAccComputeDisk_storagePoolSpecified(diskName, storagePoolUrl),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_compute_disk.foobar", "storage_pool", storagePoolName),
),
},
{
ResourceName: "google_compute_disk.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})

cleanupTestingStoragePool(t, config, storagePoolName)
}

func setupTestingStoragePool(t *testing.T, config *transport_tpg.Config, storagePoolName string) func() {
return func() {
headers := make(http.Header)
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, config.Project, config.Zone)
storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/hyperdisk-throughput", config.Project, config.Zone)
defaultTimeout := 20 * time.Minute
obj := make(map[string]interface{})
obj["name"] = storagePoolName
obj["poolProvisionedCapacityGb"] = 10240
obj["poolProvisionedThroughput"] = 180
obj["storagePoolType"] = storagePoolTypeUrl
obj["capacityProvisioningType"] = "ADVANCED"

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: config.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, config.Project, "Creating StoragePool", config.UserAgent, defaultTimeout)
if err != nil {
t.Errorf("Error waiting to create StoragePool: %s", err)
}
}
}

func cleanupTestingStoragePool(t *testing.T, config *transport_tpg.Config, storagePoolName string) {
headers := make(http.Header)
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools/%s", config.ComputeBasePath, config.Project, config.Zone, storagePoolName)
defaultTimeout := 20 * time.Minute
var obj map[string]interface{}

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

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

func testAccComputeDisk_storagePoolSpecified(diskName, storagePoolUrl string) string {
return fmt.Sprintf(`
resource "google_compute_disk" "foobar" {
name = "%s"
type = "hyperdisk-throughput"
size = 2048
provisioned_throughput = 140
storage_pool = "%s"
}
`, diskName, storagePoolUrl)
}

0 comments on commit 1f2ef3c

Please sign in to comment.