Getting started with Whisk Deploy shows you how to automate deployment of a simple hello world action. Now, lets look at how to automate:

Passing parameters to the Hello World action

Use the following code to pass parameters to the Hello World action:


cat hello.js
function main(params) {	
  return {payload:  'Hello, ' + params.name + ' from ' + params.place};
}

Step 1: Create a manifest file (manifest.yaml)

Define parameters by adding an inputs section under the action name helloworld:


packages:
    helloworld:
        actions:
            helloworld:
                location: src/hello.js
                runtime: nodejs:6
	        inputs:
                    name:
                        type: string
                        description: name of a person
                    place:
                        type: string
                        description: location of a person
	        outputs:
                    payload:
                        type: string
                        description: a simple greeting message, Hello World!

You can also specify parameter values in the manifest file and skip Step 2:


packages:
    helloworld:
        actions:
            helloworld:
                location: src/hello.js
                runtime: nodejs:6
	        inputs:
                    name: Amy
                    place: Paris
	        outputs:
                    payload:
                        type: string
                        description: a simple greeting message, Hello World!

Step 2: Create a deployment file (deployment.yaml)

Set default parameters by adding an inputs section under the action name helloworld:


project:
    name: SampleHelloWorld
    namespace: _
    packages:
        helloworld:
            actions:
                helloworld:
                    inputs:
                        name: Amy
                        place: Paris

Make sure you have the same directory structure as before.

Step 3: Deploy a Hello World action

To deploy Hello World, use the following code:


./wskdeploy -p ~/SampleHelloWorldApp/
         ____      ___                   _    _ _     _     _
        /\   \    / _ \ _ __   ___ _ __ | |  | | |__ (_)___| | __
   /\  /__\   \  | | | | '_ \ / _ \ '_ \| |  | | '_ \| / __| |/ /
  /  \____ \  /  | |_| | |_) |  __/ | | | |/\| | | | | \__ \   <
  \   \  /  \/    \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
   \___\/              |_|
Packages:
Name: helloworld
  * action: helloworld
    bindings:
        - name: name value: Amy
        - name: place value: Paris

Triggers:
 Rules
Do you really want to deploy this? (y/N): y
Deploying pacakge helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deployment completed successfully.

Step 4: Verify your deployment

To verify your deployment, use the following code:


wsk action invoke --blocking --result helloworld/helloworld
{
    "payload": "Hello, Amy from Paris"
}

You can override defaults with the  — param flag:


wsk action invoke --blocking --result helloworld/helloworld --param name Mark --param place Barcelona
{
    "payload": "Hello, Mark from Barcelona"
}

Create a trigger and rule

Step 1: Update the manifest file (manifest.yaml)

Add a section called triggers and rules:


packages:
    helloworld:
        actions:
            helloworld:
                location: src/helloworld.js
                runtime: nodejs:6
                inputs:
                    name:
                        type: string
                        description: name of a person
                    place:
                        type: string
                        description: location of a person
                outputs:
                    payload:
                        type: string
                        description: a simple greeting message, Hello World!
        triggers:
            locationUpdate:													
        rules:
            helloworldOnLocationUpdate:												
                action: helloworld
                trigger: locationUpdate

Step 2: Deploy a Hello World action, trigger, and rule:

To deploy a Hello World action, trigger, and rule use the following code:


./wskdeploy -p ~/SampleHelloWorldApp/
         ____      ___                   _    _ _     _     _
        /\   \    / _ \ _ __   ___ _ __ | |  | | |__ (_)___| | __
   /\  /__\   \  | | | | '_ \ / _ \ '_ \| |  | | '_ \| / __| |/ /
  /  \____ \  /  | |_| | |_) |  __/ | | | |/\| | | | | \__ \   <
  \   \  /  \/    \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
   \___\/              |_|
Packages:
Name: helloworld
  * action: helloworld
    bindings:
        - name: name value: Amy
        - name: place value: Paris

Triggers:
* trigger: locationUpdate
    bindings:
 Rules
* rule: helloworldOnLocationUpdate
    - trigger: locationUpdate
    - action: helloworld
Do you really want to deploy this? (y/N): y
Deploying pacakge helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deploying trigger locationUpdate ... Done!
Deploying rule helloworldOnLocationUpdate ... Done!
Deployment completed successfully.

Step 3: Verify your deployment

  1. Poll for running actions:
    
    wsk activation poll
    Enter Ctrl-c to exit.
    Polling for activation logs
  2. Fire the trigger.
    Open one more terminal and fire the trigger:
    
    wsk trigger fire locationUpdate
    ok: triggered locationUpdate with id 4c3a8b1792d546a68ac58538c3f5d637
    
  3. Results from polling:
    
    wsk activation poll
    Enter Ctrl-c to exit.
    Polling for activation logs
    Activation: helloworld (d545c458f3d34d6fbf5c29173be3d29e)
    []
    Activation: locationUpdate (4c3a8b1792d546a68ac58538c3f5d637)
    []
    Activation: helloworldOnLocationUpdate (c099355c1f1f4d6d8d30f54e8dac2b84)
    []
    
  4. Determine the activation ID from polling and get the results of that action:
    
    wsk activation get d545c458f3d34d6fbf5c29173be3d29e
    ok: got activation d545c458f3d34d6fbf5c29173be3d29e
    {
    	...
        "activationId": "d545c458f3d34d6fbf5c29173be3d29e",
        "start": 1489444142544,
        "end": 1489444142598,
        "response": {
            "status": "success",
            "statusCode": 0,
            "success": true,
            "result": {
                "payload": "Hello, Amy from Paris"
            }
        },
    	...
    }
    

Enjoy!

Next, we will look at more complex triggers and actions sequences.

Join The Discussion

Your email address will not be published. Required fields are marked *