-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathdisplayServiceInfo.js
133 lines (110 loc) · 4.06 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'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 === 'gcp-types/cloudfunctions-v1:projects.locations.functions') {
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;
if (eventType === 'http') {
const region = this.options.region;
const project = this.serverless.service.provider.project;
const baseUrl = `https://${region}-${project}.cloudfunctions.net`;
const path = serviceFunc.name; // 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);
},
printInfoV2(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);
},
printInfoV3(data) {
// get all the service related information
this.serverless.addServiceOutputSection('Service', data.service);
this.serverless.addServiceOutputSection('Project', data.project);
this.serverless.addServiceOutputSection('Stage', data.stage);
this.serverless.addServiceOutputSection('Region', data.region);
// get all the functions
if (data.resources.functions.length) {
this.serverless.addServiceOutputSection(
'Deployed Functions',
data.resources.functions.reduce((previous, current) => {
return previous.push(current.name);
}, [])
);
} else {
this.serverless.addServiceOutputSection(
'Deployed Functions',
'No functions currently deployed'
);
}
},
printInfo(data) {
if (this.provider.log) {
this.printInfoV3(data);
} else {
this.printInfoV2(data);
}
return BbPromise.resolve();
},
};
const getFunctionNameInService = (funcName, service, stage) => {
let funcNameInService = funcName;
funcNameInService = funcNameInService.replace(`${service}-`, '');
funcNameInService = funcNameInService.replace(`${stage}-`, '');
return funcNameInService;
};