vRealize Orchestrator – Creating a naming convention using configuration elements

I often need a naming convention function and I use a very cool and easy script to give me a unique naming convention.

I take inputs from either an ASD form, or a catalogue form made up using drop down lists. For example, DEVWEB or PRODDB

I then match that naming convention in a configuration elememt and get the next availble number. Once I have that number, I just increment it, save it so it is ready to use next time I need to create a unique name.

In this case, the vmBaseName is the DEVWEB or PRODDB value

try {
	LockingSystem.lockAndWait(vmBaseName, workflow.id);

	if(namingConfigElement.getAttributeWithKey(vmBaseName) == null) {
		System.log("Creating attribute for " +vmBaseName);
		namingConfigElement.setAttributeWithKey(vmBaseName,1)		
	} 

	var number = namingConfigElement.getAttributeWithKey(vmBaseName).value;
	number++;
	namingConfigElement.setAttributeWithKey(vmBaseName,number)
	var hostName = vmBaseName + number;
	System.log("Generated hostname: "+hostName);	
} finally {
	LockingSystem.unlock(vmBaseName, workflow.id);
}

return hostName.toString();

This is what the configuration element looks like:
Screen Shot 2015-02-17 at 21.19.05

So in this example, the next server name would be DEVWEB1.

However I also may want a 3 digit number, beginning with lead zeros, so I use this function:

function pad(num, size) {

	var vmNumber = num+"";
	while (vmNumber.length < size) vmNumber = "0" + vmNumber;
	return vmNumber;

}

So when I look up the number, I use this line of code

var vmName = vmBaseName + pad(number,"3");

So in this case, the name I get returned is DEVWEB001. A very handy way of getting a virtual machine unique name.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s