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

Updates host get method to provide more info on the networking #313

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
73 changes: 67 additions & 6 deletions src/saltext/vmware/modules/esxi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2577,13 +2577,74 @@ def get(
ret[h.name]["datastores"][store.name]["capacity"] = store.summary.capacity
ret[h.name]["datastores"][store.name]["free_space"] = store.summary.freeSpace

ret[h.name]["nics"] = {}
ret[h.name]["vnics"] = {}
for nic in h.config.network.vnic:
ret[h.name]["nics"][nic.device] = {}
ret[h.name]["nics"][nic.device]["ip_address"] = nic.spec.ip.ipAddress
ret[h.name]["nics"][nic.device]["subnet_mask"] = nic.spec.ip.subnetMask
ret[h.name]["nics"][nic.device]["mac"] = nic.spec.mac
ret[h.name]["nics"][nic.device]["mtu"] = nic.spec.mtu
ret[h.name]["vnics"][nic.device] = {}
ret[h.name]["vnics"][nic.device]["ip_address"] = nic.spec.ip.ipAddress
ret[h.name]["vnics"][nic.device]["subnet_mask"] = nic.spec.ip.subnetMask
ret[h.name]["vnics"][nic.device]["mac"] = nic.spec.mac
ret[h.name]["vnics"][nic.device]["mtu"] = nic.spec.mtu
ret[h.name]["vnics"][nic.device]["portgroup"] = nic.spec.portgroup
if nic.spec.distributedVirtualPort:
ret[h.name]["vnics"][nic.device][
"distributed_virtual_portgroup"
] = nic.spec.distributedVirtualPort.portgroupKey
ret[h.name]["vnics"][nic.device][
"distributed_virtual_switch"
] = utils_vmware._get_dvs_by_uuid(
service_instance, nic.spec.distributedVirtualPort.switchUuid
).config.name
else:
ret[h.name]["vnics"][nic.device]["distributed_virtual_portgroup"] = None
ret[h.name]["vnics"][nic.device]["distributed_virtual_switch"] = None

ret[h.name]["pnics"] = {}
for nic in h.config.network.pnic:
ret[h.name]["pnics"][nic.device] = {}
ret[h.name]["pnics"][nic.device]["mac"] = nic.mac
if nic.linkSpeed:
ret[h.name]["pnics"][nic.device]["speed"] = nic.linkSpeed.speedMb
else:
ret[h.name]["pnics"][nic.device]["speed"] = -1

if not h.configManager.networkSystem.capabilities.supportsNetworkHints:
continue

# Add CDP/LLDP information
# TODO: Add LLDP information
ret[h.name]["pnics"][nic.device]["cdp"] = {}
for hint in h.configManager.networkSystem.QueryNetworkHint(nic.device):
csp = hint.connectedSwitchPort
if csp:
ret[h.name]["pnics"][nic.device]["cdp"]["switch_id"] = csp.devId
ret[h.name]["pnics"][nic.device]["cdp"]["system_name"] = csp.systemName
ret[h.name]["pnics"][nic.device]["cdp"]["platform"] = csp.hardwarePlatform
ret[h.name]["pnics"][nic.device]["cdp"]["ip_address"] = csp.address
ret[h.name]["pnics"][nic.device]["cdp"]["port_id"] = csp.portId
ret[h.name]["pnics"][nic.device]["cdp"]["vlan"] = csp.vlan

ret[h.name]["vswitches"] = {}
for vswitch in h.config.network.vswitch:
ret[h.name]["vswitches"][vswitch.name] = {}
ret[h.name]["vswitches"][vswitch.name]["mtu"] = vswitch.mtu
ret[h.name]["vswitches"][vswitch.name]["pnics"] = []
ret[h.name]["vswitches"][vswitch.name]["portgroups"] = []
for pnic in vswitch.pnic:
ret[h.name]["vswitches"][vswitch.name]["pnics"].append(
pnic.replace("key-vim.host.PhysicalNic-", "")
)
for pg in vswitch.portgroup:
ret[h.name]["vswitches"][vswitch.name]["portgroups"].append(
pg.replace("key-vim.host.PortGroup-", "")
)

ret[h.name]["portgroups"] = {}
for portgroup in h.config.network.portgroup:
ret[h.name]["portgroups"][portgroup.spec.name] = {}
ret[h.name]["portgroups"][portgroup.spec.name]["vlan_id"] = (portgroup.spec.vlanId,)
ret[h.name]["portgroups"][portgroup.spec.name][
"switch_name"
] = portgroup.spec.vswitchName

ret[h.name]["cpu_model"] = h.summary.hardware.cpuModel
ret[h.name]["num_cpu_cores"] = h.summary.hardware.numCpuCores
Expand Down
27 changes: 24 additions & 3 deletions src/saltext/vmware/utils/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,26 @@ def _get_dvs(service_instance, dvs_name):
return None


def _get_dvs_by_uuid(service_instance, dvs_uuid):
"""
Return a reference to a Distributed Virtual Switch object.
:param service_instance: PyVmomi service instance
:param dvs_uuid: UUID of DVS to return
:return: A PyVmomi DVS object
"""
switches = list_dvs(service_instance, ["uuid"])
if dvs_uuid in switches:
inventory = get_inventory(service_instance)
container = inventory.viewManager.CreateContainerView(
inventory.rootFolder, [vim.DistributedVirtualSwitch], True
)
for item in container.view:
if item.uuid == dvs_uuid:
return item

return None


def _get_pnics(host_reference):
"""
Helper function that returns a list of PhysicalNics and their information.
Expand Down Expand Up @@ -2348,14 +2368,15 @@ def list_folders(service_instance):
return utils_common.list_objects(service_instance, vim.Folder)


def list_dvs(service_instance):
def list_dvs(service_instance, properties=None):
"""
Returns a list of distributed virtual switches associated with a given service instance.

service_instance
The Service Instance Object from which to obtain distributed virtual switches.
properties
An optional list of object properties used to return reference results.
"""
return utils_common.list_objects(service_instance, vim.DistributedVirtualSwitch)
return utils_common.list_objects(service_instance, vim.DistributedVirtualSwitch, properties)


def list_vapps(service_instance):
Expand Down