import {z} from 'zod' export type CommitmentTerm = 'COMMITMENT_TERM_NONE' | 'COMMITMENT_TERM_1_MONTH' | 'COMMITMENT_TERM_3_MONTHS' | 'COMMITMENT_TERM_6_MONTHS' | 'COMMITMENT_TERM_12_MONTHS' | 'COMMITMENT_TERM_24_MONTHS' | 'COMMITMENT_TERM_36_MONTHS' | 'COMMITMENT_TERM_60_MONTHS'; export type MachinePowerState = 'MACHINE_POWER_STATE_UNSPECIFIED' | 'ON' | 'OFF' | 'POWERING_ON' | 'POWERING_OFF' | 'REBOOTING'; export type Decimal = {value?: string | undefined}; export type SshKeySource = 'SSH_KEY_SOURCE_UNKNOWN' | 'SSH_KEY_SOURCE_PROJECT' | 'SSH_KEY_SOURCE_USER' | 'SSH_KEY_SOURCE_NONE'; export type MachineState = 'STATE_UNSPECIFIED' | 'CREATING' | 'ACTIVE' | 'DEPLOYING' | 'DELETING' | 'FAILED' | 'UPDATING'; export type PatchApiV1ProjectsProjectIdMachinesIdArgs = {projectId: string, id: string, body: {architecture?: string | undefined, commitmentTerm?: CommitmentTerm | undefined, cpuCores?: number | undefined, cpuModel?: string | undefined, cpuSpeedMhz?: number | undefined, createBy?: string | undefined, createTime?: string | undefined, customSshKeys?: Array | undefined, dataCenterId: string, diskSizeGib?: number | undefined, disks?: number | undefined, externalIpAddresses?: Array | undefined, gpuModelId?: string | undefined, gpus?: number | undefined, hostname?: string | undefined, machineTypeId: string, memoryGib?: number | undefined, os: string, powerState?: MachinePowerState | undefined, priceHr?: Decimal | undefined, sshKeySource?: SshKeySource | undefined, startScript?: string | undefined, state?: MachineState | undefined, userData?: string | undefined}}; export type PatchApiV1ProjectsProjectIdMachinesIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commitmentTerm = z.enum(["COMMITMENT_TERM_NONE", "COMMITMENT_TERM_1_MONTH", "COMMITMENT_TERM_3_MONTHS", "COMMITMENT_TERM_6_MONTHS", "COMMITMENT_TERM_12_MONTHS", "COMMITMENT_TERM_24_MONTHS", "COMMITMENT_TERM_36_MONTHS", "COMMITMENT_TERM_60_MONTHS"]); export const machinePowerState = z.enum(["MACHINE_POWER_STATE_UNSPECIFIED", "ON", "OFF", "POWERING_ON", "POWERING_OFF", "REBOOTING"]); export const decimal = z.object({value: z.string().optional()}); export const sshKeySource = z.enum(["SSH_KEY_SOURCE_UNKNOWN", "SSH_KEY_SOURCE_PROJECT", "SSH_KEY_SOURCE_USER", "SSH_KEY_SOURCE_NONE"]); export const machineState = z.enum(["STATE_UNSPECIFIED", "CREATING", "ACTIVE", "DEPLOYING", "DELETING", "FAILED", "UPDATING"]); export const machine = z.object({architecture: z.string().optional(), commitmentTerm: commitmentTerm.optional(), cpuCores: z.number().int().optional(), cpuModel: z.string().optional(), cpuSpeedMhz: z.number().int().optional(), createBy: z.string().optional(), createTime: z.string().optional(), customSshKeys: z.array(z.string()).optional(), dataCenterId: z.string(), diskSizeGib: z.number().int().optional(), disks: z.number().int().optional(), externalIpAddresses: z.array(z.string()).optional(), gpuModelId: z.string().optional(), gpus: z.number().int().optional(), hostname: z.string().optional(), id: z.string(), machineTypeId: z.string(), memoryGib: z.number().int().optional(), os: z.string(), powerState: machinePowerState.optional(), priceHr: decimal.optional(), projectId: z.string(), sshKeySource: sshKeySource.optional(), startScript: z.string().optional(), state: machineState.optional(), userData: z.string().optional()}); 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 }; /** * Update a machine * * PATCH /v1/projects/{projectId}/machines/{id} */ export const patchApiV1ProjectsProjectIdMachinesId = async (args: PatchApiV1ProjectsProjectIdMachinesIdArgs, options?: PatchApiV1ProjectsProjectIdMachinesIdOptions) => { const {baseUrl = "https://rest.compute.cudo.org", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set("Authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/machines/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(machine, await res.json()) };