generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sandbox.ts
288 lines (259 loc) · 10.1 KB
/
sandbox.ts
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
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Duration, omit } from '@salesforce/kit';
import { Flags } from '@salesforce/sf-plugins-core';
import { Lifecycle, Messages, SandboxEvents, SandboxInfo, SfError } from '@salesforce/core';
import { Interfaces } from '@oclif/core';
import requestFunctions from '../../../shared/sandboxRequest.js';
import { SandboxCommandBase, SandboxCommandResponse } from '../../../shared/sandboxCommandBase.js';
type SandboxInfoRecord = SandboxInfo & {
attributes: {
type: 'SandboxInfo';
url: string;
};
};
// Fields of SandboxInfo
const uneditableFields = ['IsDeleted', 'CreatedDate', 'CreatedById', 'LastModifiedDate', 'LastModifiedById'];
const fields = [
'Id',
'SandboxName', // (string)
'LicenseType', // (string) DEVELOPER | DEVELOPER PRO | PARTIAL | FULL
'TemplateId', // (string) reference to PartitionLevelScheme
'HistoryDays', // (int)
'CopyChatter', // (boolean)
'AutoActivate', // (boolean)
'ApexClassId', // (string) apex class ID
'Description', // (string)
'SourceId', // (string) SandboxInfoId as the source org used for a clone
// 'ActivationUserGroupId', // Currently not supported but might be added in API v61.0
// 'CopyArchivedActivities', -- only for full sandboxes; depends if a license was purchased
...uneditableFields,
];
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'refresh.sandbox');
export default class RefreshSandbox extends SandboxCommandBase<SandboxCommandResponse> {
public static summary = messages.getMessage('summary');
public static description = messages.getMessage('description');
public static examples = messages.getMessages('examples');
public static flags = {
'no-auto-activate': Flags.boolean({
summary: messages.getMessage('flags.no-auto-activate.summary'),
description: messages.getMessage('flags.no-auto-activate.description'),
}),
wait: Flags.duration({
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
description: messages.getMessage('flags.wait.description'),
min: 1,
unit: 'minutes',
default: Duration.minutes(30),
helpValue: '<minutes>',
exclusive: ['async'],
}),
'poll-interval': Flags.duration({
char: 'i',
summary: messages.getMessage('flags.poll-interval.summary'),
min: 15,
unit: 'seconds',
default: Duration.seconds(30),
helpValue: '<seconds>',
exclusive: ['async'],
}),
async: Flags.boolean({
summary: messages.getMessage('flags.async.summary'),
description: messages.getMessage('flags.async.description'),
exclusive: ['wait', 'poll-interval'],
}),
name: Flags.string({
char: 'n',
summary: messages.getMessage('flags.name.summary'),
parse: (name: string): Promise<string> => {
if (name.length > 10) {
throw messages.createError('error.SandboxNameLength', [name]);
}
return Promise.resolve(name);
},
}),
'definition-file': Flags.file({
exists: true,
char: 'f',
summary: messages.getMessage('flags.definitionFile.summary'),
description: messages.getMessage('flags.definitionFile.description'),
}),
'target-org': Flags.requiredOrg({
char: 'o',
summary: messages.getMessage('flags.targetOrg.summary'),
required: true,
}),
'no-prompt': Flags.boolean({
summary: messages.getMessage('flags.noPrompt.summary'),
}),
};
private flags!: Interfaces.InferredFlags<typeof RefreshSandbox.flags>;
private sbxConfig!: SandboxInfo;
public async run(): Promise<SandboxCommandResponse> {
this.sandboxRequestConfig = await this.getSandboxRequestConfig();
this.flags = (await this.parse(RefreshSandbox)).flags;
this.validateFlags();
this.sbxConfig = await this.resolveConfig();
this.debug('Refresh started with args %s ', this.flags);
return this.refreshSandbox();
}
protected getCheckSandboxStatusParams(): string[] {
return [
this.config.bin,
...(this.latestSandboxProgressObj ? [this.latestSandboxProgressObj.Id] : []),
this.flags['target-org'].getUsername() as string,
];
}
private async refreshSandbox(): Promise<SandboxCommandResponse> {
this.prodOrg = this.flags['target-org'];
await this.confirmSandboxRefresh(this.sbxConfig);
const lifecycle = Lifecycle.getInstance();
this.registerLifecycleListeners(lifecycle, { isAsync: this.flags['async'], prodOrg: this.prodOrg });
// remove uneditable fields before refresh
const updateableSandboxInfo = omit(this.sbxConfig, uneditableFields);
this.debug('Calling refresh with SandboxInfo: %s ', updateableSandboxInfo);
this.initSandboxProcessData(this.sbxConfig);
try {
if (!this.flags.async) {
this.spinner.start('Sandbox Refresh');
}
const sandboxProcessObject = await this.prodOrg.refreshSandbox(updateableSandboxInfo, {
wait: this.flags['wait'],
interval: this.flags['poll-interval'],
async: this.flags['async'],
});
this.latestSandboxProgressObj = sandboxProcessObject;
// persist sandbox refresh request in cache for resume
this.sandboxRequestData = {
prodOrgUsername: this.flags['target-org'].getUsername() as string,
action: 'Refresh',
sandboxProcessObject,
sandboxRequest: this.sbxConfig,
};
this.saveSandboxProgressConfig();
if (this.flags.async) {
process.exitCode = 68;
}
return this.getSandboxCommandResponse();
} catch (err) {
this.spinner.stop();
if (this.pollingTimeOut && this.latestSandboxProgressObj) {
void lifecycle.emit(SandboxEvents.EVENT_ASYNC_RESULT, undefined);
process.exitCode = 68;
return this.getSandboxCommandResponse();
} else if (
err instanceof SfError &&
err.name === 'SandboxRefreshNotCompleteError' &&
this.latestSandboxProgressObj
) {
void lifecycle.emit(SandboxEvents.EVENT_ASYNC_RESULT, undefined);
process.exitCode = 68;
return this.latestSandboxProgressObj;
}
throw err;
}
}
private async resolveConfig(): Promise<SandboxInfo> {
const defFile = this.flags['definition-file'];
let sbxName = this.flags['name'];
const defFileContent = defFile ? requestFunctions.readSandboxDefFile(defFile) : {};
// Ensure we have a sandbox name.
if (!defFileContent?.SandboxName && !sbxName) {
throw messages.createError('error.NoSandboxName');
}
let apexId: string | undefined;
let groupId: string | undefined;
if (defFileContent.ApexClassName) {
apexId = await requestFunctions.getApexClassIdByName(
this.flags['target-org'].getConnection(),
defFileContent.ApexClassName
); // convert name to ID
delete defFileContent.ApexClassName;
}
if (defFileContent.ActivationUserGroupName) {
groupId = await requestFunctions.getUserGroupIdByName(
this.flags['target-org'].getConnection(),
defFileContent.ActivationUserGroupName
);
delete defFileContent.ActivationUserGroupName;
}
// Warn if sandbox name is in `--name` and `--definition-file` flags and they differ.
if (defFileContent?.SandboxName && sbxName && sbxName !== defFileContent?.SandboxName) {
this.warn(messages.createWarning('warning.ConflictingSandboxNames', [sbxName, defFileContent?.SandboxName]));
}
sbxName ??= defFileContent.SandboxName as string; // The code above ensures a value for sbxName
const prodOrg = this.flags['target-org'];
const prodOrgConnection = prodOrg.getConnection();
let sandboxInfo: SandboxInfo;
try {
const soql = `SELECT ${fields.join(',')} FROM SandboxInfo WHERE SandboxName='${sbxName}'`;
const sandboxInfoRecord = await prodOrgConnection.singleRecordQuery<SandboxInfoRecord>(soql, { tooling: true });
sandboxInfo = omit(sandboxInfoRecord, 'attributes');
} catch (error: unknown) {
const err =
error instanceof Error ? error : typeof error === 'string' ? SfError.wrap(error) : new SfError('unknown');
if (err.name === 'SingleRecordQuery_NoRecords') {
throw messages.createError('error.SandboxNotFound', [sbxName, prodOrg.getUsername()]);
}
throw err;
}
// assign overrides
sandboxInfo = Object.assign(sandboxInfo, defFileContent, {
SandboxName: sbxName,
AutoActivate: !this.flags['no-auto-activate'],
...(apexId ? { ApexClassId: apexId } : {}),
...(groupId ? { ActivationUserGroupId: groupId } : {}),
});
return sandboxInfo;
}
// Ensure polling flags make sense
private validateFlags(): void {
if (!this.flags['poll-interval'] || !this.flags.wait) {
return;
}
if (this.flags['poll-interval'].seconds > this.flags.wait.seconds) {
throw messages.createError('error.pollIntervalGreaterThanWait', [
this.flags['poll-interval'].seconds,
this.flags.wait.seconds,
]);
}
}
private initSandboxProcessData(sandboxInfo: SandboxInfo): void {
this.sandboxRequestData = {
...this.sandboxRequestData,
prodOrgUsername: this.flags['target-org'].getUsername() as string,
action: 'Refresh',
sandboxProcessObject: {
SandboxName: sandboxInfo.SandboxName,
},
sandboxRequest: sandboxInfo,
};
this.saveSandboxProgressConfig();
}
private async confirmSandboxRefresh(sandboxInfo: SandboxInfo): Promise<void> {
if (this.flags['no-prompt'] || this.jsonEnabled()) return;
const data = Object.entries(sandboxInfo).map(([key, value]) => ({ key, value: value ?? 'null' }));
this.table({
data,
columns: [
{ key: 'key', name: 'Field' },
{ key: 'value', name: 'Value' },
],
title: 'Config Sandbox Refresh',
});
if (
!(await this.confirm({
message: messages.getMessage('isConfigurationOk'),
ms: 30_000,
}))
) {
throw messages.createError('error.UserNotSatisfiedWithSandboxConfig');
}
}
}