-
Notifications
You must be signed in to change notification settings - Fork 452
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb832ff
Added network device discovery from Virtualhardware
c46af8d
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
e03a78e
Code cleanup
995d9e8
add errorhanling
f08a7d8
Use mvm instead of vm
mjrider 6d23f58
Add test case for VM refresh panic
vancluever File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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) | ||
|
@@ -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()) | ||
deviceList = deviceList.SelectByType((*types.VirtualEthernetCard)(nil)) | ||
log.Printf("Device list %+v", mvm.Config.Hardware.Device) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get the 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 { | ||
|
@@ -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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 themvm
MO, although the device being logged in debug is being sourced off ofmvm
. Is this an error or by design?