Compute Services

Serverless computing and Watson service chaining via OpenWhisk : Part 3 of 3 expose an action or sequence

Share this post:

By now, you should be aware of what OpenWhisk is and leverage OpenWhisk Sequence to chain Watson services. Also, you should have created Swift and NodeJS actions for transforming the JSON to required formats.

In this post, you will learn how to expose an action or a sequence (Chain of actions) as a RESTful endpoint via OpenWhisk API Gateway and OpenWhisk CLI.

You should have setup OpenWhisk CLI on your machine.

If not follow the instructions below,

  • On a browser, Move to Getting Started tab on Bluemix OpenWhisk and Click on Download OpenWhisk CLI.
  • Following the steps on CLI (left pane), Set your OpenWhisk Namespace and Authorization Key.
  • Open Terminal (Ctrl + Alt + T) or Command Prompt on your system, and type
wsk

  • On your terminal, run the below command
wsk action list

You should see all the actions you created via the Browser.

Note: OpenWhisk actions, triggers, and rules belong in a namespace, and optionally a package.

  1. For convenience, the namespace can be left off if it is the user’s default namespace.
  2. The fully qualified name of an entity is /namespaceName[/packageName]/entityName. Notice that / is used to delimit namespaces, packages, and entities. Also, namespaces must be prefixed with a /.
  3. In Bluemix, an organization+space pair corresponds to a OpenWhisk namespace. For example, the organization BobsOrg and space dev would correspond to the OpenWhisk namespace /BobsOrg_dev.
  • After you create your action, you can run it in the cloud in OpenWhisk with the ‘invoke’ command. You can invoke actions with a blocking invocation (i.e., request/response style) or a non-blocking invocation by specifying a flag in the command. A blocking invocation request will wait for the activation result to be available. The wait period is the lesser of 60 seconds or the action’s configured time limit.
wsk action invoke --blocking <action-name>

For Example, you can run this command on terminal and see the output in French

wsk action invoke myWatsonTranslator/translator --blocking --result --param payload 'Testing the service' --param translateFrom 'en' --param translateTo 'fr'

Note: To pass a parameter use –param and –result to see only the response.

  • Let’s expose Watson sequence that is already created with OpenWhisk API Gateway
wsk api-experimental create /api-demo/v1 /watsonsequence get /<namespace>/MyWatsonSequence

Note: /api-demo/v1 is your base path, /watsonsequence is the API path, get is the verb and then the action or in this case sequence with namespace returned as part of action list

  • You should see a OK response and an API link which is now ready to be used in any client application and can be tested via cURL or REST clients like Postman.
curl –g <url generated in the above step>?message=”hello”

NodeJS Query Console app using JavaScript client library for the OpenWhisk platform

In this section, you will create a nodejs app which will be deployed to Bluemix via SDK for NodeJS and uses OpenWhisk Javascript client library (a wrapper around OpenWhisk APIs)

  • Connect and Log into Bluemix
cf  api https://api.ng.bluemix.net
cf login -u {username} -o {org_name} -s {space_name}

Note: You can find username, organization name and space name on Bluemix console page (Top right corner).

  • Clone the code repo into a folder on your machine
git clone https://github.com/IBM-Bluemix/nodejs-openwhisk-console.git
  • cd nodejs-openwhisk-console
  • Run the below command
npm install
  • Launch an IDE of your choice ( I use Atom) and Open the cloned repo folder (File->Open)
  • Open manifest.yml file and give a unique name, host.
  • Create a new file (File->New File) and save it as .env. Add the below values to .env file and save
OpenWhisk_HOST = "openwhisk.ng.bluemix.net"

OpenWhisk_AuthKey=" "

Note: You can get the auth key by running the below command on terminal

wsk property get --auth
  • To run the app locally,
npm start
cf push

Note: If you see permission denied, use Sudo on your linux or macOS machine.

  • Once you run the push command, based on the artifacts provided in manifest.yml file, a nodeJS runtime is created on IBM Bluemix (IBM’s Cloud platform) by pulling nodeJS buildpack.
  • Wait until you this message on the terminal
requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: InterconnectOpenWhiskApp.mybluemix.net
last uploaded: Fri Feb 17 11:43:14 UTC 2017
stack: cflinuxfs2
buildpack: SDK for Node.js(TM) (ibm-node.js-4.7.2, buildpack-v3.10-20170119-1146)

state     since                    cpu    memory       disk         details
#0   running   2017-02-17 05:14:16 PM   0.0%   1M of 256M   1.3M of 1G
  • Test your app running on Bluemix with the urls mentioned in the above step by replacing localhost:6007 with your Bluemix endpoint.

To summarise, in this lab you have created actions (NodeJS and Swift) and a Watson sequence (action chaining) connecting Watson packages and actions you created. You exposed the sequence and actions as an REST endpoint. Also, you have created a NodeJS webui app which uses npm packages.

There is more to explore in OpenWhisk

  • Automate the actions via Triggers.
  • Explore Rules.
  • Create a package and Create to Feed.

For more featured Bluemix code samples, refer IBM-Bluemix.github.io

More Watson Stories

Rapidly developing applications (part 6): designing and versioning APIs

In part 6, we review best practices for creating and maintaining APIs within an application and between a cloud-deployed application and components that live on premises. This is a guide to the overall series.

Continue reading

WebSphere on the Cloud: Application Modernization (Phase 1)

In our last post, we took high-level view of our four-phase process for moving WebSphere applications to the cloud. In this entry, we'll look closely at the tools that help you with Phase 1, modernizing your existing WebSphere application.

Continue reading

Taking WebSphere Applications to the Cloud

With many traditional workloads moving to cloud-based infrastructures, now is the perfect time to assess your existing Java-based workloads and migrate them to WebSphere Application Server on IBM Bluemix. More commonly known as “WAS on Cloud”, this service provides a fully-configured, turn-key WebSphere-based application environment, supporting both Traditional WAS and Liberty applications. You use your same tried & true wsadmin automation scripts, same CI/CD techniques, but just point them at Bluemix instead of your own infrastructure! This post summarizes the steps from WAS to the cloud.

Continue reading