-
Notifications
You must be signed in to change notification settings - Fork 22
/
project_property_manager.js
275 lines (241 loc) · 9.62 KB
/
project_property_manager.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2018. Akamai Technologies, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const path = require('path');
const _ = require('underscore');
const errors = require('../errors');
const logger = require("../logging").createLogger("pm-cli.project");
const project = require("../project");
/**
* Represents the data model of the property manager property
* responsible for all storage operations within the property/project.
*/
class ProjectPropertyManager extends project {
/**
* @param projectName
* @param dependencies devops: mandatory DevOps instance, elClass, utilsClass are optional.
*
*/
constructor(projectName, dependencies) {
super(projectName, dependencies)
}
createProjectFolders(createProjectInfo) {
logger.info("creating property %s", this.projectFolder);
this.__createProjectFolders();
this.createProjectSettings(createProjectInfo);
}
__createProjectFolders() {
this.utils.mkdir(this.projectFolder);
const folders = ["cache", "dist", "config-snippets"];
_.each(folders, function(name) {
this.utils.mkdir(path.join(this.projectFolder, name));
}, this);
}
createEnvironments() {
//Overwrite project to do nothing.. No environment folders should exist
}
checkEnvironmentName() {
//Do nothing, no need to check environment names! Right!?
}
/*
* overwriting the check for environment names
*/
validateEnvironmentNames() {}
/*
* overwriting new location of envInfo.json
*/
storeEnvironmentInfo(environmentInfo) {
let infoPath = path.join(this.projectFolder, "envInfo.json");
this.utils.writeJsonFile(infoPath, environmentInfo);
}
/*
* loading new location of envInfo.json
*/
loadEnvironmentInfo() {
let infoPath = path.join(this.projectFolder, "envInfo.json");
if (this.utils.fileExists(infoPath)) {
return this.utils.readJsonFile(infoPath);
}
return null;
}
/*
* loading new location of hostname.json
*/
loadEnvironmentHostnames() {
let hostNames = path.join(this.projectFolder, "hostnames.json");
return this.utils.readJsonFile(hostNames);
}
/*
* overwriting new location of hostname.json
*/
storeEnvironmentHostnames(envName, hostnames) {
let hostNames = path.join(this.projectFolder, "hostnames.json");
return this.utils.writeJsonFile(hostNames, hostnames);
}
/*
* overwriting new location of "config-snippets"
*/
storeTemplate(name, template) {
let configSnippetsPath = path.join(this.projectFolder, "config-snippets", name);
this.utils.writeJsonFile(configSnippetsPath, template);
return configSnippetsPath;
}
/*
* loading new location of the "config-snippets"
*/
loadTemplate(name) {
let configSnippetsPath = path.join(this.projectFolder, "config-snippets", name);
return {
resource: this.utils.readJsonFile(configSnippetsPath),
resourcePath: path.join("config-snippets", name)
}
}
storeVariableDefinitions(variables) {
let resourcePath = path.join(this.projectFolder, "variableDefinitions.json");
this.utils.writeJsonFile(resourcePath, variables);
return resourcePath;
}
loadVariableDefinitions() {
let resourcePath = path.join(this.projectFolder, "variableDefinitions.json");
return {
resource: this.utils.readJsonFile(resourcePath),
resourcePath: path.join("variableDefinitions.json")
}
}
variableDefinitionsExist() {
let resourcePath = path.join(this.projectFolder, "variableDefinitions.json");
return this.utils.fileExists(resourcePath);
}
storeEnvironmentVariableValues(variableValues) {
let resourcePath = path.join(this.projectFolder, "variables.json");
this.utils.writeJsonFile(resourcePath, variableValues);
return resourcePath;
}
variablesExist() {
let resourcePath = path.join(this.projectFolder, "variables.json");
return this.utils.fileExists(resourcePath);
}
loadEnvironmentVariableValues() {
let resourcePath = path.join(this.projectFolder, "variables.json");
return {
resource: this.utils.readJsonFile(resourcePath),
resourcePath: path.join("variables.json")
}
}
checkInfoPath(infoPath) {
if (!this.utils.fileExists(infoPath)) {
throw new errors.DependencyError(`projectInfo file: ${infoPath} does not exist!`,
"missing_pm_cli_info_file", infoPath);
}
}
/**
* Setup templates. We can "use" a conversion file but as of right now we don't want any variables.
* @returns {Promise.<void>}
*/
async setupPropertyTemplate(ruleTree, variableMode, update = false) {
let suggestedRuleFormat;
let projectInfo = this.getProjectInfo();
let createTemplates = true;
let env = this.getEnvironment(this.getProjectInfo().name);
if (!_.isObject(ruleTree)) {
let clientSettings = await this.dependencies.getPAPI().getClientSettings();
ruleTree = await env.getRuleTree(clientSettings.ruleFormat);
if (_.isArray(ruleTree.warnings)) {
for (let warning of ruleTree.warnings) {
if (warning.type === "https://problems.luna.akamaiapis.net/papi/v0/unstable_rule_format") {
suggestedRuleFormat = warning.suggestedRuleFormat;
//see comment above, same reason.
ruleTree = await env.getRuleTree(warning.suggestedRuleFormat);
break;
}
}
}
}
env.setupEnvRuleFormat(ruleTree);
let productId = projectInfo.productId;
let resourceData = this.loadAndSubstituteProjectResourceData("snippets.converter.data.json", {
environment: env
});
//if we are doing an "update" we shouldn't overwrite the ruletree with whats on local
if (!update) {
ruleTree.rules.options.is_secure = projectInfo.isSecure;
}
let template = this.dependencies.getTemplate(ruleTree, resourceData, productId);
let templateData = template.process(variableMode);
if (createTemplates) {
this.storeTemplate("main.json", templateData.main);
_.each(templateData.templates, (value, key) => {
this.storeTemplate(key, value);
}, this);
//Only write to variable definitions if there already exists a variable definitions file
//or if we are using 'user-var-value' mode for user variables
if (this.variableDefinitionsExist() || !_.isEmpty(templateData.variables.definitions)) {
this.storeVariableDefinitions(templateData.variables);
}
}
createTemplates = false;
if (_.isString(suggestedRuleFormat)) {
await this.dependencies.getPAPI().setRuleFormat(suggestedRuleFormat);
}
}
/**
* Overwriting this one, since there is no concept of "previous" environment
*/
getPreviousEnvironment() {
}
createProjectSettings(createProjectInfo) {
this.projectInfo = {
productId: createProjectInfo.productId,
contractId: createProjectInfo.contractId,
groupId: createProjectInfo.groupId,
version: this.version,
isSecure: createProjectInfo.secureOption || false
};
if (_.isObject(this.devopsSettings) &&
_.isObject(this.devopsSettings.edgeGridConfig) &&
_.isString(this.devopsSettings.edgeGridConfig.section)) {
this.projectInfo.edgeGridConfig = {
section: this.devopsSettings.edgeGridConfig.section
};
}
this.projectInfo.name = this.projectName;
//todo: what other stuff can go here?
let infoPath = path.join(this.projectFolder, "projectInfo.json");
this.utils.writeJsonFile(infoPath, this.projectInfo);
}
/**
* Promote changes to the project
* @param projectName
* @param network
* @param emails
* @param message
* @returns {Promise<Promise<*>|Promise<{envInfo: *, pending: {network: *, activationId: Number}}>|*>}
*/
async promote(projectName, network, emails, message) {
logger.info(`Activating property '${projectName}' on network '${network}'`);
return this.getEnvironment(projectName).promote(network, emails, message);
}
/**
* Deactivate a property
* @param projectName
* @param network
* @param emails
* @param message
* @returns {Promise<Promise<*>|Promise<{envInfo: *, pending: {network: *, activationId: Number}}>|*>}
*/
async deactivate(projectName, network, emails, message) {
logger.info(`Deactivating property '${projectName}' on network '${network}'`);
return this.getEnvironment(projectName).deactivate(network, emails, message);
}
}
module.exports = ProjectPropertyManager;