import {z} from 'zod' export type NocCloudProvider = 'amazons3' | 'amazons3gov' | 'azure' | 'google' | 'googles3' | 'atmos' | 'ironmountain' | 'rackspace' | 'nirvanix' | 'synaptic' | 'castor' | 'delldx' | 'cloudone' | 'hp' | 'cleversafe' | 'vipr' | 'ibmcos' | 'hcp' | 's3_compatible'; export type CloudCredentialSortBy = 'name' | 'shortname' | 'hostname' | 'access_key' | 'note' | 'created_at' | 'updated_at' | 'appliance_name'; export type SortDirection = 'ascending' | 'descending'; export type GetApiCloudCredentialsArgs = {search?: (string | null) | undefined, providers?: (Array | null) | undefined, appliance_ids?: (Array | null) | undefined, volume_ids?: (Array | null) | undefined, sort_by?: CloudCredentialSortBy | undefined, sort_direction?: SortDirection | undefined, iterator_id?: (string | null) | undefined, limit?: number | undefined, page?: (number | null) | undefined}; export type GetApiCloudCredentialsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const nocCloudProvider = z.enum(["amazons3", "amazons3gov", "azure", "google", "googles3", "atmos", "ironmountain", "rackspace", "nirvanix", "synaptic", "castor", "delldx", "cloudone", "hp", "cleversafe", "vipr", "ibmcos", "hcp", "s3_compatible"]); export const nocCloudStorageProtocol = z.enum(["amazons3", "amazons3gov", "azure", "ibmcos", "hcp", "vipr", "cleversafe", "googles3", "s3_compatible"]); export const nocCloudVendor = z.enum(["customer", "private", "nasuni"]); export const cloudCredentialStatus = z.enum(["synced", "pending", "failed", "creating"]); export const cloudCredentialErrorDto = z.object({task_id: z.string(), action: z.string().nullable().optional(), can_acknowledge: z.boolean().optional(), code: z.number().int().nullable().optional(), error: z.string(), message: z.string(), detected_at: z.string()}); export const cloudCredentialVolumeDto = z.object({volume_id: z.string(), name: z.string()}); export const cloudCredentialDto = z.object({id: z.string(), appliance_id: z.string(), appliance_name: z.string(), appliance_serial: z.string(), appliance_credential_uuid: z.string(), name: z.string(), shortname: nocCloudProvider, cred_type: nocCloudStorageProtocol, hostname: z.string(), access_key: z.string(), vendor: nocCloudVendor, note: z.string().nullable().optional(), verify_certificates: z.boolean().optional(), certificate: z.string().nullable().optional(), region_code: z.string().nullable().optional(), is_stub: z.boolean().optional(), status: cloudCredentialStatus, error: cloudCredentialErrorDto.nullable().optional(), volumes: z.array(cloudCredentialVolumeDto).optional(), created_at: z.string(), updated_at: z.string()}); export const cloudCredentialPaginationMetadataDto = z.object({iterator_id: z.string().nullable().optional(), page_size: z.number().int().optional(), total_pages: z.number().int().nullable().optional(), total_items: z.number().int(), page: z.number().int().nullable().optional()}); export const cloudCredentialsDto = z.object({items: z.array(cloudCredentialDto).optional(), metadata: cloudCredentialPaginationMetadataDto}); 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 Cloud Credentials * * GET /cloud-credentials */ export const getApiCloudCredentials = async (args: GetApiCloudCredentialsArgs, options?: GetApiCloudCredentialsOptions) => { const {baseUrl = "https://am1.portal.api.nasuni.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) for (const item of args.providers ?? []) query.append("providers", String(item)) for (const item of args.appliance_ids ?? []) query.append("appliance_ids", String(item)) for (const item of args.volume_ids ?? []) query.append("volume_ids", String(item)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.iterator_id != null) query.append("iterator_id", String(args.iterator_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/cloud-credentials${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cloudCredentialsDto, await res.json()) };