I am working on my new 'VM auto deploy' workflow. The one thing I learned from the 1st I built was I do not like to use configuration items (too clumsy and breaks everything when an object changes).
I have this part of a module that finds networks based on naming convention, then selects a preferred network (VDS higher priority).
My main question is around the code concerning building the object based array. Is it the most efficient way to accomplish this? (the full module is attached)
// Find networks matching RegEx
var ipNetworkRegEx = ipNetwork.replace("\.","\\\.","g");
var regExNetwork = "^(" + ipNetworkRegEx + ")_(" + vdsNetworkBody + "|" + vssNetworkBody + ")(_" + vmNetworkSuffix + "|" + vmNetworkSuffix + ")";
var matchedHostNetworks = [];
for (i in allHostNetworks) {
var currentObj = allHostNetworks[i];
if (currentObj.name.match(new RegExp(regExNetwork,"gmi"))) {
matchedHostNetworks.push({
network:currentObj,
type:currentObj.vimType
});
}
}
for (key in matchedHostNetworks) {
System.log("Matched networks: " + matchedHostNetworks[key].network + " with type of " + matchedHostNetworks[key].type);
}
if (matchedHostNetworks.length < 1) {
System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.error("Module=SetVmNetwork");
System.error("No networks were found to match criteria: " + regExNetwork);
System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
throw exception;
}
if (matchedHostNetworks.length == 1) {
System.log("ESXi host " + host.name + " has a network that matched the criteria: " + matchedHostNetworks[0].network);
var choosenHostNetworks = matchedHostNetworks;
}
if (matchedHostNetworks.length > 1) {
System.log("Multiple networks matched the criteria.");
System.log("The following network meet the preference priority.");
var choosenHostNetworks = matchedHostNetworks.filter(function(item){
return item.type === networkPreference;
})
}
System.log("The final network choosen is: " + choosenHostNetworks[0].network);