cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2708
Views
2
Helpful
4
Comments
Orf Gelbrich
Cisco Employee
Cisco Employee
Task Name

Example on REST API integration

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.

Thank you Ryan Criss.

The Custom Task:

Screen Shot 2016-02-25 at 11.25.48 AM.png

The Document:

Screen Shot 2016-02-25 at 11.35.57 AM.pngScreen Shot 2016-02-25 at 11.36.05 AM.png

Screen Shot 2016-02-25 at 11.36.12 AM.pngScreen Shot 2016-02-25 at 11.36.18 AM.png

Screen Shot 2016-02-25 at 11.36.25 AM.png

Screen Shot 2016-02-25 at 11.36.32 AM.png

Screen Shot 2016-02-25 at 11.36.40 AM.png

Screen Shot 2016-02-25 at 11.36.48 AM.png

Screen Shot 2016-02-25 at 11.36.54 AM.png

Screen Shot 2016-02-25 at 11.37.01 AM.png

Screen Shot 2016-02-25 at 11.37.07 AM.png

Screen Shot 2016-02-25 at 11.37.13 AM.png

Screen Shot 2016-02-25 at 11.37.20 AM.png

Screen Shot 2016-02-25 at 11.37.26 AM.png

Screen Shot 2016-02-25 at 11.37.32 AM.png

Screen Shot 2016-02-25 at 11.37.39 AM.png

Screen Shot 2016-02-25 at 11.37.45 AM.png

Screen Shot 2016-02-25 at 11.37.52 AM.png

Screen Shot 2016-02-25 at 11.38.00 AM.png

Screen Shot 2016-02-25 at 11.38.06 AM.png

The Code Example:

importPackage(com.cloupia.lib.util);

importPackage(org.apache.commons.httpclient);

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

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

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

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

//----------------------------------------------------------------------------------------

//

// Author: Ryan Criss (rcriss@cisco.com)

//

// Workflow Task Name: Open Library REST Example

//

// Version: 1.0

//

// Description:

// This custom task retrieves book details from the Open Library database when given an ISBN-10

//

// Inputs:

// ISBN10: The ISBN 10 of the book

//

// Outputs:

// None

//

//----------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------

//                                 ### FUNCTIONS ###

//----------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------

//

//    Created By: Ryan Criss (rcriss@cisco.com)

//

// Function Name: openlibraryRetrieveBookNameByIsbn()

//

//       Version: 1.0

//

//   Description: Retrieves the book name given an ISBN10

//

//       Inputs:

// isbn10: The ISBN-10 of the book

//

//       Outputs:

// bookName: The book name

//

//----------------------------------------------------------------------------------------

function openlibraryRetrieveBookNameByIsbn10(isbn10) {

  logger.addInfo("function openlibraryRetrieveBookNameByIsbn10");

  // Setup HTTPS Connection

  var httpClient = new HttpClient();

  httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");

  httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);

  httpMethod = new GetMethod("/query.json?type=/type/edition&isbn_10="+isbn10+"&*=&limit=1");

  // If basic authorization is required, you could add an authorization header

  // httpMethod.addRequestHeader("Authorization", "Basic Y9laY346UjL7GmFAdSE=");

  // If an authorization cookie is required, you can add a cookie

  // httpMethod.addRequestHeader("Cookie", "cookiestring");

  // Execute HTTPS and Return Results

  httpClient.executeMethod(httpMethod);

  statusCode = httpMethod.getStatusCode();

  response = httpMethod.getResponseBodyAsString();

  httpMethod.releaseConnection();

  // Check Response Code and Parse Results

  if (statusCode == 200){

  logger.addInfo("Response received: "+response);

  var jsonResponse = JSON.getJsonElement(response, null);

  // Make sure the query found a matching book.

  if (jsonResponse.size() == 0){

  logger.addError("Request Failed. The book search did not find any books with that ISBN-10.");

  ctxt.setFailed("Open Library book search failed.");

  ctxt.exit();

  }

  // Retrieve the Title from the JSON object

  var bookName = jsonResponse.get(0).get("title").getAsString();

  logger.addInfo("Book Name: " + bookName);

  return bookName;

  } else {

  // HTTP Request Failed

  logger.addError("Request Failed. HTTP response code: "+statusCode);

  logger.addError("Response = " + response); 

  ctxt.setFailed("Open Library request failed.");

  ctxt.exit();

  }

} //function openlibraryRetrieveBookNameByIsbn10

