import {z} from 'zod' export type BootDisk = {sizeGib: number}; 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 CreateVMRequestNIC = {assignPublicIp?: boolean | undefined, networkId?: string | undefined, securityGroupIds?: Array | undefined}; export type SshKeySource = 'SSH_KEY_SOURCE_UNKNOWN' | 'SSH_KEY_SOURCE_PROJECT' | 'SSH_KEY_SOURCE_USER' | 'SSH_KEY_SOURCE_NONE'; export type CpuTopology = {cores?: number | undefined, sockets?: number | undefined, threads?: number | undefined}; export type CreateVMBody = {bootDisk?: BootDisk | undefined, bootDiskImageId: string, bootDiskSizeGib?: number | undefined, commitmentTerm?: CommitmentTerm | undefined, customSshKeys?: Array | undefined, dataCenterId: string, expireTime?: string | undefined, gpus: number, machineType: string, memoryGib: number, metadata?: {[key: string]: string} | undefined, nics?: Array | undefined, password?: string | undefined, securityGroupIds?: Array | undefined, sshKeySource?: SshKeySource | undefined, startScript?: string | undefined, storageDiskIds?: Array | undefined, topology?: CpuTopology | undefined, ttl?: string | undefined, validateOnly?: boolean | undefined, vcpus: number, vmId: string}; export type CreateApiV1ProjectsProjectIdVmArgs = {projectId: string, body: CreateVMBody}; export type CreateApiV1ProjectsProjectIdVmOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const diskState = z.enum(["UNKNOWN", "ATTACHED", "CLONING", "CREATING", "DELETING", "DISABLED", "FAILED", "READY", "UPDATING"]); export const diskType = z.enum(["DISK_TYPE_UNKNOWN", "DISK_TYPE_BOOT", "DISK_TYPE_STORAGE"]); export const disk = z.object({createTime: z.string().optional(), dataCenterId: z.string(), diskState: diskState.optional(), diskType: diskType.optional(), id: z.string(), privateImageId: z.string().optional(), projectId: z.string(), publicImageId: z.string().optional(), sizeGib: z.number().int(), vmId: z.string().optional()}); 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 vMNIC = z.object({externalIpAddress: z.string().optional(), internalIpAddress: z.string().optional(), networkAddress: z.string().optional(), networkId: z.string().optional(), securityGroupIds: z.array(z.string()).optional()}); export const decimal = z.object({value: z.string().optional()}); export const vMPrice = z.object({gpuPriceHr: decimal, ipv4AddressPriceHr: decimal, memoryGibPriceHr: decimal, storageGibPriceHr: decimal, totalGpuPriceHr: decimal, totalMemoryPriceHr: decimal, totalPriceHr: decimal, totalStoragePriceHr: decimal, totalVcpuPriceHr: decimal, vcpuPriceHr: decimal}); export const securityGroupRuleProtocol = z.enum(["PROTOCOL_UNKNOWN", "PROTOCOL_ALL", "PROTOCOL_TCP", "PROTOCOL_UDP", "PROTOCOL_ICMP", "PROTOCOL_ICMPv6", "PROTOCOL_IPSEC"]); export const securityGroupRuleRuleType = z.enum(["RULE_TYPE_UNKNOWN", "RULE_TYPE_INBOUND", "RULE_TYPE_OUTBOUND"]); export const securityGroupRule = z.object({icmpType: z.string().optional(), id: z.string().optional(), ipRangeCidr: z.string().optional(), ports: z.string().optional(), protocol: securityGroupRuleProtocol.optional(), ruleType: securityGroupRuleRuleType.optional()}); export const ruleProtocol = z.enum(["PROTOCOL_UNKNOWN", "PROTOCOL_ALL", "PROTOCOL_TCP", "PROTOCOL_UDP", "PROTOCOL_ICMP", "PROTOCOL_ICMPv6", "PROTOCOL_IPSEC"]); export const ruleRuleType = z.enum(["RULE_TYPE_UNKNOWN", "RULE_TYPE_INBOUND", "RULE_TYPE_OUTBOUND"]); export const rule = z.object({icmpType: z.string().optional(), id: z.string().optional(), ipRangeCidr: z.string().optional(), ports: z.string().optional(), protocol: ruleProtocol.optional(), ruleType: ruleRuleType.optional()}); export const securityGroup = z.object({dataCenterId: z.string(), description: z.string().optional(), id: z.string(), projectId: z.string().optional(), rules: z.array(rule).optional()}); export const sshKeySource = z.enum(["SSH_KEY_SOURCE_UNKNOWN", "SSH_KEY_SOURCE_PROJECT", "SSH_KEY_SOURCE_USER", "SSH_KEY_SOURCE_NONE"]); export const vmState = z.enum(["CLONING", "CREATING_SNAPSHOT", "DELETED", "DELETING", "DELETING_SNAPSHOT", "FAILED", "HOTPLUGGING", "MIGRATING", "RECREATING", "REVERTING_SNAPSHOT", "RESIZING", "RESIZING_DISK", "ACTIVE", "STARTING", "STOPPED", "STOPPING", "SUSPENDED", "SUSPENDING", "UNKNOWN", "PENDING"]); export const vM = z.object({authorizedSshKeys: z.string().optional(), bootDisk: disk.optional(), bootDiskSizeGib: z.number().int().optional(), commitmentEndTime: z.string().optional(), commitmentTerm: commitmentTerm, cpuModel: z.string().optional(), createBy: z.string().optional(), createTime: z.string().optional(), datacenterId: z.string().optional(), expireTime: z.string().optional(), externalIpAddress: z.string().optional(), gpuModel: z.string().optional(), gpuModelId: z.string().optional(), gpuQuantity: z.number().int().optional(), id: z.string().optional(), imageId: z.string().optional(), imageName: z.string().optional(), internalIpAddress: z.string().optional(), machineType: z.string().optional(), memory: z.number().int().optional(), metadata: z.record(z.string(), z.string()).optional(), nics: z.array(vMNIC).optional(), price: vMPrice.optional(), privateImageId: z.string().optional(), projectId: z.string(), publicImageId: z.string().optional(), publicImageName: z.string().optional(), publicIpAddress: z.string().optional(), renewableEnergy: z.boolean().optional(), rules: z.array(securityGroupRule).optional(), securityGroupIds: z.array(z.string()).optional(), securityGroups: z.array(securityGroup).optional(), shortState: z.string().optional(), sshKeySource: sshKeySource.optional(), state: vmState.optional(), storageDisks: z.array(disk).optional(), vcpus: z.number().int().optional()}); export const createVMResponse = z.object({id: z.string(), vm: vM.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 }; /** * Create a virtual machine * * POST /v1/projects/{projectId}/vm */ export const createApiV1ProjectsProjectIdVm = async (args: CreateApiV1ProjectsProjectIdVmArgs, options?: CreateApiV1ProjectsProjectIdVmOptions) => { 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))}/vm${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(createVMResponse, await res.json()) };