Created
November 15, 2018 21:48
-
-
Save standit/9400bdd56b4e4bf6fcb05bbfadf41d83 to your computer and use it in GitHub Desktop.
Get MAC Address to Portgroup Name Mapping of vCenter VM
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
// VMware vRealize Orchestrator action sample. Valid for vRA/vRO 7.0+ | |
// | |
// Get MAC Address to Portgroup Name Mapping of vCenter VM | |
// | |
// Action Inputs: | |
// cluster VC:ClusterComputeResource | |
// vm VC:VirtualMachine | |
// | |
// Action Output: | |
// return type: Properties | |
// MAC Address returned from vCenter has "-" which is replaced by ":" as in Windows OS | |
System.log("VM Name: "+vm.name); | |
var macAddressRgexp = new RegExp(":", "gim"); | |
var dvNetworkKeyToNameMapping = new Properties(); | |
var vmNetworks = vm.network; | |
for (var n in vmNetworks) { | |
//System.debug("----- VM Network "+n+" -----"); | |
vmNetwork = vmNetworks[n]; | |
if(vmNetwork instanceof VcDistributedVirtualPortgroup) { | |
//System.debug("Network is a Distributed Port Group"); | |
if(! dvNetworkKeyToNameMapping.get(vmNetwork.key)) { | |
dvNetworkKeyToNameMapping.put(vmNetwork.key,vmNetwork.summary.name); | |
} | |
} | |
} | |
var allNetworks = cluster.network; | |
for (var n in allNetworks) { | |
//System.debug("----- Cluster Network "+n+" -----"); | |
network = allNetworks[n]; | |
if(network instanceof VcDistributedVirtualPortgroup) { | |
//System.debug("Network is a Distributed Port Group"); | |
if(! dvNetworkKeyToNameMapping.get(network.key)) { | |
dvNetworkKeyToNameMapping.put(network.key,network.summary.name); | |
} | |
} | |
} | |
//System.debug("dvNetworkKeyToNameMapping JSON: "+JSON.stringify(dvNetworkKeyToNameMapping,null,4)); | |
macAddressToPortgroupMapping = new Properties(); | |
var vmDeviceArray = vm.config.hardware.device; | |
for (var i in vmDeviceArray) { | |
//System.debug("----- Device "+i+" -----"); | |
var device = vmDeviceArray[i]; | |
if(device && device.backing) { | |
deviceBacking = device.backing; | |
//Virtual Ethernet Adapters: E1000, E1000e, AMD Lance PCNet32, SR-IOV enabled | |
if(System.getModule("com.vmware.library.vc.vm.network").isSupportedNic(device)) { //all current types of VirtualEthernetCard | |
//System.debug("Device is instance of a Virtual Ethernet Card"); | |
var macAddress = device.macAddress; | |
//System.debug("MAC Address: "+macAddress); | |
if (deviceBacking instanceof VcVirtualEthernetCardNetworkBackingInfo) { | |
System.debug("Device Backing is standard port group"); | |
var portgroupName = device.backing.deviceName; | |
System.debug("\tPort Group Name: "+portgroupName); | |
macAddressToPortgroupMapping.put(macAddress.replace(macAddressRgexp, "-").toUpperCase(),portgroupName); | |
} else if (deviceBacking instanceof VcVirtualEthernetCardDistributedVirtualPortBackingInfo) { | |
System.debug("Device Backing is distributed port group"); | |
//System.debug("\t----- Device Connection to Switch -----"); | |
//var deviceBackingPort = device.backing.port; | |
//System.debug("\t\tPort key: "+device.backing.port.portKey); | |
//System.debug("\t\tPort Group key: "+device.backing.port.portgroupKey); | |
var portgroupKey = device.backing.port.portgroupKey; | |
var portgroupName = dvNetworkKeyToNameMapping.get(portgroupKey); | |
System.debug("\tPort Group Name: "+portgroupName); | |
macAddressToPortgroupMapping.put(macAddress.replace(macAddressRgexp, "-").toUpperCase(),portgroupName); | |
} else { | |
System.debug("Unknown Device Backing"); | |
} | |
} else { | |
System.debug("This is not a network type"); | |
continue; | |
} | |
} else { | |
continue; | |
} | |
} | |
System.debug("macAddressToPortgroupMapping JSON: "+JSON.stringify(macAddressToPortgroupMapping,null,4)); | |
return macAddressToPortgroupMapping; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment