Skip to content

Commit 6e6975e

Browse files
Enable 3000 GB Local SSDs in Terraform validation. (#7469) (#14061)
Signed-off-by: Modular Magician <[email protected]>
1 parent 16dfaaf commit 6e6975e

5 files changed

+214
-11
lines changed

.changelog/7469.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: changed `disk_size_gb` allowable sizes for SCRATCH disks from 375 GB to [375, 3000] GB in `google_compute_instance_template`
3+
```

google/resource_compute_instance.go

+10
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,14 @@ func ResourceComputeInstance() *schema.Resource {
635635
ValidateFunc: validation.StringInSlice([]string{"SCSI", "NVME"}, false),
636636
Description: `The disk interface used for attaching this disk. One of SCSI or NVME.`,
637637
},
638+
"size": {
639+
Type: schema.TypeInt,
640+
Optional: true,
641+
ForceNew: true,
642+
ValidateFunc: validation.IntAtLeast(375),
643+
Default: 375,
644+
Description: `The size of the disk in gigabytes. One of 375 or 3000.`,
645+
},
638646
},
639647
},
640648
},
@@ -2455,6 +2463,7 @@ func expandScratchDisks(d *schema.ResourceData, config *Config, project string)
24552463
AutoDelete: true,
24562464
Type: "SCRATCH",
24572465
Interface: d.Get(fmt.Sprintf("scratch_disk.%d.interface", i)).(string),
2466+
DiskSizeGb: int64(d.Get(fmt.Sprintf("scratch_disk.%d.size", i)).(int)),
24582467
InitializeParams: &compute.AttachedDiskInitializeParams{
24592468
DiskType: diskType.RelativeLink(),
24602469
},
@@ -2467,6 +2476,7 @@ func expandScratchDisks(d *schema.ResourceData, config *Config, project string)
24672476
func flattenScratchDisk(disk *compute.AttachedDisk) map[string]interface{} {
24682477
result := map[string]interface{}{
24692478
"interface": disk.Interface,
2479+
"size": disk.DiskSizeGb,
24702480
}
24712481
return result
24722482
}

google/resource_compute_instance_template.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ var (
3434
}
3535
)
3636

37-
var REQUIRED_SCRATCH_DISK_SIZE_GB = 375
37+
var DEFAULT_SCRATCH_DISK_SIZE_GB = 375
38+
var VALID_SCRATCH_DISK_SIZES_GB [2]int = [2]int{375, 3000}
3839

3940
func ResourceComputeInstanceTemplate() *schema.Resource {
4041
return &schema.Resource{
@@ -132,7 +133,7 @@ func ResourceComputeInstanceTemplate() *schema.Resource {
132133
Optional: true,
133134
ForceNew: true,
134135
Computed: true,
135-
Description: `The size of the image in gigabytes. If not specified, it will inherit the size of its base image. For SCRATCH disks, the size must be exactly 375GB.`,
136+
Description: `The size of the image in gigabytes. If not specified, it will inherit the size of its base image. For SCRATCH disks, the size must be one of 375 or 3000 GB, with a default of 375 GB.`,
136137
},
137138

138139
"disk_type": {
@@ -902,8 +903,13 @@ func resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(diff TerraformR
902903
}
903904

904905
diskSize := diff.Get(fmt.Sprintf("disk.%d.disk_size_gb", i)).(int)
905-
if typee == "SCRATCH" && diskSize != REQUIRED_SCRATCH_DISK_SIZE_GB {
906-
return fmt.Errorf("SCRATCH disks must be exactly %dGB, disk %d is %d", REQUIRED_SCRATCH_DISK_SIZE_GB, i, diskSize)
906+
if typee == "SCRATCH" && !(diskSize == 375 || diskSize == 3000) { // see VALID_SCRATCH_DISK_SIZES_GB
907+
return fmt.Errorf("SCRATCH disks must be one of %v GB, disk %d is %d", VALID_SCRATCH_DISK_SIZES_GB, i, diskSize)
908+
}
909+
910+
interfacee := diff.Get(fmt.Sprintf("disk.%d.interface", i)).(string)
911+
if typee == "SCRATCH" && diskSize == 3000 && interfacee != "NVME" {
912+
return fmt.Errorf("SCRATCH disks with a size of 3000 GB must have an interface of NVME. disk %d has interface %s", i, interfacee)
907913
}
908914
}
909915

@@ -1234,7 +1240,7 @@ func flattenDisk(disk *compute.AttachedDisk, configDisk map[string]any, defaultP
12341240
// The API does not return a disk size value for scratch disks. They can only be one size,
12351241
// so we can assume that size here.
12361242
if disk.InitializeParams.DiskSizeGb == 0 && disk.Type == "SCRATCH" {
1237-
diskMap["disk_size_gb"] = REQUIRED_SCRATCH_DISK_SIZE_GB
1243+
diskMap["disk_size_gb"] = DEFAULT_SCRATCH_DISK_SIZE_GB
12381244
} else {
12391245
diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb
12401246
}

google/resource_compute_instance_template_test.go

+106-3
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,42 @@ func TestComputeInstanceTemplate_scratchDiskSizeCustomizeDiff(t *testing.T) {
167167
Typee string // misspelled on purpose, type is a special symbol
168168
DiskType string
169169
DiskSize int
170+
Interfacee string
170171
ExpectError bool
171172
}{
172-
"scratch disk correct size": {
173+
"scratch disk correct size 1": {
173174
Typee: "SCRATCH",
174175
DiskType: "local-ssd",
175176
DiskSize: 375,
177+
Interfacee: "NVME",
178+
ExpectError: false,
179+
},
180+
"scratch disk correct size 2": {
181+
Typee: "SCRATCH",
182+
DiskType: "local-ssd",
183+
DiskSize: 3000,
184+
Interfacee: "NVME",
176185
ExpectError: false,
177186
},
178187
"scratch disk incorrect size": {
179188
Typee: "SCRATCH",
180189
DiskType: "local-ssd",
181190
DiskSize: 300,
191+
Interfacee: "NVME",
192+
ExpectError: true,
193+
},
194+
"scratch disk incorrect interface": {
195+
Typee: "SCRATCH",
196+
DiskType: "local-ssd",
197+
DiskSize: 3000,
198+
Interfacee: "SCSI",
182199
ExpectError: true,
183200
},
184201
"non-scratch disk": {
185202
Typee: "PERSISTENT",
186203
DiskType: "",
187204
DiskSize: 300,
205+
Interfacee: "NVME",
188206
ExpectError: false,
189207
},
190208
}
@@ -196,6 +214,7 @@ func TestComputeInstanceTemplate_scratchDiskSizeCustomizeDiff(t *testing.T) {
196214
"disk.0.type": tc.Typee,
197215
"disk.0.disk_type": tc.DiskType,
198216
"disk.0.disk_size_gb": tc.DiskSize,
217+
"disk.0.interface": tc.Interfacee,
199218
},
200219
}
201220
err := resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(d)
@@ -953,7 +972,27 @@ func TestAccComputeInstanceTemplate_withScratchDisk(t *testing.T) {
953972
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
954973
Steps: []resource.TestStep{
955974
{
956-
Config: testAccComputeInstanceTemplate_withScratchDisk(RandString(t, 10)),
975+
Config: testAccComputeInstanceTemplate_with375GbScratchDisk(RandString(t, 10)),
976+
},
977+
{
978+
ResourceName: "google_compute_instance_template.foobar",
979+
ImportState: true,
980+
ImportStateVerify: true,
981+
ImportStateVerifyIgnore: []string{"name_prefix"},
982+
},
983+
},
984+
})
985+
}
986+
987+
func TestAccComputeInstanceTemplate_with18TbScratchDisk(t *testing.T) {
988+
t.Parallel()
989+
990+
VcrTest(t, resource.TestCase{
991+
PreCheck: func() { testAccPreCheck(t) },
992+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
993+
Steps: []resource.TestStep{
994+
{
995+
Config: testAccComputeInstanceTemplate_with18TbScratchDisk(RandString(t, 10)),
957996
},
958997
{
959998
ResourceName: "google_compute_instance_template.foobar",
@@ -1977,7 +2016,7 @@ resource "google_compute_instance_template" "foobar" {
19772016
`, suffix, suffix)
19782017
}
19792018

