"""
Aria Automation handler emulation for Python with the use of the bearer
token.
Hint: All inputs are prepared for the HOL.
@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 0.2.0
"""
import getpass
import ssl
import urllib.request
# Begin-----------------------------------------------------------------
import json
def handler(context, inputs):
""" Standard Aria Automation handler function """
result = {}
try:
vcoUrl = context["vcoUrl"]
bearerToken = context['getToken']()
jsonOut = json.dumps(inputs, separators=(',', ':'))
print("Inputs were {0}".format(jsonOut))
outputs = {
"status": "done",
"error": None,
"result": result
}
except Exception as err:
outputs = {
"status": "incomplete",
"error": repr(err),
"result": result
}
return outputs
# End-------------------------------------------------------------------
def getBearerToken(url, userName, userPassword):
""" Delivers the bearer token """
try:
data = { 'username': userName, 'password': userPassword }
data = json.dumps(data)
data = str(data)
data = data.encode('utf-8')
# Gets refresh token
requestRefreshToken = urllib.request.Request(
url = url + "/csp/gateway/am/api/login?access_token",
method = "POST",
data = data
)
requestRefreshToken.add_header(
"Content-Type", "application/json"
)
responseRefreshToken = urllib.request.urlopen(
requestRefreshToken,
context = ssl._create_unverified_context()
)
if responseRefreshToken.getcode() == 200:
refreshToken = json.loads(responseRefreshToken.read())
data = { 'refreshToken': refreshToken["refresh_token"] }
data = json.dumps(data)
data = str(data)
data = data.encode('utf-8')
# Gets bearer token
requestBearerToken = urllib.request.Request(
url = url + "/iaas/api/login",
method = "POST",
data = data
)
requestBearerToken.add_header(
"Content-Type", "application/json"
)
responseBearerToken = urllib.request.urlopen(
requestBearerToken,
context = ssl._create_unverified_context()
)
if responseBearerToken.getcode() == 200:
bearerToken = json.loads(responseBearerToken.read())
return bearerToken["token"]
except Exception as err:
raise Exception("An error occurred at receiving Bearer token") \
from err
def getToken():
""" Delivers the bearer token at the handler context parameter """
userName = input("User name [holadmin@vcf.holo.lab]: ").strip() \
or "holadmin@vcf.holo.lab"
userPassword = getpass.getpass(prompt = "Password [VMware123!]: ") \
or "VMware123!"
return getBearerToken(url, userName, userPassword)
def main():
""" Main function """
global url
url = input("URL [https://rainpole.auto.vcf.sddc.lab]: ").strip() \
or "https://rainpole.auto.vcf.sddc.lab"
context = {
"executionId": "a92e5e2b-7e8e-4c83-ad16-34f4c4f68528",
"returnType": "Properties",
"vcoUrl": url + "/vco",
"getToken": getToken
}
inputs = {
}
outputs = handler(context, inputs)
print(outputs["status"])
print(outputs["error"])
print(outputs["result"])
# Main
if __name__ == "__main__" :
main()
|