|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +/** |
| 7 | + * Sandbox ID lookup utilities for resolving friendly sandbox identifiers. |
| 8 | + * |
| 9 | + * @module operations/ods/sandbox-lookup |
| 10 | + */ |
| 11 | +import type {OdsClient} from '../../clients/ods.js'; |
| 12 | + |
| 13 | +/** |
| 14 | + * UUID regex pattern (standard 8-4-4-4-12 format). |
| 15 | + */ |
| 16 | +const UUID_REGEX = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i; |
| 17 | + |
| 18 | +/** |
| 19 | + * Friendly sandbox ID pattern: realm-instance or realm_instance |
| 20 | + * - realm: 4 alphanumeric characters |
| 21 | + * - separator: dash or underscore |
| 22 | + * - instance: 1+ alphanumeric characters |
| 23 | + */ |
| 24 | +const FRIENDLY_ID_REGEX = /^([a-z\d]{4})[-_]([a-z\d]+)$/i; |
| 25 | + |
| 26 | +/** |
| 27 | + * Error thrown when a sandbox cannot be found by its friendly identifier. |
| 28 | + */ |
| 29 | +export class SandboxNotFoundError extends Error { |
| 30 | + constructor( |
| 31 | + public readonly identifier: string, |
| 32 | + public readonly realm?: string, |
| 33 | + public readonly instance?: string, |
| 34 | + ) { |
| 35 | + const message = |
| 36 | + realm && instance |
| 37 | + ? `Sandbox not found: ${identifier} (realm=${realm}, instance=${instance})` |
| 38 | + : `Sandbox not found: ${identifier}`; |
| 39 | + super(message); |
| 40 | + this.name = 'SandboxNotFoundError'; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Checks if a string is a valid UUID. |
| 46 | + * |
| 47 | + * @param value - The string to check |
| 48 | + * @returns true if the value is a valid UUID |
| 49 | + */ |
| 50 | +export function isUuid(value: string): boolean { |
| 51 | + return UUID_REGEX.test(value); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Checks if a string matches the friendly sandbox ID format (realm-instance or realm_instance). |
| 56 | + * |
| 57 | + * @param value - The string to check |
| 58 | + * @returns true if the value matches the friendly format |
| 59 | + */ |
| 60 | +export function isFriendlySandboxId(value: string): boolean { |
| 61 | + return FRIENDLY_ID_REGEX.test(value); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Parses a friendly sandbox ID into its realm and instance components. |
| 66 | + * |
| 67 | + * @param value - The friendly ID to parse (e.g., "abcd-123" or "abcd_123") |
| 68 | + * @returns Object with realm and instance, or null if not a valid friendly ID |
| 69 | + */ |
| 70 | +export function parseFriendlySandboxId(value: string): {realm: string; instance: string} | null { |
| 71 | + const match = value.match(FRIENDLY_ID_REGEX); |
| 72 | + if (!match) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + return { |
| 76 | + realm: match[1].toLowerCase(), |
| 77 | + instance: match[2].toLowerCase(), |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Resolves a sandbox identifier to a UUID. |
| 83 | + * |
| 84 | + * If the identifier is already a UUID, it is returned directly without making an API call. |
| 85 | + * If the identifier is a friendly format (realm-instance), it queries the ODS API to find |
| 86 | + * the matching sandbox and returns its UUID. |
| 87 | + * |
| 88 | + * @param client - The ODS API client |
| 89 | + * @param identifier - Sandbox identifier (UUID or friendly format like "abcd-123") |
| 90 | + * @returns The sandbox UUID |
| 91 | + * @throws {SandboxNotFoundError} If the sandbox cannot be found |
| 92 | + * |
| 93 | + * @example |
| 94 | + * ```typescript |
| 95 | + * // UUID is returned directly |
| 96 | + * const uuid = await resolveSandboxId(client, 'abc12345-1234-1234-1234-abc123456789'); |
| 97 | + * // => 'abc12345-1234-1234-1234-abc123456789' |
| 98 | + * |
| 99 | + * // Friendly ID is looked up |
| 100 | + * const uuid = await resolveSandboxId(client, 'zzzv-123'); |
| 101 | + * // => 'abc12345-1234-1234-1234-abc123456789' (actual UUID from API) |
| 102 | + * ``` |
| 103 | + */ |
| 104 | +export async function resolveSandboxId(client: OdsClient, identifier: string): Promise<string> { |
| 105 | + // If already a UUID, return directly |
| 106 | + if (isUuid(identifier)) { |
| 107 | + return identifier; |
| 108 | + } |
| 109 | + |
| 110 | + // Try to parse as friendly ID |
| 111 | + const parsed = parseFriendlySandboxId(identifier); |
| 112 | + if (!parsed) { |
| 113 | + // Not a UUID and not a friendly ID - treat as UUID and let API return 404 |
| 114 | + return identifier; |
| 115 | + } |
| 116 | + |
| 117 | + // Query sandboxes filtered by realm |
| 118 | + const {data, error} = await client.GET('/sandboxes', { |
| 119 | + params: { |
| 120 | + query: { |
| 121 | + filter_params: `realm=${parsed.realm}`, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + if (error || !data?.data) { |
| 127 | + throw new SandboxNotFoundError(identifier, parsed.realm, parsed.instance); |
| 128 | + } |
| 129 | + |
| 130 | + // Find sandbox with matching instance |
| 131 | + const sandbox = data.data.find((s) => s.instance?.toLowerCase() === parsed.instance); |
| 132 | + |
| 133 | + if (!sandbox?.id) { |
| 134 | + throw new SandboxNotFoundError(identifier, parsed.realm, parsed.instance); |
| 135 | + } |
| 136 | + |
| 137 | + return sandbox.id; |
| 138 | +} |
0 commit comments