heidloff.net - Building is my Passion
Post
Cancel

How to trigger OpenWhisk Actions on Cloudant Databases Changes

Yesterday I wrote about how to write JavaScript Actions for OpenWhisk on Bluemix. Actions can be triggered manually (as described in previous article) and when certain events occur. Below is a simple sample how to trigger OpenWhisk actions when documents in Cloudant NoSQL databases are changed or added.

Let’s use the same scenario as previously. In a help desk application you want to translate tickets which have not been written in English to English so that support agents understand. Rather than doing this when tickets are stored or opened the OpenWhisk action is triggered when new documents are stored in a Cloudant NoSQL database.

The documentation describes how to build this scenario. Essentially you need in addition to the JavaScript action a trigger and a rule. Invoke the following commands via the CLI and replace your credentials, namespace and database name.

1
2
3
4
5
wsk property set --auth <yourOpenWhiskUser>:<yourOpenWhiskPassword> --namespace "<yourOpenWhiskNameSpace>"
wsk package bind /whisk.system/cloudant myCloudant -p username '<yourCloudantUser>' -p password '<yourCloudantPassword>' -p host '<yourCloudantHost>'
wsk package list
wsk package get /<yourOpenWhiskNameSpace>/myCloudant
wsk trigger create myCloudantTrigger --feed /<yourOpenWhiskNameSpace>/myCloudant/changes --param <yourCloudantDBName> testdb --param includeDocs true

To see the trigger in action create a JavaScript action ‘hello.js’ and and rule.

1
2
3
function main() {
   return {payload: 'Hello world'};
}
1
2
3
wsk action create hello hello.js
wsk rule create --enable sampleRule myCloudantTrigger hello
wsk activation poll

When you change documents or add documents in your Cloudant database you can see logs in your terminal window and the OpenWhisk dashboard.

image

Featured Blog Posts
Disclaimer
The postings on this site are my own and don't necessarily represent my employer IBM's positions, strategies or opinions.
Trending Tags