1980-
func testAccComputeInstanceTemplate_withScratchDisk(suffix string) string {
2019+
func testAccComputeInstanceTemplate_with375GbScratchDisk(suffix string) string {
19812020
return fmt.Sprintf(`
19822021
data "google_compute_image" "my_image" {
19832022
family = "centos-7"
@@ -2005,6 +2044,70 @@ resource "google_compute_instance_template" "foobar" {
20052044
`, suffix)
20062045
}
20072046

2047+
func testAccComputeInstanceTemplate_with18TbScratchDisk(suffix string) string {
2048+
return fmt.Sprintf(`
2049+
data "google_compute_image" "my_image" {
2050+
family = "centos-7"
2051+
project = "centos-cloud"
2052+
}
2053+
2054+
resource "google_compute_instance_template" "foobar" {
2055+
name = "tf-test-instance-template-%s"
2056+
machine_type = "n2-standard-16"
2057+
can_ip_forward = false
2058+
disk {
2059+
source_image = data.google_compute_image.my_image.name
2060+
auto_delete = true
2061+
boot = true
2062+
}
2063+
disk {
2064+
auto_delete = true
2065+
disk_size_gb = 3000
2066+
type = "SCRATCH"
2067+
disk_type = "local-ssd"
2068+
interface = "NVME"
2069+
}
2070+
disk {
2071+
auto_delete = true
2072+
disk_size_gb = 3000
2073+
type = "SCRATCH"
2074+
disk_type = "local-ssd"
2075+
interface = "NVME"
2076+
}
2077+
disk {
2078+
auto_delete = true
2079+
disk_size_gb = 3000
2080+
type = "SCRATCH"
2081+
disk_type = "local-ssd"
2082+
interface = "NVME"
2083+
}
2084+
disk {
2085+
auto_delete = true
2086+
disk_size_gb = 3000
2087+
type = "SCRATCH"
2088+
disk_type = "local-ssd"
2089+
interface = "NVME"
2090+
}
2091+
disk {
2092+
auto_delete = true
2093+
disk_size_gb = 3000
2094+
type = "SCRATCH"
2095+
disk_type = "local-ssd"
2096+
interface = "NVME"
2097+
}
2098+
disk {
2099+
auto_delete = true
2100+
disk_size_gb = 3000
2101+
type = "SCRATCH"
2102+
disk_type = "local-ssd"
2103+
interface = "NVME"
2104+
}
2105+
network_interface {
2106+
network = "default"
2107+
}
2108+
}`, suffix)
2109+
}
2110+
20082111
func testAccComputeInstanceTemplate_regionDisks(suffix string) string {
20092112
return fmt.Sprintf(`
20102113
data "google_compute_image" "my_image" {

google/resource_compute_instance_test.go

+84-3
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ func TestAccComputeInstance_bootDisk_mode(t *testing.T) {
721721
})
722722
}
723723

724-
func TestAccComputeInstance_scratchDisk(t *testing.T) {
724+
func TestAccComputeInstance_with375GbScratchDisk(t *testing.T) {
725725
t.Parallel()
726726

727727
var instance compute.Instance
@@ -733,7 +733,7 @@ func TestAccComputeInstance_scratchDisk(t *testing.T) {
733733
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
734734
Steps: []resource.TestStep{
735735
{
736-
Config: testAccComputeInstance_scratchDisk(instanceName),
736+
Config: testAccComputeInstance_with375GbScratchDisk(instanceName),
737737
Check: resource.ComposeTestCheckFunc(
738738
testAccCheckComputeInstanceExists(
739739
t, "google_compute_instance.foobar", &instance),
@@ -745,6 +745,33 @@ func TestAccComputeInstance_scratchDisk(t *testing.T) {
745745
})
746746
}
747747

748+
func TestAccComputeInstance_with18TbScratchDisk(t *testing.T) {
749+
// Skip this test until the quota for the GitHub presubmit GCP project is increased
750+
// to handle the size of the resource this test spins up.
751+
t.Skip()
752+
t.Parallel()
753+
754+
var instance compute.Instance
755+
var instanceName = fmt.Sprintf("tf-test-%s", RandString(t, 10))
756+
757+
VcrTest(t, resource.TestCase{
758+
PreCheck: func() { testAccPreCheck(t) },
759+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
760+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
761+
Steps: []resource.TestStep{
762+
{
763+
Config: testAccComputeInstance_with18TbScratchDisk(instanceName),
764+
Check: resource.ComposeTestCheckFunc(
765+
testAccCheckComputeInstanceExists(
766+
t, "google_compute_instance.foobar", &instance),
767+
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "NVME", "NVME", "NVME", "NVME", "NVME"}),
768+
),
769+
},
770+
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
771+
},
772+
})
773+
}
774+
748775
func TestAccComputeInstance_forceNewAndChangeMetadata(t *testing.T) {
749776
t.Parallel()
750777

@@ -4389,7 +4416,7 @@ resource "google_compute_instance" "foobar" {
43894416
`, instance, diskMode)
43904417
}
43914418

4392-
func testAccComputeInstance_scratchDisk(instance string) string {
4419+
func testAccComputeInstance_with375GbScratchDisk(instance string) string {
43934420
return fmt.Sprintf(`
43944421
data "google_compute_image" "my_image" {
43954422
family = "debian-11"
@@ -4422,6 +4449,60 @@ resource "google_compute_instance" "foobar" {
44224449
`, instance)
44234450
}
44244451

4452+
func testAccComputeInstance_with18TbScratchDisk(instance string) string {
4453+
return fmt.Sprintf(`
4454+
data "google_compute_image" "my_image" {
4455+
family = "debian-11"
4456+
project = "debian-cloud"
4457+
}
4458+
4459+
resource "google_compute_instance" "foobar" {
4460+
name = "%s"
4461+
machine_type = "n2-standard-64" // must be a large n2 to be paired with 18Tb local-ssd
4462+
zone = "us-central1-a"
4463+
4464+
boot_disk {
4465+
initialize_params {
4466+
image = data.google_compute_image.my_image.self_link
4467+
}
4468+
}
4469+
4470+
scratch_disk {
4471+
interface = "NVME"
4472+
size = 3000
4473+
}
4474+
4475+
scratch_disk {
4476+
interface = "NVME"
4477+
size = 3000
4478+
}
4479+
4480+
scratch_disk {
4481+
interface = "NVME"
4482+
size = 3000
4483+
}
4484+
4485+
scratch_disk {
4486+
interface = "NVME"
4487+
size = 3000
4488+
}
4489+
4490+
scratch_disk {
4491+
interface = "NVME"
4492+
size = 3000
4493+
}
4494+
4495+
scratch_disk {
4496+
interface = "NVME"
4497+
size = 3000
4498+
}
4499+
4500+
network_interface {
4501+
network = "default"
4502+
}
4503+
}`, instance)
4504+
}
4505+
44254506
func testAccComputeInstance_serviceAccount(instance string) string {
44264507
return fmt.Sprintf(`
44274508
data "google_compute_image" "my_image" {

0 commit comments

Comments
 (0)