Skip to content

Instantly share code, notes, and snippets.

@virtualhobbit
Created May 19, 2020 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualhobbit/e08b26bfd0de15d2362976b5ab82eb7b to your computer and use it in GitHub Desktop.
Save virtualhobbit/e08b26bfd0de15d2362976b5ab82eb7b to your computer and use it in GitHub Desktop.
// Retrieve the virtual machine properties
var machine = payload.get("machine");
// Check the machine's properties exist
if (machine !== null) {
// Retrieve the values
var attVirtualMachineId = machine.get("id");
}
// Get the hostname generated by vRA
var currentHostname = machine.get("name");
System.log("Assigned hostname is: " + currentHostname);
// Retrieve the variables
var machineProperties = machine.get("properties");
var datacenterLocation = machineProperties.get("Vrm.DataCenter.Location");
var location;
switch(datacenterLocation) {
case "Utrecht":
location = "NL-UTC";
break;
case "Southport":
location = "GB-SRT";
break;
}
// Set the environment type
var environment = System.getModule("com.hobbitcloud.misc").getConfigurationElement("_HobbitCloud/vRA","Environment","attName");
switch(environment) {
case "Development":
environment = "D";
break;
case "Test":
environment = "T";
break;
case "Staging":
environment = "S";
break;
case "Production":
environment = "P";
break;
}
// Get the workload type
var workloadType = machineProperties.get("hc.workloadType");
// Create the lock to prevent concurrent execution
LockingSystem.lockAndWait(attLockId, attLockOwner);
// Get the current highest VM number
var currentNum = System.getModule("com.hobbitcloud.misc").getConfigurationElement("_HobbitCloud/vRA/Naming",workloadType,"attNumber");
// Generate the next VM number
var newNum = System.getModule("com.hobbitcloud.vra.naming").newNum(currentNum);
// Write the new number back
System.getModule("com.hobbitcloud.misc").setConfigurationElement("_HobbitCloud/vRA/Naming",workloadType,"attNumber",newNum);
// Release the lock
LockingSystem.unlock(attLockId, attLockOwner);
// Construct new hostname
var attNewHostname = location + "-" + environment + "-" + workloadType + "-" + newNum;
// Return the new hostname
System.log("The new hostname is: " + attNewHostname);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment