Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ functions:
HTTP event configuration also supports using explicit parameters.

- `method` - HTTP method (mandatory).
- `basepath` - base path of the API in which the event is added (optional, defaults to service name)
- `path` - URI path for API gateway (mandatory).
- `resp` - controls [web action content type](https://github.com/apache/incubator-openwhisk/blob/master/docs/webactions.md#additional-features), values include: `json`, `html`, `http`, `svg`or `text` (optional, defaults to `json`).

Expand All @@ -787,6 +788,7 @@ functions:
events:
- http:
method: GET
basepath: /mybasepath
path: /api/http
resp: http
```
Expand Down
3 changes: 2 additions & 1 deletion compile/apigw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ definitions.

It loops over all functions which are defined in `serverless.yaml` looking for
the defined events. For each `http` event defined for the function, the
corresponding API gateway endpoint definitoin will be created.
corresponding API gateway endpoint definition will be created.

## Examples

Expand All @@ -37,6 +37,7 @@ functions:
events:
- http:
method: GET
basepath: /mypath
path: /api/greeting
resp: json
```
Expand Down
6 changes: 5 additions & 1 deletion compile/apigw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class OpenWhiskCompileHttpEvents {
|| '_';
}

calculateBasePath(httpEvent) {
return httpEvent.basepath || `/${this.serverless.service.service}`;
}

retrieveAuthKey(functionObject) {
const annotations = functionObject.annotations || {}
return annotations['require-whisk-auth']
Expand All @@ -82,7 +86,7 @@ class OpenWhiskCompileHttpEvents {
compileHttpEvent(funcName, funcObj, http) {
const options = this.parseHttpEvent(http);
options.action = this.calculateFunctionName(funcName, funcObj);
options.basepath = `/${this.serverless.service.service}`;
options.basepath = this.calculateBasePath(http);

const secure_key = this.retrieveAuthKey(funcObj)
if (secure_key) {
Expand Down
8 changes: 8 additions & 0 deletions compile/apigw/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ describe('OpenWhiskCompileHttpEvents', () => {
return expect(result).to.deep.equal({basepath: '/my-service', relpath: '/api/foo/bar', operation: 'GET', action: '/sample_ns/my-service_action-name', responsetype: 'http'});
});

it('should define http events with explicit base path', () => {
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
const http = {path: "/api/foo/bar", method: "GET", resp: 'http', basepath: '/custompath'}
const result = openwhiskCompileHttpEvents.compileHttpEvent('action-name', {}, http);
return expect(result).to.deep.equal({basepath: '/custompath', relpath: '/api/foo/bar', operation: 'GET', action: '/sample_ns/my-service_action-name', responsetype: 'http'});
});

it('should throw if http event value invalid', () => {
expect(() => openwhiskCompileHttpEvents.compileHttpEvent('', {}, 'OPERATION'))
.to.throw(Error, /Incorrect HTTP event/);
Expand Down