import {z} from 'zod' export type SortOrder = string; export type VmSnapshotListMetadata = {filter?: string | undefined, kind?: string | undefined, sort_order?: SortOrder | undefined, offset?: number | undefined, length?: number | undefined, sort_attribute?: string | undefined}; export type CreateApiVmSnapshotsListArgs = {body: VmSnapshotListMetadata}; export type CreateApiVmSnapshotsListOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const clusterReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const availabilityZoneReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const messageResource = z.object({message: z.string(), reason: z.string(), details: z.record(z.string(), z.string()).optional()}); export const vmSnapshotDefStatus = z.object({consistency_group_uuid: z.string().optional(), location_agnostic_uuid: z.string().optional(), name: z.string().optional(), replication_target_list: z.array(z.object({cluster_reference: clusterReference.optional(), availability_zone_reference: availabilityZoneReference.optional()})).optional(), state: z.string().optional(), snapshot_type: z.string().optional(), snapshot_file_list: z.array(z.object({file_path: z.string().optional(), snapshot_file_path: z.string().optional()})), expiration_time_msecs: z.number().int().optional(), message_list: z.array(messageResource).optional(), resources: z.object({entity_uuid: z.string().optional()}).optional()}); export const vmSnapshot = z.object({expiration_time_msecs: z.number().int().optional(), resources: z.object({entity_uuid: z.string().optional()}).optional(), snapshot_type: z.string().optional(), replication_target_list: z.array(z.object({cluster_reference: clusterReference.optional(), availability_zone_reference: availabilityZoneReference.optional()})).optional(), name: z.string().max(64).optional()}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const vmSnapshotMetadata = 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 vmSnapshotIntentResource = z.object({status: vmSnapshotDefStatus.optional(), spec: vmSnapshot.optional(), api_version: z.string().optional(), metadata: vmSnapshotMetadata}); export const sortOrder = z.string(); export const vmSnapshotListMetadataOutput = z.object({filter: z.string().optional(), kind: z.string().optional(), sort_order: sortOrder.optional(), offset: z.number().int().optional(), total_matches: z.number().int().optional(), length: z.number().int().optional(), sort_attribute: z.string().optional()}); export const vmSnapshotListIntentResponse = z.object({entities: z.array(vmSnapshotIntentResource).optional(), api_version: z.string(), metadata: vmSnapshotListMetadataOutput}); 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 }; /** * Get VM snapshots * * POST /vm_snapshots/list */ export const createApiVmSnapshotsList = async (args: CreateApiVmSnapshotsListArgs, options: CreateApiVmSnapshotsListOptions) => { 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(/\/+$/, '')}/vm_snapshots/list${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(vmSnapshotListIntentResponse, await res.json()) };