UCSD - REST API Browser Tab (XML Parsing)

Version 4
    Task Name

    UCSD has a Rest API browser.   The browser will render a URL.  This example takes the URL as input in make a request to UCSD for information.

    Description

     

    Prerequisites

    Minimum UCSD version: 5.4.0.2

    CategoryCustom task
    Components
    User Inputs

     

    User Output

    Instructions for Regular Workflow Use:

    Instructions for Regular Workflow Use:

    1. Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
    2. Unzip the file on your computer. Should end up with a .WFD file.
    3. Log in to UCS Director as a user that has "system-admin" privileges.
    4. Navigate to "Policies-->Orchestration" and click on "Import".
    5. Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
    6. Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
    7. Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
    8. Click "Submit".
    9. A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment.

     

    Inspired by Logesh Kandasamy

     

    The workflow:

     

    Screen Shot 2016-03-03 at 8.17.29 AM.png

     

    Task 1 input:

     

    Screen Shot 2016-03-03 at 8.17.21 AM.png

     

    The REST API call in question:

     

    Screen Shot 2016-03-03 at 8.23.27 AM.png

     

    The Resource URL:

     

    Screen Shot 2016-03-03 at 8.24.26 AM.png

     

    The response:

    Screen Shot 2016-03-03 at 8.25.18 AM.png

     

    Workflow Execution:

     

    Service Request ID: 6316

     

     

     

     

    Mar 03, 2016 13:20:37 UTC Request submitted

    Mar 03, 2016 13:20:40 UTC Executing workflow item number 1

    Mar 03, 2016 13:20:40 UTC Completed workflow item number 0, with status Completed

    Mar 03, 2016 13:20:47 UTC Executing workflow item number 2

    Mar 03, 2016 13:20:47 UTC Trigger context executeWorkFlowStep called

    Mar 03, 2016 13:20:47 UTC Executing custom action XMLAPITest (custom_XMLAPITest Custom Tasks)

    Mar 03, 2016 13:20:47 UTC Executing custom action XMLAPITest (custom_XMLAPITest Custom Tasks)

    Mar 03, 2016 13:20:47 UTC Executing custom script for XMLAPITest Custom Tasks

    Mar 03, 2016 13:20:50 UTC ip: 172.17.32.74

    Mar 03, 2016 13:20:50 UTC key: 5CF4C115F0034B189616B2B8EBA0F220

    Mar 03, 2016 13:20:50 UTC resourceurl: http://172.17.32.74/cloupia/api-v2/VLAN

    Mar 03, 2016 13:20:50 UTC Request:

    Mar 03, 2016 13:20:51 UTC Response status code: 200

    Mar 03, 2016 13:20:51 UTC Status message : OK

    Mar 03, 2016 13:20:51 UTC Response body: <cuicOperationResponse><cuicOperationStatus>0</cuicOperationStatus><response><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.8</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.6</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.6</Ip><ID>901</ID><Name>910t1</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports>Te1/0/1, Te1/0/2</Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>17</ID><Name>VLAN0017</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>32</ID><Name>MGMT</Name><Status>active</Status><Ports>Gi1/0/1, Gi1/0/2, Gi1/0/3, Gi1/0/4, Gi1/0/5, Gi1/0/6, Gi1/0/7, Gi1

    Mar 03, 2016 13:20:51 UTC Processing output: ResponseStatusCode

    Mar 03, 2016 13:20:52 UTC Processing output: StatusMessage

    Mar 03, 2016 13:20:53 UTC Processing output: ResponseBody

    Mar 03, 2016 13:20:54 UTC Task #1 (XMLAPITest (custom_XMLAPITest Custom Tasks)) completed successfully in 6 seconds

    Mar 03, 2016 13:20:54 UTC Input/Output values for Task #1 (XMLAPITest (custom_XMLAPITest Custom Tasks)):

    Mar 03, 2016 13:20:54 UTC [Local Input: IPAddress = 172.17.32.74]

    Mar 03, 2016 13:20:54 UTC [Local Input: Key = 5CF4C115F0034B189616B2B8EBA0F220]

    Mar 03, 2016 13:20:54 UTC [Local Input: ResourceURL = http://172.17.32.74/cloupia/api-v2/VLAN]

    Mar 03, 2016 13:20:54 UTC [Output: ResponseStatusCode = 200]

    Mar 03, 2016 13:20:54 UTC [Output: StatusMessage = OK]

    Mar 03, 2016 13:20:54 UTC [Output: ResponseBody = <cuicOperationResponse><cuicOperationStatus>0</cuicOperationStatus><response><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.8</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.6</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.6</Ip><ID>901</ID><Name>910t1</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>1</ID><Name>default</Name><Status>active</Status><Ports>Te1/0/1, Te1/0/2</Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>17</ID><Name>VLAN0017</Name><Status>active</Status><Ports></Ports></VLAN><VLAN><Datacenter>Default Datacenter</Datacenter><Ip>172.17.32.7</Ip><ID>32</ID><Name>MGMT</Name><Status>active</Status><Ports>Gi1/0/1, Gi1/0/2, Gi1/0/3, Gi1/0/4, Gi1/0/5, Gi1/0/6, Gi

    Mar 03, 2016 13:20:54 UTC Completed workflow item number 1, with status Completed

    Mar 03, 2016 13:20:55 UTC Executing workflow item number 3

    Mar 03, 2016 13:20:55 UTC Trigger context executeWorkFlowStep called

    Mar 03, 2016 13:20:55 UTC Executing custom action XMLAPITest (ExecuteCloupiaScript)

    Mar 03, 2016 13:20:55 UTC Executing custom action XMLAPITest (ExecuteCloupiaScript)

    Mar 03, 2016 13:20:58 UTC Executing cloupia script: xmlparsing

    Mar 03, 2016 13:20:58 UTC Root element :Cars

    Mar 03, 2016 13:20:58 UTC nList length: 3

    Mar 03, 2016 13:20:58 UTC Current Element : Car

    Mar 03, 2016 13:20:58 UTC Object: Opel

    Mar 03, 2016 13:20:58 UTC Current Element : Car

    Mar 03, 2016 13:20:58 UTC Object: Mercedes

    Mar 03, 2016 13:20:58 UTC Current Element : Car

    Mar 03, 2016 13:20:58 UTC Object: Porsche

    Mar 03, 2016 13:20:58 UTC Completed cloupia script xmlparsing

    Mar 03, 2016 13:20:58 UTC Task #2 (XMLAPITest (ExecuteCloupiaScript)) completed successfully in 3 seconds

    Mar 03, 2016 13:20:58 UTC Input/Output values for Task #2 (XMLAPITest (ExecuteCloupiaScript)):

    Mar 03, 2016 13:20:58 UTC [Local Input: Label = xmlparsing]

    Mar 03, 2016 13:20:58 UTC [Local Input: Script = importPackage(org.w3c.dom); importPackage(javax.xml.parsers); importPackage(java.io); importPackage(java.lang); importPackage(java.util); var factory = DocumentBuilderFactory.newInstance(); var builder = factory.newDocumentBuilder(); var xmlStringBuilder = new StringBuilder(); xmlStringBuilder.append('<?xml version='1.0'?><Cars><Car Type='System.String'>Opel</Car><Car Type='System.String'>Mercedes</Car><Car Type='System.String'>Porsche</Car></Cars>'); var input = new ByteArrayInputStream( xmlStringBuilder.toString().getBytes('UTF-8') ); var doc = builder.parse(input); doc.getDocumentElement().normalize(); var nList = doc.getElementsByTagName('Car'); logger.addInfo('Root element :' + doc.getDocumentElement().getNodeName()); logger.addInfo('nList length: ' +nList.getLength() ); for (var i = 0; i < nList.getLength(); i++) { var nNode = nList.item(i); logger.addInfo('\nCurrent Element : ' + nNode.getNodeName()); // logger.addInfo('\nNodeType : ' + nNode.getNodeType());

    Mar 03, 2016 13:20:58 UTC [Local Input: Undo Script = ]

    Mar 03, 2016 13:20:58 UTC Completed workflow item number 2, with status Completed

    Mar 03, 2016 13:20:59 UTC Executing workflow item number 4

    Mar 03, 2016 13:20:59 UTC Completed workflow item number 3, with status Completed

     

    The code for the API call:

     

    importPackage(org.apache.commons.httpclient);

    importPackage(org.apache.commons.httpclient.methods);

    importPackage(java.io);

    importPackage(org.w3c.dom);

    importPackage(com.cloupia.lib.cIaaS.ucs);

    importPackage(java.lang);

    importPackage(java.util);

    importPackage(com.cloupia.lib.cIaaS.ucs);

     

    var ip = input.IPAddress;

    var key = input.Key;

    var resourceurl =input.ResourceURL;

     

    logger.addInfo("ip: "+ ip);

    logger.addInfo("key: "+ key);

    logger.addInfo("resourceurl: "+ resourceurl);

     

    var strURL = resourceurl;

     

    var msgXML = "";

    logger.addInfo("Request: "+ msgXML);

     

    var httpMethod = new GetMethod(strURL);

    httpMethod.addRequestHeader("X-Cloupia-Request-Key", key);

    var httpclient = new HttpClient();

    var result = httpclient.executeMethod(httpMethod);

     

    logger.addInfo("Response status code: " + result);

    logger.addInfo("Status message : "+httpMethod.getStatusText());

    logger.addInfo("Response body: "+httpMethod.getResponseBodyAsString());

     

    output.ResponseStatusCode = result;

    output.StatusMessage = httpMethod.getStatusText();

    output.ResponseBody = httpMethod.getResponseBodyAsString();

     

     

     

    The code for the XML parsing:

     

    importPackage(org.w3c.dom);

    importPackage(javax.xml.parsers);

    importPackage(java.io);

    importPackage(java.lang);

    importPackage(java.util);

     

    var factory = DocumentBuilderFactory.newInstance();

    var builder = factory.newDocumentBuilder();

    var xmlStringBuilder = new StringBuilder();

     

    //xmlStringBuilder.append("<?xml version='1.0'?><Cars><Car Type='System.String'>Opel</Car><Car Type='System.String'>Mercedes</Car><Car Type='System.String'>Porsche</Car></Cars>");

     

    xmlStringBuilder.append(input.XML);

     

    //var a1 = "\"" + input.TAG + "\"";

    //var a1 = "''" + input.TAG + "''";

    var a1 = input.TAG;

    logger.addInfo("a1 : " + a1);

     

    var input =  new ByteArrayInputStream( xmlStringBuilder.toString().getBytes('UTF-8') );

    var doc = builder.parse(input);

    doc.getDocumentElement().normalize();

    //

    // note the TAG is hardcoded

    // I am still trying to figure out how to make a variable out of this

    //

    var nList = doc.getElementsByTagName("*");

    //var nList = doc.getElementsByTagName("VLAN");

    //var nList = doc.getElementsByTagName("Datacenter");

    //var nList = doc.getElementsByTagName(input.TAG);

    //var nList = doc.getElementsByTagName(a1);

     

    logger.addInfo("Root element :" + doc.getDocumentElement().getNodeName());

    logger.addInfo("nList length: " +nList.getLength() );

    for (var i = 0; i < nList.getLength(); i++) {

        var nNode = nList.item(i);

        logger.addInfo("\nCurrent Element : " + nNode.getNodeName());

       logger.addInfo("\nNodeType : " + nNode.getNodeType());

       logger.addInfo("typeof nNode: "+typeof nNode);

        var value = nNode.getTextContent();

            logger.addInfo("Object: " + value );

    }