vRA 8.1 – Upload file as part of a catalog request

A couple of weeks ago I’ve been working with a customer in this use case and I think is very useful to share with you

Basically, the idea is to upload a file in a Catalog Item Request and then, process it in vRO.

So, let’s start working!!

Service design

The idea for this simple service is to upload a file in Service Broker Catalog so the information in the file can be processed in a vRO workflow. For this example, the workflow itself doesn’t have any input parameters. The “file Upload” parameter is added in Form Designer and retrieved the value from workflow during execution.

vRA Catalog Item Form

From a Catalog perspective the Blueprint, for this example, is a simple XaaS workflow presented throught the Service Catalog.

This workflow doesn’t have any input parameters, all parameter are gathered via API calls in vRO workflow.

I’ve added, using the form designer, an input value of type “File Upload”, as we can see in the screenshot.

Here we can see the input to be used is a “File Upload” type, and no vRO input is associated with it.

vRO Workflow

For this example, the Workflow we are using, is divided in two main parts

Get Deployment Details”

  1. Get Token

First step is to get the token so we can start executing rest calls to vRA.

The following example is a simple code to obtain a token based on user and password:

//Get Token

var url = "/csp/gateway/am/api/login?access_token";

var requestContent = '{ "username": "' + username + '", "password": "' + password + '"}';

var request = vRARestHost.createRequest("POST", url, requestContent);

request.setHeader("Content-Type", "application/json");

request.setHeader("Cache-Control", "no-cache");

var RestResponse = request.execute();

var token = JSON.parse(RestResponse.contentAsString).access_token;

  1. Get Deployment based on deploymentId

Using the token we’ve obtained in the previous step, we will use vRA API to get the deployment details for the request.

The requisites to run this code are:

  • The API’s Url base to obtain the deployment details which is deployment/api/deployments/{deploymentId}
  • The deploymentId can be obtained using System.getContext().getParameter(“__metadata_deploymentId”)

A simple example code to obtain the deployment details:

//Get Deployment based on deploymentId

var deploymentId = System.getContext().getParameter("__metadata_deploymentId")

var url = "deployment/api/deployments/"+deploymentId

var request = vRARestHost.createRequest("GET",url,"")

request.setHeader("Content-Type", "application/json");

request.setHeader("Cache-Control", "no-cache");

request.setHeader("Authorization","Bearer "+token);

var RestResponse = request.execute();

Process Input variables”

  1. Get Input Variables

Based on the request details obtained previously, we will get the input variables for the request, that includes the file that we want to process

//Get Input Variables

var inputs = JSON.parse(RestResponse.contentAsString).inputs;

var ObjectKeys = Object.keys(inputs);

for(var i in ObjectKeys){

    if (ObjectKeys[i].indexOf("fileUpload")!=-1){

    var value = inputs[ObjectKeys[i]];

    var filelabel = value.label

    var filevalue = value.value.split(",")[1]

    System.log("filelabel: "+filelabel)

    System.log("filevalue: "+filevalue)

    var Base64fileData = filevalue

    };

};

  1. Transform Base64 file to String

The file is stored in the vRA request as base64, now we need to convert it to string variable

For this I’ve used an action developed in Python, because I already have this one for a previous workflow i made in the past. So, here, I’m calling that action

var fileValues = System.getModule("xxxx.xxxx.xxx.xx").convertBase64(Base64fileData);

System.log(fileValues)

The action I’m using is like this one:

import base64

import json

def handler(context, inputs):

    Base_file = inputs['Base64file']

    base_bytes = Base_file.encode('ascii')

    base_message = base64.b64decode(base_bytes)

    message = base_message.decode('ascii')

    return(message)

When I execute this catalog item from service Broker, the form will request the file to be used and the vRO workflow can capture that input and read the file without issues

Cheers and any comments are welcome!

See you in the next post!!

Comments

Leave a Reply

Your email address will not be published.