From 84752a1e9774ef62cd52af038d5be0c623a22eca Mon Sep 17 00:00:00 2001 From: Edward Sun Date: Thu, 28 Jan 2021 15:51:06 -0800 Subject: [PATCH 1/5] add gVNIC support for compute instance --- .../resource_compute_instance.go.erb | 8 ++ .../resource_compute_instance_test.go.erb | 85 +++++++++++++++++++ .../utils/compute_instance_helpers.go.erb | 2 + .../docs/r/compute_instance.html.markdown | 3 + 4 files changed, 98 insertions(+) diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb b/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb index 683d38bd06b9..bff492c64afc 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb +++ b/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb @@ -298,6 +298,14 @@ func resourceComputeInstance() *schema.Resource { 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/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb index 9dab45b6f22a..ed34ef7fa4ba 100644 --- a/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb @@ -1053,6 +1053,35 @@ 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 +4169,62 @@ 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/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb index 37b35547d19f..682d1f2e05c4 100644 --- a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb +++ b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb @@ -192,6 +192,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 @@ -254,6 +255,7 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput Subnetwork: sf.RelativeLink(), AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})), AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})), + NicType: data["nic_type"].(string), } } diff --git a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown b/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown index 43617edd6872..392ef1c1faa3 100644 --- a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown +++ b/mmv1/third_party/terraform/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) 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 From 16a41e8d2bdf4fc6872a6592994c9dcca1fdb6fa Mon Sep 17 00:00:00 2001 From: Edward Sun Date: Sat, 6 Feb 2021 12:01:40 -0800 Subject: [PATCH 2/5] add google_compute_instance_template & fix a panic --- .../resources/resource_compute_instance_template.go.erb | 8 ++++++++ .../terraform/utils/compute_instance_helpers.go.erb | 9 ++++++++- .../docs/r/compute_instance_template.html.markdown | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb b/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb index 8021dd18a4ed..e45d8b90f3b8 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb +++ b/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb @@ -331,6 +331,14 @@ func resourceComputeInstanceTemplate() *schema.Resource { 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/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb index 682d1f2e05c4..99f47512f2d4 100644 --- a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb +++ b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb @@ -249,13 +249,20 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput return nil, fmt.Errorf("cannot determine self_link for subnetwork %q: %s", subnetwork, err) } + var nt string + if data["nic_type"] != nil { + nt = data["nic_type"].(string) + } else { + nt = "" + } + ifaces[i] = &computeBeta.NetworkInterface{ NetworkIP: data["network_ip"].(string), Network: nf.RelativeLink(), Subnetwork: sf.RelativeLink(), AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})), AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})), - NicType: data["nic_type"].(string), + NicType: nt, } } diff --git a/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown b/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown index 1faf26428c7d..1e0a62aee10c 100644 --- a/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown +++ b/mmv1/third_party/terraform/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) 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 From aa867a8b821fa5464c65979ebcc315be329c1fe4 Mon Sep 17 00:00:00 2001 From: Edward Sun Date: Mon, 8 Feb 2021 12:21:18 -0800 Subject: [PATCH 3/5] Using a func to replace inline code --- .../utils/compute_instance_helpers.go.erb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb index 99f47512f2d4..40476d2abac9 100644 --- a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb +++ b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb @@ -249,26 +249,26 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput return nil, fmt.Errorf("cannot determine self_link for subnetwork %q: %s", subnetwork, err) } - var nt string - if data["nic_type"] != nil { - nt = data["nic_type"].(string) - } else { - nt = "" - } - ifaces[i] = &computeBeta.NetworkInterface{ NetworkIP: data["network_ip"].(string), Network: nf.RelativeLink(), Subnetwork: sf.RelativeLink(), AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})), AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})), - NicType: nt, + 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)) for i, serviceAccount := range serviceAccounts { From f71a0935cd054ef248401c7c23f0ce44fbb14b86 Mon Sep 17 00:00:00 2001 From: Edward Sun Date: Mon, 8 Feb 2021 15:29:22 -0800 Subject: [PATCH 4/5] add ga filter --- .../terraform/resources/resource_compute_instance.go.erb | 4 ++-- .../resources/resource_compute_instance_template.go.erb | 4 ++-- .../terraform/tests/resource_compute_instance_test.go.erb | 6 ++++-- .../terraform/utils/compute_instance_helpers.go.erb | 7 ++++++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb b/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb index bff492c64afc..419a1a1f87ee 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb +++ b/mmv1/third_party/terraform/resources/resource_compute_instance.go.erb @@ -297,7 +297,7 @@ func resourceComputeInstance() *schema.Resource { Computed: true, Description: `The name of the interface`, }, - +<% unless version == 'ga' -%> "nic_type": { Type: schema.TypeString, Optional: true, @@ -305,7 +305,7 @@ func resourceComputeInstance() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false), Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`, }, - +<% end -%> "access_config": { Type: schema.TypeList, Optional: true, diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb b/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb index e45d8b90f3b8..27cdb865bb68 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb +++ b/mmv1/third_party/terraform/resources/resource_compute_instance_template.go.erb @@ -330,7 +330,7 @@ func resourceComputeInstanceTemplate() *schema.Resource { Computed: true, Description: `The name of the network_interface.`, }, - +<% unless version == 'ga' -%> "nic_type": { Type: schema.TypeString, Optional: true, @@ -338,7 +338,7 @@ func resourceComputeInstanceTemplate() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false), Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`, }, - +<% end -%> "access_config": { Type: schema.TypeList, Optional: true, diff --git a/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb index ed34ef7fa4ba..032fb0093f22 100644 --- a/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_compute_instance_test.go.erb @@ -1052,7 +1052,7 @@ func TestAccComputeInstance_multiNic(t *testing.T) { }, }) } - +<% unless version == 'ga' -%> func TestAccComputeInstance_nictype_update(t *testing.T) { t.Parallel() @@ -1081,6 +1081,7 @@ func TestAccComputeInstance_nictype_update(t *testing.T) { }, }) } +<% end -%> func TestAccComputeInstance_guestAccelerator(t *testing.T) { t.Parallel() @@ -4169,6 +4170,7 @@ resource "google_compute_subnetwork" "inst-test-subnetwork" { `, instance, network, subnetwork) } +<% unless version == 'ga' -%> func testAccComputeInstance_nictype(image, instance, nictype string) string { return fmt.Sprintf(` resource "google_compute_image" "example" { @@ -4223,7 +4225,7 @@ resource "google_compute_instance" "foobar" { } `, image, instance, nictype) } - +<% end -%> func testAccComputeInstance_guestAccelerator(instance string, count uint8) string { return fmt.Sprintf(` diff --git a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb index 40476d2abac9..0339e4f1095f 100644 --- a/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb +++ b/mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb @@ -192,7 +192,9 @@ func flattenNetworkInterfaces(d *schema.ResourceData, config *Config, networkInt "subnetwork_project": subnet.Project, "access_config": ac, "alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges), +<% unless version == 'ga' -%> "nic_type": iface.NicType, +<% end -%> } // Instance template interfaces never have names, so they're absent // in the instance template network_interface schema. We want to use the @@ -255,19 +257,22 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput Subnetwork: sf.RelativeLink(), AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})), AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})), +<% unless version == 'ga' -%> NicType: expandNicType(data["nic_type"].(interface{})), +<% end -%> } } return ifaces, nil } - +<% unless version == 'ga' -%> func expandNicType(d interface{}) string { if d == nil { return "" } return d.(string) } +<% end -%> func flattenServiceAccounts(serviceAccounts []*computeBeta.ServiceAccount) []map[string]interface{} { result := make([]map[string]interface{}, len(serviceAccounts)) From 045f149c5297f0c9d99e09e1e46dddd2d5724f89 Mon Sep 17 00:00:00 2001 From: Edward Sun Date: Mon, 8 Feb 2021 20:47:19 -0800 Subject: [PATCH 5/5] add beta in documents --- .../terraform/website/docs/r/compute_instance.html.markdown | 2 +- .../website/docs/r/compute_instance_template.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown b/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown index 392ef1c1faa3..296552a49601 100644 --- a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown @@ -276,7 +276,7 @@ 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) The type of vNIC to be used on this interface. +* `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: diff --git a/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown b/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown index 1e0a62aee10c..91b1673fe474 100644 --- a/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/compute_instance_template.html.markdown @@ -359,7 +359,7 @@ 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) The type of vNIC to be used on this interface. +* `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: