import {z} from 'zod' export type GetApiApiV2FleetDeploymentsArgs = {page_size?: number | undefined, page_number?: number | undefined, sort?: string | undefined, ascending?: boolean | undefined, filter?: string | undefined}; export type GetApiApiV2FleetDeploymentsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fleetDeploymentFileOp = z.enum(["merge-patch", "delete"]); export const fleetDeploymentOperation = z.object({file_op: fleetDeploymentFileOp, file_path: z.string(), patch: z.record(z.string(), z.unknown()).optional()}); export const fleetDeploymentV2Attributes = z.object({author: z.string().optional(), config_operations: z.array(fleetDeploymentOperation).optional(), duration_seconds: z.number().int().optional(), error_summary: z.string().optional(), estimated_finished_at: z.number().int().optional(), finished_at: z.number().int().optional(), is_scheduled: z.boolean().optional(), query: z.string().optional(), schedule_id: z.string().optional(), started_at: z.number().int().optional(), status: z.string().optional(), target_versions: z.array(z.string()).optional(), total_hosts: z.number().int().optional(), update_type: z.string().optional()}); export const fleetDeploymentResourceType = z.literal("deployment"); export const fleetDeploymentV2 = z.object({attributes: fleetDeploymentV2Attributes, id: z.string(), type: fleetDeploymentResourceType}); export const fleetDeploymentsV2Page = z.object({total_count: z.number().int().optional(), total_filtered_count: z.number().int().optional()}); export const fleetDeploymentsV2ResponseMeta = z.object({page: fleetDeploymentsV2Page.optional()}); export const fleetDeploymentsV2Response = z.object({data: z.array(fleetDeploymentV2), meta: fleetDeploymentsV2ResponseMeta.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 }; /** * List all deployments * * GET /api/v2/fleet/deployments * * Also accepts auth: AuthZ */ export const getApiApiV2FleetDeployments = async (args: GetApiApiV2FleetDeploymentsArgs, options?: GetApiApiV2FleetDeploymentsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_number != null) query.append("page_number", String(args.page_number)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.ascending != null) query.append("ascending", String(args.ascending)) if (args.filter != null) query.append("filter", String(args.filter)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/fleet/deployments${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(fleetDeploymentsV2Response, await res.json()) };