From b816671a1e45fb93a2dc4eaee5d581f012c393b5 Mon Sep 17 00:00:00 2001 From: Valentin Olteanu Date: Wed, 7 Nov 2018 10:28:50 +0100 Subject: [PATCH] Add basepath as optional parameter to http events --- README.md | 2 ++ compile/apigw/README.md | 3 ++- compile/apigw/index.js | 6 +++++- compile/apigw/tests/index.js | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 444586e..71eff37 100644 --- a/README.md +++ b/README.md @@ -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`). @@ -787,6 +788,7 @@ functions: events: - http: method: GET + basepath: /mybasepath path: /api/http resp: http ``` diff --git a/compile/apigw/README.md b/compile/apigw/README.md index c9633a2..4d04b87 100644 --- a/compile/apigw/README.md +++ b/compile/apigw/README.md @@ -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 @@ -37,6 +37,7 @@ functions: events: - http: method: GET + basepath: /mypath path: /api/greeting resp: json ``` diff --git a/compile/apigw/index.js b/compile/apigw/index.js index c7366b2..1759221 100644 --- a/compile/apigw/index.js +++ b/compile/apigw/index.js @@ -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'] @@ -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) { diff --git a/compile/apigw/tests/index.js b/compile/apigw/tests/index.js index 349ff52..b0ca3ff 100644 --- a/compile/apigw/tests/index.js +++ b/compile/apigw/tests/index.js @@ -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/);