import {z} from 'zod' export type RackReference = {kind: string, name?: string | undefined, uuid: unknown}; export type RackableUnitResources = {rack_reference?: RackReference | undefined}; export type RackableUnit = {resources?: RackableUnitResources | undefined}; export type UserReference = {kind: string, name?: string | undefined, uuid: unknown}; export type RackableUnitMetadata = {last_update_time?: string | undefined, kind: string, uuid?: unknown | undefined, spec_version?: number | undefined, creation_time?: string | undefined, spec_hash?: string | undefined, should_force_translate?: boolean | undefined, owner_reference?: UserReference | undefined, categories?: {[key: string]: string} | undefined, name?: string | undefined}; export type RackableUnitIntentInput = {spec: RackableUnit, api_version?: string | undefined, metadata: RackableUnitMetadata}; export type UpdateApiRackableUnitsUuidArgs = {uuid: unknown, body: RackableUnitIntentInput}; export type UpdateApiRackableUnitsUuidOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageResource = z.object({message: z.string(), reason: z.string(), details: z.record(z.string(), z.string()).optional()}); export const rackReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const rackableUnitResources = z.object({rack_reference: rackReference.optional()}); export const rackableUnitDefStatus = z.object({serial: z.string(), message_list: z.array(messageResource).optional(), model: z.string(), resources: rackableUnitResources, state: z.string().optional()}); export const rackableUnit = z.object({resources: rackableUnitResources.optional()}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const rackableUnitMetadata = z.object({last_update_time: z.string().optional(), kind: z.string(), uuid: z.string().optional(), spec_version: z.number().int().optional(), creation_time: z.string().optional(), spec_hash: z.string().optional(), should_force_translate: z.boolean().optional(), owner_reference: userReference.optional(), categories: z.record(z.string(), z.string()).optional(), name: z.string().max(64).optional()}); export const rackableUnitIntentResponse = z.object({status: rackableUnitDefStatus.optional(), spec: rackableUnit.optional(), api_version: z.string(), metadata: rackableUnitMetadata}); 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 rackable unit * * PUT /rackable_units/{uuid} */ export const updateApiRackableUnitsUuid = async (args: UpdateApiRackableUnitsUuidArgs, options: UpdateApiRackableUnitsUuidOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/rackable_units/${encodeURIComponent(String(args.uuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(rackableUnitIntentResponse, await res.json()) };