VMware Cloud Community
marsherian
Enthusiast
Enthusiast

Leveraging vRO REST API from within Node.JS actions

I am working on an action to interface with an external REST API. I am leveraging a custom node.JS Environment configuration to add in the required modules to interface with the API. However, I would like to access other actions within the Docker container running the environment. I have been able to access the API by passing the primary vRO URL as an environment variable and leveraging the 'https' module. I was hoping to leverage the map passed in to the hander as "context". 

This passes in a function getToken() which returns [object Promise]. Can this token be used to access the REST API, and is the context.vroURL the same as the primary vRO REST API?

Reply
0 Kudos
2 Replies
mayank_goyal
Enthusiast
Enthusiast

There are two parts to this. 

- Getting the token in Node.js: No, It seems to be working in other polyglots but due to the nature of Node.js, it seems only to return a promise that couldn't be used (Please comment if anyone else found a way to get an actual value instead of Promise)

- Using the token and URL for REST operations: Yes. Here is a piece of code where Python easily accesses these context parameters and performs REST calls.

import io
import mimetypes
from urllib import request
import uuid
import ssl
import base64

def handler(context, inputs):

    # Create the form with simple fields
    form = MultiPartForm()

    # Add the file
    form.add_file(
        'file', inputs["file"].split(',', 1)[0][1:],       # 'HelloWorld.workflow',
        fileHandle=io.BytesIO(base64.b64decode(inputs["file"].split('\n',1)[1])))
    # Build the request, including the byte-string
    # for the data to be posted.
    data = bytes(form)
    r = request.Request(context["vcoUrl"] + "/api/content/workflows/" + inputs["categoryId"] + "?overwrite=true", data=data)
    r.add_header('Content-type', form.get_content_type())
    r.add_header('Authorization', 'Bearer ' + context["getToken"]())​
.
.
.

Reference: https://kuklis.github.io/cma/post/vro8-import-workflow/



-
For more interesting content on Aria Automation, check my blog:
https://cloudblogger.co.in
Reply
0 Kudos
xian_
Expert
Expert

exports.handler = (context, inputs, callback) => {
    context.getToken()
        .then(token => {
            // console.log(token);
            return {
                method: "GET",
                headers: {
                    "Authorization": "Bearer " + token
                }
            }
        })
        .then(requestOptions => fetch(context.vcoUrl + "/api/server-configuration", requestOptions))
        .then(response => response.text())
        .then(result => {
            console.log(result);
            callback(null, result);
        })
}

I wrote a blog post about tokens with further details, see Getting token within vRO polyglot code | CMA

Reply
0 Kudos