Skip to content

Commit

Permalink
ga for gVNIC on google_compute_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmedia committed Feb 25, 2021
1 parent 6a38803 commit df16729
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,13 @@ 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,15 +330,6 @@ 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 Expand Up @@ -869,7 +860,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
return err
}

networks, err := expandNetworkInterfaces(d, config)
networks, err := expandComputeInstanceTemplateNetworkInterfaces(d, config)
if err != nil {
return err
}
Expand Down Expand Up @@ -1233,7 +1224,7 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting project: %s", err)
}
if instanceTemplate.Properties.NetworkInterfaces != nil {
networkInterfaces, region, _, _, err := flattenNetworkInterfaces(d, config, instanceTemplate.Properties.NetworkInterfaces)
networkInterfaces, region, _, _, err := flattenComputeInstanceTemplateNetworkInterfaces(d, config, instanceTemplate.Properties.NetworkInterfaces)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ func TestAccComputeInstance_multiNic(t *testing.T) {
},
})
}
<% unless version == 'ga' -%>

func TestAccComputeInstance_nictype_update(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1081,7 +1081,6 @@ func TestAccComputeInstance_nictype_update(t *testing.T) {
},
})
}
<% end -%>

func TestAccComputeInstance_guestAccelerator(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -4170,7 +4169,6 @@ 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" {
Expand Down Expand Up @@ -4225,7 +4223,6 @@ resource "google_compute_instance" "foobar" {
}
`, image, instance, nictype)
}
<% end -%>

func testAccComputeInstance_guestAccelerator(instance string, count uint8) string {
return fmt.Sprintf(`
Expand Down
76 changes: 70 additions & 6 deletions mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,43 @@ 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
// same flattening code for both resource types, so we avoid trying to
// set the name field when it's not set at the GCE end.
if iface.Name != "" {
flattened[i]["name"] = iface.Name
}
if internalIP == "" {
internalIP = iface.NetworkIP
}
}
return flattened, region, internalIP, externalIP, nil
}

func flattenComputeInstanceTemplateNetworkInterfaces(d *schema.ResourceData, config *Config, networkInterfaces []*computeBeta.NetworkInterface) ([]map[string]interface{}, string, string, string, error) {
flattened := make([]map[string]interface{}, len(networkInterfaces))
var region, internalIP, externalIP string

for i, iface := range networkInterfaces {
var ac []map[string]interface{}
ac, externalIP = flattenAccessConfigs(iface.AccessConfigs)

subnet, err := ParseSubnetworkFieldValue(iface.Subnetwork, d, config)
if err != nil {
return nil, "", "", "", err
}
region = subnet.Region

flattened[i] = map[string]interface{}{
"network_ip": iface.NetworkIP,
"network": ConvertSelfLinkToV1(iface.Network),
"subnetwork": ConvertSelfLinkToV1(iface.Subnetwork),
"subnetwork_project": subnet.Project,
"access_config": ac,
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges),
}
// 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 @@ -257,22 +291,52 @@ 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
}

func expandComputeInstanceTemplateNetworkInterfaces(d TerraformResourceData, config *Config) ([]*computeBeta.NetworkInterface, error) {
configs := d.Get("network_interface").([]interface{})
ifaces := make([]*computeBeta.NetworkInterface, len(configs))
for i, raw := range configs {
data := raw.(map[string]interface{})

network := data["network"].(string)
subnetwork := data["subnetwork"].(string)
if network == "" && subnetwork == "" {
return nil, fmt.Errorf("exactly one of network or subnetwork must be provided")
}

nf, err := ParseNetworkFieldValue(network, d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for network %q: %s", network, err)
}

subnetProjectField := fmt.Sprintf("network_interface.%d.subnetwork_project", i)
sf, err := ParseSubnetworkFieldValueWithProjectField(subnetwork, subnetProjectField, d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for subnetwork %q: %s", subnetwork, err)
}

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{})),
}
}
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,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, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) The type of vNIC to be used on this interface.
* `nic_type` - (Optional) The type of vNIC to be used on this interface.
Possible values: GVNIC, VIRTIO_NET.

The `access_config` block supports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,6 @@ 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 df16729

Please sign in to comment.