Setting Default Parameters
Starting in lab 3, we introduced how parameters can be utilized in actions as the primary method of data input. Aside from specifying parameters at invocation time, actions support the concept of utilizing a set of default parameters. Default parameters can be specified against an individual action or scoped to all actions that are part of a given package.
To demonstrate how default parameters can be utilized, lets first create a new package called iot-serverless-parameters
:
$ wsk -i package create --shared yes iot-serverless-parameters
ok: created package iot-serverless-parameters
Throughout this lab, we have utilized several actions that made use of input parameters; one in particular was the formatInput that was used at the beginning of the action sequence to format input parameters that were sent from IoT devices. As a reminder, the action took in two input parameters:
-
topic
- Name of the topic the IoT was processed from -
data
- Latitude and longitude that are separated by a space
Add the action to the iot-serverless-parameters
$ cd /home/lab-user/iot-serverless
$ wsk -i action update iot-serverless-parameters/formatInput iot-serverless-openwhisk-functions/format/formatInput.js
ok: updated action iot-serverless-parameters/formatInput
In previous invocations, we have already provided at least one or more parmeters. However, if no parameters are entered, none will be returned.
Let’s demonstrate invoking the formatInput action without any parameters:
$ wsk -i action invoke iot-serverless-parameters/formatInput --result
{}
As you can see, no results were retured as no parameters were provided.
The Feed Provider is configured to provide the name of the topic that the IoT device utilized, but to provide default options in the event these values are not passed, we can configure a set of default parameters to protect ourselves.
A default parameter can be added to an action by adding the --param
flag during action creation or update.
Add a default parameter to the formatInput action with a topic name of .sf.asset.component
in the format that would be presented from AMQ:
$ wsk -i action update iot-serverless-parameters/formatInput iot-serverless-openwhisk-functions/format/formatInput.js --param topic .sf.asset.component
ok: updated action iot-serverless-parameters/formatInput
Invoke the action again without any parameters:
$ wsk -i action invoke iot-serverless-parameters/formatInput --result
{
"topic": "/sf/asset/component"
}
Notice how the topic has been returned, but also how the logic of the formatInput executed to replace the .
with /
.
Default parameters can also be specified at a package level in a similar fashion as in an action using the --param
parameter
Update the iot-serverless-parameters
package with a default topic parameter of .sf.asset.pacakgeComponent
$ wsk -i action invoke iot-serverless-parameters/formatInput --result
{
"topic": "/sf/asset/component"
}
The topic value returned from the action continues to return /sf/asset/component
even though a package parameter was specified. This is because default parameters have a precedence. Default parameters specified on an action have a higher precedence than one at a package.
The formatInput action must be deleted and recreated to remove the default parameter:
$ wsk -i action delete iot-serverless-parameters/formatInput
ok: deleted action iot-serverless-parameters/formatInput
Recreate the formatInput action without utilizing default parameters:
$ wsk -i action update iot-serverless-parameters/formatInput iot-serverless-openwhisk-functions/format/formatInput.js
ok: updated action iot-serverless-parameters/formatInput
Once again, invoke the action:
$ wsk -i action invoke iot-serverless-parameters/formatInput --result
{
"topic": "/sf/asset/pacakgeComponent"
}
The topic value returned now utilizes the default parameter that is defined on the package.
Customizing GUI
The GUI for this lab uses PatternFly, an open source UI framework for enterprise web applications. The GUI design uses the sample code from Vertical Navigation template.
You can install PatternFly on your local system and easily build amazing UIs. Visit PatternFly Github Repo for details.