import {z} from 'zod' export type GetApiApiV2FleetSchedulesIdArgs = {id: string}; export type GetApiApiV2FleetSchedulesIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fleetScheduleV2NotificationRule = z.object({handles: z.array(z.string()).optional(), tags: z.array(z.string()).optional()}); export const fleetScheduleV2RecurrenceRule = z.object({days_of_week: z.array(z.string()).optional(), interval: z.number().int().optional(), maintenance_window_duration: z.number().int().optional(), start_maintenance_window: z.string().optional(), timezone: z.string().optional()}); export const fleetScheduleStatus = z.enum(["active", "inactive"]); export const fleetScheduleV2Attributes = z.object({created_at: z.string().optional(), created_by: z.string().optional(), is_default: z.boolean().optional(), name: z.string().optional(), next_run: z.string().optional(), notification_rule: fleetScheduleV2NotificationRule.optional(), query: z.string().optional(), rule: fleetScheduleV2RecurrenceRule.optional(), status: fleetScheduleStatus.optional(), updated_at: z.string().optional(), updated_by: z.string().optional(), version_to_latest: z.number().int().gte(0).lte(2).optional()}); export const fleetScheduleResourceType = z.literal("schedule"); export const fleetScheduleV2 = z.object({attributes: fleetScheduleV2Attributes, id: z.string(), type: fleetScheduleResourceType}); export const fleetScheduleV2Response = z.object({data: fleetScheduleV2}); 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 a schedule by ID * * GET /api/v2/fleet/schedules/{id} * * Also accepts auth: AuthZ */ export const getApiApiV2FleetSchedulesId = async (args: GetApiApiV2FleetSchedulesIdArgs, options?: GetApiApiV2FleetSchedulesIdOptions) => { 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 (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/fleet/schedules/${encodeURIComponent(String(args.id))}${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(fleetScheduleV2Response, await res.json()) };