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

IBM Cloud Log Analysis – Now Available in Germany and Sydney!

Starting today, IBM Cloud Log Analysis expands its availability from US-South to include the Germany and Sydney regions. Empower your DevOps team with IBM Cloud Log Analysis. Gain insights into your environment to quickly detect, diagnose, and identify issues. Keep your log data and your application workloads safeguarded on a centralized cloud class economical storage solution. Visit us in the IBM Cloud Catalog.

Continue reading

Simplify binding your IBM Cloud services to serverless Functions

Introducing a new command for the IBM Cloud Functions CLI plug-in to make it easier to configure your actions to have access to your IBM Cloud service credentials. The new service command is a convenient way to make your IBM Cloud service credentials available to your Cloud Functions code at run time. The new command helps with a form of secret management for your IBM Cloud services within the context of IBM Cloud Functions.

Continue reading

Speed deployment on Kubernetes with Helm Chart – Quick YAML example from scratch

Are you working with Kubernetes, with all the recent supporting releases on IBM Cloud Private, the IBM Bluemix Container Service on IBM Cloud Platform, or elsewhere? Are you in the middle of containerizing workloads across your portfolio? Have you adopted Kubernetes and looking to speed up deployment and reuse? This post is a learn-by-doing example that introduces you to Helm and Helm Charts.

Continue reading