Skip to content

Commit b440381

Browse files
Adds device name parameter to scratch disk (#9069) (#16049)
* Adds device name parameter to scratch disk * Updates instance template test with device name parameter * Updates instance from template tests with device name parameter Signed-off-by: Modular Magician <[email protected]>
1 parent b2c7038 commit b440381

5 files changed

+126
-8
lines changed

.changelog/9069.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `device_name` field to `scratch_disk` block of `google_compute_instance` resource
3+
```

google/services/compute/resource_compute_instance.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ be from 0 to 999,999,999 inclusive.`,
764764
Description: `The scratch disks attached to the instance.`,
765765
Elem: &schema.Resource{
766766
Schema: map[string]*schema.Schema{
767+
"device_name": {
768+
Type: schema.TypeString,
769+
Optional: true,
770+
Computed: true,
771+
Description: `Name with which the attached disk is accessible under /dev/disk/by-id/`,
772+
},
767773
"interface": {
768774
Type: schema.TypeString,
769775
Required: true,
@@ -2684,6 +2690,7 @@ func expandScratchDisks(d *schema.ResourceData, config *transport_tpg.Config, pr
26842690
scratchDisks = append(scratchDisks, &compute.AttachedDisk{
26852691
AutoDelete: true,
26862692
Type: "SCRATCH",
2693+
DeviceName: d.Get(fmt.Sprintf("scratch_disk.%d.device_name", i)).(string),
26872694
Interface: d.Get(fmt.Sprintf("scratch_disk.%d.interface", i)).(string),
26882695
DiskSizeGb: int64(d.Get(fmt.Sprintf("scratch_disk.%d.size", i)).(int)),
26892696
InitializeParams: &compute.AttachedDiskInitializeParams{
@@ -2697,8 +2704,9 @@ func expandScratchDisks(d *schema.ResourceData, config *transport_tpg.Config, pr
26972704

26982705
func flattenScratchDisk(disk *compute.AttachedDisk) map[string]interface{} {
26992706
result := map[string]interface{}{
2700-
"interface": disk.Interface,
2701-
"size": disk.DiskSizeGb,
2707+
"device_name": disk.DeviceName,
2708+
"interface": disk.Interface,
2709+
"size": disk.DiskSizeGb,
27022710
}
27032711
return result
27042712
}

google/services/compute/resource_compute_instance_from_template_test.go

+50-1
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ func TestAccComputeInstanceFromTemplate_overrideScratchDisk(t *testing.T) {
209209
testAccCheckComputeInstanceExists(t, resourceName, &instance),
210210

211211
// Check that fields were set based on the template
212-
resource.TestCheckResourceAttr(resourceName, "scratch_disk.#", "1"),
212+
resource.TestCheckResourceAttr(resourceName, "scratch_disk.#", "2"),
213213
resource.TestCheckResourceAttr(resourceName, "scratch_disk.0.interface", "NVME"),
214+
resource.TestCheckResourceAttr(resourceName, "scratch_disk.1.interface", "NVME"),
215+
resource.TestCheckResourceAttr(resourceName, "scratch_disk.1.device_name", "override-local-ssd"),
214216
),
215217
},
216218
},
@@ -359,6 +361,14 @@ resource "google_compute_instance_template" "foobar" {
359361
disk_size_gb = 375
360362
}
361363
364+
disk {
365+
device_name = "test-local-ssd"
366+
disk_type = "local-ssd"
367+
type = "SCRATCH"
368+
interface = "NVME"
369+
disk_size_gb = 375
370+
}
371+
362372
disk {
363373
source_image = data.google_compute_image.my_image.self_link
364374
auto_delete = true
@@ -433,6 +443,14 @@ resource "google_compute_instance_template" "foobar" {
433443
disk_size_gb = 375
434444
}
435445
446+
disk {
447+
device_name = "test-local-ssd"
448+
disk_type = "local-ssd"
449+
type = "SCRATCH"
450+
interface = "NVME"
451+
disk_size_gb = 375
452+
}
453+
436454
disk {
437455
source_image = data.google_compute_image.my_image.self_link
438456
auto_delete = true
@@ -511,6 +529,14 @@ resource "google_compute_instance_template" "foobar" {
511529
disk_size_gb = 375
512530
}
513531
532+
disk {
533+
device_name = "test-local-ssd"
534+
disk_type = "local-ssd"
535+
type = "SCRATCH"
536+
interface = "NVME"
537+
disk_size_gb = 375
538+
}
539+
514540
disk {
515541
source_image = data.google_compute_image.my_image.self_link
516542
auto_delete = true
@@ -593,6 +619,14 @@ resource "google_compute_instance_template" "foobar" {
593619
disk_size_gb = 375
594620
}
595621
622+
disk {
623+
device_name = "test-local-ssd"
624+
disk_type = "local-ssd"
625+
type = "SCRATCH"
626+
interface = "NVME"
627+
disk_size_gb = 375
628+
}
629+
596630
disk {
597631
source_image = data.google_compute_image.my_image.self_link
598632
auto_delete = true
@@ -808,6 +842,16 @@ resource "google_compute_instance_template" "template" {
808842
boot = false
809843
}
810844
845+
disk {
846+
device_name = "test-local-ssd"
847+
type = "SCRATCH"
848+
disk_type = "local-ssd"
849+
disk_size_gb = 375
850+
interface = "SCSI"
851+
auto_delete = true
852+
boot = false
853+
}
854+
811855
network_interface {
812856
network = "default"
813857
}
@@ -823,6 +867,11 @@ resource "google_compute_instance_from_template" "inst" {
823867
scratch_disk {
824868
interface = "NVME"
825869
}
870+
871+
scratch_disk {
872+
device_name = "override-local-ssd"
873+
interface = "NVME"
874+
}
826875
}
827876
`, templateDisk, overrideDisk, template, instance)
828877
}

google/services/compute/resource_compute_instance_template_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ data "google_compute_image" "my_image" {
18971897
}
18981898
resource "google_compute_instance_template" "foobar" {
18991899
name = "tf-test-instance-template-%s"
1900-
machine_type = "e2-medium"
1900+
machine_type = "n1-standard-1" // can't be e2 because of local-ssd
19011901
can_ip_forward = false
19021902
disk {
19031903
source_image = data.google_compute_image.my_image.name
@@ -1910,6 +1910,13 @@ resource "google_compute_instance_template" "foobar" {
19101910
type = "SCRATCH"
19111911
disk_type = "local-ssd"
19121912
}
1913+
disk {
1914+
auto_delete = true
1915+
device_name = "test-local-ssd"
1916+
disk_size_gb = 375
1917+
type = "SCRATCH"
1918+
disk_type = "local-ssd"
1919+
}
19131920
network_interface {
19141921
network = "default"
19151922
}

google/services/compute/resource_compute_instance_test.go

+55-4
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,22 @@ func TestAccComputeInstance_with375GbScratchDisk(t *testing.T) {
835835
Check: resource.ComposeTestCheckFunc(
836836
testAccCheckComputeInstanceExists(
837837
t, "google_compute_instance.foobar", &instance),
838-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "SCSI"}),
838+
testAccCheckComputeInstanceScratchDisk(&instance, []map[string]string{
839+
{
840+
"interface": "NVME",
841+
},
842+
{
843+
"interface": "SCSI",
844+
},
845+
{
846+
"interface": "NVME",
847+
"deviceName": "nvme-local-ssd",
848+
},
849+
{
850+
"interface": "SCSI",
851+
"deviceName": "scsi-local-ssd",
852+
},
853+
}),
839854
),
840855
},
841856
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
@@ -862,7 +877,26 @@ func TestAccComputeInstance_with18TbScratchDisk(t *testing.T) {
862877
Check: resource.ComposeTestCheckFunc(
863878
testAccCheckComputeInstanceExists(
864879
t, "google_compute_instance.foobar", &instance),
865-
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "NVME", "NVME", "NVME", "NVME", "NVME"}),
880+
testAccCheckComputeInstanceScratchDisk(&instance, []map[string]string{
881+
{
882+
"interface": "NVME",
883+
},
884+
{
885+
"interface": "NVME",
886+
},
887+
{
888+
"interface": "NVME",
889+
},
890+
{
891+
"interface": "NVME",
892+
},
893+
{
894+
"interface": "NVME",
895+
},
896+
{
897+
"interface": "NVME",
898+
},
899+
}),
866900
),
867901
},
868902
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
@@ -2820,7 +2854,7 @@ func testAccCheckComputeInstanceBootDiskType(t *testing.T, instanceName string,
28202854
}
28212855
}
28222856

2823-
func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfaces []string) resource.TestCheckFunc {
2857+
func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfaces []map[string]string) resource.TestCheckFunc {
28242858
return func(s *terraform.State) error {
28252859
if instance.Disks == nil {
28262860
return fmt.Errorf("no disks")
@@ -2832,10 +2866,17 @@ func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfac
28322866
if i >= len(interfaces) {
28332867
return fmt.Errorf("Expected %d scratch disks, found more", len(interfaces))
28342868
}
2835-
if disk.Interface != interfaces[i] {
2869+
if disk.Interface != interfaces[i]["interface"] {
28362870
return fmt.Errorf("Mismatched interface on scratch disk #%d, expected: %q, found: %q",
28372871
i, interfaces[i], disk.Interface)
28382872
}
2873+
if deviceName, ok := interfaces[i]["deviceName"]; ok {
2874+
if disk.DeviceName != deviceName {
2875+
return fmt.Errorf("Mismatched device name on scratch disk #%d, expected: %q, found: %q",
2876+
i, deviceName, disk.DeviceName)
2877+
}
2878+
}
2879+
28392880
i++
28402881
}
28412882
}
@@ -4729,6 +4770,16 @@ resource "google_compute_instance" "foobar" {
47294770
interface = "SCSI"
47304771
}
47314772
4773+
scratch_disk {
4774+
interface = "NVME"
4775+
device_name = "nvme-local-ssd"
4776+
}
4777+
4778+
scratch_disk {
4779+
interface = "SCSI"
4780+
device_name = "scsi-local-ssd"
4781+
}
4782+
47324783
network_interface {
47334784
network = "default"
47344785
}

0 commit comments

Comments
 (0)