OpenWhisk

OpenWhisk is an event-driven compute platform. OpenWhisk allows application developers to write and host application logic agnostic of the infrastructure on which it will run. This application logic is then triggered through events that can come from Bluemix services or other applications or sources. Such application logic are also called as actions. The uniqueness of this compute platform is that it is triggered only when an event occurs, which means that the trigger caused by an event will result in the application on OpenWhisk to be deployed and executed; if no event occurs the application unit does not run and hence does not use any infrastructure resources:

OpenWhisk

Note

At the time of writing this book, OpenWhisk is in beta. To know the scope for use and support for experimental services, you can refer to http://ibm.co/2dMu4f7 .

You will see the editor, where you can write the application logic or actions. A sample Hello World action written in JavaScript is provided. You can make changes to the file in the editor and can run the action by clicking Run This Action:

OpenWhisk

The terminology that you will need to understand to work with OpenWhisk are as follows:

  • Actions: An application logic that runs when an event occurs is called an action.
  • Triggers: An event that triggers a certain action to occur is called a trigger.
  • Rules: A rule associates a trigger to an action. A rule defines what action happens when a certain trigger occurs or is fired.
  • Sequence: An action can be defined in a way such that it is a sequence of multiple actions.

You can work with OpenWhisk using the Bluemix UI, OpenWhisk CLI, or by using an iOS mobile SDK. Here we will look at using the OpenWhisk CLI.

Installing the OpenWhisk CLI

The OpenWhisk tool is also called the wsk command line tool.

You will need to follow the steps at  https://ibm.biz/BdrxMfto to install wsk command line tool.

Tip

Make sure pip is already installed on your system before you attempt to install the wsk command line tool.

The results of executing the  pip install command for OpenWhisk on the system used for this demo is as shown in the following screenshot:

Installing the OpenWhisk CLI

You will need to copy the commands from the URL mentioned previously and run them in your terminal window or command prompt, based on your operating system, to complete the wsk tool installation and configuration.

Creating the trigger source for our demonstration

Let us learn how to use the Bluemix service as a trigger source. In the example that we used earlier in this chapter, we created a CloudantNoSQLDB service instance on Bluemix, we shall now learn how we can configure this Cloudant database as our trigger source. This means that we would like to build a system where we would like some action to happen when there is a change in the Cloudant database we have created earlier:

  1. Go to your terminal window and type the following command to get the details of the services you have created within the space you are working on within your organization in Bluemix:
    cf services
    

    The result of execution of the command is shown here:

    Creating the trigger source for our demonstration

  2. Make a note of the Cloudant service instance name, which is  B05307-07-01-eclipse-cloudantNoSQLDB, in this case.
  3. Ensure that the OpenWhisk CLI namespace corresponds to your Bluemix organization and space. You can execute the following command to check the namespace from your terminal window:
    wsk property get --namespace
    

    The result of execution of this command gives you the  namespace property value that is set in wsk, as shown in the following screenshot:

    Creating the trigger source for our demonstration

  4. If the  namespace is not set, you can use the following command to set the property namespace:
    wsk property set --namespace <Bluemix_Org>_<Bluemix_Space>
    
  5. You will need to create a package binding for your Cloudant service instance.

    Note

    To know more about Cloudant package you can refer to https://ibm.biz/BdrxMM .

    To create the package binding, execute the following command from the terminal window:

    wsk package refresh
    

    This results of this command are shown here:

    Creating the trigger source for our demonstration

  6. You can create the fully qualified name of the package binding created for your Cloudant data source by executing the following command:
    wsk package list
    

    The result of execution is as shown in the screenshot here:

    Creating the trigger source for our demonstration

    Make a note of the package binding name from the results of the execution of the preceding command.

  7. To create the trigger, execute the following command:
    wsk trigger create <name_of_your_cloudanttrigger> 
          --feed /<package_binding_name_from_step_above>/changes
          --paramdbname<name_of_cloudant_database_created> 
          --paramincludeDoc true
    

    The name of the Cloudant database was  sample_nosql_db, from the example that we created during the Eclipse plugin demonstration. You can use that as  name_of_cloudant_database_created, in the preceding command.  name_of_your_cloudanttrigger is the name you would want to give to the trigger you are creating in this demo the name of the trigger created is  B5307_07_CloudantTrigger, as shown here:

    Creating the trigger source for our demonstration

  8. You can start polling for changes in your Cloudant database by executing this command:
    wsk activation poll
    

    This will create the listener for any changes in your Cloudant database.

  9. Go to the application URL of the application that we created and deployed earlier in this chapter using the Eclipse Bluemix plugin, which is  https://b05307-07-01-eclipse.mybluemix.net/.
  10. Here let us upload a file in the  Sample category and let us also create a new file category and add another file to this new category, as here:

    Creating the trigger source for our demonstration

  11. Go to your terminal window where you have executed the command for polling the changes to Cloudant database. You will see that the events are created by the trigger when there was a change to the Cloudant database from our actions in the application:

    Creating the trigger source for our demonstration

