import {z} from 'zod' export type GovernanceControlDetectionUpdateState = 'exception' | 'active'; export type GovernanceControlDetectionUpdateAttributes = {assigned_team?: string | undefined, assigned_to?: string | undefined, mitigate_after?: string | undefined, state?: GovernanceControlDetectionUpdateState | undefined}; export type GovernanceControlDetectionResourceType = 'governance_control_detection'; export type GovernanceControlDetectionUpdateData = {attributes?: GovernanceControlDetectionUpdateAttributes | undefined, type: GovernanceControlDetectionResourceType}; export type GovernanceControlDetectionUpdateRequest = {data: GovernanceControlDetectionUpdateData}; export type PatchApiApiV2GovernanceDetectionsDetectionIdArgs = {detection_id: string, body: GovernanceControlDetectionUpdateRequest}; export type PatchApiApiV2GovernanceDetectionsDetectionIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const governanceControlDetectionAssignmentSource = z.enum(["auto_resolved", "manual", "reassigned", "cleared"]); export const governanceControlDetectionState = z.enum(["active", "exception", "mitigated", "inactive", "obsolete", "resolved_externally", "mitigation_in_progress"]); export const governanceControlDetectionAttributes = z.object({assigned_team: z.string().optional(), assigned_to: z.string().optional(), assignment_source: governanceControlDetectionAssignmentSource, control_id: z.string(), created_at: z.string(), detection_type: z.string(), display_name: z.string(), exception_at: z.string().optional(), exception_by: z.string().optional(), metadata: z.unknown(), mitigate_after: z.string().optional(), mitigated_at: z.string().optional(), priority: z.number().int(), resource_id: z.string(), resource_type: z.string(), state: governanceControlDetectionState}); export const governanceControlDetectionResourceType = z.literal("governance_control_detection"); export const governanceControlDetectionData = z.object({attributes: governanceControlDetectionAttributes, id: z.string(), type: governanceControlDetectionResourceType}); export const governanceControlDetectionResponse = z.object({data: governanceControlDetectionData}); 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 }; /** * Update a detection * * PATCH /api/v2/governance/detections/{detection_id} * * Also accepts auth: AuthZ */ export const patchApiApiV2GovernanceDetectionsDetectionId = async (args: PatchApiApiV2GovernanceDetectionsDetectionIdArgs, options?: PatchApiApiV2GovernanceDetectionsDetectionIdOptions) => { 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/governance/detections/${encodeURIComponent(String(args.detection_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(governanceControlDetectionResponse, await res.json()) };