Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vnic import #1162

Merged
merged 3 commits into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions vsphere/resource_vsphere_vnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceVsphereNic() *schema.Resource {
Update: resourceVsphereNicUpdate,
Delete: resourceVsphereNicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceVSphereNicImport,
},
Schema: vNicSchema(),
}
Expand All @@ -43,9 +43,7 @@ func resourceVsphereNicRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.TODO()
client := meta.(*VSphereClient).vimClient

toks := strings.Split(d.Id(), "_")
hostId := toks[0]
nicId := toks[1]
hostId, nicId := splitHostIdNicId(d)

vnic, err := getVnicFromHost(ctx, client, hostId, nicId)
if err != nil {
Expand All @@ -54,6 +52,7 @@ func resourceVsphereNicRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

_ = d.Set("netstack", vnic.Spec.NetStackInstanceKey)
_ = d.Set("portgroup", vnic.Portgroup)
if vnic.Spec.DistributedVirtualPort != nil {
_ = d.Set("distributed_switch_port", vnic.Spec.DistributedVirtualPort.SwitchUuid)
Expand All @@ -63,7 +62,8 @@ func resourceVsphereNicRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("mac", vnic.Spec.Mac)

// Do we have any ipv4 config ?
if _, ok := d.GetOk("ipv4"); ok {
// IpAddress will be an empty string if ipv4 is off
if vnic.Spec.Ip.IpAddress != "" {
// if DHCP is true then we should ignore whatever addresses are set here.
ipv4dict := make(map[string]interface{})
ipv4dict["dhcp"] = vnic.Spec.Ip.Dhcp
Expand All @@ -81,7 +81,8 @@ func resourceVsphereNicRead(d *schema.ResourceData, meta interface{}) error {
}

// Do we have any ipv6 config ?
if _, ok := d.GetOk("ipv6"); ok {
// IpV6Config will be nil if ipv6 is off
if vnic.Spec.Ip.IpV6Config != nil {
ipv6dict := map[string]interface{}{
"dhcp": *vnic.Spec.Ip.IpV6Config.DhcpV6Enabled,
"autoconfig": *vnic.Spec.Ip.IpV6Config.AutoConfigurationEnabled,
Expand Down Expand Up @@ -138,9 +139,7 @@ func resourceVsphereNicUpdate(d *schema.ResourceData, meta interface{}) error {

func resourceVsphereNicDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
idParts := strings.Split(d.Id(), "_")
hostId := idParts[0]
nicId := idParts[1]
hostId, nicId := splitHostIdNicId(d)

err := removeVnic(client, hostId, nicId)
if err != nil {
Expand All @@ -149,6 +148,17 @@ func resourceVsphereNicDelete(d *schema.ResourceData, meta interface{}) error {
return resourceVsphereNicRead(d, meta)
}

func resourceVSphereNicImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
hostID, _ := splitHostIdNicId(d)

err := d.Set("host", hostID)
if err != nil {
return []*schema.ResourceData{}, err
}

return []*schema.ResourceData{d}, nil
}

// VmKernelSchema returns the schema required to represent a vNIC adapter on an ESX Host.
// We make this public so we can pull this from the host resource as well.
func BaseVMKernelSchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -262,9 +272,7 @@ func BaseVMKernelSchema() map[string]*schema.Schema {

func updateVNic(d *schema.ResourceData, meta interface{}) (string, error) {
client := meta.(*VSphereClient).vimClient
idParts := strings.Split(d.Id(), "_")
hostId := idParts[0]
nicId := idParts[1]
hostId, nicId := splitHostIdNicId(d)
ctx := context.TODO()

nic, err := getNicSpecFromSchema(d)
Expand Down Expand Up @@ -519,3 +527,8 @@ func getVnicFromHost(ctx context.Context, client *govmomi.Client, hostId, nicId
}
return &vNics[nicIdx], nil
}

func splitHostIdNicId(d *schema.ResourceData) (string, string) {
idParts := strings.Split(d.Id(), "_")
return idParts[0], idParts[1]
}