-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cCodeVersionToggle.js
More file actions
203 lines (165 loc) · 6.95 KB
/
_b2cCodeVersionToggle.js
File metadata and controls
203 lines (165 loc) · 6.95 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// noinspection ParameterNamingConventionJS
'use strict';
// Initialize constants
const config = require('config');
// Include local libraries
const common = require('../../lib/cli-api/_common');
// Include B2C Commerce API functions
const b2cAuthenticate = require('../apis/ci/_authenticate');
const codeVersions = require('../apis/ci/code-versions');
/**
* @private
* @unction getCodeVersionList
* @description Abstracted helper-function containing the async snippet used to repeatedly retrieve
* code-version lists in between the activation events required by the toggle
*
* @param {environmentDef} environmentDef Represents the current runtime-environment definition
* @param {String} authToken Represents the authKey used to communicate with B2C Commerce
* @param {String} outputKey Describes the local key used to store errorObj and request responses
* @param {String} codeVersionInstanceKey Describes the local key used to capture the summarized code-versions
* @returns {String} The error message if an error occurred, or nothing in case of success
*/
// eslint-disable-next-line no-shadow
async function getCodeVersionList(environmentDef, authToken, outputKey, codeVersionInstanceKey) {
// Initialize local variables
let output = {};
try {
// Attempt to retrieve the code-versions for the configured environment
const codeVersionsResult = await codeVersions.list(environmentDef, authToken);
// Audit the code-version list results
output.apiCalls[outputKey] = {
error: undefined,
codeVersions: codeVersionsResult
};
// Create the roll-up summary for the B2C Commerce code versions
output[codeVersionInstanceKey] = common.createCodeVersionSummary(codeVersionsResult.data);
// Prepare the data to be displayed in the output
output.outputDisplay[codeVersionInstanceKey] = output[codeVersionInstanceKey].map(codeVersion => [
codeVersion.id,
codeVersion.active,
codeVersion.lastModificationTime,
codeVersion.compatibilityMode,
codeVersion.webDavUrl
]);
} catch (e) {
output.apiCalls[outputKey] = {
error: e,
codeVersions: []
};
}
return output;
}
/**
* @function codeVersionsToggle
* @description Attempts to toggle the active B2C Commerce code versions configured for the specified
* environment -- leveraging SFCC-CI's API to do the work.
*
* @param {Object} environmentDef Represents the already-validated environment details
* to use when performing the actions
* @returns {Promise} Returns the result of the B2C Commerce code-version toggle activity
*/
module.exports = environmentDef => new Promise(async (resolve, reject) => {
// Roll-up the validation results to a single object
const output = {
apiCalls: {
authenticate: {},
toggleCodeVersion: {},
activeCodeVersion: {}
},
outputDisplay: {
authenticate: {}
}
};
// Authenticate first
try {
// Audit the authorization token for future rest requests
output.apiCalls.authenticate.authToken = await b2cAuthenticate(environmentDef);
output.outputDisplay.authenticate.authToken = output.apiCalls.authenticate.authToken;
} catch (e) {
reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
return;
}
// Leverage the code-version list wrapper function to retrieve the code-version listing
output.apiCalls.codeVersionsList = await getCodeVersionList(
environmentDef,
output.apiCalls.authenticate.authToken,
'codeVersionsList',
'codeVersions'
);
if (output.apiCalls.codeVersionsList.error) {
reject(output.apiCalls.codeVersionsList.error);
return;
}
// Identify the toggle code version that we'll leverage
output.apiCalls.toggleCodeVersion = codeVersions.findToggle(output.codeVersions);
// Was a toggle code-version found?
if (output.apiCalls.toggleCodeVersion === undefined) {
// If not, flag that no toggle was found
output.noToggle = true;
// Exit early
resolve(output);
} else {
// Default the codeToggle version details
output.outputDisplay.toggleCodeVersion = [
output.apiCalls.toggleCodeVersion.id,
output.apiCalls.toggleCodeVersion.active,
output.apiCalls.toggleCodeVersion.lastModificationTime,
output.apiCalls.toggleCodeVersion.compatibilityMode,
output.apiCalls.toggleCodeVersion.webDavUrl
];
// Then toggle the code version
try {
output.apiCalls.toggleCodeVersion.toggleResult = await codeVersions.activate(
environmentDef,
output.apiCalls.authenticate.authToken,
output.apiCalls.toggleCodeVersion.id
);
} catch (e) {
reject(`${config.get('errors.b2c.unableToActivateCodeVersion')}`);
return;
}
// Leverage the code-version list wrapper function to retrieve the code-version listing
output.apiCalls.codeVersionsToggleList = await getCodeVersionList(
environmentDef,
output.apiCalls.authenticate.authToken,
'codeVersionsToggleList',
'codeVersionsToggle'
);
if (output.apiCalls.codeVersionsToggleList.error) {
reject(output.apiCalls.codeVersionsToggleList.error);
return;
}
// Next, identify the toggle code version that's active (to toggle back to)
output.apiCalls.activeCodeVersion = codeVersions.findToggle(output.codeVersions, true);
output.outputDisplay.activeCodeVersion = [
output.apiCalls.activeCodeVersion.id,
output.apiCalls.activeCodeVersion.active,
output.apiCalls.activeCodeVersion.lastModificationTime,
output.apiCalls.activeCodeVersion.compatibilityMode,
output.apiCalls.activeCodeVersion.webDavUrl
];
// Then toggle the code version
try {
output.apiCalls.activeCodeVersion.toggleResult = await codeVersions.activate(
environmentDef,
output.apiCalls.authenticate.authToken,
output.apiCalls.activeCodeVersion.id
);
} catch (e) {
reject(`${config.get('errors.b2c.unableToActivateCodeVersion')}`);
return;
}
// Leverage the code-version list wrapper function to retrieve the code-version listing
output.apiCalls.codeVersionsActivateList = await getCodeVersionList(
environmentDef,
output.apiCalls.authenticate.authToken,
'codeVersionsActivateList',
'codeVersionsActivate'
);
if (output.apiCalls.codeVersionsActivateList.error) {
reject(output.apiCalls.codeVersionsActivateList.error);
return;
}
resolve(output);
}
});