import {z} from 'zod' export type FleetDeploymentFileOp = 'merge-patch' | 'delete'; export type FleetDeploymentOperation = {file_op: FleetDeploymentFileOp, file_path: string, patch?: {[key: string]: unknown} | undefined}; export type FleetDeploymentConfigureV2Package = {apm_instrumentation?: string | undefined, name: string, version: string}; export type FleetDeploymentConfigureV2Attributes = {config_operations: Array, dry_run?: boolean | undefined, filter_query: string, target_packages?: Array | undefined}; export type FleetDeploymentResourceType = 'deployment'; export type FleetDeploymentConfigureV2Create = {attributes: FleetDeploymentConfigureV2Attributes, type: FleetDeploymentResourceType}; export type FleetDeploymentConfigureV2CreateRequest = {data: FleetDeploymentConfigureV2Create}; export type CreateApiApiV2FleetDeploymentsConfigureArgs = {body: FleetDeploymentConfigureV2CreateRequest}; export type CreateApiApiV2FleetDeploymentsConfigureOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fleetDeploymentConfigureV2DryRunResult = z.object({config_validated: z.boolean().optional(), non_upgradable_by_reason: z.record(z.string(), z.number().int()).optional(), non_upgradable_hosts: z.number().int().optional()}); export const fleetDeploymentConfigureV2DryRunAttributes = z.object({dry_run: fleetDeploymentConfigureV2DryRunResult.optional(), query: z.string().optional(), total_hosts: z.number().int().optional()}); export const fleetDeploymentResourceType = z.literal("deployment"); export const fleetDeploymentConfigureV2DryRun = z.object({attributes: fleetDeploymentConfigureV2DryRunAttributes, id: z.string(), type: fleetDeploymentResourceType}); export const fleetDeploymentConfigureV2DryRunResponse = z.object({data: fleetDeploymentConfigureV2DryRun}); 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 }; /** * Create a configuration deployment * * POST /api/v2/fleet/deployments/configure * * Also accepts auth: AuthZ */ export const createApiApiV2FleetDeploymentsConfigure = async (args: CreateApiApiV2FleetDeploymentsConfigureArgs, options?: CreateApiApiV2FleetDeploymentsConfigureOptions) => { 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/configure${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(fleetDeploymentConfigureV2DryRunResponse, await res.json()) };