import {z} from 'zod' export type PortAttrs = {port: number, pp?: boolean | undefined}; export type PortPolicy = {ports?: Array | undefined, restrict_mode?: boolean | undefined}; export type GpuPolicy = {allow_debug?: boolean | undefined, allow_devtools?: boolean | undefined, allow_insecure_boot?: boolean | undefined, attest_gpu?: boolean | undefined, rego?: (string | null) | undefined}; export type Requirements = {ephemeral_data_disk?: boolean | undefined, gpu_policy?: GpuPolicy | undefined, health_check?: boolean | undefined, health_status_file?: (string | null) | undefined, launch_token_hash?: (string | null) | undefined, platforms?: (Array | null) | undefined, tdx_measure_acpi_tables?: (boolean | null) | undefined}; export type VerityVolume = {source: string, target: string, verity_root: string}; export type ProvisionComposeFileUpdateRequest = {allowed_envs?: Array | undefined, bash_script?: (string | null) | undefined, default_gateway_domain?: (string | null) | undefined, docker_compose_file?: (string | null) | undefined, key_provider?: ('none' | 'kms' | 'local' | 'tpm' | null) | undefined, key_provider_id?: (string | null) | undefined, kms_enabled?: boolean | undefined, local_key_provider_enabled?: boolean | undefined, manifest_version?: (number | string) | undefined, name: string, no_instance_id?: boolean | undefined, port_policy?: (PortPolicy | null) | undefined, pre_launch_script?: (string | null) | undefined, public_logs?: boolean | undefined, public_sysinfo?: boolean | undefined, public_tcbinfo?: boolean | undefined, runner?: string | undefined, requirements?: (Requirements | null) | undefined, storage_discard?: (boolean | null) | undefined, event_log_version?: (1 | 2 | null) | undefined, snapshotter?: ('overlayfs' | 'stargz' | null) | undefined, verity_volumes?: (Array | null) | undefined, gateway_enabled?: boolean | undefined, features?: Array | undefined, salt?: (string | null) | undefined, secure_time?: boolean | undefined, storage_fs?: ('ext4' | 'zfs' | null) | undefined, swap_size?: (string | null) | undefined, tproxy_enabled?: boolean | undefined, update_env_vars?: boolean | undefined}; export type CreateApiApiV1CvmsCvmIdComposeFileProvisionArgs = {cvm_id: string, body: ProvisionComposeFileUpdateRequest}; export type CreateApiApiV1CvmsCvmIdComposeFileProvisionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const kMSInfo = z.object({id: z.string(), slug: z.string().nullable().optional(), url: z.string(), version: z.string(), chain_id: z.number().int().nullable().optional(), kms_contract_address: z.string().nullable().optional(), gateway_app_id: z.string().nullable().optional(), ca_pubkey: z.string().nullable().optional(), k256_pubkey: z.string().nullable().optional()}); export const composeFileProvisionResponse = z.object({app_id: z.string(), device_id: z.string().nullable().optional(), compose_hash: z.string(), kms_info: kMSInfo.nullable().optional(), compose_hash_registered: z.boolean().optional(), compose_unchanged: z.boolean().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 }; /** * Provision compose file update * * POST /api/v1/cvms/{cvm_id}/compose_file/provision */ export const createApiApiV1CvmsCvmIdComposeFileProvision = async (args: CreateApiApiV1CvmsCvmIdComposeFileProvisionArgs, options?: CreateApiApiV1CvmsCvmIdComposeFileProvisionOptions) => { const {baseUrl = "https://cloud-api.phala.com", 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(/\/+$/, '')}/api/v1/cvms/${encodeURIComponent(String(args.cvm_id))}/compose_file/provision${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(composeFileProvisionResponse, await res.json()) };