diff --git a/.changelog/11598.txt b/.changelog/11598.txt new file mode 100644 index 00000000000..42b910df155 --- /dev/null +++ b/.changelog/11598.txt @@ -0,0 +1,3 @@ +```release-note:none + +``` \ No newline at end of file diff --git a/google/acctest/bootstrap_test_utils.go b/google/acctest/bootstrap_test_utils.go index 791a837fc42..46fdd9924e4 100644 --- a/google/acctest/bootstrap_test_utils.go +++ b/google/acctest/bootstrap_test_utils.go @@ -1109,6 +1109,78 @@ func BootstrapFirewallForDataprocSharedNetwork(t *testing.T, firewallName string return firewall.Name } +const SharedStoragePoolPrefix = "tf-bootstrap-storage-pool-" + +func BootstrapComputeStoragePool(t *testing.T, storagePoolName, storagePoolType string) string { + projectID := envvar.GetTestProjectFromEnv() + zone := envvar.GetTestZoneFromEnv() + + storagePoolName = SharedStoragePoolPrefix + storagePoolType + "-" + storagePoolName + + config := BootstrapConfig(t) + if config == nil { + t.Fatal("Could not bootstrap config.") + } + + computeService := config.NewComputeClient(config.UserAgent) + if computeService == nil { + t.Fatal("Could not create compute client.") + } + + _, err := computeService.StoragePools.Get(projectID, zone, storagePoolName).Do() + if err != nil && transport_tpg.IsGoogleApiErrorWithCode(err, 404) { + log.Printf("[DEBUG] Storage pool %q not found, bootstrapping", storagePoolName) + + url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, projectID, zone) + storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/%s", projectID, zone, storagePoolType) + + storagePoolObj := map[string]interface{}{ + "name": storagePoolName, + "poolProvisionedCapacityGb": 10240, + "poolProvisionedThroughput": 180, + "storagePoolType": storagePoolTypeUrl, + "capacityProvisioningType": "ADVANCED", + } + + if storagePoolType == "hyperdisk-balanced" { + storagePoolObj["poolProvisionedIops"] = 10000 + storagePoolObj["poolProvisionedThroughput"] = 1024 + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: projectID, + RawURL: url, + UserAgent: config.UserAgent, + Body: storagePoolObj, + Timeout: 20 * time.Minute, + }) + + log.Printf("Response is, %s", res) + if err != nil { + t.Fatalf("Error bootstrapping storage pool %s: %s", storagePoolName, err) + } + + log.Printf("[DEBUG] Waiting for storage pool creation to finish") + err = tpgcompute.ComputeOperationWaitTime(config, res, projectID, "Error bootstrapping storage pool", config.UserAgent, 4*time.Minute) + if err != nil { + t.Fatalf("Error bootstrapping test storage pool %s: %s", storagePoolName, err) + } + } + + storagePool, err := computeService.StoragePools.Get(projectID, zone, storagePoolName).Do() + + if storagePool == nil { + t.Fatalf("Error getting storage pool %s: is nil", storagePoolName) + } + + if err != nil { + t.Fatalf("Error getting storage pool %s: %s", storagePoolName, err) + } + return storagePool.SelfLink +} + func SetupProjectsAndGetAccessToken(org, billing, pid, service string, config *transport_tpg.Config) (string, error) { // Create project-1 and project-2 rmService := config.NewResourceManagerClient(config.UserAgent) diff --git a/google/services/compute/resource_compute_disk_test.go b/google/services/compute/resource_compute_disk_test.go index fc8a3a5a152..80407ff62f8 100644 --- a/google/services/compute/resource_compute_disk_test.go +++ b/google/services/compute/resource_compute_disk_test.go @@ -4,17 +4,14 @@ package compute_test import ( "fmt" - "net/http" "os" "testing" - "time" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/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" "google.golang.org/api/compute/v1" ) @@ -1419,12 +1416,9 @@ resource "google_compute_disk" "foobar" { } func TestAccComputeDisk_storagePoolSpecified(t *testing.T) { - // Currently failing - acctest.SkipIfVcr(t) t.Parallel() - 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) + storagePoolNameLong := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-throughput") diskName := fmt.Sprintf("tf-test-disk-%s", acctest.RandString(t, 10)) acctest.VcrTest(t, resource.TestCase{ @@ -1432,11 +1426,7 @@ func TestAccComputeDisk_storagePoolSpecified(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), Steps: []resource.TestStep{ { - PreConfig: setupTestingStoragePool(t, storagePoolName), - Config: testAccComputeDisk_storagePoolSpecified(diskName, storagePoolUrl), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("google_compute_disk.foobar", "storage_pool", storagePoolName), - ), + Config: testAccComputeDisk_storagePoolSpecified(diskName, storagePoolNameLong), }, { ResourceName: "google_compute_disk.foobar", @@ -1445,74 +1435,6 @@ func TestAccComputeDisk_storagePoolSpecified(t *testing.T) { }, }, }) - - cleanupTestingStoragePool(t, storagePoolName) -} - -func setupTestingStoragePool(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-throughput", project, 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: 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 cleanupTestingStoragePool(t *testing.T, storagePoolName string) { - config := acctest.GoogleProviderConfig(t) - headers := make(http.Header) - project := envvar.GetTestProjectFromEnv() - zone := envvar.GetTestZoneFromEnv() - url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools/%s", config.ComputeBasePath, project, zone, storagePoolName) - defaultTimeout := 20 * time.Minute - var obj map[string]interface{} - - res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "DELETE", - Project: 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, 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 { diff --git a/google/services/compute/resource_compute_instance_test.go b/google/services/compute/resource_compute_instance_test.go index 98efb586eb1..079e2b6bc74 100644 --- a/google/services/compute/resource_compute_instance_test.go +++ b/google/services/compute/resource_compute_instance_test.go @@ -4,7 +4,6 @@ package compute_test import ( "fmt" - "net/http" "reflect" "regexp" "sort" @@ -20,7 +19,6 @@ 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" "github.com/stretchr/testify/assert" "google.golang.org/api/compute/v1" @@ -8541,24 +8539,17 @@ resource "google_compute_instance" "foobar" { } func TestAccComputeInstance_bootDisk_storagePoolSpecified(t *testing.T) { - // Currently failing - acctest.SkipIfVcr(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) + storagePoolNameLong := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") 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), - ), + Config: testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolNameLong, envvar.GetTestZoneFromEnv()), }, { ResourceName: "google_compute_instance.foobar", @@ -8567,46 +8558,6 @@ func TestAccComputeInstance_bootDisk_storagePoolSpecified(t *testing.T) { }, }, }) - - 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 {