Search This Blog

Monday, 11 April 2016

Taking IBM Bluemix OpenWhisk for a Test Drive


OpenWhisk is a new event-driven platform that lets developers quickly and easily build feature-rich apps that automatically trigger responses to events. To read more about it view the link below. In this simple example we will explore it it's use from IBM Bluemix by returning Todays date.

https://developer.ibm.com/open/openwhisk/

Steps

1. Login to Bluemix using http://bluemix.net

2. Click on "Try OpenWhisk" as shown below


3. Once logged in to the new Bluemix Console you should see a screen as follows


At this point we can only use OpenWhisk from the command line

4. Click on "Configure CLI" button to install it

5. Once installed you can verify it's installed as follows

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk/actions$ wsk --help
usage: wsk [-h] [-v] [--apihost hostname] [--apiversion version]
           {action,activation,namespace,package,rule,trigger,sdk,property,list}
           ...

OpenWhisk is a distributed compute service to add event-driven logic to your
apps.

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         verbose output
  --apihost hostname    whisk API host
  --apiversion version  whisk API version

available commands:
  {action,activation,namespace,package,rule,trigger,sdk,property,list}
    action              work with actions
    activation          work with activations
    namespace           work with namespaces
    package             work with packages
    rule                work with rules
    trigger             work with triggers
    sdk                 work with the SDK
    property            work with whisk properties
    list                list all triggers, actions, and rules in the registry

Learn more at https://developer.ibm.com/openwhisk fork on GitHub
https://github.com/openwhisk. All trademarks are the property of their
respective owners.


6. Now lets Set your OpenWhisk Namespace and Authorization Key. They are provided when you are taken to the install CLI for OpenWhisk, I simply create a script for this as follows

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk$ cat set-namespace-key
wsk property set --auth your-rather-long-key --namespace "pasapi@au1.ibm.com_dev"

Once run you get output as follows

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk$ ./set-namespace-key
ok: whisk auth set
ok: namespace set to pasapi@au1.ibm.com_dev


7.  Now create an OpenWhisk JavaScript function as shown below

todaysdate.js

/**
 * Todays Date as an OpenWhisk action.
 */
function main(params) {
    var currentTime = new Date();
    return {payload:  'Todays date is, ' + currentTime + '!'};
}

8. Create the OpenWhisk action as shown below

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk/actions$ wsk action create todaysdate todaysdate.js
ok: created action todaysdate


9. Invoke the action as shown below

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk/actions$  wsk action invoke todaysdate --blocking --result
{
    "payload": "Todays date is, Mon Apr 11 2016 10:14:56 GMT+0000 (UTC)!"
}


10. Invoke it again this displaying the full Response Object as shown below

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk/actions$  wsk action invoke todaysdate --blocking
ok: invoked todaysdate with id dbb447b65b06433a8b511187011f5715
response:
{
    "result": {
        "payload": "Todays date is, Mon Apr 11 2016 10:19:07 GMT+0000 (UTC)!"
    },
    "status": "success",
    "success": true
}


11. We can view our current actions using a command as follows, you can see we have added the "todaysdate" function

pasapicella@pas-macbook-pro:~/ibm/bluemix/openwhisk/actions$ wsk list
entities in namespace: pasapi@au1.ibm.com_dev
packages
actions
/pasapi@au1.ibm.com_dev/todaysdate                            private
/pasapi@au1.ibm.com_dev/hello                                     private
triggers
rules


12. Now lets return to the Bluemix Web Console and click on "Inspect activity metrics and logs"
Here you can view what you have invoked


There is clearly more to OpenWhisk then just this BUT for more information the IBM Bluemix docs have some more demos to try out.

https://new-console.ng.bluemix.net/docs/openwhisk/index.html

No comments: