Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for display device to compute instance #4775

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions google/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,21 @@ func flattenShieldedVmConfig(shieldedVmConfig *computeBeta.ShieldedVmConfig) []m
"enable_integrity_monitoring": shieldedVmConfig.EnableIntegrityMonitoring,
}}
}

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

func flattenEnableDisplay(displayDevice *computeBeta.DisplayDevice) interface{} {
if displayDevice == nil {
return nil
}

return 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("enable_display", flattenEnableDisplay(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
5 changes: 4 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", "enable_display", "true"),
),
},
},
Expand Down Expand Up @@ -119,7 +120,9 @@ resource "google_compute_instance" "foo" {
labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}

enable_display = true
}

data "google_compute_instance" "bar" {
Expand Down
27 changes: 25 additions & 2 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ func resourceComputeInstance() *schema.Resource {
},
},

"enable_display": {
Type: schema.TypeBool,
Optional: true,
},

"guest_accelerator": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -732,6 +737,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 +958,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("enable_display", flattenEnableDisplay(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 +1336,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("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 enable display 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 +1411,22 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
d.SetPartial("service_account")
}

if d.HasChange("enable_display") {
req := &compute.DisplayDevice{
EnableDisplay: d.Get("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("enable_display")
}

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

"enable_display": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"instance_description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -704,6 +710,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 +1076,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("enable_display", flattenEnableDisplay(instanceTemplate.Properties.DisplayDevice)); err != nil {
return fmt.Errorf("Error setting enable_display: %s", err)
}
}
return nil
}

Expand Down
46 changes: 46 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_enableDisplay(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_enableDisplay(),
},
{
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,29 @@ resource "google_compute_instance_template" "foobar" {
}
}`, acctest.RandString(10), enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

func testAccComputeInstanceTemplate_enableDisplay() 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"
}

enable_display = true
}`, acctest.RandString(10))
}
80 changes: 80 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_enableDisplay(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_enableDisplay(instanceName),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
{
Config: testAccComputeInstance_enableDisplayUpdated(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,61 @@ resource "google_compute_instance" "foobar" {
}
`, instance, enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

func testAccComputeInstance_enableDisplay(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"
}

enable_display = true

allow_stopping_for_update = true
}
`, instance)
}

func testAccComputeInstance_enableDisplayUpdated(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"
}

enable_display = false

allow_stopping_for_update = true
}
`, instance)
}
4 changes: 3 additions & 1 deletion 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.

* `enable_display` -- Whether the instance has virtual displays enabled.

* `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 @@ -201,4 +203,4 @@ The `shielded_instance_config` block supports:

* `enable_vtpm` -- Whether the instance uses vTPM.

* `enable_integrity_monitoring` -- Whether integrity monitoring is enabled for the instance.
* `enable_integrity_monitoring` -- Whether integrity monitoring is enabled for the instance.
4 changes: 4 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).

* `enable_display` - (Optional) Enable [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) on this instance.
**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
3 changes: 3 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).

* `enable_display` - (Optional) Enable [Virtual Displays](https://cloud.google.com/compute/docs/instances/enable-instance-virtual-display#verify_display_driver) on this instance.
**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