Skip to content

Commit

Permalink
add support for display device to compute instance
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
c2thorn authored and modular-magician committed Oct 29, 2019
1 parent a0fb67e commit 5fbb997
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 3 deletions.
20 changes: 20 additions & 0 deletions google/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,23 @@ func flattenShieldedVmConfig(shieldedVmConfig *computeBeta.ShieldedVmConfig) []m
"enable_integrity_monitoring": shieldedVmConfig.EnableIntegrityMonitoring,
}}
}

func expandDisplayDevice(d *schema.ResourceData) *computeBeta.DisplayDevice {
if _, ok := d.GetOk("display_device"); !ok {
return nil
}
return &computeBeta.DisplayDevice{
EnableDisplay: d.Get("display_device.0.enable_display").(bool),
ForceSendFields: []string{"EnableDisplay"},
}
}

func flattenDisplayDevice(displayDevice *computeBeta.DisplayDevice) []map[string]bool {
if displayDevice == nil {
return nil
}

return []map[string]bool{{
"enable_display": displayDevice.EnableDisplay,
}}
}
5 changes: 5 additions & 0 deletions google/data_source_google_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{
return err
}

err = d.Set("display_device", flattenDisplayDevice(instance.DisplayDevice))
if err != nil {
return err
}

d.Set("attached_disk", ads)
d.Set("cpu_platform", instance.CpuPlatform)
d.Set("min_cpu_platform", instance.MinCpuPlatform)
Expand Down
6 changes: 5 additions & 1 deletion google/data_source_google_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestAccDataSourceComputeInstance_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.google_compute_instance.bar", "boot_disk.0.initialize_params.0.type", "pd-standard"),
resource.TestCheckResourceAttr("data.google_compute_instance.bar", "scratch_disk.0.interface", "SCSI"),
resource.TestCheckResourceAttr("data.google_compute_instance.bar", "network_interface.0.access_config.0.network_tier", "PREMIUM"),
resource.TestCheckResourceAttr("data.google_compute_instance.bar", "display_device.0.enable_display", "true"),
),
},
},
Expand Down Expand Up @@ -119,7 +120,10 @@ resource "google_compute_instance" "foo" {
labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
display_device {
enable_display = "true"
}
}
data "google_compute_instance" "bar" {
Expand Down
36 changes: 34 additions & 2 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ func resourceComputeInstance() *schema.Resource {
},
},

"display_device": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_display": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},

"guest_accelerator": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -732,6 +746,7 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *Confi
Hostname: d.Get("hostname").(string),
ForceSendFields: []string{"CanIpForward", "DeletionProtection"},
ShieldedVmConfig: expandShieldedVmConfigs(d),
DisplayDevice: expandDisplayDevice(d),
}, nil
}

Expand Down Expand Up @@ -952,6 +967,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
d.Set("scheduling", flattenScheduling(instance.Scheduling))
d.Set("guest_accelerator", flattenGuestAccelerators(instance.GuestAccelerators))
d.Set("shielded_instance_config", flattenShieldedVmConfig(instance.ShieldedVmConfig))
d.Set("display_device", flattenDisplayDevice(instance.DisplayDevice))
d.Set("cpu_platform", instance.CpuPlatform)
d.Set("min_cpu_platform", instance.MinCpuPlatform)
d.Set("deletion_protection", instance.DeletionProtection)
Expand Down Expand Up @@ -1329,9 +1345,9 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}

// Attributes which can only be changed if the instance is stopped
if scopesChange || d.HasChange("service_account.0.email") || d.HasChange("machine_type") || d.HasChange("min_cpu_platform") {
if scopesChange || d.HasChange("service_account.0.email") || d.HasChange("machine_type") || d.HasChange("min_cpu_platform") || d.HasChange("display_device.0.enable_display") {
if !d.Get("allow_stopping_for_update").(bool) {
return fmt.Errorf("Changing the machine_type, min_cpu_platform, or service_account on an instance requires stopping it. " +
return fmt.Errorf("Changing the machine_type, min_cpu_platform, service_account, or display device on an instance requires stopping it. " +
"To acknowledge this, please set allow_stopping_for_update = true in your config.")
}
op, err := config.clientCompute.Instances.Stop(project, zone, instance.Name).Do()
Expand Down Expand Up @@ -1404,6 +1420,22 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
d.SetPartial("service_account")
}

if d.HasChange("display_device.0.enable_display") {
req := &compute.DisplayDevice{
EnableDisplay: d.Get("display_device.0.enable_display").(bool),
ForceSendFields: []string{"EnableDisplay"},
}
op, err = config.clientCompute.Instances.UpdateDisplayDevice(project, zone, instance.Name, req).Do()
if err != nil {
return fmt.Errorf("Error updating display device: %s", err)
}
opErr := computeOperationWaitTime(config.clientCompute, op, project, "updating display device", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if opErr != nil {
return opErr
}
d.SetPartial("display_device")
}

op, err = config.clientCompute.Instances.Start(project, zone, instance.Name).Do()
if err != nil {
return errwrap.Wrapf("Error starting instance: {{err}}", err)
Expand Down
22 changes: 22 additions & 0 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ func resourceComputeInstanceTemplate() *schema.Resource {
ForceNew: true,
},

"display_device": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_display": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
},
},

"instance_description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -704,6 +720,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
ServiceAccounts: expandServiceAccounts(d.Get("service_account").([]interface{})),
Tags: resourceInstanceTags(d),
ShieldedVmConfig: expandShieldedVmConfigs(d),
DisplayDevice: expandDisplayDevice(d),
}

if _, ok := d.GetOk("labels"); ok {
Expand Down Expand Up @@ -1069,6 +1086,11 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting shielded_instance_config: %s", err)
}
}
if instanceTemplate.Properties.DisplayDevice != nil {
if err = d.Set("display_device", flattenDisplayDevice(instanceTemplate.Properties.DisplayDevice)); err != nil {
return fmt.Errorf("Error setting display device: %s", err)
}
}
return nil
}

