import {z} from 'zod' export type OrganizationSshKeysSelectiveConfig = {mode: 'selective', ssh_key_ids: Array}; export type OrganizationSshKeysNonSelectiveConfig = {mode: 'all' | 'none'}; export type CloudInitWriteFile = {encoding?: (string | null) | undefined, content: string, owner?: (string | null) | undefined, path: string, permissions?: (string | null) | undefined}; export type BareMetalCloudInit = {packages?: Array | undefined, write_files?: Array | undefined, runcmd?: Array | undefined}; export type KubernetesVersion = '1.35' | '1.34' | '1.33' | '1.32'; export type KubernetesAddon = {authentication_config_b64?: (unknown | null) | undefined, version: KubernetesVersion}; export type SlurmAddon = {version: string}; export type BareMetalAddons = {kubernetes?: (KubernetesAddon | null) | undefined, slurm?: (SlurmAddon | null) | undefined}; export type BareMetalDeployConfig = {location_id: string, gpu_count: number, name: string, organization_ssh_keys?: (OrganizationSshKeysSelectiveConfig | OrganizationSshKeysNonSelectiveConfig) | undefined, ssh_keys?: Array | undefined, network_type: 'infiniband' | 'ethernet', sub_order?: (number | null) | undefined, storage_id?: (string | null) | undefined, tags?: Array | undefined, cloudinit_script?: (BareMetalCloudInit | null) | undefined, addons?: (BareMetalAddons | null) | undefined, custom_hostname?: (string | null) | undefined}; export type CreateApiBareMetalArgs = {'Idempotency-Key'?: (string | null) | undefined, body: BareMetalDeployConfig}; export type CreateApiBareMetalOptions = {baseUrl: string, apiTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deployBareResult = z.object({rental_id: z.string(), warning: z.string().nullable()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Provision a new bare-metal server rental * * POST /bare-metal/ * * Also accepts auth: APIKeyHeader */ export const createApiBareMetal = async (args: CreateApiBareMetalArgs, options: CreateApiBareMetalOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (apiTokenAuth !== undefined) headers.set('authorization', `Bearer ${apiTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/bare-metal/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deployBareResult, await res.json()) };