VRA CustomHostnaming

From Bitbull Wiki
Jump to navigation Jump to search

1 General

I created this as an exercise, hopefully this is useful for others too. This workflow got developed and tested with vRealize Automation 7.1.

VRO custom hostname wf.png

File: defineHostname.js

// ----------------------------------------------- GLOBAL SETTINGS -----------------------------------------------------
var fallback_hostname_number_length = 4;

// ----------------------------------------------- FUNCTIONS -----------------------------------------------------------
// fill numbers with leading zeros
function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

// ---------------------------------------------- GET DATA -------------------------------------------------------------
// Retrieve the machine properties
var machine = payload.get("machine");
// Check the machine properties exists
if(machine != null) {
    // Retrieve the values required
    virtualMachineId = machine.get("id");
}
 
var machineProperties = machine.get('properties');
//Read the hostname generated by vRA
newHostname = machine.get('name');
System.log(workflow.currentWorkflow.name+" Default hostname is "+newHostname);

var custom_hostname_prefix_group = machineProperties.get("Custom.Hostname.Prefix.Group");
var custom_hostname_prefix = machineProperties.get("Custom.Hostname.Prefix."+custom_hostname_prefix_group);
var custom_hostname_uppercase = machineProperties.get("Custom.Hostname.Prefix."+custom_hostname_prefix_group+".UpperCase");


// -------------------------------------------- CHECK DATA VALIDITY ----------------------------------------------------
// check hostname prefix validity
if(!custom_hostname_prefix){
	throw("custom_hostname_prefix is null");
}else if(custom_hostname_prefix == ''){
	throw("custom_hostname_prefix is empty");
}

// ------------------------------------ CONSTRUCT NEW HOSTNAME NUMBER --------------------------------------------------
// -----------------hostname number length------------------
var element_exists = false;
var element_name = custom_hostname_prefix.toLowerCase()+"_number_length";
for each(element in PrefixConfigElement.attributes){
	if (element.name.toLowerCase() == element_name){
		element_exists = true;
		System.log("Configuration element "+PrefixConfigElement.name+" / "+element_name+" does exist" );
	}
}
if(!element_exists){
	System.warn("Configuration element "+PrefixConfigElement.name+" / "+element_name+" does not exist, so I create new one" );
	PrefixConfigElement.setAttributeWithKey(element_name , fallback_hostname_number_length);
}
var custom_hostname_number_length = PrefixConfigElement.getAttributeWithKey(element_name.toLowerCase()).value;

// -----------------hostname number-------------------------
var element_exists = false;
var element_name = custom_hostname_prefix.toLowerCase()+"_number";
for each(element in PrefixConfigElement.attributes){
	if (element.name.toLowerCase() == element_name){
		element_exists = true;
		System.log("Configuration element "+PrefixConfigElement.name+" / "+element_name+" does exist" );
	}
}
if(!element_exists){
	System.warn("Configuration element "+PrefixConfigElement.name+" / "+element_name+" does not exist, so I create new one" );
	PrefixConfigElement.setAttributeWithKey(element_name , 1);
}else{
	System.warn("Configuration element "+PrefixConfigElement.name+" / "+element_name+" does not exist, so I create new one" );
	PrefixConfigElement.setAttributeWithKey(element_name , Number(PrefixConfigElement.getAttributeWithKey(element_name.toLowerCase()).value) + 1);
}
var custom_hostname_number = PrefixConfigElement.getAttributeWithKey(element_name.toLowerCase()).value;

// ------------------------------------ CONSTRUCT NEW HOSTNAME ---------------------------------------------------------
// Build new hostname
newHostname = custom_hostname_prefix+pad(custom_hostname_number, custom_hostname_number_length);

// set to upper case or lower case
var convertToUpperCase = (custom_hostname_uppercase == 'true');
if(convertToUpperCase){
	newHostname= newHostname.toUpperCase()
}else{
		newHostname= newHostname.toLowerCase()
}

System.log(workflow.currentWorkflow.name+" New hostname will be "+newHostname);

1.1 Concept

  • Select DropDown "hostname set" as a property in vRA
  • Store Hostname Numbers for every pattern as configuration element in vRO
  • Store hostname number length as configuration element in vRO
  • If configuration elements do not exist (eg. new pattern) create them automatically
  • Make it easy to work with property group in vRA to add prefix collection to blueprint component

2 Configuration

2.1 Workflows

You can import my package from github:
https://github.com/joe-speedboat/vRealizeOrchestrator/blob/master/ch.bitbull.hostname.package

2.2 vRA Event Broker

Create new Subscription.
Add the following conditions:

  • Data > Lifecycle state name Equals VMPSMasterWorkflow32.BuildingMachine
  • Data > Lifecycle state phase Equals PRE
  • Data > Machine > Machine type Equals Virtual Machine
Vra custom hostname 1.png


Vra custom hostname 2.png


Vra custom hostname 3.png


Vra custom hostname 4.png


Do not forget to hand over the properties from EB to vRO, by setting this custom property to Blueprint Component:

Extensibility.Lifecycle.Properties.VMPSMasterWorkflow32.BuildingMachine = *

2.3 Create Properties / Property Group

Create vRA properties like:
Note: PRD is a placeholder for a set of hostname prefixes

Extensibility.Lifecycle.Properties.VMPSMasterWorkflow32.BuildingMachine -> *
Custom.Hostname.Prefix.Group -> empty (set in property group)
Custom.Hostname.Prefix.PRD -> drop down list with needed prefixes
Custom.Hostname.Prefix.PRD.UpperCase -> [true|false] (as you need)

Below you can find an example:

Vra custom hostname 5.png


Vra custom hostname 6.png


Add all the needed patterns to the dropdown list


Vra custom hostname 8.png


2.4 Final note

Now you can add the property group to a blueprint component and get the desired dropdown.

3 Links