VMware Cloud Community
KBragdon20
Contributor
Contributor

Get all Deployment info - Python to JavaScript

Hello everyone,

I have currently a Python script that does a Get request for a specified deployment (input is deployment_id).

I am trying to convert to JS to get around having to repackage it into a Zip file.

I tried a converter with no luck. Can anyone provide me with some direction:
Ordinal (edited) code:

<

import requests, json

def handler(context, inputs):
ca_cert = "Certificate_Athority.pem"
proxies = {
"http": None,
"https": None,
}

connection_creds = {'username': "BLANK", 'password': "WOULDNT_YOU_LOVE_TO_KNOW","domain": "System Domain"}

identity_token_request = requests.post('https://redacted/csp/gateway/am/api/login?access_token', \
json=connection_creds, headers={'Content-Type': 'application/json'}, proxies=proxies, verify=ca_cert)

access_token = identity_token_request.json()['refresh_token']

iaas_connection_creds = {'refreshToken': access_token}

iaas_token_request = requests.post('https://redacted/iaas/api/login', \
json=iaas_connection_creds, headers={'Content-Type': 'application/json'}, proxies=proxies, verify=ca_cert)

iaas_access_token = iaas_token_request.json()['token']

get_deployment = requests.get('https://redacted/deployment/api/deployments/{}'.format(inputs['deployment_id']), \
headers={'Accept': 'application/json', 'Authorization': 'Bearer ' + iaas_access_token}, proxies=proxies, verify=ca_cert)

get_resources = requests.get('https://redacted/deployment/api/deployments/{}/resources'.format(inputs['deployment_id']), \
headers={'Accept': 'application/json', 'Authorization': 'Bearer ' + iaas_access_token}, proxies=proxies, verify=ca_cert)

for resource in get_resources.json()['content']:
if resource['type'] == "Cloud.vSphere.Machine":
vm_name = resource['properties']['resourceName']
all_deployment_info = get_deployment.json()["inputs"]
all_deployment_info['createdBy'] = get_deployment.json()["createdBy"]
all_deployment_info['resource_vm_name'] = vm_name
return all_deployment_info

>

Here is what the converter spit out:

<

import * as requests from 'requests';
import * as json from 'json';
function handler(context, inputs) {
var access_token, all_deployment_info, ca_cert, connection_creds, get_deployment, get_resources, iaas_access_token, iaas_connection_creds, iaas_token_request, identity_token_request, proxies, vm_name;
ca_cert = "ca4.pem";
proxies = {"http": null, "https": null};
connection_creds = {"username": "blank", "password": "blank", "domain": "System Domain"};
identity_token_request = requests.post("https://redacted/csp/gateway/am/api/login?access_token", {"json": connection_creds, "headers": {"Content-Type": "application/json"}, "proxies": proxies, "verify": ca_cert});
access_token = identity_token_request.json()["refresh_token"];
iaas_connection_creds = {"refreshToken": access_token};
iaas_token_request = requests.post("https://redacted/iaas/api/login", {"json": iaas_connection_creds, "headers": {"Content-Type": "application/json"}, "proxies": proxies, "verify": ca_cert});
iaas_access_token = iaas_token_request.json()["token"];
get_deployment = requests.get("https://redacted/deployment/api/deployments/{}".format(inputs["deployment_id"]), {"headers": {"Accept": "application/json", "Authorization": ("Bearer " + iaas_access_token)}, "proxies": proxies, "verify": ca_cert});
get_resources = requests.get("https://redacted/deployment/api/deployments/{}/resources".format(inputs["deployment_id"]), {"headers": {"Accept": "application/json", "Authorization": ("Bearer " + iaas_access_token)}, "proxies": proxies, "verify": ca_cert});
for (var resource, _pj_c = 0, _pj_a = get_resources.json()["content"], _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
resource = _pj_a[_pj_c];
if ((resource["type"] === "Cloud.vSphere.Machine")) {
vm_name = resource["properties"]["resourceName"];
}
}
all_deployment_info = get_deployment.json()["inputs"];
all_deployment_info["createdBy"] = get_deployment.json()["createdBy"];
all_deployment_info["resource_vm_name"] = vm_name;
return all_deployment_info;
}

 

>

 

I am not a programmer, I am supposed to essentially be the admin but I am now having to fill this role.

I have taken the VRA ICM course on Udemy, but I am severely lacking knowledge regarding JS. I didn't write the original Python, just edits for new similar scripts. The first parts of the script are just authentication and obtaining a bearer token, then the actual request for the form inputs. I have three versions of this script that pull various information, this specific one should pull almost all of the information about the deployment.

Please help, and keeping it Python isn't really an option. We are required to use WinZip and the stupid thing is breaking the handler when it repackages the file... fun times. I have a similar problem with Windows Zip function also (I belive that this is a remnant of WinZip causing the issue).

Labels (3)
0 Kudos
2 Replies
Ankush11s
VMware Employee
VMware Employee

I am not JS guy but if you are looking for something in JS which you think in Python may not feasible i can try to give input on that.

Also on zip , from 8.8 onwards we can use environment ,do not to use zip method now.

0 Kudos
rwk1982
Enthusiast
Enthusiast

Hello!

Are you looking for a Orchestrator Action or pure JS Code?

Drink coffee.. Do stupid things faster with more energy...
0 Kudos