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

Discovery network interfaces from pci devices instead from Guest information #129

Merged
merged 6 commits into from
Aug 17, 2017
Merged
Changes from 2 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
90 changes: 71 additions & 19 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ func resourceVSphereVirtualMachine() *schema.Resource {
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},

"label": &schema.Schema{
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -979,7 +984,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})

log.Printf("[DEBUG] Datacenter - %#v", dc)
log.Printf("[DEBUG] mvm.Summary.Config - %#v", mvm.Summary.Config)
log.Printf("[DEBUG] mvm.Summary.Config - %#v", mvm.Config)
log.Printf("[DEBUG] mvm.Config - %#v", mvm.Config)
log.Printf("[DEBUG] mvm.Guest.Net - %#v", mvm.Guest.Net)

err = d.Set("moid", mvm.Reference().Value)
Expand Down Expand Up @@ -1057,32 +1062,54 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Invalid disks to set: %#v", disks)
}

// start
networkInterfaces := make([]map[string]interface{}, 0)

deviceList, err := vm.Device(context.TODO())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjrider just a note here - am I right in assuming that err is not being caught here?

Also = I notice this device list is being sourced off of the vm MO instead of the mvm MO, although the device being logged in debug is being sourced off of mvm. Is this an error or by design?

deviceList = deviceList.SelectByType((*types.VirtualEthernetCard)(nil))
log.Printf("Device list %+v", mvm.Config.Hardware.Device)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get the [DEBUG] prefix here?

Looks like there are some other entries that need this as well - can we get those there as well?

for _, device := range deviceList {
networkInterface := make(map[string]interface{})
log.Printf("device %+v", device)
virtualDevice := device.GetVirtualDevice()
nic := device.(types.BaseVirtualEthernetCard)
log.Printf("virtualDevice info %+v\n", virtualDevice)
DeviceName, _ := getNetworkName(client, vm, nic)
log.Printf("device name %s", DeviceName)

networkInterface["label"] = DeviceName
networkInterface["mac_address"] = nic.GetVirtualEthernetCard().MacAddress
networkInterface["key"] = virtualDevice.Key
networkInterfaces = append(networkInterfaces, networkInterface)
}
log.Printf("[DEBUG] networks: %#v", networkInterfaces)
// end

for _, v := range mvm.Guest.Net {
if v.DeviceConfigId >= 0 {
log.Printf("[DEBUG] v.Network - %#v", v.Network)
networkInterface := make(map[string]interface{})
networkInterface["label"] = v.Network
networkInterface["mac_address"] = v.MacAddress
for _, ip := range v.IpConfig.IpAddress {
p := net.ParseIP(ip.IpAddress)
if p.To4() != nil {
log.Printf("[DEBUG] p.String - %#v", p.String())
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
networkInterface["ipv4_address"] = p.String()
networkInterface["ipv4_prefix_length"] = ip.PrefixLength
} else if p.To16() != nil {
log.Printf("[DEBUG] p.String - %#v", p.String())
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
networkInterface["ipv6_address"] = p.String()
networkInterface["ipv6_prefix_length"] = ip.PrefixLength
for _, networkInterface := range networkInterfaces {
if networkInterface["key"] == v.DeviceConfigId {
for _, ip := range v.IpConfig.IpAddress {
p := net.ParseIP(ip.IpAddress)
if p.To4() != nil {
log.Printf("[DEBUG] p.String - %#v", p.String())
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
networkInterface["ipv4_address"] = p.String()
networkInterface["ipv4_prefix_length"] = ip.PrefixLength
} else if p.To16() != nil {
log.Printf("[DEBUG] p.String - %#v", p.String())
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
networkInterface["ipv6_address"] = p.String()
networkInterface["ipv6_prefix_length"] = ip.PrefixLength
}
}
log.Printf("[DEBUG] networkInterface: %#v", networkInterface)
}
log.Printf("[DEBUG] networkInterface: %#v", networkInterface)
}
log.Printf("[DEBUG] networkInterface: %#v", networkInterface)
networkInterfaces = append(networkInterfaces, networkInterface)
}
}
log.Printf("[DEBUG] networks: %#v", networkInterfaces)
if mvm.Guest.IpStack != nil {
for _, v := range mvm.Guest.IpStack {
if v.IpRouteConfig != nil && v.IpRouteConfig.IpRoute != nil {
Expand Down Expand Up @@ -2164,3 +2191,28 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
}
return nil
}
func getNetworkName(c *govmomi.Client, vm *object.VirtualMachine, nic types.BaseVirtualEthernetCard) (string, error) {
backingInfo := nic.GetVirtualEthernetCard().Backing
var deviceName string
switch backingInfo.(type) {
case *types.VirtualEthernetCardNetworkBackingInfo:
deviceName = backingInfo.(*types.VirtualEthernetCardNetworkBackingInfo).DeviceName
break
case *types.VirtualEthernetCardDistributedVirtualPortBackingInfo:
portInfo := backingInfo.(*types.VirtualEthernetCardDistributedVirtualPortBackingInfo).Port
log.Printf("network Port %#v", portInfo)
o := object.NewDistributedVirtualPortgroup(c.Client, types.ManagedObjectReference{
Type: "DistributedVirtualPortgroup",
Value: portInfo.PortgroupKey,
})
var dvp mo.DistributedVirtualPortgroup
err := o.Properties(context.TODO(), o.Reference(), []string{"name", "config.distributedVirtualSwitch"}, &dvp)
if err != nil {
log.Printf("[ERROR]: Error retrieving portgroup %v", err)
return "", err
}
deviceName = dvp.Name
}
log.Printf("network Port DeviceName %#v", deviceName)
return deviceName, nil
}