From e0420f6e3af9aa1451e463dc9ddcbde2d987bee1 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 9 Feb 2021 11:50:19 -0800 Subject: [PATCH] add gVNIC support for compute instance (#4443) (#2941) * add gVNIC support for compute instance * add google_compute_instance_template & fix a panic * Using a func to replace inline code * add ga filter * add beta in documents Signed-off-by: Modular Magician --- .changelog/4443.txt | 6 ++ google-beta/compute_instance_helpers.go | 8 ++ google-beta/resource_compute_instance.go | 8 +- .../resource_compute_instance_template.go | 8 +- google-beta/resource_compute_instance_test.go | 83 +++++++++++++++++++ ...esource_dataflow_flex_template_job_test.go | 2 +- website/docs/r/compute_instance.html.markdown | 3 + .../r/compute_instance_template.html.markdown | 3 + 8 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 .changelog/4443.txt diff --git a/.changelog/4443.txt b/.changelog/4443.txt new file mode 100644 index 0000000000..fd4c1b73f5 --- /dev/null +++ b/.changelog/4443.txt @@ -0,0 +1,6 @@ +```release-note:enhancement +compute: added `nic_type` field to `google_compute_instance` resource to support gVNIC +``` +```release-note:enhancement +compute: added `nic_type` field to `google_compute_instance_template ` resource to support gVNIC +``` diff --git a/google-beta/compute_instance_helpers.go b/google-beta/compute_instance_helpers.go index 79a38b8bce..528ee329b5 100644 --- a/google-beta/compute_instance_helpers.go +++ b/google-beta/compute_instance_helpers.go @@ -187,6 +187,7 @@ func flattenNetworkInterfaces(d *schema.ResourceData, config *Config, networkInt "subnetwork_project": subnet.Project, "access_config": ac, "alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges), + "nic_type": iface.NicType, } // Instance template interfaces never have names, so they're absent // in the instance template network_interface schema. We want to use the @@ -249,11 +250,18 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput Subnetwork: sf.RelativeLink(), AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})), AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})), + NicType: expandNicType(data["nic_type"].(interface{})), } } return ifaces, nil } +func expandNicType(d interface{}) string { + if d == nil { + return "" + } + return d.(string) +} func flattenServiceAccounts(serviceAccounts []*computeBeta.ServiceAccount) []map[string]interface{} { result := make([]map[string]interface{}, len(serviceAccounts)) diff --git a/google-beta/resource_compute_instance.go b/google-beta/resource_compute_instance.go index 36183fa603..2f1903e904 100644 --- a/google-beta/resource_compute_instance.go +++ b/google-beta/resource_compute_instance.go @@ -293,7 +293,13 @@ func resourceComputeInstance() *schema.Resource { Computed: true, Description: `The name of the interface`, }, - + "nic_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false), + Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`, + }, "access_config": { Type: schema.TypeList, Optional: true, diff --git a/google-beta/resource_compute_instance_template.go b/google-beta/resource_compute_instance_template.go index 05d8d1dec9..b55191d51d 100644 --- a/google-beta/resource_compute_instance_template.go +++ b/google-beta/resource_compute_instance_template.go @@ -326,7 +326,13 @@ func resourceComputeInstanceTemplate() *schema.Resource { Computed: true, Description: `The name of the network_interface.`, }, - + "nic_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false), + Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`, + }, "access_config": { Type: schema.TypeList, Optional: true, diff --git a/google-beta/resource_compute_instance_test.go b/google-beta/resource_compute_instance_test.go index 4ecae79408..a2f0fe030d 100644 --- a/google-beta/resource_compute_instance_test.go +++ b/google-beta/resource_compute_instance_test.go @@ -1051,6 +1051,34 @@ func TestAccComputeInstance_multiNic(t *testing.T) { }, }) } +func TestAccComputeInstance_nictype_update(t *testing.T) { + t.Parallel() + + var instance compute.Instance + var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10)) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeInstance_nictype(instanceName, instanceName, "GVNIC"), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + t, "google_compute_instance.foobar", &instance), + ), + }, + { + Config: testAccComputeInstance_nictype(instanceName, instanceName, "VIRTIO_NET"), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + t, "google_compute_instance.foobar", &instance), + ), + }, + }, + }) +} func TestAccComputeInstance_guestAccelerator(t *testing.T) { t.Parallel() @@ -4140,6 +4168,61 @@ resource "google_compute_subnetwork" "inst-test-subnetwork" { `, instance, network, subnetwork) } +func testAccComputeInstance_nictype(image, instance, nictype string) string { + return fmt.Sprintf(` +resource "google_compute_image" "example" { + name = "%s" + raw_disk { + source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz" + } + + guest_os_features { + type = "SECURE_BOOT" + } + + guest_os_features { + type = "MULTI_IP_SUBNET" + } + + guest_os_features { + type = "GVNIC" + } +} + +resource "google_compute_instance" "foobar" { + name = "%s" + machine_type = "e2-medium" + zone = "us-central1-a" + can_ip_forward = false + tags = ["foo", "bar"] + + //deletion_protection = false is implicit in this config due to default value + + boot_disk { + initialize_params { + image = google_compute_image.example.id + } + } + + network_interface { + network = "default" + nic_type = "%s" + } + + metadata = { + foo = "bar" + baz = "qux" + startup-script = "echo Hello" + } + + labels = { + my_key = "my_value" + my_other_key = "my_other_value" + } +} +`, image, instance, nictype) +} + func testAccComputeInstance_guestAccelerator(instance string, count uint8) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { diff --git a/google-beta/resource_dataflow_flex_template_job_test.go b/google-beta/resource_dataflow_flex_template_job_test.go index 0d037482e0..df04d28bb0 100644 --- a/google-beta/resource_dataflow_flex_template_job_test.go +++ b/google-beta/resource_dataflow_flex_template_job_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - compute "google.golang.org/api/compute/v1" + "google.golang.org/api/compute/v1" ) func TestAccDataflowFlexTemplateJob_basic(t *testing.T) { diff --git a/website/docs/r/compute_instance.html.markdown b/website/docs/r/compute_instance.html.markdown index 43617edd68..296552a496 100644 --- a/website/docs/r/compute_instance.html.markdown +++ b/website/docs/r/compute_instance.html.markdown @@ -276,6 +276,9 @@ The `network_interface` block supports: array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks. Structure documented below. +* `nic_type` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) The type of vNIC to be used on this interface. + Possible values: GVNIC, VIRTIO_NET. + The `access_config` block supports: * `nat_ip` - (Optional) The IP address that will be 1:1 mapped to the instance's diff --git a/website/docs/r/compute_instance_template.html.markdown b/website/docs/r/compute_instance_template.html.markdown index 1faf26428c..91b1673fe4 100644 --- a/website/docs/r/compute_instance_template.html.markdown +++ b/website/docs/r/compute_instance_template.html.markdown @@ -359,6 +359,9 @@ The `network_interface` block supports: array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks. Structure documented below. +* `nic_type` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) The type of vNIC to be used on this interface. + Possible values: GVNIC, VIRTIO_NET. + The `access_config` block supports: * `nat_ip` - (Optional) The IP address that will be 1:1 mapped to the instance's