Skip to content

Commit

Permalink
add mode option to google compute instance boot disk
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
megan07 authored and modular-magician committed Sep 12, 2019
1 parent 1befeb8 commit be0feb8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions google-beta/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func resourceComputeInstance() *schema.Resource {
},
},

"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "READ_WRITE",
ValidateFunc: validation.StringInSlice([]string{"READ_WRITE", "READ_ONLY"}, false),
},

"source": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1664,13 +1672,18 @@ func expandBootDisk(d *schema.ResourceData, config *Config, project string) (*co
}
}

if v, ok := d.GetOk("boot_disk.0.mode"); ok {
disk.Mode = v.(string)
}

return disk, nil
}

func flattenBootDisk(d *schema.ResourceData, disk *computeBeta.AttachedDisk, config *Config) []map[string]interface{} {
result := map[string]interface{}{
"auto_delete": disk.AutoDelete,
"device_name": disk.DeviceName,
"mode": disk.Mode,
"source": ConvertSelfLinkToV1(disk.Source),
// disk_encryption_key_raw is not returned from the API, so copy it from what the user
// originally specified to avoid diffs.
Expand Down
47 changes: 47 additions & 0 deletions google-beta/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,25 @@ func TestAccComputeInstance_bootDisk_type(t *testing.T) {
})
}

func TestAccComputeInstance_bootDisk_mode(t *testing.T) {
t.Parallel()

var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var diskMode = "READ_WRITE"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_bootDisk_mode(instanceName, diskMode),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
},
})
}

func TestAccComputeInstance_scratchDisk(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2693,6 +2712,34 @@ resource "google_compute_instance" "foobar" {
`, instance, diskType)
}

func testAccComputeInstance_bootDisk_mode(instance string, diskMode string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "${data.google_compute_image.my_image.self_link}"
type = "pd-ssd"
}
mode = "%s"
}
network_interface {
network = "default"
}
}
`, instance, diskMode)
}

func testAccComputeInstance_scratchDisk(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ The `boot_disk` block supports:
* `device_name` - (Optional) Name with which attached disk will be accessible.
On the instance, this device will be `/dev/disk/by-id/google-{{device_name}}`.

* `mode` - (Optional) 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.

* `disk_encryption_key_raw` - (Optional) A 256-bit [customer-supplied encryption key]
(https://cloud.google.com/compute/docs/disks/customer-supplied-encryption),
encoded in [RFC 4648 base64](https://tools.ietf.org/html/rfc4648#section-4)
Expand Down

0 comments on commit be0feb8

Please sign in to comment.