diff --git a/.changelog/4850.txt b/.changelog/4850.txt new file mode 100644 index 0000000000..9ee591c996 --- /dev/null +++ b/.changelog/4850.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `advanced_machine_features` fields to `google_compute_instance_template` +``` diff --git a/google-beta/compute_instance_helpers.go b/google-beta/compute_instance_helpers.go index 7e441962a3..ede9bc70ae 100644 --- a/google-beta/compute_instance_helpers.go +++ b/google-beta/compute_instance_helpers.go @@ -345,6 +345,29 @@ func flattenConfidentialInstanceConfig(ConfidentialInstanceConfig *computeBeta.C }} } +func expandAdvancedMachineFeatures(d TerraformResourceData) *computeBeta.AdvancedMachineFeatures { + if _, ok := d.GetOk("advanced_machine_features"); !ok { + return nil + } + + prefix := "advanced_machine_features.0" + return &computeBeta.AdvancedMachineFeatures{ + EnableNestedVirtualization: d.Get(prefix + ".enable_nested_virtualization").(bool), + ThreadsPerCore: int64(d.Get(prefix + ".threads_per_core").(int)), + // ForceSendFields: []string{"EnableSecureBoot"}, + } +} + +func flattenAdvancedMachineFeatures(AdvancedMachineFeatures *computeBeta.AdvancedMachineFeatures) []map[string]interface{} { + if AdvancedMachineFeatures == nil { + return nil + } + return []map[string]interface{}{{ + "enable_nested_virtualization": AdvancedMachineFeatures.EnableNestedVirtualization, + "threads_per_core": AdvancedMachineFeatures.ThreadsPerCore, + }} +} + func flattenShieldedVmConfig(shieldedVmConfig *computeBeta.ShieldedInstanceConfig) []map[string]bool { if shieldedVmConfig == nil { return nil diff --git a/google-beta/resource_compute_instance_template.go b/google-beta/resource_compute_instance_template.go index 06b0074a7a..2a7e8bcbc4 100644 --- a/google-beta/resource_compute_instance_template.go +++ b/google-beta/resource_compute_instance_template.go @@ -555,11 +555,38 @@ func resourceComputeInstanceTemplate() *schema.Resource { "enable_confidential_compute": { Type: schema.TypeBool, Required: true, + ForceNew: true, Description: `Defines whether the instance should have confidential compute enabled.`, }, }, }, }, + "advanced_machine_features": { + Type: schema.TypeList, + MaxItems: 1, + Optional: true, + Computed: true, + ForceNew: true, + Description: `Controls for advanced machine-related behavior features.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enable_nested_virtualization": { + Type: schema.TypeBool, + Optional: true, + Default: false, + ForceNew: true, + Description: `Whether to enable nested virtualization or not.`, + }, + "threads_per_core": { + Type: schema.TypeInt, + Optional: true, + Computed: false, + ForceNew: true, + Description: `The number of threads per physical core. To disable simultaneous multithreading (SMT) set this to 1. If unset, the maximum number of threads supported per core by the underlying processor is assumed.`, + }, + }, + }, + }, "guest_accelerator": { Type: schema.TypeList, Optional: true, @@ -935,6 +962,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac Tags: resourceInstanceTags(d), ConfidentialInstanceConfig: expandConfidentialInstanceConfig(d), ShieldedInstanceConfig: expandShieldedVmConfigs(d), + AdvancedMachineFeatures: expandAdvancedMachineFeatures(d), DisplayDevice: expandDisplayDevice(d), ReservationAffinity: reservationAffinity, } @@ -1326,6 +1354,11 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{ return fmt.Errorf("Error setting confidential_instance_config: %s", err) } } + if instanceTemplate.Properties.AdvancedMachineFeatures != nil { + if err = d.Set("advanced_machine_features", flattenAdvancedMachineFeatures(instanceTemplate.Properties.AdvancedMachineFeatures)); err != nil { + return fmt.Errorf("Error setting advanced_machine_features: %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) diff --git a/google-beta/resource_compute_instance_template_test.go b/google-beta/resource_compute_instance_template_test.go index cbdbdaec4e..dee90dbb89 100644 --- a/google-beta/resource_compute_instance_template_test.go +++ b/google-beta/resource_compute_instance_template_test.go @@ -858,6 +858,26 @@ func TestAccComputeInstanceTemplate_ConfidentialInstanceConfigMain(t *testing.T) }) } +func TestAccComputeInstanceTemplate_AdvancedMachineFeatures(t *testing.T) { + t.Parallel() + + var instanceTemplate computeBeta.InstanceTemplate + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceTemplateAdvancedMachineFeatures(randString(t, 10)), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceTemplateExists(t, "google_compute_instance_template.foobar", &instanceTemplate), + ), + }, + }, + }) +} + func TestAccComputeInstanceTemplate_enableDisplay(t *testing.T) { t.Parallel() @@ -2347,6 +2367,40 @@ resource "google_compute_instance_template" "foobar" { `, suffix, enableConfidentialCompute) } +func testAccComputeInstanceTemplateAdvancedMachineFeatures(suffix string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "ubuntu-2004-lts" + project = "ubuntu-os-cloud" +} + +resource "google_compute_instance_template" "foobar" { + name = "tf-test-instance-template-%s" + machine_type = "n2-standard-2" // Nested Virt isn't supported on E2 and N2Ds https://cloud.google.com/compute/docs/instances/nested-virtualization/overview#restrictions and https://cloud.google.com/compute/docs/instances/disabling-smt#limitations + + disk { + source_image = data.google_compute_image.my_image.self_link + auto_delete = true + boot = true + } + + network_interface { + network = "default" + } + + advanced_machine_features { + threads_per_core = 1 + enable_nested_virtualization = true + } + + scheduling { + on_host_maintenance = "TERMINATE" + } + +} +`, suffix) +} + func testAccComputeInstanceTemplate_enableDisplay(suffix string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { diff --git a/google-beta/resource_gke_hub_feature_membership_test.go b/google-beta/resource_gke_hub_feature_membership_test.go index 4daf2967c1..7dd9a8e59b 100644 --- a/google-beta/resource_gke_hub_feature_membership_test.go +++ b/google-beta/resource_gke_hub_feature_membership_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" + dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" diff --git a/website/docs/r/compute_instance_template.html.markdown b/website/docs/r/compute_instance_template.html.markdown index b661b05613..eed5cd9bd3 100644 --- a/website/docs/r/compute_instance_template.html.markdown +++ b/website/docs/r/compute_instance_template.html.markdown @@ -271,6 +271,8 @@ The following arguments are supported: * `confidential_instance_config` (Optional) - Enable [Confidential Mode](https://cloud.google.com/compute/confidential-vm/docs/about-cvm) on this VM. +* `advanced_machine_features` (Optional) - Configure Nested Virtualisation and Simultaneous Hyper Threading on this VM. + The `disk` block supports: * `auto_delete` - (Optional) Whether or not the disk should be auto-deleted. @@ -445,6 +447,12 @@ The `confidential_instance_config` block supports: * `enable_confidential_compute` (Optional) Defines whether the instance should have confidential compute enabled. [`on_host_maintenance`](#on_host_maintenance) has to be set to TERMINATE or this will fail to create the VM. +The `advanced_machine_features` block supports: + +* `enable_nested_virtualization` (Optional) Defines whether the instance should have [nested virtualization](#on_host_maintenance) enabled. Defaults to false. + +* `threads_per_core` (Optional) he number of threads per physical core. To disable [simultaneous multithreading (SMT)](https://cloud.google.com/compute/docs/instances/disabling-smt) set this to 1. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are