Python wrapper for the Apache OpenWhisk REST API
Start by instantiating an OpenWhisk object:
whisk = OpenWhisk(wsk_auth) # see notes below on how to obtain your wsk_auth
OpenWhisk has five main concepts:
- Actions
- Activations
- Packages
- Rules
- Triggers
An OpenWhisk object has a property for each of these so you can easily access the current list of each.
print(whisk.actions) # return a list of json records, one for each whisk action
print(whisk.action_names) # returns a list of names of the current whisk actions
See the repl example below to understand how to invoke whisk.systems actions and how to create and then invoke your own Python-based whisk actions.
The key prerequisite is an Open Whisk authentication token that is generated via the wsk
command line interface (cli) tool or via a webpage of your Open Whisk server. This authentication token is a ~100 character string with a ":" in it. If you have the wsk cli tool, then you can do wsk property get
to get your “wsk auth”. Alternatively, if you are on Bluemix, you can get your authentication token by logging into https://console.ng.bluemix.net/openwhisk/learn/cli .
The openwhisk.py script needs a valid Open Whisk authentication token to initialize and will attempt to accept this token in four different ways:
- As a parameter in the
openwhisk.OpenWhisk()
call (unwieldy, but it works) - From a $OPENWHISK_TOKEN environment variable (recommended)
- By importing a local
wsk_auth.py
which defines a wsk_auth variable - If all of these fail, it opens a webpage to see if the user can login, copy, and paste in their wsk auth
>>>
import openwhisk
>>>
whisk = openwhisk.OpenWhisk() < ># Assumes that $OPENWHISK_TOKEN environment variable has been set
>>>
whisk.namespaces
['_', 'wendel_p_whisk@whisknamics.org_dev', 'wendel_p_whisk@whisknamics.org']
>>>
whisk.action_names
['Hello World', 'Hello World With Params', 'python_action', 'xyz_action']
>>>
whisk.actions
[{'name': 'python_action', 'publish': False, [...]}]
>>>
whisk.activations
['echo', 'python_action']
>>>
whisk.activation_counts
Counter({'echo': 30, 'python_action': 12})
>>>
whisk.rules
[]
>>>
whisk.triggers
[]
>>>
whisk.packages
['Bluemix_Weather Company Data for IBM Bluemix-k6_Credentials-1']
>>>
whisk.invoke_echo('Anyone home!!')
{'message': 'Anyone home!!'}
>>>
whisk.system_utils_invoke('echo', message='my message')
{'message': 'my message'}
>>>
whisk.action_create(filename='hello.py', action_name='python_hello')
>>>
whisk.action_invoke('python_hello', name='Wendel')
{'greeting': 'Hello Wendel!'}
WARNING: THIS IS PROOF-OF-CONCEPT LEVEL CODE.
DO NOT USE IN PRODUCTION YET!!!
Set of utility functions to communicate with OpenWhisk at Bluemix
Requires:
Define environment variable
OPENWHISK_TOKEN ~100 character authorization token with a colon ':'
can be found at https://console.ng.bluemix.net/openwhisk/learn/cli
or by doing `wsk property get`
Optional:
Define environment variables
OPENWHISK_APIHOST or will default to 'openwhisk.ng.bluemix.net'
OPENWHISK_NAMESPACE or will default to '_' (TOKEN's email address)
DEBUG = False
If a requests.Session has been opened, then close() it
See: https://console.ng.bluemix.net/openwhisk/learn/cli Your ~100 char auth can be found at that URL or by doing
wsk property get
Uploads contents of the specified file to the specified action.
Deletes the specified action.
Invokes the specified action in blocking mode.
Lists the actions defined in openwhisk.
Returns a sorted list of the ids of all activation.
Returns the logs for a given activation id.
Returns a sorted list of the ids of all activation.
Lists the activations defined in openwhisk.
Issues a very basic echo request
Lists the namespaces defined in openwhisk.
Uploads contents of the specified file to the specified package.
Deletes the specified package.
Deletes the specified package.
Lists the packages defined in openwhisk.
Uploads contents of the specified file to the specified rule.
Deletes the specified package.
Deletes the specified package.
Lists the rules defined in openwhisk.
Invokes any action in whisk.system/utils
Uploads contents of the specified file to the specified trigger.
Deletes the specified package.
Deletes the specified package.
Lists the triggers defined in openwhisk.