UNPKG

17.9 kBMarkdownView Raw
1# OpenWhisk Client for JavaScript
2
3[![Build Status](https://travis-ci.org/apache/incubator-openwhisk-client-js.svg?branch=master)](https://travis-ci.org/apache/incubator-openwhisk-client-js)
4[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
5[![codecov](https://codecov.io/gh/apache/incubator-openwhisk-client-js/branch/master/graph/badge.svg)](https://codecov.io/gh/apache/incubator-openwhisk-client-js)
6
7JavaScript client library for the [Apache OpenWhisk](https://github.com/apache/incubator-openwhisk) platform.
8Provides a wrapper around the [OpenWhisk APIs](https://github.com/apache/incubator-openwhisk/blob/fb001afa237476eda0c0f6494ee92702e5986538/core/controller/src/main/resources/apiv1swagger.json) (Swagger JSON).
9
10## installation
11
12```bash
13$ npm install openwhisk
14```
15
16## usage
17
18### within openwhisk platform
19
20This client library can use environment parameters to automatically configure the authentication credentials, platform endpoint and namespace. These parameters are defined within the Node.js runtime environment in OpenWhisk. Unless you want to override these values, you can construct the client instance without further configuration.
21
22```javascript
23var openwhisk = require('openwhisk');
24
25function action() {
26 var ow = openwhisk();
27 return ow.actions.invoke('sample')
28}
29
30exports.main = action
31```
32
33_All methods return a Promise resolved asynchronously with the results. Failures are available through the catch method._
34
35```javascript
36ow.resource.operation().then(function () { // success! }).catch(function (err) { // failed! })
37```
38
39Users can override default constructor parameters by passing in explicit options as shown in the example below.
40
41_**Please note**: Due to [an issue](https://github.com/openwhisk/openwhisk/issues/1751) with the Node.js runtime in OpenWhisk, environment variables used by the constructor are not available until the invocation function handler is called. If you want to define the client instance outside this function, you will need to manually pass in the constructor options ._
42
43```javascript
44var openwhisk = require('openwhisk');
45// DOES NOT WORK! Environment parameters not set.
46var ow = openwhisk();
47
48function action() {
49 return ow.actions.invoke('sample')
50}
51
52exports.main = action
53```
54
55### outside openwhisk platform
56
57```javascript
58var openwhisk = require('openwhisk');
59var options = {apihost: 'openwhisk.ng.bluemix.net', api_key: '...'};
60var ow = openwhisk(options);
61ow.actions.invoke('sample').then(result => console.log(result))
62```
63
64### constructor options
65
66_Client constructor supports the following mandatory parameters:_
67
68- **apihost.** Hostname and optional port for openwhisk platform, e.g. `openwhisk.ng.bluemix.net` or `my_whisk_host:80`. Used with API URL template `${protocol}://${apihost}/api/v1/`. If port is missing or port value is 443 in the apihost string, protocol is HTTPS. Otherwise, protocol is HTTP.
69- **api_key.** Authorisation key for user account registered with OpenWhisk platform.
70
71*Client constructor supports the following optional parameters:*
72
73- **api.** Full API URL for OpenWhisk platform, e.g. `https://openwhisk.ng.bluemix.net/api/v1/`. This value overrides `apihost` if both are present.
74- **namespace**. Namespace for resource requests, defaults to `_`.
75- **ignore_certs**. Turns off server SSL/TLS certificate verification. This allows the client to be used against local deployments of OpenWhisk with a self-signed certificate. Defaults to false.
76- **apigw_token**. API Gateway service authentication token. This is mandatory for using an external API Gateway service, rather than the built-in api gateway.
77- **apigw_space_guid**. API Gateway space identifier. This is optional when using an API gateway service, defaults to the authentication uuid.
78
79### environment variables
80
81Client constructor will read values for the `apihost`, `namespace`, `api_key`, `apigw_token` and `apigw_space_guid` options from the environment if the following parameters are set. Explicit options have precedence over environment values.
82
83- *__OW_API_HOST*
84- *__OW_NAMESPACE*
85- *__OW_API_KEY*
86- *__OW_APIGW_TOKEN*
87- *__OW_APIGW_SPACE_SUID*
88
89
90
91## Examples
92
93### invoke action, blocking for result
94
95```javascript
96const name = 'reverseWords'
97const blocking = true, result = true
98const params = {msg: 'these are some words to reverse'}
99
100ow.actions.invoke({name, blocking, result, params}).then(result => {
101 console.log('here is the reversed string', result.reversed)
102}).catch(err => {
103 console.error('failed to invoke actions', err)
104})
105```
106
107### fire trigger
108
109```javascript
110const name = 'eventTrigger'
111const params = {msg: 'event trigger message string'}
112ow.triggers.invoke({name, params}).then(result => {
113 console.log('trigger fired!')
114}).catch(err => {
115 console.error('failed to fire trigger', err)
116})
117```
118
119### create action from source file
120
121```javascript
122const name = 'reverseWords'
123const action = fs.readFileSync('source.js', {encoding: 'utf8'})
124
125ow.actions.create({name, action}).then(result => {
126 console.log('action created!')
127}).catch(err => {
128 console.error('failed to create action', err)
129})
130```
131
132### create action from zip package
133
134```javascript
135const name = 'reverseWords'
136const action = fs.readFileSync('package.zip')
137
138ow.actions.create({name, action}).then(result => {
139 console.log('action created!')
140}).catch(err => {
141 console.error('failed to create action', err)
142})
143```
144
145### retrieve action resource
146
147```javascript
148const name = 'reverseWords'
149ow.actions.get(name).then(action => {
150 console.log('action resource', action)
151}).catch(err => {
152 console.error('failed to retrieve action', err)
153})
154```
155
156### chaining calls
157
158```javascript
159ow.actions.list()
160 .then(actions => ow.actions.invoke(actions))
161 .then(result => ...)
162```
163
164### list packages
165
166```javascript
167ow.packages.list().then(packages => {
168 packages.forEach(package => console.log(package.name))
169}).catch(err => {
170 console.error('failed to list packages', err)
171})
172```
173
174### update package parameters
175
176```javascript
177const name = 'myPackage'
178const package = {
179 parameters: [
180 {key: "colour", value: "green"},
181 {key: "name", value: "Freya"}
182 ]
183}
184
185ow.packages.update({name, package}).then(package => {
186 console.log('updated package:', package.name)
187}).catch(err => {
188 console.error('failed to update package', err)
189})
190```
191
192### create trigger feed from alarm package
193
194```javascript
195// alarmTrigger MUST already exist in default namespace
196const params = {cron: '*/8 * * * * *', trigger_payload: {name: 'James'}}
197const name = '/whisk.system/alarms/alarm'
198const trigger = 'alarmTrigger'
199ow.feeds.create({name, trigger, params}).then(package => {
200 console.log('alarm trigger feed created')
201}).catch(err => {
202 console.error('failed to create alarm trigger', err)
203})
204```
205
206
207
208## API Details
209
210### resource identifiers + namespaces
211
212When passing resource identifiers as parameters you can either use a short name, without an explicit namespace, or a fully-qualified identifier, including namespace and package details.
213
214If the namespace is missing from the resource identifier, the client will use the namespace from configuration options following this ordering.
215
216- `namespace` from method parameter options OR
217- `namespace` from options passed into client constructor OR
218- `namespace` from environment variable (`__OW_NAMESPACE`) OR
219- default namespace: `_`
220
221### list resources
222
223```javascript
224ow.actions.list()
225ow.activations.list()
226ow.triggers.list()
227ow.rules.list()
228ow.namespaces.list()
229ow.packages.list()
230```
231
232Query parameters for the API calls are supported (e.g. limit, skip, etc.) by passing an object with the named parameters as the first argument.
233
234```javascript
235ow.actions.list({skip: 100, limit: 50})
236```
237
238The following optional parameters are supported:
239- `namespace` - set custom namespace for endpoint
240
241### retrieve resource
242
243```javascript
244ow.actions.get({name: '...'})
245ow.activations.get({name: '...'})
246ow.triggers.get({name: '...'})
247ow.rules.get({name: '...'})
248ow.namespaces.get({name: '...'})
249ow.packages.get({name: '...'})
250```
251
252The following optional parameters are supported:
253- `namespace` - set custom namespace for endpoint
254
255This method also supports passing the `name` property directly without wrapping within an object.
256
257```javascript
258const name = "actionName"
259ow.actions.get(name)
260```
261
262If you pass in an array for the first parameter, the `get` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
263
264```javascript
265ow.actions.get(["a", {name: "b"}])
266```
267
268### delete resource
269
270```javascript
271ow.actions.delete({name: '...'})
272ow.triggers.delete({name: '...'})
273ow.rules.delete({name: '...'})
274ow.packages.delete({name: '...'})
275```
276
277The following optional parameters are supported:
278- `namespace` - set custom namespace for endpoint
279
280This method also supports passing the `name` property directly without wrapping within an object.
281
282```javascript
283const name = "actionName"
284ow.actions.delete(name)
285```
286
287If you pass in an array for the first parameter, the `delete` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
288
289```javascript
290ow.actions.delete(["a", {name: "b"}])
291```
292
293### invoke action
294
295```javascript
296ow.actions.invoke({name: '...'})
297```
298
299The `actionName` parameter supports the following formats: `actionName`, `package/actionName`, `/namespace/actionName`, `/namespace/package/actionName`.
300
301If `actionName` includes a namespace, this overrides any other `namespace` properties.
302
303The following optional parameters are supported:
304- `blocking` - delay returning until action has finished executing (default: `false`)
305- `result` - return function result (`obj.response.result`) rather than entire API result (default: `false`)
306- `params` - JSON object containing parameters for the action being invoked (default: `{}`)
307- `namespace` - set custom namespace for endpoint
308
309This method also supports passing the `name` property directly without wrapping within an object.
310
311```javascript
312const name = "actionName"
313ow.actions.invoke(name)
314```
315
316If you pass in an array for the first parameter, the `invoke` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
317
318```javascript
319ow.actions.invoke(["a", {name: "b", blocking: true}])
320```
321
322### create & update action
323
324```javascript
325ow.actions.create({name: '...', action: 'function main() {};'})
326ow.actions.update({name: '...', action: 'function main() {};'})
327```
328
329The following mandatory parameters are supported:
330- `name` - action identifier
331- `action` - String containing JS function source code, Buffer [containing package action zip file](https://github.com/openwhisk/openwhisk/blob/master/docs/actions.md#packaging-an-action-as-a-nodejs-module) or JSON object containing full parameters for the action body
332
333The following optional parameters are supported:
334- `namespace` - set custom namespace for endpoint
335- `params` - object containing default parameters for the action (default: `{}`)
336- `annotations` - object containing annotations for the action (default: `{}`)
337- `limits` - object containing limits for the action (default: `{}`)
338- `kind` - runtime environment parameter, ignored when `action` is an object (default: `nodejs:default`)
339- `version` - set semantic version of the action. If parameter is empty when create new action openwisk generate 0.0.1 value when update an action increase the patch version.
340
341If you pass in an array for the first parameter, the `create` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
342
343```javascript
344ow.actions.create([{...}, {...}])
345```
346
347### fire trigger
348
349```javascript
350ow.triggers.invoke({name: '...'})
351```
352
353The following optional parameters are supported:
354- `params` - JSON object containing parameters for the trigger being fired (default: `{}`)
355- `namespace` - set custom namespace for endpoint
356
357This method also supports passing the `name` property directly without wrapping within an object.
358
359```javascript
360const name = "actionName"
361ow.triggers.invoke(name)
362```
363
364If you pass in an array for the first parameter, the `invoke` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
365
366```javascript
367ow.triggers.invoke(["a", {name: "b", blocking: true}])
368```
369
370### create & update trigger
371
372```javascript
373ow.triggers.create({name: '...'})
374ow.triggers.update({name: '...'})
375```
376
377The following optional parameters are supported:
378- `trigger` - JSON object containing parameters for the trigger body (default: `{}`)
379- `namespace` - set custom namespace for endpoint
380
381### create & update packages
382
383```javascript
384ow.packages.create({name: '...'})
385ow.packages.update({name: '...'})
386```
387
388The following optional parameters are supported:
389- `package` - JSON object containing parameters for the package body (default: `{}`)
390- `namespace` - set custom namespace for endpoint
391
392### create & update rule
393
394```javascript
395ow.rules.create({name: '...', action: '...', trigger: '...'})
396ow.rules.update({name: '...', action: '...', trigger: '...'})
397```
398
399`trigger` and `action` identifiers will have the default namespace (`/_/`)
400appended in the request, unless a fully qualified name is passed in
401(`/custom_ns/action_or_trigger_name`).
402
403The following optional parameters are supported:
404- `namespace` - set namespace for rule
405
406### enable & disable rule
407
408```javascript
409ow.rules.enable({name: '...'})
410ow.rules.disable({name: '...'})
411```
412
413The following optional parameters are supported:
414- `namespace` - set custom namespace for endpoint
415
416### create & delete feeds
417
418```javascript
419ow.feeds.create({feedName: '...', trigger: '...'})
420ow.feeds.delete({feedName: '...', trigger: '...'})
421```
422
423The following optional parameters are supported:
424- `namespace` - set custom namespace for endpoint
425- `params` - JSON object containing parameters for the feed being invoked (default: `{}`)
426
427## api gateway
428
429OpenWhisk supports a [built-in API gateway service](https://github.com/apache/incubator-openwhisk/blob/master/docs/apigateway.md) and external third-party providers.
430
431This client library defaults to using the platform service. If the `apigw_token` parameter is passed into the client constructor, the implementation will switch to the [IBM Bluemix API Gateway](https://console.ng.bluemix.net/docs/openwhisk/openwhisk_apigateway.html#openwhisk_apigateway).
432
433*The interface for managing routes through the library does not change between providers.*
434
435### list routes
436
437```javascript
438ow.routes.list()
439```
440
441The following optional parameters are supported to filter the result set:
442- `relpath` - relative URI path for endpoints
443- `basepath` - base URI path for endpoints
444- `operation` - HTTP methods
445- `limit` - limit result set size
446- `skip` - skip results from index
447
448*`relpath` is only valid when `basepath` is also specified.*
449
450### delete routes
451
452```javascript
453ow.routes.delete({basepath: '...'})
454```
455
456The following optional parameters are supported to filter the result set:
457- `relpath` - relative URI path for endpoints
458- `operation` - HTTP methods
459
460### add route
461```javascript
462ow.routes.create({relpath: '...', operation: '...', action: '...'})
463```
464
465*`action` supports normal (actionName) and fully-qualified (/namespace/actionName) formats.*
466
467The following optional parameters are supported to filter the result set:
468- `basepath` - base URI path for endpoints (default: `/`)
469
470## Debugging
471
472Setting an environment parameter (`NODE_DEBUG=request`) will dump the HTTP requests from the client library and responses received to `stderr`.
473
474```bash
475NODE_DEBUG=request node script.js
476```
477
478This parameter can also be set dynamically at runtime, provided this happens before the `openwhisk` module is required.
479
480```javascript
481process.env.NODE_DEBUG='request';
482var openwhisk = require('openwhisk');
483```
484
485## Development
486
487### unit tests
488
489```bash
490$ npm test
491```
492
493### integration tests
494
495*Please [see the instructions](https://github.com/openwhisk/openwhisk-client-js/tree/master/test/integration) for setting up the integration test environment prior to running these tests.*
496
497```bash
498$ npm run-script test-integration
499```
500
501**Note:** The test integration runs in secure mode by default, which means that all trusted signers must be present and available to the client process.
502If your local environment is using self-signed certificates, you can use the following command to start the script in insecure mode:
503
504`npm run test-integration -i`
505
506This will disable SSL/TLS verification for all SSL communication.
507
508Alternatively, you can run the `prepIntegrationTests.sh` script using guest credentials or by specifying specific credentials.
509Run the script with openwhisk credentials:
510```bash
511$ ./test/integration/prepIntegrationTests.sh <your key in the form of ABCD:EFGH> <openwhisk instance hostname> <openwhisk namespace> <api gatewaytoken>
512```
513The `prepIntegrationTests.sh` script is designed to give you feedback if it detects a setting that is not correct on your machine. ex: `node 6 or above is not detected`
514
515## Code-Coverage:
516
517You can customize how comprehensive the tests are over the code, and generate reports to view the results by using
518the provided `code-coverage` commands below.
519
520**Note:** Ensure that you use guest credentials with the wsk CLI.
521
522To compile down to ECMA5 run the following command:
5231 `$ npm run code-coverage-build`
524
525To generate combined reports of both the unit and integration tests, run the following command:
5262 `$ npm run code-coverage-run <key> <host> <namespace> <token> <options>`
527
528The report is viewable under `/coverage`. Click **`/coverage/index.html`** to view the full report.
529
\No newline at end of file