{"id":119,"date":"2020-08-26T10:34:02","date_gmt":"2020-08-26T10:34:02","guid":{"rendered":"http:\/\/192.168.10.3:8585\/?p=119"},"modified":"2020-11-18T00:05:18","modified_gmt":"2020-11-17T23:05:18","slug":"vra-8-1-upload-file-as-part-of-a-catalog-request","status":"publish","type":"post","link":"https:\/\/thecloudarchitects.es\/?p=119&lang=en","title":{"rendered":"vRA 8.1 \u2013 Upload file as part of a catalog request"},"content":{"rendered":"\n<p>A couple of weeks ago I\u2019ve been working with a customer in this use case and I think is very useful to share with you<\/p>\n\n\n\n<p>Basically, the idea is to upload a file in a Catalog Item Request and then, process it in vRO.<\/p>\n\n\n\n<p>So, let\u2019s start working!!<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Service design<\/h1>\n\n\n\n<p>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\u2019t have any input parameters. The \u201cfile Upload\u201d parameter is added in Form Designer and retrieved the value from workflow during execution.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">vRA Catalog Item Form<\/h1>\n\n\n\n<p>From a Catalog perspective the Blueprint, for this example, is a simple XaaS workflow presented throught the Service Catalog.<\/p>\n\n\n\n<p>This workflow doesn\u2019t have any input parameters, all parameter are gathered via API calls in vRO workflow.<\/p>\n\n\n\n<p>I\u2019ve added, using the form designer, an input value of type \u201cFile Upload\u201d, as we can see in the screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"478\" src=\"http:\/\/192.168.10.3:8585\/wp-content\/uploads\/2020\/08\/Form-Designer-FileUpload-1-1024x478.png\" alt=\"\" class=\"wp-image-120\" srcset=\"https:\/\/thecloudarchitects.es\/wp-content\/uploads\/2020\/08\/Form-Designer-FileUpload-1-1024x478.png 1024w, https:\/\/thecloudarchitects.es\/wp-content\/uploads\/2020\/08\/Form-Designer-FileUpload-1-300x140.png 300w, https:\/\/thecloudarchitects.es\/wp-content\/uploads\/2020\/08\/Form-Designer-FileUpload-1-768x359.png 768w, https:\/\/thecloudarchitects.es\/wp-content\/uploads\/2020\/08\/Form-Designer-FileUpload-1.png 1424w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here we can see the input to be used is a \u201cFile Upload\u201d type, and no vRO input is associated with it.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">vRO Workflow<\/h1>\n\n\n\n<p>For this example, the Workflow we are using, is divided in two main parts<\/p>\n\n\n\n<p>\u00ab<strong>Get Deployment Details\u00bb<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"a\"><li>Get Token<\/li><\/ol>\n\n\n\n<p>First step is to get the token so we can start executing rest calls to vRA.<\/p>\n\n\n\n<p>The following example is a simple code to obtain a token based on user and password:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>\/\/Get Token<\/code><\/p><p><code>var url = \"\/csp\/gateway\/am\/api\/login?access_token\";<\/code><\/p><p><code>var requestContent = '{ \"username\": \"' + username + '\", \"password\": \"' + password + '\"}';<\/code><\/p><p><code>var request = vRARestHost.createRequest(\"POST\", url, requestContent);<\/code><\/p><p><code>request.setHeader(\"Content-Type\", \"application\/json\");<\/code><\/p><p><code>request.setHeader(\"Cache-Control\", \"no-cache\");<\/code><\/p><p><code>var RestResponse = request.execute();<\/code><\/p><p><code>var token = JSON.parse(RestResponse.contentAsString).access_token;<\/code><\/p><\/blockquote>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Get Deployment based on <em>deploymentId<\/em><\/li><\/ol>\n\n\n\n<p>Using the token we\u2019ve obtained in the previous step, we will use vRA API to get the deployment details for the request.<\/p>\n\n\n\n<p>The requisites to run this code are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The API\u2019s Url base to obtain the deployment details which is <em>deployment\/api\/deployments\/{deploymentId}<\/em><\/li><li>The <em>deploymentId<\/em> can be obtained using <em>System.getContext().getParameter(\u00ab__metadata_deploymentId\u00bb)<\/em><\/li><\/ul>\n\n\n\n<p>A simple example code to obtain the deployment details:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>\/\/Get Deployment based on deploymentId<\/code><\/p><p><code>var deploymentId = System.getContext().getParameter(\"__metadata_deploymentId\")<\/code><\/p><p><code>var url = \"deployment\/api\/deployments\/\"+deploymentId<\/code><\/p><p><code>var request = vRARestHost.createRequest(\"GET\",url,\"\")<\/code><\/p><p><code>request.setHeader(\"Content-Type\", \"application\/json\");<\/code><\/p><p><code>request.setHeader(\"Cache-Control\", \"no-cache\");<\/code><\/p><p><code>request.setHeader(\"Authorization\",\"Bearer \"+token);<\/code><\/p><p><code>var RestResponse = request.execute();<\/code><\/p><p><\/p><\/blockquote>\n\n\n\n<p>\u00ab<strong>Process Input variables\u00bb<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"a\"><li>Get Input Variables<\/li><\/ol>\n\n\n\n<p>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<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>\/\/Get Input Variables<\/code><\/p><p><code>var inputs = JSON.parse(RestResponse.contentAsString).inputs;<\/code><\/p><p><code>var ObjectKeys = Object.keys(inputs);<\/code><\/p><p><code>for(var i in ObjectKeys){<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; if (ObjectKeys[i].indexOf(\"fileUpload\")!=-1){<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; var value = inputs[ObjectKeys[i]];<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; var filelabel = value.label<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; var filevalue = value.value.split(\",\")[1]<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; System.log(\"filelabel: \"+filelabel)<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; System.log(\"filevalue: \"+filevalue)<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; var Base64fileData = filevalue<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; };<\/code><\/p><p><code>};<\/code><\/p><\/blockquote>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Transform Base64 file to String<\/li><\/ol>\n\n\n\n<p>The file is stored in the vRA request as base64, now we need to convert it to string variable<\/p>\n\n\n\n<p>For this I\u2019ve used an action developed in Python, because I already have this one for a previous workflow i made in the past. So, here, I\u2019m calling that action<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>var fileValues = System.getModule(\"xxxx.xxxx.xxx.xx\").convertBase64(Base64fileData);<\/code><\/p><p><code>System.log(fileValues)<\/code><\/p><\/blockquote>\n\n\n\n<p>The action I\u2019m using is like this one:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>import base64<\/code><\/p><p><code>import json<\/code><\/p><p><code>def handler(context, inputs):<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; Base_file = inputs['Base64file']<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; base_bytes = Base_file.encode('ascii')<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; base_message = base64.b64decode(base_bytes)<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; message = base_message.decode('ascii')<\/code><\/p><p><code>&nbsp;&nbsp;&nbsp; return(message)<\/code><\/p><\/blockquote>\n\n\n\n<p>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<\/p>\n\n\n\n<p>Cheers and any comments are welcome!<\/p>\n\n\n\n<p>See you in the next post!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of weeks ago I\u2019ve 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\u2019s start working!! Service design The idea for this simple &hellip;<br \/><a href=\"https:\/\/thecloudarchitects.es\/?p=119&#038;lang=en\" class=\"more-link pen_element_default pen_icon_none\">Seguir leyendo <span class=\"screen-reader-text\">vRA 8.1 \u2013 Upload file as part of a catalog request<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":120,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[43],"tags":[57,59],"class_list":["post-119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vra_english","tag-vra_english","tag-vro_english"],"_links":{"self":[{"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/posts\/119"}],"collection":[{"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=119"}],"version-history":[{"count":6,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions\/130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=\/wp\/v2\/media\/120"}],"wp:attachment":[{"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecloudarchitects.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}