From e61f77dab9ba22ba0290ecc5ead7f9edaa0dc508 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 28 Sep 2023 18:47:34 -0400 Subject: [PATCH] Adds device name parameter to scratch disk (#9069) (#6401) * 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 --- .changelog/9069.txt | 3 + .../compute/resource_compute_instance.go | 12 +++- ...rce_compute_instance_from_template_test.go | 59 ++++++++++++++++++- ...resource_compute_instance_template_test.go | 9 ++- .../compute/resource_compute_instance_test.go | 59 +++++++++++++++++-- 5 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 .changelog/9069.txt diff --git a/.changelog/9069.txt b/.changelog/9069.txt new file mode 100644 index 0000000000..d318394c6d --- /dev/null +++ b/.changelog/9069.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `device_name` field to `scratch_disk` block of `google_compute_instance` resource +``` diff --git a/google-beta/services/compute/resource_compute_instance.go b/google-beta/services/compute/resource_compute_instance.go index dc94adb1b5..f17518cd22 100644 --- a/google-beta/services/compute/resource_compute_instance.go +++ b/google-beta/services/compute/resource_compute_instance.go @@ -824,6 +824,12 @@ be from 0 to 999,999,999 inclusive.`, Description: `The scratch disks attached to the instance.`, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "device_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: `Name with which the attached disk is accessible under /dev/disk/by-id/`, + }, "interface": { Type: schema.TypeString, Required: true, @@ -2790,6 +2796,7 @@ func expandScratchDisks(d *schema.ResourceData, config *transport_tpg.Config, pr scratchDisks = append(scratchDisks, &compute.AttachedDisk{ AutoDelete: true, Type: "SCRATCH", + DeviceName: d.Get(fmt.Sprintf("scratch_disk.%d.device_name", i)).(string), Interface: d.Get(fmt.Sprintf("scratch_disk.%d.interface", i)).(string), DiskSizeGb: int64(d.Get(fmt.Sprintf("scratch_disk.%d.size", i)).(int)), InitializeParams: &compute.AttachedDiskInitializeParams{ @@ -2803,8 +2810,9 @@ func expandScratchDisks(d *schema.ResourceData, config *transport_tpg.Config, pr func flattenScratchDisk(disk *compute.AttachedDisk) map[string]interface{} { result := map[string]interface{}{ - "interface": disk.Interface, - "size": disk.DiskSizeGb, + "device_name": disk.DeviceName, + "interface": disk.Interface, + "size": disk.DiskSizeGb, } return result } diff --git a/google-beta/services/compute/resource_compute_instance_from_template_test.go b/google-beta/services/compute/resource_compute_instance_from_template_test.go index e86fe8ff87..046554746f 100644 --- a/google-beta/services/compute/resource_compute_instance_from_template_test.go +++ b/google-beta/services/compute/resource_compute_instance_from_template_test.go @@ -237,8 +237,10 @@ func TestAccComputeInstanceFromTemplate_overrideScratchDisk(t *testing.T) { testAccCheckComputeInstanceExists(t, resourceName, &instance), // Check that fields were set based on the template - resource.TestCheckResourceAttr(resourceName, "scratch_disk.#", "1"), + resource.TestCheckResourceAttr(resourceName, "scratch_disk.#", "2"), resource.TestCheckResourceAttr(resourceName, "scratch_disk.0.interface", "NVME"), + resource.TestCheckResourceAttr(resourceName, "scratch_disk.1.interface", "NVME"), + resource.TestCheckResourceAttr(resourceName, "scratch_disk.1.device_name", "override-local-ssd"), ), }, }, @@ -387,6 +389,14 @@ resource "google_compute_instance_template" "foobar" { disk_size_gb = 375 } + disk { + device_name = "test-local-ssd" + disk_type = "local-ssd" + type = "SCRATCH" + interface = "NVME" + disk_size_gb = 375 + } + disk { source_image = data.google_compute_image.my_image.self_link auto_delete = true @@ -461,6 +471,14 @@ resource "google_compute_instance_template" "foobar" { disk_size_gb = 375 } + disk { + device_name = "test-local-ssd" + disk_type = "local-ssd" + type = "SCRATCH" + interface = "NVME" + disk_size_gb = 375 + } + disk { source_image = data.google_compute_image.my_image.self_link auto_delete = true @@ -539,6 +557,14 @@ resource "google_compute_instance_template" "foobar" { disk_size_gb = 375 } + disk { + device_name = "test-local-ssd" + disk_type = "local-ssd" + type = "SCRATCH" + interface = "NVME" + disk_size_gb = 375 + } + disk { source_image = data.google_compute_image.my_image.self_link auto_delete = true @@ -631,6 +657,14 @@ resource "google_compute_region_instance_template" "foobar" { disk_size_gb = 375 } + disk { + device_name = "test-local-ssd" + disk_type = "local-ssd" + type = "SCRATCH" + interface = "NVME" + disk_size_gb = 375 + } + network_interface { network = "default" } @@ -696,6 +730,14 @@ resource "google_compute_instance_template" "foobar" { disk_size_gb = 375 } + disk { + device_name = "test-local-ssd" + disk_type = "local-ssd" + type = "SCRATCH" + interface = "NVME" + disk_size_gb = 375 + } + disk { source_image = data.google_compute_image.my_image.self_link auto_delete = true @@ -911,6 +953,16 @@ resource "google_compute_instance_template" "template" { boot = false } + disk { + device_name = "test-local-ssd" + type = "SCRATCH" + disk_type = "local-ssd" + disk_size_gb = 375 + interface = "SCSI" + auto_delete = true + boot = false + } + network_interface { network = "default" } @@ -926,6 +978,11 @@ resource "google_compute_instance_from_template" "inst" { scratch_disk { interface = "NVME" } + + scratch_disk { + device_name = "override-local-ssd" + interface = "NVME" + } } `, templateDisk, overrideDisk, template, instance) } diff --git a/google-beta/services/compute/resource_compute_instance_template_test.go b/google-beta/services/compute/resource_compute_instance_template_test.go index cad7839965..fe23a77399 100644 --- a/google-beta/services/compute/resource_compute_instance_template_test.go +++ b/google-beta/services/compute/resource_compute_instance_template_test.go @@ -2102,7 +2102,7 @@ data "google_compute_image" "my_image" { } resource "google_compute_instance_template" "foobar" { name = "tf-test-instance-template-%s" - machine_type = "e2-medium" + machine_type = "n1-standard-1" // can't be e2 because of local-ssd can_ip_forward = false disk { source_image = data.google_compute_image.my_image.name @@ -2115,6 +2115,13 @@ resource "google_compute_instance_template" "foobar" { type = "SCRATCH" disk_type = "local-ssd" } + disk { + auto_delete = true + device_name = "test-local-ssd" + disk_size_gb = 375 + type = "SCRATCH" + disk_type = "local-ssd" + } network_interface { network = "default" } diff --git a/google-beta/services/compute/resource_compute_instance_test.go b/google-beta/services/compute/resource_compute_instance_test.go index 43c8244325..bf719f552e 100644 --- a/google-beta/services/compute/resource_compute_instance_test.go +++ b/google-beta/services/compute/resource_compute_instance_test.go @@ -835,7 +835,22 @@ func TestAccComputeInstance_with375GbScratchDisk(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( t, "google_compute_instance.foobar", &instance), - testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "SCSI"}), + testAccCheckComputeInstanceScratchDisk(&instance, []map[string]string{ + { + "interface": "NVME", + }, + { + "interface": "SCSI", + }, + { + "interface": "NVME", + "deviceName": "nvme-local-ssd", + }, + { + "interface": "SCSI", + "deviceName": "scsi-local-ssd", + }, + }), ), }, computeInstanceImportStep("us-central1-a", instanceName, []string{}), @@ -862,7 +877,26 @@ func TestAccComputeInstance_with18TbScratchDisk(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( t, "google_compute_instance.foobar", &instance), - testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "NVME", "NVME", "NVME", "NVME", "NVME"}), + testAccCheckComputeInstanceScratchDisk(&instance, []map[string]string{ + { + "interface": "NVME", + }, + { + "interface": "NVME", + }, + { + "interface": "NVME", + }, + { + "interface": "NVME", + }, + { + "interface": "NVME", + }, + { + "interface": "NVME", + }, + }), ), }, computeInstanceImportStep("us-central1-a", instanceName, []string{}), @@ -3295,7 +3329,7 @@ func testAccCheckComputeInstanceBootDiskType(t *testing.T, instanceName string, } } -func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfaces []string) resource.TestCheckFunc { +func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfaces []map[string]string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Disks == nil { return fmt.Errorf("no disks") @@ -3307,10 +3341,17 @@ func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfac if i >= len(interfaces) { return fmt.Errorf("Expected %d scratch disks, found more", len(interfaces)) } - if disk.Interface != interfaces[i] { + if disk.Interface != interfaces[i]["interface"] { return fmt.Errorf("Mismatched interface on scratch disk #%d, expected: %q, found: %q", i, interfaces[i], disk.Interface) } + if deviceName, ok := interfaces[i]["deviceName"]; ok { + if disk.DeviceName != deviceName { + return fmt.Errorf("Mismatched device name on scratch disk #%d, expected: %q, found: %q", + i, deviceName, disk.DeviceName) + } + } + i++ } } @@ -5256,6 +5297,16 @@ resource "google_compute_instance" "foobar" { interface = "SCSI" } + scratch_disk { + interface = "NVME" + device_name = "nvme-local-ssd" + } + + scratch_disk { + interface = "SCSI" + device_name = "scsi-local-ssd" + } + network_interface { network = "default" }