Skip to content

Commit

Permalink
add gVNIC support for compute instance (#4443)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
edwardmedia authored Feb 9, 2021
1 parent 2774921 commit 50df209
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,15 @@ func resourceComputeInstance() *schema.Resource {
Computed: true,
Description: `The name of the interface`,
},

<% unless version == 'ga' -%>
"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`,
},
<% end -%>
"access_config": {
Type: schema.TypeList,
Optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,15 @@ func resourceComputeInstanceTemplate() *schema.Resource {
Computed: true,
Description: `The name of the network_interface.`,
},

<% unless version == 'ga' -%>
"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`,
},
<% end -%>
"access_config": {
Type: schema.TypeList,
Optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,36 @@ func TestAccComputeInstance_multiNic(t *testing.T) {
},
})
}
<% unless version == 'ga' -%>
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),
),
},
},
})
}
<% end -%>

func TestAccComputeInstance_guestAccelerator(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -4140,6 +4170,63 @@ 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" {
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)
}
<% end -%>

func testAccComputeInstance_guestAccelerator(instance string, count uint8) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
14 changes: 14 additions & 0 deletions mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +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
Expand Down Expand Up @@ -254,11 +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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50df209

Please sign in to comment.