"""
@module de.stschnell
@version 0.1.0
@description Reads the content of an action as text.
@runtime python:3.10
@outputType Properties
"""
"""
@function getActionAsText
@param {string} in_moduleName - Module name which contains the action
@param {string} in_actionName - Action name which contains the text
@returns {Properties}
@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 0.1.0
Checked with Aria Automation 8.18.1
"""
import http
import json
import ssl
import urllib
def getActionId(
vcoUrl: str,
bearerToken: str,
actionModule: str,
actionName: str
) -> str:
returnValue: str = ""
response: http.client.HTTPResponse | None = None
try:
if (
not vcoUrl
or not vcoUrl.strip()
or not bearerToken
or not bearerToken.strip()
):
raise ValueError("vcoUrl or bearerToken argument "
"can not be undefined or null")
if (
not actionModule
or not actionModule.strip()
or not actionName
or not actionName.strip()
):
raise ValueError("actionModule or actionName argument "
"can not be undefined or null")
request: urllib.request.Request = urllib.request.Request(
url = vcoUrl + "/api/actions",
method = "GET"
)
request.add_header(
"Authorization", "Bearer " + bearerToken
)
request.add_header(
"Content-Type", "application/json;charset=utf-8"
)
request.add_header(
"Accept", "application/json"
)
response = urllib.request.urlopen(
request,
context = ssl._create_unverified_context()
)
if response.getcode() != 200:
raise RuntimeError(f"HTTP error: {response.getcode()}")
actions: dict = json.loads(
response.read().decode("utf-8")
)
found: bool = False
action: dict
for action in actions["link"]:
attribute: dict
for attribute in action["attributes"]:
if attribute["name"] == "fqn" and \
attribute["value"] == actionModule + "/" + actionName:
for attribute in action["attributes"]:
if attribute["name"] == "id":
returnValue = attribute["value"]
found = True
if found:
break
except Exception as err:
raise ValueError(
f"An error occurred at get Action ID - {err}"
) from err
finally:
if response:
response.close()
return returnValue
def getActionAsText(
vcoUrl: str,
bearerToken: str,
actionModule: str,
actionName: str
) -> str:
returnValue: str = ""
response: http.client.HTTPResponse | None = None
try:
actionId: str = getActionId(
vcoUrl,
bearerToken,
actionModule,
actionName
)
if actionId:
request: urllib.request.Request = urllib.request.Request(
url = vcoUrl + "/api/actions/" + actionId,
method = "GET"
)
request.add_header(
"Authorization", "Bearer " + bearerToken
)
request.add_header(
"Content-Type", "application/json;charset=utf-8"
)
request.add_header(
"Accept", "application/json"
)
response = urllib.request.urlopen(
request,
context = ssl._create_unverified_context()
)
if response.getcode() != 200:
raise RuntimeError(f"HTTP error: {response.getcode()}")
actionDetails: dict = json.loads(
response.read().decode("utf-8")
)
if actionDetails.get("script"):
returnValue = actionDetails["script"]
except Exception as err:
raise ValueError(
f"An error occurred at retrieve action definition - {err}"
) from err
finally:
if response:
response.close()
return returnValue
def handler(context: dict, inputs: dict) -> dict:
vcoUrl: str = context["vcoUrl"]
bearerToken: str = context["getToken"]()
# actionModule: str = inputs["in_moduleName"]
# actionName: str = inputs["in_actionName"]
actionModule: str = "com.vmware.basic"
actionName: str = "createDirectory"
result: str = ""
outputs: dict = {}
try:
result = getActionAsText(
vcoUrl,
bearerToken,
actionModule,
actionName
)
outputs = {
"status": "done",
"error": None,
"result": result
}
except Exception as err:
outputs = {
"status": "incomplete",
"error": repr(err),
"result": None
}
return outputs
|