Write to a vRO Configuration Element Attribute

You can store configuration values in vRealize Orchestrator with configuration elements.  VMware’s website states a configuration element is:

a list of attributes you can use to configure constants across a whole Orchestrator server deployment.

Configuration elements have attributes that store values which you can read from and write to in your scripts.  In this example, we will have a single configuration element called “testElement” that contains an attribute called “testAttrib”.  This attribute is an array of string values in which we will add entries using an action.

In the picture above, we have a folder called “Blog” that contains the configuration element “testElement”. It is important to know what the path is to the configuration element, because we will be searching in the folder for that specific name. Within the “testElement” we have the “testAttrib” that contains an empty array at this point.

We will use an action to find the configuration element, locate the attribute we want, and finally read the data into a variable. Once we have the data, we will have to save off the current data to a temporary variable, push the new values we want, and overwrite the data currently saved in the attribute. This action has one input variable that is a string called “testEntry”. This is the value we will add to the attribute array stored in the configuration element.


I created a brand new action called “blogConfigRead” for this article. This contains all of the code needed to read the data from the configuration elements. We can then insert this into a vRO workflow execute the action and print us the data we want.

Finding the Data:

First, we open the folder that contains the configuration element we want to read. If we don’t find it, we throw an exception:
var pgconfig = Server.getConfigurationElementCategoryWithPath('Blog');
if (pgconfig == null) {
throw "Configuration element path not found!!";
}

From there, we search for the config element by name:

for (var x=0;x<pgconfig.configurationElements.length;x++) {
//if we find one that matches the name of the testElement
if (pgconfig.configurationElements[x].name == 'testElement') {
//Save it off in a variable
var configElem = pgconfig.configurationElements[x];
System.debug("Found the testElement config element!");
break;
}
}

If we find the config element, then we save it off to a variable and break from the loop. If we get to the end of the loop and the variable is still set to ‘NULL’, then we didn’t find anything. We should just throw an exception to get out of the routine.

//Throw exception on error if we cannot find it!
if (configElem == null) {
throw "Configuration Element not found!";
}

Now we can finally search for the attribute by name:

//Get the attribute that is the portCatalog with all the entries
var testAttrib = configElem.getAttributeWithKey('testAttrib');
//Throw exception on error if we cannot find it!
if (testAttrib == null) {
throw "Attribute not found in configuration element!";
}

Again, if we did not find it, then we throw an exception.

Reading Data:

We are going to now save off the current array value for the attribute so that we can add an entry later. We will pull the values into an object:

//We are working with an array for this attribute, so let's get the current values. If it is null, create a blank array
if (testAttrib.value == null) {
var objArray = new Array()
} else {
var objArray = testAttrib.value;
}

Notice how we check to see if the value is null or “not set”. If it is, we need to just create a new Array so we can use the “push” string function below. Otherwise, we will get an exception error.

Finally, we are going to take out input value of “testEntry”, push that onto the objArray Object, and overwrite the attribute value with the new objArray.

//Add value to current array and then push new value to attribute
objArray.push(newEntry);
configElem.setAttributeWithKey('testAttrib',objArray);

Let’s create a workflow with one input variable and link it to the “testEntry” input variable needed to run the action.



Now that we have the workflow created, let’s add an entry by running it:

Looks good to me! Let’s check the configuration element:

Success!! ‘tester123’ was added to the array stored in the attribute. Let’s add another one:

It was successful and here it is in the configuration element:

Wrap-up:

Configuration elements are a nice and easy way to keep track of values within the vRO server. They can be used across multiple actions and workflows to help coordinate data. Full code is below:

//Get all the configuration elements on the server
var pgconfig = Server.getConfigurationElementCategoryWithPath('Blog');
//If we didn't find the config element, then we should die out
if (pgconfig == null) {
throw "Configuration element path not found!!"
}
//Loop through all the found config elements
for (var x=0;x<pgconfig.configurationElements.length;x++) {
//if we find one that matches the name of the testElement
if (pgconfig.configurationElements[x].name == 'testElement') {
//Save it off in a variable
var configElem = pgconfig.configurationElements[x];
System.debug("Found the testElement config element!");
break;
}
}
//Throw exception on error if we cannot find it!
if (configElem == null) {
throw "Configuration Element not found!";
}
//Get the attribute that is the portCatalog with all the entries
var testAttrib = configElem.getAttributeWithKey('testAttrib');
//Throw exception on error if we cannot find it!
if (testAttrib == null) {
throw "Attribute not found in configuration element!";
}
//We are working with an array for this attribute, so let's get the current values. If it is null, create a blank array
if (testAttrib.value == null) {
var objArray = new Array()
} else {
var objArray = testAttrib.value;
}
//Add value to current array and then push new value to attribute
objArray.push(newEntry);
configElem.setAttributeWithKey('testAttrib',objArray);

Hope this helps,

AJ

Leave a Reply