import {z} from 'zod' export type SshUserRequestDetails = {username: string, access_request_uuid: unknown, entity_list: Array<{entity_ip: string, entity_uuid?: string | undefined, entity_type: string}>, user_key_name: string, user_key: string, user_uuid: unknown, user_unix_id?: number | undefined}; export type SshUser = {resources: SshUserRequestDetails}; export type UserReference = {kind: string, name?: string | undefined, uuid: unknown}; export type SshUserMetadata = {last_update_time?: string | undefined, kind: string, uuid?: unknown | undefined, spec_version?: number | undefined, creation_time?: string | undefined, spec_hash?: string | undefined, should_force_translate?: boolean | undefined, owner_reference?: UserReference | undefined, categories?: {[key: string]: string} | undefined, name?: string | undefined}; export type SshUserIntentInput = {spec: SshUser, api_version?: string | undefined, metadata: SshUserMetadata}; export type CreateApiSshUserArgs = {body: SshUserIntentInput}; export type CreateApiSshUserOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const sshUserDetails = z.object({username: z.string(), user_uuid: z.string(), access_request_uuid: z.string(), entity_list: z.array(z.object({entity_ip: z.string().optional(), entity_uuid: z.string().optional(), entity_type: z.string().optional()}))}); export const sshUserDefStatus = z.object({resources: sshUserDetails}); export const sshUserRequestDetails = z.object({username: z.string(), access_request_uuid: z.string(), entity_list: z.array(z.object({entity_ip: z.string(), entity_uuid: z.string().optional(), entity_type: z.string()})), user_key_name: z.string(), user_key: z.string(), user_uuid: z.string(), user_unix_id: z.number().int().optional()}); export const sshUser = z.object({resources: sshUserRequestDetails}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const sshUserMetadata = 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 sshUserIntentResponse = z.object({status: sshUserDefStatus.optional(), spec: sshUser.optional(), api_version: z.string(), metadata: sshUserMetadata}); 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 }; /** * Add SSH User to the entity * * POST /ssh_user */ export const createApiSshUser = async (args: CreateApiSshUserArgs, options: CreateApiSshUserOptions) => { 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(/\/+$/, '')}/ssh_user${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(sshUserIntentResponse, await res.json()) };