VCF Automation Blog

from Stefan Schnell

It is possible to store variables in configuration elements in the Orchestrator. 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 configurations as JSON in the log window via an action.

How to Export Orchestrator Configurations as JSON


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 <mail@stefan-schnell.de>
 * @version 1.0.0
 *
 * @param {string} in_configurationName - Folder name of the configuration
 * @param {string} in_jsonName - Name of the root node in the JSON
 * @returns {Properties} output - Configuration as 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 JSON.parse(output);

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

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

vcf automation action to export configuration as json
Here in direct comparison the display of the configuration element.

vcf automation orchestrator configuration as example to the result of the action
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 <mail@stefan-schnell.de>
 * @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("de.stschnell")
      //   .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 detects 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 Orchestrator in JSON format, partial or complete. An alternative against the export via a package.