VMware Cloud Community
StefanSchnell
Enthusiast
Enthusiast

Tip: How to Export vRO Configurations as JSON

It is possible to store variables in configuration elements in the vRealize Orchestrator (vRO). The structuring of these configuration elements is done in folders. It is possible to export these content as a package, or part of a package, in XML format. In this post I describe the possibility how to export vRO configurations as JSON in the log window via an action.

The action to export any configuration is very easy to understand. The first step is to detect the folder, the ConfigurationElementCategory. In the second step a loop is made over all elements and in the third step a loop over all attributes of an element. All data is collected in an output variable. This variable is displayed in the log window. The advantage of outputting in one line is that very large amounts of data can be output completed.

/* Begin----------------------------------------------------------------
 * Export of any configuration as JSON
 * @name exportConfigurationAsJSON
 * @AUTHOR Stefan Schnell
 * @version 1.0.0
 * @param {string} in_configurationName - Folder-Name der Konfiguration
 * @param {string} in_jsonName - Name des Root-Knotens in der JSON
 * @param {string} output - Konfiguration als JSON
 * @Example
 * in_configurationName = "web-root"
 * in_jsonName = "web-root"
 */

var output;

var configCategory = Server.getConfigurationElementCategoryWithPath(in_configurationName);
if (configCategory) {

  var configElements = configCategory.configurationElements;

  if (configElements != null) {

    output = ('{"' + in_jsonName + '": [');

    configElements.forEach( function(configElement, index) {

      output += '{"name": "' + configElement.name + '",';
      output += '"keys": [';

      configElement.attributes.forEach( function(attribute, index)  {
        var key = configElement.getAttributeWithKey(attribute.name);
        output += '{"name": "' + attribute.name + '",';
        if (index == configElement.attributes.length - 1) {
          output += '"value": ' + JSON.stringify(key.value) + '}';
        } else {
          output += '"value": ' + JSON.stringify(key.value) + '},';
        }
      });

      if (index == configElements.length - 1) {
        output += ']}';
      } else {
        output += ']},';
      }

    });

  output += ']}';

  }

}

System.log(output);
return output;

// End------------------------------------------------------------------

The following image shows the web-root folder with its elements as example.

exportConfigurationAsJSON001.jpg

Here in direct comparison the display of the configuration element.

exportConfigurationAsJSON002.jpg

It is not neccessary to enter each folder manually, all configuration items can also be output with an action using a loop over the entire folder structure.

/* Begin----------------------------------------------------------------
 * Detect folder structure of configurations
 * @name exportFolderStructureOfConfigurations
 * @AUTHOR Stefan Schnell
 * @version 1.0.0
 */

function getAllSubcatgories(configurationCategory, spaces) {
  var configurationSubCategories = configurationCategory.subCategories;
  spaces = spaces + "  ";
  if (configurationSubCategories) {
    configurationSubCategories.forEach( function(subCategory) {
      System.log(spaces + "Folder: " + subCategory.path + " - Name: " + subCategory.name);

      // If the configurations should be exported too
      // System.getModule("yourModule").exportConfigurationAsJSON(subCategory.path, subCategory.path)

      getAllSubcatgories(subCategory, spaces);
    });
  }
}

var configurationCategories = Server.getAllConfigurationElementCategories();
configurationCategories.forEach( function(configurationCategory) {
  System.log("Configuration: " + configurationCategory.path);
  getAllSubcatgories(configurationCategory, "");
});

// End------------------------------------------------------------------

This action loops over all folders (ConfigurationElementCategories) and detect all subcategories, using a recursive function. In this recursive function the call to output the configuration elements can be embedded (here commented out).

Conclusion

These two tiny code snippets offers us the possibility to export configuration elements of the vRO in JSON format, partial or complete. An alternative against the export via a package.


More interesting information at blog.stschnell.de

0 Kudos
0 Replies