import {z} from 'zod' export type DiskTemplate = {size: string, type: 'persistent-ssd' | 'shared-volume'}; export type VirtualizationFeatures = {nested_virtualization?: boolean | undefined}; export type InstanceTemplatePostRequestV1 = {custom_image_name?: string | undefined, disks?: Array | undefined, ib_partition_id?: string | undefined, image_name?: string | undefined, location?: string | undefined, maintenance_policy?: string | undefined, nvlink_domain_id?: string | undefined, placement_policy?: string | undefined, public_ip_address_type?: string | undefined, reservation_id?: string | undefined, shutdown_script?: string | undefined, ssh_public_key: string, startup_script?: string | undefined, subnet_id?: string | undefined, template_name: string, transport_partition_id?: string | undefined, type: string, virtualization_features?: VirtualizationFeatures | undefined}; export type BulkCreateLocationInfo = {ib_partition_id?: string | undefined, location: string, subnet_id?: string | undefined, transport_partition_id?: string | undefined}; export type ReservationSpecification = {id?: string | undefined, selection_strategy?: ('lowest_cost' | 'on_demand') | undefined}; export type BulkInstancePostRequestV1 = {count: number, crusoe_watch_agent_install_mode?: ('docker' | 'native') | undefined, install_crusoe_watch_agent?: (boolean | null) | undefined, instance_group_id?: string | undefined, instance_template?: InstanceTemplatePostRequestV1 | undefined, instance_template_id?: string | undefined, locations?: Array | undefined, name_prefix: string, nvlink_domain_id?: string | undefined, reservation_specification?: ReservationSpecification | undefined}; export type CreateApiProjectsProjectIdComputeVmsBulkInstancesArgs = {project_id: string, body: BulkInstancePostRequestV1}; export type CreateApiProjectsProjectIdComputeVmsBulkInstancesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const operation = z.object({completed_at: z.string(), metadata: z.string(), operation_id: z.string(), result: z.string().optional(), started_at: z.string(), state: z.enum(["IN_PROGRESS", "SUCCEEDED", "FAILED"])}); export const asyncOperationResponse = z.object({operation: operation}); 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 }; /** * Creates multiple VMs in the project and returns the async operation. * * POST /projects/{project_id}/compute/vms/bulk-instances */ export const createApiProjectsProjectIdComputeVmsBulkInstances = async (args: CreateApiProjectsProjectIdComputeVmsBulkInstancesArgs, options?: CreateApiProjectsProjectIdComputeVmsBulkInstancesOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/compute/vms/bulk-instances${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(asyncOperationResponse, await res.json()) };