import {z} from 'zod' export type GetApiWorkloadIdArgs = {'X-Auth-Token': string, id: string}; export type GetApiWorkloadIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTScanBrief = z.object({status: z.string(), high: z.number().int(), medium: z.number().int(), result: z.string(), scanned_timestamp: z.number().int(), scanned_at: z.string(), base_os: z.string(), scanner_version: z.string(), cvedb_create_time: z.string()}); export const rESTIPAddr = z.object({ip: z.string(), ip_prefix: z.number().int(), gateway: z.string()}); export const rESTWorkloadPorts = z.object({ip_proto: z.number().int(), port: z.number().int(), host_ip: z.string(), host_port: z.number().int()}); export const rESTWorkloadDetail: z.ZodType = z.object({id: z.string(), name: z.string(), display_name: z.string(), platform_role: z.string(), domain: z.string(), state: z.string(), service: z.string(), service_group: z.string(), share_ns_with: z.string().optional(), policy_mode: z.string().optional(), scan_summary: rESTScanBrief.optional(), host_name: z.string(), host_id: z.string(), enforcer_id: z.string(), image: z.string(), network_mode: z.string(), privileged: z.boolean(), cap_quarantine: z.boolean(), created_at: z.string(), started_at: z.string(), finished_at: z.string(), running: z.boolean(), secured_at: z.string(), exit_code: z.number().int(), interfaces: z.object({key: z.string().optional(), value: z.array(rESTIPAddr).optional()}), ports: z.array(rESTWorkloadPorts), labels: z.object({key: z.string().optional(), value: z.string().optional()}), applications: z.array(z.string()), memory_limit: z.number().int(), cpus: z.string(), groups: z.array(z.string()), app_ports: z.object({key: z.string().optional(), value: z.string().optional()}), children: z.array(z.lazy(() => rESTWorkloadDetail))}); export const rESTWorkloadDetailData = z.object({workload: rESTWorkloadDetail}); 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 }; /** * Show workload * * GET /workload/{id} */ export const getApiWorkloadId = async (args: GetApiWorkloadIdArgs, options: GetApiWorkloadIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Auth-Token'] != null) headers.set("X-Auth-Token", String(args['X-Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/workload/${encodeURIComponent(String(args.id))}${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(rESTWorkloadDetailData, await res.json()) };