import {z} from 'zod' export type GetApiServiceTenantNamePbmRuleArgs = {tenant_name: unknown, limit?: number | undefined, offset?: number | undefined}; export type GetApiServiceTenantNamePbmRuleOptions = {baseUrl: string, HTTPBearerFromCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const filterType = z.enum(["src_network", "dst_network", "src_l4_port", "dst_l4_port", "ip_protocol", "src_segment", "dst_segment", "src_object", "dst_object", "src_group", "dst_group", "ingress_vi", "tcp_control", "is_fragmented", "tos", "traffic_class", "ttl", "hop_limit", "ip_version", "src_dia", "dst_dia"]); export const ruleFilter = z.object({filter_type: filterType, values: z.unknown()}); export const pBMMeteringUnits = z.enum(["unspecified", "kps", "pps"]); export const nBPBMRuleResponse = z.object({id: z.number().int(), name: z.string(), priority: z.number().int().optional(), enable: z.boolean().optional(), filters: z.array(ruleFilter).optional(), count: z.number().int(), unit: pBMMeteringUnits, labels: z.record(z.string(), z.unknown()), description: z.string().nullable().optional()}); export const itemsListNBPBMRuleResponse = z.object({items: z.array(nBPBMRuleResponse), count: z.number().int().optional(), total_count: z.number().int().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 }; /** * Get All Tenant Pbm Rules * * GET /service/{tenant_name}/pbm_rule */ export const getApiServiceTenantNamePbmRule = async (args: GetApiServiceTenantNamePbmRuleArgs, options: GetApiServiceTenantNamePbmRuleOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearerFromCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (HTTPBearerFromCookie !== undefined) headers.set('authorization', `Bearer ${HTTPBearerFromCookie}`) const url = `${baseUrl.replace(/\/+$/, '')}/service/${encodeURIComponent(String(args.tenant_name))}/pbm_rule${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(itemsListNBPBMRuleResponse, await res.json()) };