Skip to content

Commit 50ca7b2

Browse files
Support interface for attached_disk (#11286)
1 parent 29a1163 commit 50ca7b2

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_attached_disk.go.erb

+10-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func ResourceComputeAttachedDisk() *schema.Resource {
8787
Description: `The mode in which to attach this disk, either READ_WRITE or READ_ONLY. If not specified, the default is to attach the disk in READ_WRITE mode.`,
8888
ValidateFunc: validation.StringInSlice([]string{"READ_ONLY", "READ_WRITE"}, false),
8989
},
90+
"interface": {
91+
Type: schema.TypeString,
92+
ForceNew: true,
93+
Optional: true,
94+
Description: `The disk interface used for attaching this disk. One of SCSI or NVME. (This field is only used for specific cases, please don't specify this field without advice from Google.)`,
95+
ValidateFunc: validation.StringInSlice([]string{"SCSI", "NVME"}, false),
96+
},
97+
9098
},
9199
UseJSONNumber: true,
92100
}
@@ -121,6 +129,7 @@ func resourceAttachedDiskCreate(d *schema.ResourceData, meta interface{}) error
121129
Source: diskSrc,
122130
Mode: d.Get("mode").(string),
123131
DeviceName: d.Get("device_name").(string),
132+
Interface: d.Get("interface").(string),
124133
}
125134

126135
op, err := config.NewComputeClient(userAgent).Instances.AttachDisk(zv.Project, zv.Zone, zv.Name, &attachedDisk).Do()
@@ -179,7 +188,7 @@ func resourceAttachedDiskRead(d *schema.ResourceData, meta interface{}) error {
179188
}
180189
if err := d.Set("mode", ad.Mode); err != nil {
181190
return fmt.Errorf("Error setting mode: %s", err)
182-
}
191+
}
183192

184193
// Force the referenced resources to a self-link in state because it's more specific then name.
185194
instancePath, err := tpgresource.GetRelativePath(instance.SelfLink)

mmv1/third_party/terraform/services/compute/resource_compute_attached_disk_test.go

+107
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,110 @@ resource "google_compute_attached_disk" "test" {
315315
}
316316
`, diskPrefix, count, instanceName)
317317
}
318+
319+
func TestAccComputeAttachedDisk_diskInterface(t *testing.T) {
320+
t.Parallel()
321+
322+
diskName1 := fmt.Sprintf("tf-test1-%d", acctest.RandInt(t))
323+
diskName2 := fmt.Sprintf("tf-test2-%d", acctest.RandInt(t))
324+
attachedDiskName1 := fmt.Sprintf("tf-test1-%d", acctest.RandInt(t))
325+
attachedDiskName2 := fmt.Sprintf("tf-test2-%d", acctest.RandInt(t))
326+
instanceName1 := fmt.Sprintf("tf-test1-%d", acctest.RandInt(t))
327+
instanceName2 := fmt.Sprintf("tf-test2-%d", acctest.RandInt(t))
328+
importID1 := fmt.Sprintf("%s/us-central1-a/%s/%s", envvar.GetTestProjectFromEnv(), instanceName1, diskName1)
329+
importID2 := fmt.Sprintf("%s/us-central1-a/%s/%s", envvar.GetTestProjectFromEnv(), instanceName2, diskName2)
330+
acctest.VcrTest(t, resource.TestCase{
331+
PreCheck: func() { acctest.AccTestPreCheck(t) },
332+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
333+
CheckDestroy: nil,
334+
Steps: []resource.TestStep{
335+
{
336+
Config: testAttachedDiskResource(diskName1, instanceName1) + testAccComputeAttachedDisk_interface(attachedDiskName1, "SCSI"),
337+
},
338+
{
339+
ResourceName: "google_compute_attached_disk." + attachedDiskName1,
340+
ImportStateId: importID1,
341+
ImportState: true,
342+
ImportStateVerify: false,
343+
},
344+
{
345+
Config: testAttachedDiskResource(diskName1, instanceName1) + testAccComputeAttachedDisk_noInterface(attachedDiskName1),
346+
},
347+
{
348+
ResourceName: "google_compute_attached_disk." + attachedDiskName1,
349+
ImportStateId: importID1,
350+
ImportState: true,
351+
ImportStateVerify: true,
352+
},
353+
{
354+
Config: testAttachedDiskResource(diskName1, instanceName1) + testAccComputeAttachedDisk_interface(attachedDiskName1, "SCSI"),
355+
},
356+
{
357+
ResourceName: "google_compute_attached_disk." + attachedDiskName1,
358+
ImportStateId: importID1,
359+
ImportState: true,
360+
ImportStateVerify: false,
361+
},
362+
// API server will use NVME even SCSI is specified
363+
{
364+
Config: testAttachedDiskResourceWithMachineType(diskName2, instanceName2, "h3-standard-88") + testAccComputeAttachedDisk_interface(attachedDiskName2, "SCSI"),
365+
},
366+
{
367+
ResourceName: "google_compute_attached_disk." + attachedDiskName2,
368+
ImportStateId: importID2,
369+
ImportState: true,
370+
ImportStateVerify: false,
371+
},
372+
},
373+
})
374+
375+
}
376+
377+
func testAccComputeAttachedDisk_interface(resourceName, diskInterface string) string {
378+
return fmt.Sprintf(`
379+
resource "google_compute_attached_disk" "%s" {
380+
disk = google_compute_disk.test1.self_link
381+
instance = google_compute_instance.test.self_link
382+
interface = "%s"
383+
}
384+
`, resourceName, diskInterface)
385+
}
386+
387+
func testAccComputeAttachedDisk_noInterface(resourceName string) string {
388+
return fmt.Sprintf(`
389+
resource "google_compute_attached_disk" "%s" {
390+
disk = google_compute_disk.test1.self_link
391+
instance = google_compute_instance.test.self_link
392+
}
393+
`, resourceName)
394+
}
395+
396+
func testAttachedDiskResourceWithMachineType(diskName, instanceName, machineType string) string {
397+
return fmt.Sprintf(`
398+
resource "google_compute_disk" "test1" {
399+
name = "%s"
400+
zone = "us-central1-a"
401+
type = "hyperdisk-balanced"
402+
}
403+
404+
resource "google_compute_instance" "test" {
405+
name = "%s"
406+
machine_type = "%s"
407+
zone = "us-central1-a"
408+
409+
lifecycle {
410+
ignore_changes = [attached_disk]
411+
}
412+
413+
boot_disk {
414+
initialize_params {
415+
image = "debian-cloud/debian-11"
416+
}
417+
}
418+
419+
network_interface {
420+
network = "default"
421+
}
422+
}
423+
`, diskName, instanceName, machineType)
424+
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ The following arguments are supported:
101101
"READ_ONLY"
102102
"READ_WRITE"
103103

104+
* `interface` -
105+
(Optional)
106+
The disk interface used for attaching this disk.
107+
108+
This field is only used for specific cases, please don't specify
109+
this field without advice from Google. Not specifying the field
110+
will allow the the server to assign the correct interface.
111+
112+
Possible values:
113+
"SCSI"
114+
"NVME"
115+
104116
## Attributes Reference
105117

106118
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)