//----------------------------------------------------------------------------------------

//

//    Created By: Ryan Criss (rcriss@cisco.com)

//

// Function Name: openlibraryRetrieveBookDetailsByIsbn()

//

//       Version: 1.0

//

//   Description: Retrieves the book details given an ISBN10

//

//       Inputs:

// isbn10: The ISBN-10 of the book

//

//       Outputs:

// bookDetails: The book details as an object with individual details stored as properties

//

//----------------------------------------------------------------------------------------

function openlibraryRetrieveBookDetailsByIsbn10(isbn10) {

  logger.addInfo("function openlibraryRetrieveBookDetailsByIsbn10");

  // Setup HTTPS Connection

  var httpClient = new HttpClient();

  httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");

  httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);

  httpMethod = new GetMethod("/query.json?type=/type/edition&isbn_10="+isbn10+"&*=&limit=1");

  // If basic authorization is required, you could add an authorization header

  // httpMethod.addRequestHeader("Authorization", "Basic Y936glaY346UjhL7Gm3FAdSE=");

  // If an authorization cookie is required, you can add a cookie

  // httpMethod.addRequestHeader("Cookie", "cookiestring");

  // Execute HTTPS and Return Results

  httpClient.executeMethod(httpMethod);

  statusCode = httpMethod.getStatusCode();

  response = httpMethod.getResponseBodyAsString();

  httpMethod.releaseConnection();

  // Check Response Code and Parse Results

  if (statusCode == 200){

  logger.addInfo("Response received: "+response);

  var jsonResponse = JSON.getJsonElement(response, null);

  // Make sure the query found a matching book.

  if (jsonResponse.size() == 0){

  logger.addError("Request Failed. The book search did not find any books with that ISBN-10.");

  ctxt.setFailed("Open Library book search failed.");

  ctxt.exit();

  }

  // Retrieve the Details from the JSON object

  var bookDetails = {};

  // Book Title

  bookDetails.title = jsonResponse.get(0).get("title").getAsString();

  logger.addInfo("Book Title: " + bookDetails.title);

  // Book Description

  bookDetails.description = jsonResponse.get(0).get("description").getAsString();

  logger.addInfo("Book Description: " + bookDetails.description);

  // Book Genres - Might be multiple genres, so will create an array

  bookDetails.genres = [];

  for (var i = 0; i < jsonResponse.get(0).get("genres").size(); i++){

  bookDetails.genres.push(jsonResponse.get(0).get("genres").get(i).getAsString());

  }

  logger.addInfo("Book Genres: " + bookDetails.genres.toString());

  // Book Publishers - Might be multiple publishers, so will create an array

  bookDetails.publishers = [];

  for (var i = 0; i < jsonResponse.get(0).get("publishers").size(); i++){

  bookDetails.publishers.push(jsonResponse.get(0).get("publishers").get(i).getAsString());

  }

  logger.addInfo("Book Publishers: " + bookDetails.publishers.toString());

  // Author Keys - Might be multiple author keys, so will create an array

  bookDetails.authorKeys = [];

  for (var i = 0; i < jsonResponse.get(0).get("authors").size(); i++){

  bookDetails.authorKeys.push(jsonResponse.get(0).get("authors").get(i).get("key").getAsString());

  }

  logger.addInfo("Author Keys: " + bookDetails.authorKeys.toString());

  // Return the bookDetails Object

  return bookDetails;

  } else {

  // HTTP Request Failed

  logger.addError("Request Failed. HTTP response code: "+statusCode);

  logger.addError("Response = " + response); 

  ctxt.setFailed("Open Library request failed.");

  ctxt.exit();

  }

} //function openlibraryRetrieveBookDetailsByIsbn10

//----------------------------------------------------------------------------------------

//

//    Created By: Ryan Criss (rcriss@cisco.com)

//

