Skip to content

Instantly share code, notes, and snippets.

@jpadams
Last active June 6, 2018 19:49
Show Gist options
  • Save jpadams/3bd46c84198ceb29b5c8337fedd6cb97 to your computer and use it in GitHub Desktop.
Save jpadams/3bd46c84198ceb29b5c8337fedd6cb97 to your computer and use it in GitHub Desktop.
//inputs: string uuid, number timeout (minutes), number interval (polling interval in seconds)
//output: string actionResult (IP address)
//returns IP address or throws timeout exception
if (uuid == null) throw "vRA/vCAC VM IaaS UUID is required";
var remainingTime = timeout * 60; //convert timeout minutes to seconds
while(remainingTime > 0) {
try {
if (vm == undefined || vm == null) { //get VM once
var vm = Server.findForType("vCAC:VirtualMachine", uuid);
if (vm == null) throw "VM not found for UUID: " + uuid; //continue to wait for VM by UUID
var hostId = vm.getEntity().hostId;
}
if (host == undefined || host == null) {
var host = Server.findForType("vCAC:VCACHost", hostId); //get IaaS host once
if (host == null) throw "IaaS host object could not be found by id: " + hostId;
}
var vCACVmExtendedProps = System.getModule("com.vmware.library.vcac").getPropertiesFromVirtualMachine(host, vm);
if (vCACVmExtendedProps == null) throw "No properties yet for VM";
var address = vCACVmExtendedProps.get("__datacollected_ipaddress"); //get IP
if (address == null) throw "IP Address not present yet"; //continue to wait for IP address
System.log("Found IP Address for VM with UUID '" + uuid + "' on vRA IaaS host '" + host.name + "': " + address);
return address;
}
catch (e) {
System.log(e);
System.log("Waiting on IP address...");
System.sleep(interval * 1000);
remainingTime -= interval;
if (remainingTime <= 0) throw "Timeout waiting for IP address on VM: " + vm.name;
continue; //continue in while loop
}
}
throw "Timeout waiting for IP address on VM: " + vm.name;
@jpadams
Copy link
Author

jpadams commented Jun 6, 2018

Get the vm and the IaaS host once, poll for new copy of extended props to see if ipaddress is in them yet.
If anything we need is not present, throw an exception with a helpful message and wait for our polling interval and try again.
Do this until you find the ip address or run out of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment