import {z} from 'zod' export type KubernetesLabelValue = string; export type Tag = {name: string, value: string}; export type TagList = Array; export type ResourceMetadata = {name: KubernetesLabelValue, description?: string | undefined, tags?: TagList | undefined}; export type RegionId = string; export type NFSV2Spec = {rootSquash: boolean}; export type StorageTypeV2Spec = {NFS?: NFSV2Spec | undefined}; export type NetworkIDList = Array; export type StorageAttachmentV2Spec = {networkIds: NetworkIDList}; export type StorageSnapshotScheduleIntervalV2 = 'hourly' | 'daily' | 'weekly' | 'monthly'; export type StorageSnapshotDayOfWeekV2 = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'; export type StorageSnapshotScheduleV2Spec = {interval: StorageSnapshotScheduleIntervalV2, timeOfDay?: string | undefined, dayOfWeek?: StorageSnapshotDayOfWeekV2 | undefined, dayOfMonth?: number | undefined}; export type StorageSnapshotRetentionV2Spec = {keep: number}; export type StorageSnapshotPolicyV2Spec = {name: string, schedule: StorageSnapshotScheduleV2Spec, retention: StorageSnapshotRetentionV2Spec}; export type StorageSnapshotPolicyListV2Spec = Array; export type StorageV2Create = {metadata: ResourceMetadata, spec: {storageClassId: string, organizationId: string, regionId: RegionId, projectId: string, sizeGiB: number, storageType: StorageTypeV2Spec, attachments?: StorageAttachmentV2Spec | undefined, defaultSnapshotProtectionEnabled?: boolean | undefined, snapshotPolicies?: StorageSnapshotPolicyListV2Spec | undefined}}; export type CreateApiApiV2FilestorageArgs = {body: StorageV2Create}; export type CreateApiApiV2FilestorageOptions = {baseUrl: string, oauth2Authentication?: string | undefined, fetch?: typeof fetch | undefined}; export const kubernetesLabelValue = z.string(); export const tag = z.object({name: z.string(), value: z.string()}); export const tagList = z.array(tag); export const resourceProvisioningStatus = z.enum(["unknown", "provisioning", "provisioned", "deprovisioning", "error"]); export const resourceHealthStatus = z.enum(["unknown", "healthy", "degraded", "error"]); export const projectScopedResourceReadMetadata = z.object({name: kubernetesLabelValue, description: z.string().optional(), tags: tagList.optional(), id: z.string(), creationTime: z.string(), createdBy: z.string().optional(), modifiedTime: z.string().optional(), modifiedBy: z.string().optional(), deletionTime: z.string().optional(), provisioningStatus: resourceProvisioningStatus, healthStatus: resourceHealthStatus, organizationId: z.string(), projectId: z.string()}); export const nFSV2Spec = z.object({rootSquash: z.boolean()}); export const storageTypeV2Spec = z.object({NFS: nFSV2Spec.optional()}); export const networkIDList = z.array(z.string()); export const storageAttachmentV2Spec = z.object({networkIds: networkIDList}); export const storageSnapshotScheduleIntervalV2 = z.enum(["hourly", "daily", "weekly", "monthly"]); export const storageSnapshotDayOfWeekV2 = z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]); export const storageSnapshotScheduleV2Spec = z.object({interval: storageSnapshotScheduleIntervalV2, timeOfDay: z.string().optional(), dayOfWeek: storageSnapshotDayOfWeekV2.optional(), dayOfMonth: z.number().int().gte(1).lte(28).optional()}); export const storageSnapshotRetentionV2Spec = z.object({keep: z.number().int().gte(1)}); export const storageSnapshotPolicyV2Spec = z.object({name: z.string().max(19), schedule: storageSnapshotScheduleV2Spec, retention: storageSnapshotRetentionV2Spec}); export const storageSnapshotPolicyListV2Spec = z.array(storageSnapshotPolicyV2Spec); export const storageV2Spec = z.object({sizeGiB: z.number().int(), storageType: storageTypeV2Spec, attachments: storageAttachmentV2Spec.optional(), defaultSnapshotProtectionEnabled: z.boolean().optional(), snapshotPolicies: storageSnapshotPolicyListV2Spec.optional()}); export const storageUsageV2Status = z.object({capacityBytes: z.number().int(), usedBytes: z.number().int().optional(), updatedAt: z.string().optional()}); export const storageAttachmentV2Status = z.object({networkId: z.string(), mountSource: z.string().optional(), mountOptions: z.record(z.string(), z.string()).optional(), provisioningStatus: resourceProvisioningStatus}); export const storageAttachmentListV2Status = z.array(storageAttachmentV2Status); export const storageSnapshotPolicyV2Status = z.object({name: z.string().max(19), provisioningStatus: resourceProvisioningStatus, message: z.string().optional()}); export const storageSnapshotPolicyListV2Status = z.array(storageSnapshotPolicyV2Status); export const storageV2Status = z.object({storageClassId: z.string(), regionId: z.string(), usage: storageUsageV2Status.optional(), attachments: storageAttachmentListV2Status.optional(), snapshotPolicies: storageSnapshotPolicyListV2Status}); export const storageV2Read = z.object({metadata: projectScopedResourceReadMetadata, spec: storageV2Spec, status: storageV2Status}); 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 file storage * * POST /api/v2/filestorage */ export const createApiApiV2Filestorage = async (args: CreateApiApiV2FilestorageArgs, options: CreateApiApiV2FilestorageOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2Authentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Authentication !== undefined) headers.set('authorization', `Bearer ${oauth2Authentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/filestorage${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(storageV2Read, await res.json()) };