"""
This action creates an XML file with all details of all existing actions
and its script code in the log.
@name getActions
@param {string} in_moduleFilter - Search for substring in module, optional
@param {string} in_nameFilter - Search for substring in name, optional
@returns {Properties}
@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 2.0.0
@runtime python:3.10 and 3.11
Checked with VMware Aria Automation 8.17.0 and VCF Automation 9.0.0
"""
import json
import ssl
import urllib.request
_xmlEscapeTable = str.maketrans({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
# "\u00e4": "ae", "\u00c4": "Ae",
# "\u00f6": "oe", "\u00d6": "Oe",
# "\u00fc": "ue", "\u00dc": "Ue",
# "\u00df": "ss"
})
def escapeXML(strXML):
"""
Escapes characters in a string, which could be misinterpreted as
markup in XML.
@name escapeXML
@param {string} strXML - XML which characters to convert
@returns {string} Converted XML
"""
if strXML is None or strXML == "":
return ""
return str(strXML).translate(_xmlEscapeTable)
def getAllActions(vcoUrl, bearerToken):
"""
Get all actions.
@name getAllActions
@param {string} vcoUrl
@param {string} bearerToken
@returns {dictionary}
"""
returnValue = {}
try:
requestActions = urllib.request.Request(
url = vcoUrl + "/api/actions"
)
requestActions.add_header(
"Authorization", "Bearer " + bearerToken
)
requestActions.add_header(
"Content-Type", "application/json"
)
responseActions = urllib.request.urlopen(
requestActions,
context = ssl._create_unverified_context()
)
if responseActions.getcode() == 200:
returnValue = json.loads(responseActions.read())
except Exception as err:
raise Exception("An error occurred at detecting all actions") \
from err
return returnValue
def getActionDetails(actionHref, bearerToken):
"""
Gets the details of the given action.
@name getActionDetails
@param {string} actionHref
@param {string} bearerToken
@returns {dictionary}
"""
returnValue = {}
try:
requestAction = urllib.request.Request(
url = actionHref
)
requestAction.add_header(
"Authorization", "Bearer " + bearerToken
)
requestAction.add_header(
"Content-Type", "application/json"
)
responseAction = urllib.request.urlopen(
requestAction,
context = ssl._create_unverified_context()
)
if responseAction.getcode() == 200:
returnValue = json.loads(responseAction.read())
except Exception as err:
raise Exception("An error occurred at detecting action details") \
from err
return returnValue
def handler(context, inputs):
"""
Standard VCF Automation handler function.
"""
output = "<?xml version=\"1.0\"?>"
output += "<actions>"
try:
vcoUrl = context["vcoUrl"]
bearerToken = context['getToken']()
actions = getAllActions(
vcoUrl,
bearerToken
)
if not actions:
raise ValueError("No actions were detected")
# Sort actions by module and name
actionList = []
for action in actions["link"]:
fqn = ""
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"])
# Filter for substring in module
if inputs["in_moduleFilter"]:
actionList = [
x for x in actionList \
if inputs["in_moduleFilter"].lower() in x["fqn"].lower()
]
# Filter for substring in script name
if inputs["in_nameFilter"]:
actionList = [
x for x in actionList \
if inputs["in_nameFilter"].lower() in x["fqn"].lower()
]
# Write action data
for action in actionList:
actionDetails = getActionDetails(
action["href"],
bearerToken
)
if not actionDetails:
raise Exception("No action details were detected")
output += "<action module=\""
output += actionDetails["module"]
output += "\" name=\""
output += escapeXML(actionDetails["name"])
output += "\">"
# General
output += "<id>"
output += actionDetails["id"]
output += "</id>"
if "description" in actionDetails:
actionDescription = actionDetails["description"]
actionDescription = actionDescription.replace("\r", "")
actionDescription = actionDescription.replace("\n", "")
output += "<description>"
output += escapeXML(actionDescription)
output += "</description>"
else:
output += "<description/>"
output += "<version>"
output += actionDetails["version"]
output += "</version>"
if "runtime" in actionDetails:
output += "<runtime>"
output += actionDetails["runtime"]
output += "</runtime>"
else:
output += "<runtime/>"
# Script > Properties > Inputs
if "input-parameters" in actionDetails:
output += "<inputs>"
for inputParameter in actionDetails["input-parameters"]:
output += "<input>"
output += "<name>"
output += inputParameter["name"]
output += "</name>"
output += "<type>"
output += inputParameter["type"]
output += "</type>"
output += "<description>"
output += escapeXML(inputParameter["description"])
output += "</description>"
output += "</input>"
output += "</inputs>"
else:
output += "<inputs/>"
# Script > Properties > Return type
output += "<returnType>"
output += actionDetails["output-type"]
output += "</returnType>"
# Script > Code
if "script" in actionDetails:
output += "<script>"
output += escapeXML(actionDetails["script"])
output += "</script>"
else:
output += "<script/>"
output += "</action>"
output += "</actions>"
print(repr(str(output)))
outputs = {
"status": "done",
"result": output
}
except Exception as err:
outputs = {
"status": "incomplete",
"error": repr(err),
"result": output
}
return outputs
|