// Function Name: openlibraryRetrieveAuthorNameByAuthorKey()

//

//       Version: 1.0

//

//   Description: Retrieves the author name given an author key

//

//       Inputs:

// authorKey: The author key

//

//       Outputs:

// authorName: The author name

//

//----------------------------------------------------------------------------------------

function openlibraryRetrieveAuthorNameByAuthorKey(authorKey) {

  logger.addInfo("function openlibraryRetrieveAuthorNameByAuthorKey");

  // Setup HTTPS Connection

  var httpClient = new HttpClient();

  httpClient.getHostConfiguration().setHost("openlibrary.org", 443, "https");

  httpClient.getHostConfiguration().setProxy("proxy.test.com", 80);

  httpMethod = new GetMethod(authorKey+".json");

  // If basic authorization is required, you could add an authorization header

  // httpMethod.addRequestHeader("Authorization", "Basic Y9laY346UjL7GmFAdSE=");

  // If an authorization cookie is required, you can add a cookie

  // httpMethod.addRequestHeader("Cookie", "cookiestring");

  // Execute HTTPS and Return Results

  httpClient.executeMethod(httpMethod);

  statusCode = httpMethod.getStatusCode();

  response = httpMethod.getResponseBodyAsString();

  httpMethod.releaseConnection();

  // Check Response Code and Parse Results

  if (statusCode == 200){

  logger.addInfo("Response received: "+response);

  var jsonResponse = JSON.getJsonElement(response, null);

  // Retrieve the author name from the JSON object

  var authorName = jsonResponse.get("name").getAsString();

  logger.addInfo("Author Name: " + authorName);

  return authorName;

  } else {

  // HTTP Request Failed

  logger.addError("Request Failed. HTTP response code: "+statusCode);

  logger.addError("Response = " + response); 

  ctxt.setFailed("Open Library request failed.");

  ctxt.exit();

  }

} //function openlibraryRetrieveAuthorNameByAuthorKey

//----------------------------------------------------------------------------------------

//                                 ### MAIN PROGRAM ###

//----------------------------------------------------------------------------------------

// Workflow Inputs Mappings

var isbn10 = input.isbn10;

// Retrieve Book Name By ISBN-10 (Example to show retrieving and returning a single item)

var bookName = openlibraryRetrieveBookNameByIsbn10(isbn10);

// Retrieve Book Details By ISBN-10 (Retrieves the bookDetails object with the details stored in properties)

var bookDetails = openlibraryRetrieveBookDetailsByIsbn10(isbn10);

// Retrieve Author Names by Author Keys (Because the above method only returned the author key - not the name)

var authorNames = [];

for (var i = 0; i < bookDetails.authorKeys.length; i++){

  authorNames.push(openlibraryRetrieveAuthorNameByAuthorKey(bookDetails.authorKeys[i]));

}

// Pass Outputs

output.bookName = bookName;

output.bookTitle = bookDetails.title;

output.bookDescription = bookDetails.description;

output.bookGenres = bookDetails.genres.toString();

output.bookPublishers = bookDetails.publishers.toString();

output.bookAuthors = authorNames.toString();

Comments
thom.schumacher
Level 1
Level 1

I added the following function:

function authenticateUser(user,password)

{

    var token = user +":" + password;

    var hash = Base64.encode(token);

    return "Basic " + hash;

}

So that  I can base 64 encode the input..  Director tells me that both these functions don't exist

boa and

Base64.encode

am I missing something

Orf Gelbrich
Cisco Employee
Cisco Employee

try this

importPackage(java.util);

importPackage(java.lang);

importPackage(java.io);


var x=input.mask;

logger.addInfo("My Netmask = "+x);


var base64encodedString = Base64.getEncoder().encodeToString(x.getBytes("utf-8"));

logger.addInfo("Base64 Encoded String: " + base64encodedString);


thom.schumacher
Level 1
Level 1

That worked.

function authenticateUser(user,password)

{

    var token = user +":" + password;

    var hash = Base64.getEncoder().encodeToString(token.getBytes("utf-8"));

    return hash

}

Orf Gelbrich
Cisco Employee
Cisco Employee

Good..

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links