"""
Python action to calculate Secure Hash Algorithm SHA256 values for
all actions.
@name getHashesForActions
@runtime python:3.11
@returns {Properties}
@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 0.2.0
Checked with Aria Automation 8.12.0, 8.17.0, 8.18.1 and
VCF Automation 9.0.0
"""
import hashlib
import json
from util.http import Http
http = Http()
def getAllActions(
vcoUrl: str,
bearerToken: str
) -> dict:
""" Get all actions
@params {string} vcoUrl - Url of the vRA orchestrator
@params {string} bearerToken
@returns {dictionary}
"""
returnValue: dict = {}
try:
returnValue = http.request(
url = vcoUrl + "/api/actions",
bearerToken = bearerToken
)
except Exception as err:
raise Exception("An error occurred at detecting all actions") \
from err
return returnValue
def getActionDetails(
actionHref: str,
bearerToken: str
) -> dict:
"""
Gets the details of the given action
@params {string} actionHref - Hyper reference of the action
@params {string} bearerToken
@returns {dictionary}
"""
returnValue: dict = {}
try:
returnValue = http.request(
url = actionHref,
bearerToken = bearerToken
)
except Exception as err:
raise Exception("An error occurred at detecting action details") \
from err
return returnValue
def handler(context: dict, inputs: dict) -> dict:
""" Aria Automation standard handler, the main function.
"""
results: dict = {}
output: dict = {}
try:
vcoUrl: str = context["vcoUrl"]
bearerToken: str = context["getToken"]()
actions: dict = getAllActions(
vcoUrl,
bearerToken
)
if not actions:
raise ValueError("No actions were detected")
for action in actions["link"]:
actionDetails: dict = getActionDetails(
action["href"],
bearerToken
)
if not actionDetails:
raise Exception("No action details were detected")
actionName: str = actionDetails["name"]
actionVersion: str = actionDetails["version"]
actionDescription: str = ""
if "description" in actionDetails:
actionDescription: str = actionDetails["description"]
actionOutputType: str = ""
if "output-type" in actionDetails:
actionOutputType = str(actionDetails["output-type"])
actionInputParameters: str = ""
if "input-parameters" in actionDetails:
actionInputParameters = \
str(actionDetails["input-parameters"])
actionScript = ""
if "script" in actionDetails:
actionScript = actionDetails["script"]
hashData: str = actionName + " ~ " + \
actionVersion + " ~ " + \
actionDescription + " ~ " + \
actionOutputType + " ~ " + \
actionInputParameters + " ~ " + \
actionScript
key: str = \
actionDetails["module"] + "." + actionDetails["name"]
value: str = hashlib.sha256(
hashData.encode("utf-8")
).hexdigest()
results[key] = value
output = dict(sorted(results.items()))
# for key in output:
# print(key, output[key])
outputs = {
"status": "done",
"error": None,
"results": output
}
except Exception as err:
outputs = {
"status": "incomplete",
"error": repr(err),
"results": None
}
return outputs
|