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 Compute Services Stories

Bluemix Cloud Foundry: 10 Lessons Learned in 2016

IBM has a number of development, test, and site reliability engineering (SRE) teams that are deployed worldwide. This allows the entire team to maintain Bluemix 24 hours a day, 365 days a year. Critical to that maintenance is the BOSH technology. BOSH is an open source tool for release engineering, deployment, lifecycle management, and monitoring of distributed systems. It is core to the Cloud Foundry technology and allows IBM operators to perform maintenance and recovery actions on any Bluemix deployments. What follows are the top lessons we have learned in the past year operating Bluemix at this incredible and unsurpassed scale. Dr. Michael Maximilien, IBM scientist and researcher, walks us through these lessons.

Continue reading

Easily stream events into Bluemix using our new Message Connect service

I’m delighted to announce the availability of our new experimental service, Message Connect. Message Connect is a service which enables you to feed events from a variety of sources into Message Hub. So, if you want to get events from sources such as Twitter into your Bluemix apps, this is the easiest way to achieve it.

Continue reading

Migrate an application from Azure to Bluemix

Bluemix is an implementation of IBM’s Open Cloud Architecture, leveraging Cloud Foundry to enable developers to rapidly build, deploy, and manage applications and services. Although Microsoft Azure, another popular PaaS platform, shares quite a few similarities with Cloud Foundry, several elements still need to be addressed when porting Microsoft Azure applications to IBM Bluemix. This tutorial will take you step-by-step from creating a Node.js app on Azure and then migrating that app onto IBM Bluemix; it will also give you the key differences between Microsoft Azure and IBM Bluemix from a developer’s point of view.

Continue reading