This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdeployApiGw.js
64 lines (54 loc) · 2.13 KB
/
deployApiGw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const BbPromise = require('bluebird');
module.exports = {
deployRouteSwagger(route) {
return this.provider.client().then(ow => {
if (this.options.verbose) {
this.serverless.cli.log(`Deploying API Gateway Route: ${JSON.stringify(route)}`);
}
return ow.routes.create(route)
.then(() => {
if (this.options.verbose) {
this.serverless.cli.log(`Deployed API Gateway Route: ${JSON.stringify(route)}`);
}
}).catch(err => {
throw new this.serverless.classes.Error(
`Failed to deploy API Gateway route due to error: ${err.message}`
);
})
});
},
replaceDefaultNamespace(swagger) {
return this.provider.client()
.then(ow => ow.actions.list())
.then(allActions => {
for(let path in swagger.paths) {
for(let verb in swagger.paths[path]) {
const operation = swagger.paths[path][verb]
if (operation['x-openwhisk'].namespace === '_') {
const swaggerAction = operation['x-openwhisk']
const action = allActions.find(item => item.name === swaggerAction.action)
const namespace = `${action.namespace}`.replace(/\/.*/, '')
swaggerAction.namespace = namespace
swaggerAction.url = swaggerAction.url.replace(/web\/_/, `web/${namespace}`)
const id = operation.operationId
const stmts = swagger["x-ibm-configuration"].assembly.execute[0]['operation-switch'].case
const stmt = stmts.find(stmt => stmt.operations[0] === id)
const invoke = stmt.execute[stmt.execute.length -1].invoke
invoke['target-url'] = invoke['target-url'].replace(/web\/_/, `web/${namespace}`)
}
}
}
return swagger
})
},
deployRoutes() {
const apigw = this.serverless.service.apigw;
if (!apigw.swagger) {
return BbPromise.resolve();
}
this.serverless.cli.log('Deploying API Gateway definitions...');
return this.replaceDefaultNamespace(apigw.swagger)
.then(swagger => this.deployRouteSwagger({ swagger }))
}
};