-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdelete.ts
More file actions
187 lines (163 loc) · 5.61 KB
/
delete.ts
File metadata and controls
187 lines (163 loc) · 5.61 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
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import * as readline from 'node:readline';
import {Args, Flags} from '@oclif/core';
import {OdsCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {
getApiErrorMessage,
SandboxPollingError,
SandboxPollingTimeoutError,
SandboxTerminalStateError,
waitForSandbox,
type SandboxState,
} from '@salesforce/b2c-tooling-sdk';
import {t, withDocs} from '../../i18n/index.js';
/**
* Simple confirmation prompt.
*/
async function confirm(message: string): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stderr,
});
return new Promise((resolve) => {
rl.question(`${message} `, (answer) => {
rl.close();
resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
});
});
}
/**
* Command to delete an on-demand sandbox.
*/
export default class SandboxDelete extends OdsCommand<typeof SandboxDelete> {
static aliases = ['ods:delete'];
static args = {
sandboxId: Args.string({
description: 'Sandbox ID (UUID or realm-instance, e.g., abcd-123)',
required: true,
}),
};
static description = withDocs(
t('commands.sandbox.delete.description', 'Delete an on-demand sandbox'),
'/cli/sandbox.html#b2c-sandbox-delete',
);
static examples = [
'<%= config.bin %> <%= command.id %> abc12345-1234-1234-1234-abc123456789',
'<%= config.bin %> <%= command.id %> zzzv-123',
'<%= config.bin %> <%= command.id %> zzzv_123 --force',
];
static flags = {
force: Flags.boolean({
char: 'f',
description: 'Skip confirmation prompt',
default: false,
}),
wait: Flags.boolean({
char: 'w',
description: 'Wait for the sandbox to be fully deleted before returning',
default: false,
}),
'poll-interval': Flags.integer({
description: 'Polling interval in seconds when using --wait',
default: 10,
dependsOn: ['wait'],
}),
timeout: Flags.integer({
description: 'Maximum time to wait in seconds when using --wait (0 for no timeout)',
default: 600,
dependsOn: ['wait'],
}),
};
async run(): Promise<void> {
// Prevent deletion in safe mode
this.assertDestructiveOperationAllowed('delete sandbox');
const sandboxId = await this.resolveSandboxId(this.args.sandboxId);
const wait = this.flags.wait as boolean;
const pollInterval = this.flags['poll-interval'] as number;
const timeout = this.flags.timeout as number;
// Get sandbox details first to show in confirmation
const getResult = await this.odsClient.GET('/sandboxes/{sandboxId}', {
params: {
path: {sandboxId},
},
});
if (!getResult.data?.data) {
this.error(t('commands.sandbox.delete.notFound', 'Sandbox not found: {{sandboxId}}', {sandboxId}));
}
const sandbox = getResult.data.data;
const sandboxInfo = `${sandbox.realm}/${sandbox.instance || sandboxId}`;
// Confirm deletion unless --force is used
if (!this.flags.force) {
const confirmed = await confirm(
t('commands.sandbox.delete.confirm', 'Are you sure you want to delete sandbox "{{sandboxInfo}}"? (y/n)', {
sandboxInfo,
}),
);
if (!confirmed) {
this.log(t('commands.sandbox.delete.cancelled', 'Deletion cancelled'));
return;
}
}
this.log(t('commands.sandbox.delete.deleting', 'Deleting sandbox {{sandboxInfo}}...', {sandboxInfo}));
const result = await this.odsClient.DELETE('/sandboxes/{sandboxId}', {
params: {
path: {sandboxId},
},
});
if (result.response.status !== 202) {
this.error(
t('commands.sandbox.delete.error', 'Failed to delete sandbox: {{message}}', {
message: getApiErrorMessage(result.error, result.response),
}),
);
}
this.log(t('commands.sandbox.delete.success', 'Sandbox deletion initiated. The sandbox will be removed shortly.'));
if (wait) {
this.log(
t('commands.sandbox.delete.waiting', 'Waiting for sandbox to reach state {{state}}...', {
state: 'deleted' satisfies SandboxState,
}),
);
try {
await waitForSandbox(this.odsClient, {
sandboxId,
targetState: 'deleted',
pollIntervalSeconds: pollInterval,
timeoutSeconds: timeout,
onPoll: ({elapsedSeconds, state}) => {
this.logger.info({sandboxId, elapsed: elapsedSeconds, state}, `[${elapsedSeconds}s] State: ${state}`);
},
});
} catch (error) {
if (error instanceof SandboxPollingTimeoutError) {
this.error(
t('commands.sandbox.delete.timeout', 'Timeout waiting for sandbox after {{seconds}} seconds', {
seconds: String(error.timeoutSeconds),
}),
);
}
if (error instanceof SandboxTerminalStateError) {
this.error(
t('commands.sandbox.delete.failed', 'Sandbox did not reach the expected state. Current state: {{state}}', {
state: error.state || 'unknown',
}),
);
}
if (error instanceof SandboxPollingError) {
this.error(
t('commands.sandbox.delete.pollError', 'Failed to fetch sandbox status: {{message}}', {
message: error.message,
}),
);
}
throw error;
}
this.log('');
this.logger.info({sandboxId}, t('commands.sandbox.delete.ready', 'Sandbox is now deleted'));
}
}
}