Skip to content

Commit e0420f6

Browse files
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 <[email protected]>
1 parent 9e738f9 commit e0420f6

8 files changed

+118
-3
lines changed

.changelog/4443.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
compute: added `nic_type` field to `google_compute_instance` resource to support gVNIC
3+
```
4+
```release-note:enhancement
5+
compute: added `nic_type` field to `google_compute_instance_template ` resource to support gVNIC
6+
```

google-beta/compute_instance_helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func flattenNetworkInterfaces(d *schema.ResourceData, config *Config, networkInt
187187
"subnetwork_project": subnet.Project,
188188
"access_config": ac,
189189
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges),
190+
"nic_type": iface.NicType,
190191
}
191192
// Instance template interfaces never have names, so they're absent
192193
// in the instance template network_interface schema. We want to use the
@@ -249,11 +250,18 @@ func expandNetworkInterfaces(d TerraformResourceData, config *Config) ([]*comput
249250
Subnetwork: sf.RelativeLink(),
250251
AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})),
251252
AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})),
253+
NicType: expandNicType(data["nic_type"].(interface{})),
252254
}
253255

254256
}
255257
return ifaces, nil
256258
}
259+
func expandNicType(d interface{}) string {
260+
if d == nil {
261+
return ""
262+
}
263+
return d.(string)
264+
}
257265

258266
func flattenServiceAccounts(serviceAccounts []*computeBeta.ServiceAccount) []map[string]interface{} {
259267
result := make([]map[string]interface{}, len(serviceAccounts))

google-beta/resource_compute_instance.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,13 @@ func resourceComputeInstance() *schema.Resource {
293293
Computed: true,
294294
Description: `The name of the interface`,
295295
},
296-
296+
"nic_type": {
297+
Type: schema.TypeString,
298+
Optional: true,
299+
ForceNew: true,
300+
ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false),
301+
Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`,
302+
},
297303
"access_config": {
298304
Type: schema.TypeList,
299305
Optional: true,

google-beta/resource_compute_instance_template.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,13 @@ func resourceComputeInstanceTemplate() *schema.Resource {
326326
Computed: true,
327327
Description: `The name of the network_interface.`,
328328
},
329-
329+
"nic_type": {
330+
Type: schema.TypeString,
331+
Optional: true,
332+
ForceNew: true,
333+
ValidateFunc: validation.StringInSlice([]string{"GVNIC", "VIRTIO_NET"}, false),
334+
Description: `The type of vNIC to be used on this interface. Possible values:GVNIC, VIRTIO_NET`,
335+
},
330336
"access_config": {
331337
Type: schema.TypeList,
332338
Optional: true,

google-beta/resource_compute_instance_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,34 @@ func TestAccComputeInstance_multiNic(t *testing.T) {
10511051
},
10521052
})
10531053
}
1054+
func TestAccComputeInstance_nictype_update(t *testing.T) {
1055+
t.Parallel()
1056+
1057+
var instance compute.Instance
1058+
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))
1059+
1060+
vcrTest(t, resource.TestCase{
1061+
PreCheck: func() { testAccPreCheck(t) },
1062+
Providers: testAccProviders,
1063+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
1064+
Steps: []resource.TestStep{
1065+
{
1066+
Config: testAccComputeInstance_nictype(instanceName, instanceName, "GVNIC"),
1067+
Check: resource.ComposeTestCheckFunc(
1068+
testAccCheckComputeInstanceExists(
1069+
t, "google_compute_instance.foobar", &instance),
1070+
),
1071+
},
1072+
{
1073+
Config: testAccComputeInstance_nictype(instanceName, instanceName, "VIRTIO_NET"),
1074+
Check: resource.ComposeTestCheckFunc(
1075+
testAccCheckComputeInstanceExists(
1076+
t, "google_compute_instance.foobar", &instance),
1077+
),
1078+
},
1079+
},
1080+
})
1081+
}
10541082

10551083
func TestAccComputeInstance_guestAccelerator(t *testing.T) {
10561084
t.Parallel()
@@ -4140,6 +4168,61 @@ resource "google_compute_subnetwork" "inst-test-subnetwork" {
41404168
`, instance, network, subnetwork)
41414169
}
41424170

4171+
func testAccComputeInstance_nictype(image, instance, nictype string) string {
4172+
return fmt.Sprintf(`
4173+
resource "google_compute_image" "example" {
4174+
name = "%s"
4175+
raw_disk {
4176+
source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz"
4177+
}
4178+
4179+
guest_os_features {
4180+
type = "SECURE_BOOT"
4181+
}
4182+
4183+
guest_os_features {
4184+
type = "MULTI_IP_SUBNET"
4185+
}
4186+
4187+
guest_os_features {
4188+
type = "GVNIC"
4189+
}
4190+
}
4191+
4192+
resource "google_compute_instance" "foobar" {
4193+
name = "%s"
4194+
machine_type = "e2-medium"
4195+
zone = "us-central1-a"
4196+
can_ip_forward = false
4197+
tags = ["foo", "bar"]
4198+
4199+
//deletion_protection = false is implicit in this config due to default value
4200+
4201+
boot_disk {
4202+
initialize_params {
4203+
image = google_compute_image.example.id
4204+
}
4205+
}
4206+
4207+
network_interface {
4208+
network = "default"
4209+
nic_type = "%s"
4210+
}
4211+
4212+
metadata = {
4213+
foo = "bar"
4214+
baz = "qux"
4215+
startup-script = "echo Hello"
4216+
}
4217+
4218+
labels = {
4219+
my_key = "my_value"
4220+
my_other_key = "my_other_value"
4221+
}
4222+
}
4223+
`, image, instance, nictype)
4224+
}
4225+
41434226
func testAccComputeInstance_guestAccelerator(instance string, count uint8) string {
41444227
return fmt.Sprintf(`
41454228
data "google_compute_image" "my_image" {

google-beta/resource_dataflow_flex_template_job_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11-
compute "google.golang.org/api/compute/v1"
11+
"google.golang.org/api/compute/v1"
1212
)
1313

1414
func TestAccDataflowFlexTemplateJob_basic(t *testing.T) {

website/docs/r/compute_instance.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ The `network_interface` block supports:
276276
array of alias IP ranges for this network interface. Can only be specified for network
277277
interfaces on subnet-mode networks. Structure documented below.
278278

279+
* `nic_type` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) The type of vNIC to be used on this interface.
280+
Possible values: GVNIC, VIRTIO_NET.
281+
279282
The `access_config` block supports:
280283

281284
* `nat_ip` - (Optional) The IP address that will be 1:1 mapped to the instance's

website/docs/r/compute_instance_template.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ The `network_interface` block supports:
359359
array of alias IP ranges for this network interface. Can only be specified for network
360360
interfaces on subnet-mode networks. Structure documented below.
361361

362+
* `nic_type` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) The type of vNIC to be used on this interface.
363+
Possible values: GVNIC, VIRTIO_NET.
364+
362365
The `access_config` block supports:
363366

364367
* `nat_ip` - (Optional) The IP address that will be 1:1 mapped to the instance's

0 commit comments

Comments
 (0)