Skip to content

Commit 047381f

Browse files
Compute: Add storage_pool support to instance template disks (#16571)
1 parent 39afbcf commit 047381f

8 files changed

Lines changed: 190 additions & 0 deletions

mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.tmpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
394394
DiffSuppressFunc: tpgresource.CompareResourceNames,
395395
},
396396
},
397+
"storage_pool": {
398+
Type: schema.TypeString,
399+
Optional: true,
400+
ForceNew: true,
401+
DiffSuppressFunc: tpgresource.CompareResourceNames,
402+
Description: `The self_link or ID of the Storage Pool to create this disk in.`,
403+
},
397404
},
398405
},
399406
},
@@ -1534,6 +1541,15 @@ func buildDisks(d *schema.ResourceData, config *transport_tpg.Config) ([]*comput
15341541
// instance template only supports a resource name here (not uri)
15351542
disk.InitializeParams.ResourcePolicies = expandInstanceTemplateResourcePolicies(d, prefix + ".resource_policies")
15361543
}
1544+
1545+
if v, ok := d.GetOk(prefix + ".storage_pool"); ok {
1546+
// Convert name/partial ID to a full URL
1547+
storagePoolUrl, err := ExpandStoragePoolUrl(v, d, config)
1548+
if err != nil {
1549+
return nil, err
1550+
}
1551+
disk.InitializeParams.StoragePool = storagePoolUrl
1552+
}
15371553
}
15381554

15391555
if v, ok := d.GetOk(prefix + ".interface"); ok {
@@ -1810,6 +1826,9 @@ func flattenDisk(disk *compute.AttachedDisk, configDisk map[string]any, defaultP
18101826
}
18111827
diskMap["resource_policies"] = disk.InitializeParams.ResourcePolicies
18121828
diskMap["resource_manager_tags"] = disk.InitializeParams.ResourceManagerTags
1829+
if disk.InitializeParams.StoragePool != "" {
1830+
diskMap["storage_pool"] = tpgresource.GetResourceNameFromSelfLink(disk.InitializeParams.StoragePool)
1831+
}
18131832
}
18141833

18151834
if disk.DiskEncryptionKey != nil {

mmv1/third_party/terraform/services/compute/resource_compute_instance_template_meta.yaml.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ fields:
8686
api_field: 'properties.disks.initializeParams.sourceSnapshotEncryptionKey.rawKey'
8787
- field: 'disk.source_snapshot_encryption_key.rsa_encrypted_key'
8888
api_field: 'properties.disks.initializeParams.sourceSnapshotEncryptionKey.rsaEncryptedKey'
89+
- field: 'disk.storage_pool'
90+
api_field: 'properties.disks.initializeParams.storagePool'
8991
- field: 'disk.type'
9092
api_field: 'properties.disks.type'
9193
- field: 'effective_labels'

mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.tmpl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,88 @@ resource "google_compute_instance_template" "foobar" {
21682168
`, context)
21692169
}
21702170

2171+
func TestAccComputeInstanceTemplate_storagePool(t *testing.T) {
2172+
t.Parallel()
2173+
2174+
var instanceTemplate compute.InstanceTemplate
2175+
suffix := acctest.RandString(t, 10)
2176+
2177+
acctest.VcrTest(t, resource.TestCase{
2178+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2179+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2180+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
2181+
Steps: []resource.TestStep{
2182+
{
2183+
Config: testAccComputeInstanceTemplate_storagePool(suffix),
2184+
Check: resource.ComposeTestCheckFunc(
2185+
testAccCheckComputeInstanceTemplateExists(
2186+
t, "google_compute_instance_template.foobar", &instanceTemplate),
2187+
// Verify that the storage_pool attribute is set correctly in state
2188+
resource.TestCheckResourceAttr("google_compute_instance_template.foobar", "disk.0.storage_pool", "tf-test-storage-pool-"+suffix),
2189+
// Optional: Verify the API response contains the storage pool
2190+
testAccCheckComputeInstanceTemplateHasDiskStoragePool(&instanceTemplate, "tf-test-storage-pool-"+suffix),
2191+
),
2192+
},
2193+
{
2194+
ResourceName: "google_compute_instance_template.foobar",
2195+
ImportState: true,
2196+
ImportStateVerify: true,
2197+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
2198+
},
2199+
},
2200+
})
2201+
}
2202+
2203+
func testAccCheckComputeInstanceTemplateHasDiskStoragePool(instanceTemplate *compute.InstanceTemplate, poolName string) resource.TestCheckFunc {
2204+
return func(s *terraform.State) error {
2205+
for _, disk := range instanceTemplate.Properties.Disks {
2206+
if disk.InitializeParams != nil && strings.Contains(disk.InitializeParams.StoragePool, poolName) {
2207+
return nil
2208+
}
2209+
}
2210+
return fmt.Errorf("Storage pool %s not found in instance template disks", poolName)
2211+
}
2212+
}
2213+
2214+
func testAccComputeInstanceTemplate_storagePool(suffix string) string {
2215+
return fmt.Sprintf(`
2216+
resource "google_compute_storage_pool" "example" {
2217+
name = "tf-test-storage-pool-%s"
2218+
zone = "us-central1-a"
2219+
pool_provisioned_capacity_gb = 10240
2220+
pool_provisioned_throughput = 1024
2221+
pool_provisioned_iops = 10000
2222+
storage_pool_type = "hyperdisk-balanced"
2223+
# Required for automated tests to clean up successfully
2224+
deletion_protection = false
2225+
}
2226+
2227+
data "google_compute_image" "my_image" {
2228+
family = "debian-11"
2229+
project = "debian-cloud"
2230+
}
2231+
2232+
resource "google_compute_instance_template" "foobar" {
2233+
name = "tf-test-instance-template-%s"
2234+
machine_type = "e2-medium"
2235+
2236+
disk {
2237+
source_image = data.google_compute_image.my_image.self_link
2238+
auto_delete = true
2239+
boot = true
2240+
# Reference the ID (URL format) instead of the name
2241+
storage_pool = google_compute_storage_pool.example.id
2242+
disk_type = "hyperdisk-balanced"
2243+
}
2244+
2245+
network_interface {
2246+
network = "default"
2247+
}
2248+
}
2249+
`, suffix, suffix)
2250+
}
2251+
2252+
21712253
func TestUnitComputeInstanceTemplate_IpCidrRangeDiffSuppress(t *testing.T) {
21722254
cases := map[string]struct {
21732255
Old, New string
@@ -4172,6 +4254,7 @@ data "google_compute_image" "my_image" {
41724254
project = "debian-cloud"
41734255
}
41744256
4257+
41754258
resource "google_compute_instance_template" "foobar" {
41764259
name = "tf-test-instance-template-%s"
41774260
machine_type = "e2-standard-4"

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
364364
DiffSuppressFunc: tpgresource.CompareResourceNames,
365365
},
366366
},
367+
"storage_pool": {
368+
Type: schema.TypeString,
369+
Optional: true,
370+
ForceNew: true,
371+
DiffSuppressFunc: tpgresource.CompareResourceNames,
372+
Description: `The self_link or ID of the Storage Pool to create this disk in.`,
373+
},
367374
},
368375
},
369376
},

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template_meta.yaml.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ fields:
8686
api_field: 'properties.disks.initializeParams.sourceSnapshotEncryptionKey.rawKey'
8787
- field: 'disk.source_snapshot_encryption_key.rsa_encrypted_key'
8888
api_field: 'properties.disks.initializeParams.sourceSnapshotEncryptionKey.rsaEncryptedKey'
89+
- field: 'disk.storage_pool'
90+
api_field: 'properties.disks.initializeParams.storagePool'
8991
- field: 'disk.type'
9092
api_field: 'properties.disks.type'
9193
- field: 'effective_labels'

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template_test.go.tmpl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,72 @@ func testAccCheckComputeRegionInstanceTemplateDestroyProducer(t *testing.T) func
19551955
}
19561956
}
19571957

1958+
func TestAccComputeRegionInstanceTemplate_storagePool(t *testing.T) {
1959+
t.Parallel()
1960+
1961+
var instanceTemplate compute.InstanceTemplate
1962+
suffix := acctest.RandString(t, 10)
1963+
1964+
acctest.VcrTest(t, resource.TestCase{
1965+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1966+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1967+
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
1968+
Steps: []resource.TestStep{
1969+
{
1970+
Config: testAccComputeRegionInstanceTemplate_storagePool(suffix),
1971+
Check: resource.ComposeTestCheckFunc(
1972+
testAccCheckComputeRegionInstanceTemplateExists(
1973+
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
1974+
resource.TestCheckResourceAttr("google_compute_region_instance_template.foobar", "disk.0.storage_pool", "tf-test-storage-pool-"+suffix),
1975+
),
1976+
},
1977+
{
1978+
ResourceName: "google_compute_region_instance_template.foobar",
1979+
ImportState: true,
1980+
ImportStateVerify: true,
1981+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
1982+
},
1983+
},
1984+
})
1985+
}
1986+
1987+
func testAccComputeRegionInstanceTemplate_storagePool(suffix string) string {
1988+
return fmt.Sprintf(`
1989+
resource "google_compute_storage_pool" "example" {
1990+
name = "tf-test-storage-pool-%s"
1991+
zone = "us-central1-a"
1992+
pool_provisioned_capacity_gb = 10240
1993+
pool_provisioned_throughput = 1024
1994+
pool_provisioned_iops = 10000
1995+
storage_pool_type = "hyperdisk-balanced"
1996+
deletion_protection = false
1997+
}
1998+
1999+
data "google_compute_image" "my_image" {
2000+
family = "debian-11"
2001+
project = "debian-cloud"
2002+
}
2003+
2004+
resource "google_compute_region_instance_template" "foobar" {
2005+
name = "tf-test-instance-template-%s"
2006+
region = "us-central1"
2007+
machine_type = "e2-medium"
2008+
2009+
disk {
2010+
source_image = data.google_compute_image.my_image.self_link
2011+
auto_delete = true
2012+
boot = true
2013+
storage_pool = google_compute_storage_pool.example.id
2014+
disk_type = "hyperdisk-balanced"
2015+
}
2016+
2017+
network_interface {
2018+
network = "default"
2019+
}
2020+
}
2021+
`, suffix, suffix)
2022+
}
2023+
19582024
func testAccCheckComputeRegionInstanceTemplateExists(t *testing.T, n string, instanceTemplate interface{}) resource.TestCheckFunc {
19592025
if instanceTemplate == nil {
19602026
panic("Attempted to check existence of Instance template that was nil.")

mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ The following arguments are supported:
516516

517517
* `resource_policies` (Optional) -- A list (short name or id) of resource policies to attach to this disk for automatic snapshot creations. Currently a max of 1 resource policy is supported.
518518

519+
* `storage_pool` - (Optional) The URL of the storage pool in which the new disk is created.
520+
For example:
521+
* `https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/storagePools/{storagePool}`
522+
* `/projects/{project}/zones/{zone}/storagePools/{storagePool}`
523+
519524
<a name="nested_source_image_encryption_key"></a>The `source_image_encryption_key` block supports:
520525

521526
* `raw_key` - (Optional) A 256-bit [customer-supplied encryption key]

mmv1/third_party/terraform/website/docs/r/compute_region_instance_template.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ The following arguments are supported:
481481

482482
* `resource_policies` (Optional) -- A list (short name or id) of resource policies to attach to this disk for automatic snapshot creations. Currently a max of 1 resource policy is supported.
483483

484+
* `storage_pool` - (Optional) The URL of the storage pool in which the new disk is created.
485+
For example:
486+
* `https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/storagePools/{storagePool}`
487+
* `/projects/{project}/zones/{zone}/storagePools/{storagePool}`
488+
489+
484490
<a name="nested_source_image_encryption_key"></a>The `source_image_encryption_key` block supports:
485491

486492
* `raw_key` - (Optional) A 256-bit [customer-supplied encryption key]

0 commit comments

Comments
 (0)