|
| 1 | +import {Flags, ux} from '@oclif/core'; |
| 2 | +import cliui from 'cliui'; |
| 3 | +import {OdsCommand} from '@salesforce/b2c-tooling/cli'; |
| 4 | +import type {OdsComponents} from '@salesforce/b2c-tooling'; |
| 5 | +import {t} from '../../i18n/index.js'; |
| 6 | + |
| 7 | +type SandboxModel = OdsComponents['schemas']['SandboxModel']; |
| 8 | +type SandboxResourceProfile = OdsComponents['schemas']['SandboxResourceProfile']; |
| 9 | +type SandboxState = OdsComponents['schemas']['SandboxState']; |
| 10 | + |
| 11 | +/** States that indicate sandbox creation has completed (success or failure) */ |
| 12 | +const TERMINAL_STATES = new Set<SandboxState>(['deleted', 'failed', 'started']); |
| 13 | + |
| 14 | +/** |
| 15 | + * Command to create a new on-demand sandbox. |
| 16 | + */ |
| 17 | +export default class OdsCreate extends OdsCommand<typeof OdsCreate> { |
| 18 | + static description = t('commands.ods.create.description', 'Create a new on-demand sandbox'); |
| 19 | + |
| 20 | + static enableJsonFlag = true; |
| 21 | + |
| 22 | + static examples = [ |
| 23 | + '<%= config.bin %> <%= command.id %> --realm abcd', |
| 24 | + '<%= config.bin %> <%= command.id %> --realm abcd --ttl 48', |
| 25 | + '<%= config.bin %> <%= command.id %> --realm abcd --profile large', |
| 26 | + '<%= config.bin %> <%= command.id %> --realm abcd --auto-scheduled', |
| 27 | + '<%= config.bin %> <%= command.id %> --realm abcd --wait', |
| 28 | + '<%= config.bin %> <%= command.id %> --realm abcd --wait --poll-interval 15', |
| 29 | + '<%= config.bin %> <%= command.id %> --realm abcd --json', |
| 30 | + ]; |
| 31 | + |
| 32 | + static flags = { |
| 33 | + realm: Flags.string({ |
| 34 | + char: 'r', |
| 35 | + description: 'Realm ID (four-letter ID)', |
| 36 | + required: true, |
| 37 | + }), |
| 38 | + ttl: Flags.integer({ |
| 39 | + description: 'Time to live in hours (0 for infinite)', |
| 40 | + default: 24, |
| 41 | + }), |
| 42 | + profile: Flags.string({ |
| 43 | + description: 'Resource profile (medium, large, xlarge, xxlarge)', |
| 44 | + default: 'medium', |
| 45 | + options: ['medium', 'large', 'xlarge', 'xxlarge'], |
| 46 | + }), |
| 47 | + 'auto-scheduled': Flags.boolean({ |
| 48 | + description: 'Enable automatic start/stop scheduling', |
| 49 | + default: false, |
| 50 | + }), |
| 51 | + wait: Flags.boolean({ |
| 52 | + char: 'w', |
| 53 | + description: 'Wait for the sandbox to reach started or failed state before returning', |
| 54 | + default: false, |
| 55 | + }), |
| 56 | + 'poll-interval': Flags.integer({ |
| 57 | + description: 'Polling interval in seconds when using --wait', |
| 58 | + default: 10, |
| 59 | + dependsOn: ['wait'], |
| 60 | + }), |
| 61 | + timeout: Flags.integer({ |
| 62 | + description: 'Maximum time to wait in seconds when using --wait (0 for no timeout)', |
| 63 | + default: 600, |
| 64 | + dependsOn: ['wait'], |
| 65 | + }), |
| 66 | + }; |
| 67 | + |
| 68 | + async run(): Promise<SandboxModel> { |
| 69 | + const realm = this.flags.realm; |
| 70 | + const profile = this.flags.profile as SandboxResourceProfile; |
| 71 | + const ttl = this.flags.ttl; |
| 72 | + const autoScheduled = this.flags['auto-scheduled']; |
| 73 | + const wait = this.flags.wait; |
| 74 | + const pollInterval = this.flags['poll-interval']; |
| 75 | + const timeout = this.flags.timeout; |
| 76 | + |
| 77 | + this.log(t('commands.ods.create.creating', 'Creating sandbox in realm {{realm}}...', {realm})); |
| 78 | + this.log(t('commands.ods.create.profile', 'Profile: {{profile}}', {profile})); |
| 79 | + this.log(t('commands.ods.create.ttl', 'TTL: {{ttl}} hours', {ttl: ttl === 0 ? 'infinite' : String(ttl)})); |
| 80 | + |
| 81 | + const result = await this.odsClient.POST('/sandboxes', { |
| 82 | + body: { |
| 83 | + realm, |
| 84 | + ttl, |
| 85 | + resourceProfile: profile, |
| 86 | + autoScheduled, |
| 87 | + analyticsEnabled: false, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + if (!result.data?.data) { |
| 92 | + const errorResponse = result.error as OdsComponents['schemas']['ErrorResponse'] | undefined; |
| 93 | + const errorMessage = errorResponse?.error?.message || result.response?.statusText || 'Unknown error'; |
| 94 | + this.error( |
| 95 | + t('commands.ods.create.error', 'Failed to create sandbox: {{message}}', { |
| 96 | + message: errorMessage, |
| 97 | + }), |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + let sandbox = result.data.data; |
| 102 | + |
| 103 | + this.log(''); |
| 104 | + this.log(t('commands.ods.create.success', 'Sandbox created successfully!')); |
| 105 | + |
| 106 | + if (wait && sandbox.id) { |
| 107 | + this.log(''); |
| 108 | + sandbox = await this.waitForSandbox(sandbox.id, pollInterval, timeout); |
| 109 | + } |
| 110 | + |
| 111 | + if (this.jsonEnabled()) { |
| 112 | + return sandbox; |
| 113 | + } |
| 114 | + |
| 115 | + this.printSandboxSummary(sandbox); |
| 116 | + |
| 117 | + return sandbox; |
| 118 | + } |
| 119 | + |
| 120 | + private printSandboxSummary(sandbox: SandboxModel): void { |
| 121 | + const ui = cliui({width: process.stdout.columns || 80}); |
| 122 | + |
| 123 | + ui.div({text: '', padding: [0, 0, 0, 0]}); |
| 124 | + |
| 125 | + const fields: [string, string | undefined][] = [ |
| 126 | + ['ID', sandbox.id], |
| 127 | + ['Realm', sandbox.realm], |
| 128 | + ['Instance', sandbox.instance], |
| 129 | + ['State', sandbox.state], |
| 130 | + ['Profile', sandbox.resourceProfile], |
| 131 | + ['Hostname', sandbox.hostName], |
| 132 | + ]; |
| 133 | + |
| 134 | + for (const [label, value] of fields) { |
| 135 | + if (value !== undefined) { |
| 136 | + ui.div({text: `${label}:`, width: 15, padding: [0, 2, 0, 0]}, {text: value, padding: [0, 0, 0, 0]}); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (sandbox.links?.bm) { |
| 141 | + ui.div({text: '', padding: [0, 0, 0, 0]}); |
| 142 | + ui.div({text: 'BM URL:', width: 15, padding: [0, 2, 0, 0]}, {text: sandbox.links.bm, padding: [0, 0, 0, 0]}); |
| 143 | + } |
| 144 | + |
| 145 | + ux.stdout(ui.toString()); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Sleep for a given number of milliseconds. |
| 150 | + */ |
| 151 | + private async sleep(ms: number): Promise<void> { |
| 152 | + await new Promise((resolve) => { |
| 153 | + setTimeout(resolve, ms); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Polls for sandbox status until it reaches a terminal state. |
| 159 | + * @param sandboxId - The sandbox ID to poll |
| 160 | + * @param pollIntervalSeconds - Interval between polls in seconds |
| 161 | + * @param timeoutSeconds - Maximum time to wait (0 for no timeout) |
| 162 | + * @returns The final sandbox state |
| 163 | + */ |
| 164 | + private async waitForSandbox( |
| 165 | + sandboxId: string, |
| 166 | + pollIntervalSeconds: number, |
| 167 | + timeoutSeconds: number, |
| 168 | + ): Promise<SandboxModel> { |
| 169 | + const startTime = Date.now(); |
| 170 | + const pollIntervalMs = pollIntervalSeconds * 1000; |
| 171 | + const timeoutMs = timeoutSeconds * 1000; |
| 172 | + let lastState: SandboxState | undefined; |
| 173 | + |
| 174 | + this.log(t('commands.ods.create.waiting', 'Waiting for sandbox to be ready...')); |
| 175 | + |
| 176 | + while (true) { |
| 177 | + // Check for timeout |
| 178 | + if (timeoutSeconds > 0 && Date.now() - startTime > timeoutMs) { |
| 179 | + this.error( |
| 180 | + t('commands.ods.create.timeout', 'Timeout waiting for sandbox after {{seconds}} seconds', { |
| 181 | + seconds: String(timeoutSeconds), |
| 182 | + }), |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + // eslint-disable-next-line no-await-in-loop |
| 187 | + const result = await this.odsClient.GET('/sandboxes/{sandboxId}', { |
| 188 | + params: { |
| 189 | + path: {sandboxId}, |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + if (!result.data?.data) { |
| 194 | + this.error( |
| 195 | + t('commands.ods.create.pollError', 'Failed to fetch sandbox status: {{message}}', { |
| 196 | + message: result.response?.statusText || 'Unknown error', |
| 197 | + }), |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + const sandbox = result.data.data; |
| 202 | + const currentState = sandbox.state as SandboxState; |
| 203 | + |
| 204 | + // Log state changes |
| 205 | + if (currentState !== lastState) { |
| 206 | + const elapsed = Math.round((Date.now() - startTime) / 1000); |
| 207 | + this.log( |
| 208 | + t('commands.ods.create.stateChange', '[{{elapsed}}s] State: {{state}}', { |
| 209 | + elapsed: String(elapsed), |
| 210 | + state: currentState || 'unknown', |
| 211 | + }), |
| 212 | + ); |
| 213 | + lastState = currentState; |
| 214 | + } |
| 215 | + |
| 216 | + // Check for terminal states |
| 217 | + if (currentState && TERMINAL_STATES.has(currentState)) { |
| 218 | + switch (currentState) { |
| 219 | + case 'deleted': { |
| 220 | + this.error(t('commands.ods.create.deleted', 'Sandbox was deleted')); |
| 221 | + break; |
| 222 | + } |
| 223 | + case 'failed': { |
| 224 | + this.error(t('commands.ods.create.failed', 'Sandbox creation failed')); |
| 225 | + break; |
| 226 | + } |
| 227 | + case 'started': { |
| 228 | + this.log(''); |
| 229 | + this.log(t('commands.ods.create.ready', 'Sandbox is now ready!')); |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + return sandbox; |
| 234 | + } |
| 235 | + |
| 236 | + // Wait before next poll |
| 237 | + // eslint-disable-next-line no-await-in-loop |
| 238 | + await this.sleep(pollIntervalMs); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments