forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegionRemove.js
More file actions
181 lines (151 loc) · 5.57 KB
/
RegionRemove.js
File metadata and controls
181 lines (151 loc) · 5.57 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
/**
* Action: RegionRemove
* - Removes a region from your project in a provided stage. If project only
* has one stage, no stage needs to be provided.
* - Removes CF stack by default, unless noExeCf option is set to true
*
* Options:
* - region (String) the name of the new region
* - stage (String) the name of the stage you want to create a region in. Optional if only one stage in project.
* - noExeCf: (Boolean) Don't execute CloudFormation
*/
module.exports = function(S) {
const path = require('path'),
SUtils = S.utils,
SError = require(S.getServerlessPath('Error')),
SCli = require(S.getServerlessPath('utils/cli')),
BbPromise = require('bluebird'),
fs = BbPromise.promisifyAll(require('fs')),
_ = require('lodash');
/**
* RegionCreate Class
*/
class RegionRemove extends S.classes.Plugin {
static getName() {
return 'serverless.core.' + this.name;
}
registerActions() {
S.addAction(this.regionRemove.bind(this), {
handler: 'regionRemove',
description: 'Removes a region from a stage in a project. Usage: serverless region remove',
context: 'region',
contextAction: 'remove',
options: [
{
option: 'region',
shortcut: 'r',
description: 'The region you want to remove'
},
{
option: 'stage',
shortcut: 's',
description: 'The stage you want to remove a region from'
},
{
option: 'noExeCf',
shortcut: 'c',
description: 'Optional - Don\'t execute CloudFormation, just remove it. Default: false'
}
]
});
return BbPromise.resolve();
}
/**
* Action
*/
regionRemove(evt) {
this.evt = evt;
return this._prompt()
.bind(this)
.then(this._validateAndPrepare)
.then(() => { SCli.log(`Removing region "${this.evt.options.region}" in stage "${this.evt.options.stage}"...`); })
.then(this._removeFunctions)
.then(this._removeResources)
.then(this._removeRegionFromProject)
.then(this._removeVariables)
.then((reply) => {
SCli.log(`Successfully removed region "${this.evt.options.region}" within stage "${this.evt.options.stage}"`);
this.evt.data.region = this.evt.options.region;
return this.evt;
});
}
/**
* Prompt stage and region
*/
_prompt() {
// Skip if non-interactive or stage is provided
if (!S.config.interactive || (this.evt.options.stage && this.evt.options.region)) return BbPromise.resolve();
if (!S.getProject().getAllStages().length) return BbPromise.reject(new SError('No existing stages in the project'));
return this.cliPromptSelectStage('Select an existing stage: ', this.evt.options.stage, false)
.then(stage => { this.evt.options.stage = stage; })
.then(() => {
if (S.getProject().getAllRegions(this.evt.options.stage).length)
return this.cliPromptSelectRegion('Select a region to remove: ', false, true, this.evt.options.region, this.evt.options.stage);
else
return BbPromise.reject(new SError(`No existing regions in "${this.evt.options.stage}" stage`));
})
.then(region => { this.evt.options.region = region; });
}
/**
* Validate & Prepare
* - Validate all data from event, interactive CLI or non interactive CLI
* and prepare data
*/
_validateAndPrepare() {
// Check Params
if (!this.evt.options.stage || !this.evt.options.region) {
return BbPromise.reject(new SError('Missing stage or region'));
}
// Validate stage: make sure stage exists
if (!S.getProject().validateStageExists(this.evt.options.stage)) {
return BbPromise.reject(new SError('Stage ' + this.evt.options.stage + ' does not exist in your project', SError.errorCodes.UNKNOWN));
}
// Validate region: make sure region exists in stage
if (!S.getProject().validateRegionExists(this.evt.options.stage, this.evt.options.region)) {
return BbPromise.reject(new SError('Region "' + this.evt.options.region + '" does not exists in stage "' + this.evt.options.stage + '"'));
}
}
/**
* Remove Region From Project
*/
_removeRegionFromProject() {
// Update and save Meta
S.getProject().getStage(this.evt.options.stage).removeRegion(this.evt.options.region);
return S.getProject().getStage(this.evt.options.stage).save();
}
/**
* Remove Variables
* - Remove region variables from _meta folder
*/
_removeVariables() {
let fileName = `s-variables-${this.evt.options.stage}-${this.evt.options.region.replace(/-/g, '')}.json`;
return fs.unlinkAsync(S.getProject().getRootPath( '_meta', 'variables', fileName ));
}
/**
* Remove Resources from Stage/Region
*/
_removeResources() {
return S.actions.resourcesRemove({
options: {
stage: this.evt.options.stage,
region: this.evt.options.region,
noExeCf: !!this.evt.options.noExeCf
}
});
}
/**
* Remove Functions from Stage/Region
*/
_removeFunctions() {
return S.actions.functionRemove({
options: {
stage: this.evt.options.stage,
region: this.evt.options.region,
all: true
}
});
}
}
return RegionRemove;
};