Creating an action

Let us now look at creating a simple action that can be triggered when the data in the Cloudant database changes. We will see the steps to create an action from the Bluemix dashboard. You can create an action using the wsk command line tool as well:

  1. Go to your Bluemix dashboard, and go to OpenWhisk under the Compute category on your Bluemix dashboard. Click Develop, as shown here:

    Creating an action

  2. The OpenWhisk editor opens, here you can see a Hello World action in JavaScript. Click Create An Action to create a new action:

    Creating an action

  3. In the screen shown here, enter the name of your new action. You can choose to code the action in any of the supported programming languages:

    Creating an action

    Note

    Since Bluemix is an evolving platform, you may see support for more languages in future.

  4. For this example, let us choose Javascript as the execution runtime and let us select Hello World in Javascript as the sample code, from the second dropdown, as our starting point. Provide a name for your action and click Create Action.
  5. My new action is now created as shown here, with the starter code; we shall modify the code to print the  _id value of the Cloudant document that is modified:

    Creating an action

    Note

    You can modify the code in your action using the editor in your browser, to build a functionality that you would want to achieve when a certain trigger is fired.

Creating a rule

Rule creates the association or link between the trigger and action. Now that we have created our trigger and our action, let us create a rule to link both, so that when our trigger is fired (in our case caused by Cloudant database update), an action occurs, which is our action created in the previous section, and executes. We will use the  wsk command line tool to create the rule. Rules can be created from the dashboard as well. Before we create the rule we should make a note of the name of the trigger and the action we would want to link. In our case the trigger is  B5307_07_CloudantTrigger, and the action is  B05307_alert_cloudant_update. Lets look at the steps to create a rule:

  1. Execute the  wsk command to create the rule named  B5307-rule:
    wsk rule create --enable B05307-rule B5307_07_CloudantTrigger
            B05307_alert_cloudant_update
    

    The result of execution is shown in the following screenshot:

    Creating a rule

  2. The rule is created and can be seen as active in the OpenWhisk editor as well.

Testing a rule

Let us now test to see whether the trigger caused by a Cloudant update leads to the execution of the JavaScript code which you have defined as part of your action:

  1. Go to the application URL of the application that we created and deployed earlier in this chapter using the Eclipse Bluemix plugin, which is  https://b05307-07-01-eclipse.mybluemix.net/, in our demo.
  2. Let us delete the existing Sample category and create a new category with the name of  Sample, and let us upload a simple file to this category, as shown in the following screenshot:

    Testing a rule

    Note

    You can make any updates to the Cloudant database that you would like. The preceding changes are for the sake of illustration.

    This would have caused your trigger to fire and would have allowed your rule to invoke your action.

  3. Go to the OpenWhisk dashboard, Activity Log will display the output of the execution of your action code:

    Testing a rule

You will see that your action was triggered three times due to the updates you performed on your application UI, hence you will see the action code executed three times. You will see that it prints the document  _id value of the Cloudant document which was updated, as shown here:

Testing a rule

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106