VCF Automation Blog

from Stefan Schnell

Get Action as Text


JavaScript

/**
 * Reads the content of an action as text.
 *
 * Checked with Aria Automation 8.12.0, 8.13.1, 8.14.1, 8.17.0, 8.18.1
 * and 9.0.0
 *
 * @param {string} in_moduleName - Module which contains the action
 * @param {string} in_actionName - Action which contains the text
 * @returns {string} actionAsText
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.4.0
 *
 * @example
 * var in_moduleName = "com.vmware.basic";
 * var in_actionName = "createDirectory";
 */

var _getActionAsText = {

  main : function(moduleName, actionName) {

    var allActions =
      System.getModule("com.vmware.library.action").getAllActions();

    var allActionsInModule = allActions.filter( function(actionItems) {
      return actionItems.module.name === moduleName;
    });
    if (allActionsInModule.length === 0) {
      throw new Error("No actions were found in the module " +
        moduleName + ".");
    }

    var action = allActionsInModule.filter( function(actionItem) {
      return actionItem.name === actionName;
    });
    if (action.length === 0) {
      throw new Error("The action " + actionName +
        " was not found in the module " + moduleName + ".");
    } else if (action.length > 1) {
      System.warn("Too many actions, with the name " + actionName +
        ", were found in the module " + moduleName + ".");
      action.forEach( function(actionItem) {
        System.warn("ID: " + actionItem.id);
      });
      throw new Error("Too many actions, with the name " + actionName +
        ", were found in the module " + moduleName + ".");
    }

    return action[0].script;

  }

};

if (
  String(in_moduleName).trim() !== "" &&
  String(in_actionName).trim() !== ""
) {
  return _getActionAsText.main(
    in_moduleName,
    in_actionName
  );
} else {
  throw new Error(
    "in_moduleName or in_actionName argument can not be null"
  );
}

Python

def getActionAsText(
    vcoUrl: str,
    bearerToken: str,
    moduleName: str,
    actionName: str
) -> str:
    """ Delivers the content of an action as text.

    @param vcoUrl {string} - URL of Aria orchestrator
    @param bearerToken {string}
    @param moduleName {string} - Module of the action
    @param actionName {string} - Name of the action
    @returns {string}

    @author Stefan Schnell <mail@stefan-schnell.de>
    @license MIT
    @version 0.1.0
    """

    if moduleName.strip() == "" or actionName.strip() == "":
        raise ValueError(
            "moduleName or actionName argument can not be null"
        )

    http = Http()
    result: str = ""

    try:

        actions: dict = http.request(
            url = vcoUrl + "/api/actions",
            bearerToken = bearerToken
        )

        if not actions:
            raise ValueError("No actions were detected")

        actionList: list = []

        for action in actions["link"]:

            fqn: str = ""

            for attribute in action["attributes"]:
                if attribute["name"] == "fqn":
                    fqn = attribute["value"]

            if fqn != "":
                actionList.append(
                    {"href": action["href"], "fqn": fqn}
                )

        actionList.sort(key = lambda x: x["fqn"])

        actionList = [
            x for x in actionList \
            if (moduleName.lower() + "/" + actionName.lower()) \
            in x["fqn"].lower()
        ]

        for action in actionList:

            actionDetails = http.request(
                url = action["href"],
                bearerToken = bearerToken
            )
            if not actionDetails:
                raise Exception("No action details were detected")

            if "script" in actionDetails:
                result = actionDetails["script"]

    except Exception as err:
        raise Exception("Error at getActionAsText - " + repr(err))

    return result