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 CreateApiProjectsProjectIdComputeInstanceTemplatesArgs = {project_id: string, body: InstanceTemplatePostRequestV1}; export type CreateApiProjectsProjectIdComputeInstanceTemplatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const diskTemplate = z.object({size: z.string(), type: z.enum(["persistent-ssd", "shared-volume"])}); export const virtualizationFeatures = z.object({nested_virtualization: z.boolean().optional()}); export const instanceTemplate = z.object({created_at: z.string(), custom_image_name: z.string().optional(), disks: z.array(diskTemplate).optional(), ib_partition_id: z.string(), id: z.string(), image_name: z.string(), location: z.string(), maintenance_policy: z.enum(["manual", "stop-vm", "unspecified"]), name: z.string(), nvlink_domain_id: z.string().optional(), placement_policy: z.enum(["spread", "unspecified"]), project_id: z.string(), public_ip_address_type: z.string(), reservation_id: z.string().optional(), shutdown_script: z.string(), ssh_public_key: z.string(), startup_script: z.string(), subnet_id: z.string(), transport_partition_id: z.string(), type: z.string(), virtualization_features: virtualizationFeatures.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 }; /** * Creates an instance template in the project and returns the created instance template. * * POST /projects/{project_id}/compute/instance-templates */ export const createApiProjectsProjectIdComputeInstanceTemplates = async (args: CreateApiProjectsProjectIdComputeInstanceTemplatesArgs, options?: CreateApiProjectsProjectIdComputeInstanceTemplatesOptions) => { 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/instance-templates${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(instanceTemplate, await res.json()) };