-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathdisplayServiceInfo.js
113 lines (90 loc) · 3.46 KB
/
displayServiceInfo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
/* eslint no-use-before-define: 0 */
const chalk = require('chalk');
const BbPromise = require('bluebird');
const _ = require('lodash');
module.exports = {
displayServiceInfo() {
return BbPromise.bind(this)
.then(this.getResources)
.then(this.gatherData)
.then(this.printInfo);
},
getResources() {
const project = this.serverless.service.provider.project;
return this.provider.request('deploymentmanager', 'resources', 'list', {
project,
deployment: `sls-${this.serverless.service.service}-${this.options.stage}`,
});
},
gatherData(resources) {
const data = {};
// general data
data.service = this.serverless.service.service;
data.project = this.serverless.service.provider.project;
data.stage = this.options.stage;
data.region = this.options.region;
data.resources = {
functions: [],
};
_.forEach(resources.resources, (resource) => {
if (resource.type === 'cloudfunctions.v1beta2.function') {
const serviceFuncName = getFunctionNameInService(
resource.name, this.serverless.service.service, this.options.stage);
const serviceFunc = this.serverless.service.getFunction(serviceFuncName);
const eventType = Object.keys(serviceFunc.events[0])[0];
const funcEventConfig = serviceFunc.events[0][eventType];
let funcResource = funcEventConfig.resource || null;
let funcName = serviceFunc.handler;
if (serviceFunc.prependStage) {
funcName = `${this.options.stage}-${funcName}`;
}
if (serviceFunc.prependService) {
funcName = `${this.serverless.service.service}-${funcName}`;
}
if (eventType === 'http') {
const region = this.options.region;
const project = this.serverless.service.provider.project;
const baseUrl = `https://${region}-${project}.cloudfunctions.net`;
const path = funcName; // NOTE this might change
funcResource = `${baseUrl}/${path}`;
}
const func = {
name: serviceFuncName,
resource: funcResource, // how the function can be triggered (e.g. url, pubSub, etc.)
};
data.resources.functions.push(func);
}
});
return BbPromise.resolve(data);
},
printInfo(data) {
let message = '';
// get all the service related information
message += `${chalk.yellow.underline('Service Information')}\n`;
message += `${chalk.yellow('service:')} ${data.service}\n`;
message += `${chalk.yellow('project:')} ${data.project}\n`;
message += `${chalk.yellow('stage:')} ${data.stage}\n`;
message += `${chalk.yellow('region:')} ${data.region}\n`;
message += '\n';
// get all the functions
message += `${chalk.yellow.underline('Deployed functions')}\n`;
if (data.resources.functions.length) {
data.resources.functions.forEach((func) => {
message += `${chalk.yellow(func.name)}\n`;
message += ` ${func.resource}\n`;
});
} else {
message += 'There are no functions deployed yet\n';
}
this.serverless.cli.consoleLog(message);
return BbPromise.resolve();
},
};
const getFunctionNameInService = (funcName, service, stage) => {
let funcNameInService = funcName;
funcNameInService = funcNameInService.replace(service, '');
funcNameInService = funcNameInService.replace(stage, '');
funcNameInService = funcNameInService.slice(2, funcNameInService.length);
return funcNameInService;
};