Skip to content

Commit 8084671

Browse files
authored
add mode option to google compute instance boot disk
2 parents 42eb1a4 + d12c5b3 commit 8084671

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

google/resource_compute_instance.go

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ func resourceComputeInstance() *schema.Resource {
126126
},
127127
},
128128

129+
"mode": {
130+
Type: schema.TypeString,
131+
Optional: true,
132+
ForceNew: true,
133+
Default: "READ_WRITE",
134+
ValidateFunc: validation.StringInSlice([]string{"READ_WRITE", "READ_ONLY"}, false),
135+
},
136+
129137
"source": {
130138
Type: schema.TypeString,
131139
Optional: true,
@@ -1664,13 +1672,18 @@ func expandBootDisk(d *schema.ResourceData, config *Config, project string) (*co
16641672
}
16651673
}
16661674

1675+
if v, ok := d.GetOk("boot_disk.0.mode"); ok {
1676+
disk.Mode = v.(string)
1677+
}
1678+
16671679
return disk, nil
16681680
}
16691681

16701682
func flattenBootDisk(d *schema.ResourceData, disk *computeBeta.AttachedDisk, config *Config) []map[string]interface{} {
16711683
result := map[string]interface{}{
16721684
"auto_delete": disk.AutoDelete,
16731685
"device_name": disk.DeviceName,
1686+
"mode": disk.Mode,
16741687
"source": ConvertSelfLinkToV1(disk.Source),
16751688
// disk_encryption_key_raw is not returned from the API, so copy it from what the user
16761689
// originally specified to avoid diffs.

google/resource_compute_instance_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,25 @@ func TestAccComputeInstance_bootDisk_type(t *testing.T) {
522522
})
523523
}
524524

525+
func TestAccComputeInstance_bootDisk_mode(t *testing.T) {
526+
t.Parallel()
527+
528+
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
529+
var diskMode = "READ_WRITE"
530+
531+
resource.Test(t, resource.TestCase{
532+
PreCheck: func() { testAccPreCheck(t) },
533+
Providers: testAccProviders,
534+
CheckDestroy: testAccCheckComputeInstanceDestroy,
535+
Steps: []resource.TestStep{
536+
{
537+
Config: testAccComputeInstance_bootDisk_mode(instanceName, diskMode),
538+
},
539+
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
540+
},
541+
})
542+
}
543+
525544
func TestAccComputeInstance_scratchDisk(t *testing.T) {
526545
t.Parallel()
527546

@@ -2693,6 +2712,34 @@ resource "google_compute_instance" "foobar" {
26932712
`, instance, diskType)
26942713
}
26952714

2715+
func testAccComputeInstance_bootDisk_mode(instance string, diskMode string) string {
2716+
return fmt.Sprintf(`
2717+
data "google_compute_image" "my_image" {
2718+
family = "debian-9"
2719+
project = "debian-cloud"
2720+
}
2721+
2722+
resource "google_compute_instance" "foobar" {
2723+
name = "%s"
2724+
machine_type = "n1-standard-1"
2725+
zone = "us-central1-a"
2726+
2727+
boot_disk {
2728+
initialize_params {
2729+
image = "${data.google_compute_image.my_image.self_link}"
2730+
type = "pd-ssd"
2731+
}
2732+
2733+
mode = "%s"
2734+
}
2735+
2736+
network_interface {
2737+
network = "default"
2738+
}
2739+
}
2740+
`, instance, diskMode)
2741+
}
2742+
26962743
func testAccComputeInstance_scratchDisk(instance string) string {
26972744
return fmt.Sprintf(`
26982745
data "google_compute_image" "my_image" {

website/docs/r/compute_instance.html.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ The `boot_disk` block supports:
148148
* `device_name` - (Optional) Name with which attached disk will be accessible.
149149
On the instance, this device will be `/dev/disk/by-id/google-{{device_name}}`.
150150

151+
* `mode` - (Optional) The mode in which to attach this disk, either `READ_WRITE`
152+
or `READ_ONLY`. If not specified, the default is to attach the disk in `READ_WRITE` mode.
153+
151154
* `disk_encryption_key_raw` - (Optional) A 256-bit [customer-supplied encryption key]
152155
(https://cloud.google.com/compute/docs/disks/customer-supplied-encryption),
153156
encoded in [RFC 4648 base64](https://tools.ietf.org/html/rfc4648#section-4)

0 commit comments

Comments
 (0)