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

[code]wsk[/code]

  • On your terminal, run the below command

[code]wsk action list[/code]

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.

[code]wsk action invoke –blocking <action-name>[/code]

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

[code]wsk action invoke myWatsonTranslator/translator –blocking –result –param payload ‘Testing the service’ –param translateFrom ‘en’ –param translateTo ‘fr’
[/code]

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

[code]
wsk api-experimental create /api-demo/v1 /watsonsequence get /<namespace>/MyWatsonSequence

[/code]

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.

[code]
curl –g <url generated in the above step>?message=”hello”
[/code]

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

[code]
cf  api https://api.ng.bluemix.net
cf login -u {username} -o {org_name} -s {space_name}
[/code]

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

[code]git clone https://github.com/IBM-Bluemix/nodejs-openwhisk-console.git[/code]

  • cd nodejs-openwhisk-console
  • Run the below command

[code]npm install[/code]

  • 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

[code]OpenWhisk_HOST = "openwhisk.ng.bluemix.net"

OpenWhisk_AuthKey=" "[/code]

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

[code]wsk property get –auth[/code]

  • To run the app locally,

[code]npm start[/code]

[code]cf push[/code]

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

[code]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[/code]

  • 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

Technical Offering Manager & Polyglot Programmer | IBM Cloud

More Compute Services stories
February 18, 2019

Build a Container Image Inside a Kubernetes Cluster and Push it to IBM Cloud Container Registry

We're going to show you how to build a source into a container image from a Dockerfile inside a Kubernetes cluster and push the image to IBM Cloud Container Registry with Google's Kaniko tool.

Continue reading

February 12, 2019

A “Kubernetes Everywhere” Approach: Build and Deploy Enterprise-Scale Modern Applications for Hybrid Cloud

We are excited to introduce two optional cloud-managed services and capabilities designed to enable clients to quickly build and deploy enterprise-scale container-based applications across hybrid environments: Managed Istio and Managed Knative for IBM Cloud Kubernetes Service.

Continue reading

February 7, 2019

Istio Multicluster Support

The current multicluster Istio status There is a growing community interest in running workloads on multiple clusters to achieve better scaling, failure isolation, and application agility. Istio v1.0 supports some multicluster capabilities and new ones are added in v1.1. This blog post highlights the current multicluster Istio status, helping interested people understand what capabilities exist […]

Continue reading