Expand Down
48 changes: 48 additions & 0 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,26 @@ func TestAccComputeInstanceTemplate_shieldedVmConfig2(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_displayDevice(),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -1868,3 +1888,31 @@ resource "google_compute_instance_template" "foobar" {
}
}`, acctest.RandString(10), enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

func testAccComputeInstanceTemplate_displayDevice() string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "centos-7"
project = "gce-uefi-images"
}
resource "google_compute_instance_template" "foobar" {
name = "instancet-test-%s"
machine_type = "n1-standard-1"
can_ip_forward = false
disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
boot = true
}
network_interface {
network = "default"
}
display_device {
enable_display = "true"
}
}`, acctest.RandString(10))
}
84 changes: 84 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,28 @@ func TestAccComputeInstance_shieldedVmConfig2(t *testing.T) {
})
}

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

instanceName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))

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

func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -3638,3 +3660,65 @@ resource "google_compute_instance" "foobar" {
}
`, instance, enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

func testAccComputeInstance_displayDevice(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "centos-7"
project = "gce-uefi-images"
}
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}"
}
}
network_interface {
network = "default"
}
display_device {
enable_display = "true"
}
allow_stopping_for_update = true
}
`, instance)
}

func testAccComputeInstance_displayDeviceDisabled(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "centos-7"
project = "gce-uefi-images"
}
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}"
}
}
network_interface {
network = "default"
}
display_device {
enable_display = "false"
}
allow_stopping_for_update = true
}
`, instance)
}
6 changes: 6 additions & 0 deletions website/docs/d/datasource_compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ The following arguments are supported:

* `shielded_instance_config` - The shielded vm config being used by the instance. Structure is documented below.

* `display_device` - The display config for the instance. Structure is documented below.

* `network_interface.0.network_ip` - The internal ip address of the instance, either manually or dynamically assigned.

* `network_interface.0.access_config.0.nat_ip` - If the instance has an access config, either the given external ip (in the `nat_ip` field) or the ephemeral (generated) ip (if you didn't provide one).
Expand Down Expand Up @@ -202,3 +204,7 @@ The `shielded_instance_config` block supports:
* `enable_vtpm` -- Whether the instance uses vTPM.

* `enable_integrity_monitoring` -- Whether integrity monitoring is enabled for the instance.

The `display_device` block supports:

* `enable_display` -- Whether the instance has virtual displays enabled.
9 changes: 9 additions & 0 deletions website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ The following arguments are supported:
* `shielded_instance_config` - (Optional) Enable [Shielded VM](https://cloud.google.com/security/shielded-cloud/shielded-vm) on this instance. Shielded VM provides verifiable integrity to prevent against malware and rootkits. Defaults to disabled. Structure is documented below.
**Note**: [`shielded_instance_config`](#shielded_instance_config) can only be used with boot images with shielded vm support. See the complete list [here](https://cloud.google.com/compute/docs/images#shielded-images).

* `display_device` - (Optional) Enable [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) on this instance. Structure is documented below.
**Note**: [`allow_stopping_for_update`](#allow_stopping_for_update) must be set to true in order to update this field.


---

The `boot_disk` block supports:
Expand Down Expand Up @@ -319,6 +323,11 @@ The `shielded_instance_config` block supports:

* `enable_integrity_monitoring` (Optional) -- Compare the most recent boot measurements to the integrity policy baseline and return a pair of pass/fail results depending on whether they match or not. Defaults to true.

The `display_device` block supports:

* `enable_display` -- Whether the instance has [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) enabled.
**Note**: [`allow_stopping_for_update`](#allow_stopping_for_update) must be set to true in order to update this field.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/compute_instance_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ The following arguments are supported:
* `shielded_instance_config` - (Optional) Enable [Shielded VM](https://cloud.google.com/security/shielded-cloud/shielded-vm) on this instance. Shielded VM provides verifiable integrity to prevent against malware and rootkits. Defaults to disabled. Structure is documented below.
**Note**: [`shielded_instance_config`](#shielded_instance_config) can only be used with boot images with shielded vm support. See the complete list [here](https://cloud.google.com/compute/docs/images#shielded-images).

* `display_device` - (Optional) Enable [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) on this instance. Structure is documented below.
**Note**: [`allow_stopping_for_update`](#allow_stopping_for_update) must be set to true in order to update this field.

The `disk` block supports:

* `auto_delete` - (Optional) Whether or not the disk should be auto-deleted.
Expand Down Expand Up @@ -402,6 +405,11 @@ The `shielded_instance_config` block supports:

* `enable_integrity_monitoring` (Optional) -- Compare the most recent boot measurements to the integrity policy baseline and return a pair of pass/fail results depending on whether they match or not. Defaults to true.

The `display_device` block supports:

* `enable_display` -- Whether the instance has [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) enabled.
**Note**: [`allow_stopping_for_update`](#allow_stopping_for_update) must be set to true in order to update this field.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down

0 comments on commit 5fbb997

Please sign in to comment.