Skip to content

Commit

Permalink
Fix vnic import (#1162)
Browse files Browse the repository at this point in the history
* Fix read func to better support importing

* Use dedicated import func

* Put some comments back

Co-authored-by: Waquid Valiya Peedikakkal <[email protected]>
  • Loading branch information
waquidvp and Waquid Valiya Peedikakkal authored Aug 14, 2020
1 parent 2728e19 commit e702103
Showing 1 changed file with 25 additions and 12 deletions.
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]
}

0 comments on commit e702103

Please sign in to comment.