import {z} from 'zod' export type FleetDeploymentPackage = {name: string, version: string}; export type FleetDeploymentPackageUpgradeV2Attributes = {filter_query: string, target_packages: Array}; export type FleetDeploymentResourceType = 'deployment'; export type FleetDeploymentPackageUpgradeV2Create = {attributes: FleetDeploymentPackageUpgradeV2Attributes, type: FleetDeploymentResourceType}; export type FleetDeploymentPackageUpgradeV2CreateRequest = {data: FleetDeploymentPackageUpgradeV2Create}; export type CreateApiApiV2FleetDeploymentsUpgradeArgs = {body: FleetDeploymentPackageUpgradeV2CreateRequest}; export type CreateApiApiV2FleetDeploymentsUpgradeOptions = {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 fleetDeploymentV2CreateResponse = z.object({data: fleetDeploymentV2}); 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 }; /** * Upgrade hosts * * POST /api/v2/fleet/deployments/upgrade * * Also accepts auth: AuthZ */ export const createApiApiV2FleetDeploymentsUpgrade = async (args: CreateApiApiV2FleetDeploymentsUpgradeArgs, options?: CreateApiApiV2FleetDeploymentsUpgradeOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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/upgrade${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(fleetDeploymentV2CreateResponse, await res.json()) };