import {z} from 'zod' export type NetworkConfig = {subnet_mask?: string | undefined, network_uuid?: unknown | undefined, default_gateway?: string | undefined}; export type PcVmNicConfiguration = {network_configuration?: NetworkConfig | undefined, ip_list?: Array | undefined}; export type PcVm = {vm_name: string, data_disk_size_bytes: number, dns_server_ip_list?: Array | undefined, container_uuid?: unknown | undefined, nic_list: Array, num_sockets: number, memory_size_bytes: number}; export type PrismCentral = {resources: {version: string, initial_password?: string | undefined, virtual_ip?: string | undefined, pc_vm_list: Array, should_auto_register?: boolean | undefined}}; export type CreateApiPrismCentralArgs = {body: PrismCentral}; export type CreateApiPrismCentralOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const networkConfig = z.object({subnet_mask: z.string().optional(), network_uuid: z.string().optional(), default_gateway: z.string().optional()}); export const pcVmNicConfiguration = z.object({network_configuration: networkConfig.optional(), ip_list: z.array(z.string()).optional()}); export const pcVm = z.object({vm_name: z.string().max(64), data_disk_size_bytes: z.number().int(), dns_server_ip_list: z.array(z.string()).optional(), container_uuid: z.string().optional(), nic_list: z.array(pcVmNicConfiguration), num_sockets: z.number().int(), memory_size_bytes: z.number().int()}); export const prismCentral = z.object({resources: z.object({version: z.string(), initial_password: z.string().optional(), virtual_ip: z.string().optional(), pc_vm_list: z.array(pcVm), should_auto_register: z.boolean().optional()})}); export const deploymentStatus = z.object({task_uuid: z.string(), resources: prismCentral}); 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 }; /** * Create a Prism Central * * POST /prism_central */ export const createApiPrismCentral = async (args: CreateApiPrismCentralArgs, options: CreateApiPrismCentralOptions) => { 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(/\/+$/, '')}/prism_central${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(deploymentStatus, await res.json()) };