import {z} from 'zod' export type RestAccessInfoUpdate = {host?: string | undefined, port?: string | undefined, username: string, password: string}; export type SSHAccessInfoUpdate = {host?: string | undefined, port?: string | undefined, username: string, password: string, pub_key?: string | undefined, pub_key_type?: ('ed25519' | 'ecdsa' | 'rsa') | undefined}; export type CLIAccessInfoUpdate = {host?: string | undefined, port?: string | undefined, username: string, password: string}; export type SMISAccessInfoUpdate = {host?: string | undefined, port?: string | undefined, username: string, password: string, namespace?: string | undefined}; export type StorageBackendRegistryUpdate = {host?: string | undefined, port?: string | undefined, username: string, password: string, rest?: RestAccessInfoUpdate | undefined, ssh?: SSHAccessInfoUpdate | undefined, cli?: CLIAccessInfoUpdate | undefined, smis?: SMISAccessInfoUpdate | undefined, extra_attributes?: {[key: string]: string} | undefined} | {host?: string | undefined, port?: string | undefined, username: string, password: string, pub_key?: string | undefined, pub_key_type?: ('ed25519' | 'ecdsa' | 'rsa') | undefined, rest?: RestAccessInfoUpdate | undefined, ssh?: SSHAccessInfoUpdate | undefined, cli?: CLIAccessInfoUpdate | undefined, smis?: SMISAccessInfoUpdate | undefined, extra_attributes?: {[key: string]: string} | undefined} | {host?: string | undefined, port?: string | undefined, username: string, password: string, rest?: RestAccessInfoUpdate | undefined, ssh?: SSHAccessInfoUpdate | undefined, cli?: CLIAccessInfoUpdate | undefined, smis?: SMISAccessInfoUpdate | undefined, extra_attributes?: {[key: string]: string} | undefined} | {host?: string | undefined, port?: string | undefined, username: string, password: string, namespace?: string | undefined, rest?: RestAccessInfoUpdate | undefined, ssh?: SSHAccessInfoUpdate | undefined, cli?: CLIAccessInfoUpdate | undefined, smis?: SMISAccessInfoUpdate | undefined, extra_attributes?: {[key: string]: string} | undefined}; export type UpdateApiV1StoragesStorageIdAccessInfoArgs = {storage_id: string, body: StorageBackendRegistryUpdate}; export type UpdateApiV1StoragesStorageIdAccessInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const restAccessInfoResponse = z.object({host: z.string(), port: z.string(), username: z.string(), password: z.string().optional()}); export const sSHAccessInfoResponse = z.object({host: z.string(), port: z.string(), username: z.string(), password: z.string().optional(), pub_key: z.string().optional(), pub_key_type: z.enum(["ed25519", "ecdsa", "rsa"]).optional()}); export const cLIAccessInfoResponse = z.object({host: z.string(), port: z.string(), username: z.string(), password: z.string().optional()}); export const sMISAccessInfoResponse = z.object({host: z.string(), port: z.string(), username: z.string(), password: z.string().optional(), namespace: z.string()}); export const storageAccessInfoResponse = z.object({id: z.string().optional(), rest: restAccessInfoResponse.optional(), ssh: sSHAccessInfoResponse.optional(), cli: cLIAccessInfoResponse.optional(), smis: sMISAccessInfoResponse.optional(), vendor: z.string().optional(), model: z.string().optional(), extra_attributes: z.record(z.string(), z.string()).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 }; /** * Update a registered storage system access information in Infrastructure management DB. * * PUT /v1/storages/{storage_id}/access-info */ export const updateApiV1StoragesStorageIdAccessInfo = async (args: UpdateApiV1StoragesStorageIdAccessInfoArgs, options: UpdateApiV1StoragesStorageIdAccessInfoOptions) => { const {baseUrl, 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(/\/+$/, '')}/v1/storages/${encodeURIComponent(String(args.storage_id))}/access-info${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(storageAccessInfoResponse, await res.json()) };