Skip to content

Commit

Permalink
Adds device name parameter to scratch disk (#9069) (#6401)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
modular-magician authored Sep 28, 2023
1 parent ad72289 commit e61f77d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/9069.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `device_name` field to `scratch_disk` block of `google_compute_instance` resource
```
12 changes: 10 additions & 2 deletions google-beta/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand Down
59 changes: 55 additions & 4 deletions google-beta/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand All @@ -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{}),
Expand Down Expand Up @@ -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")
Expand All @@ -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++
}
}
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit e61f77d

Please sign in to comment.