Mixing in Openwhisk

1 minute read


News

Openwhisk

This is the third article in a series on how to use GitHub pages (the service that hosts this blog) and Cloudant on the BlueMix PaaS to create an automatically updating news section for a blog. This post will explore how to setup Openwhisk which will be configured to push updates to the news page.

Previous Articles

  1. Setting up Cloudant.
  2. Dynamic GitHub pages.


Openwhisk Setup

To setup Openwhisk go to this page on Bluemix. The documentation on that page will guide you through a 3 step process to for Openwhisk setup.

Openwhisk setup
Openwhisk setup instruction

Execute the supplied test command and verify you get the expected output.

 ./wsk action invoke /whisk.system/samples/echo -p message hello --blocking --result
{
    "message": "hello"
}

Update Cloudant

In a previous post we manually updated news articles by making Cloudant documents on the web interface. In this post we are going to configure Openwhisk to push updates into Cloudant.

Create a user that can read and write

Follow the steps in the previous post for creating a new user. When creating this new user enable both _reader and _writer permissions.

Store the password for this user somewhere - you can’t retrieve it later.

Cloudant read and write user
Cloudant user with read and write

Bind Openwhisk to Cloudant

Once the new user is created follow these instructions to bind the username and password to the new user that was created. Replace ‘MYUSERNAME’, ‘MYPASSWORD’ with the key and password for the new user that was created. Replace ‘MYCLOUDANTACCOUNT.cloudant.com’ with the Cloudant hostname you provided by Bluemix.

./wsk package bind /whisk.system/cloudant blog_news -p username 'MYUSERNAME' -p password 'MYPASSWORD' -p host 'MYCLOUDANTACCOUNT.cloudant.com'

Retrieve the name of the package binding and note it.

./wsk package list

packages
/user@us.ibm.com_blog/blog_news                                         private

Post a sample news item from the command line

./wsk action invoke /user@us.ibm.com_blog/blog_news/write --blocking --result --param dbname blog_news --param doc '{"title":"news 3", "link":"http://www.example.com/news3", "excerpt":"Example", "timestamp":"1471314119"}

Result

{
    "id": "5fb45105ac585df3eb5b4746ccc54120",
    "ok": true,
    "rev": "1-a0ac96533ae7991b23ea2f4d76d48b6b"
}

After executing that command the sample news item should show up in the blog’s news article list.

News documents created by whisk
News items inserted into Cloudant by Openwhisk

You can now use the command line Openwhisk to push news articles directly to your blog without having to edit database documents directly on Cloudant.

Resources

Should you run into any issues consult the BlueMix Documentation, Openwhisk Documentation, Cloudant Documentation or reach out to me on twitter