import {z} from 'zod' export type NotificationRuleSourceAlertsInput = {enabled: boolean, alertNames: Array, minAlertSeverity?: ('CRITICAL' | 'MAJOR' | 'MINOR' | 'INFO') | undefined}; export type NotificationRuleSourceInput = {alerts?: NotificationRuleSourceAlertsInput | undefined}; export type NotificationRuleDestinationEmailInput = {enabled: boolean, emailIds?: Array | undefined}; export type NotificationRuleDestinationWebhookInput = {enabled: boolean, webhookIds?: Array | undefined}; export type NotificationRuleDestinationSyslogInput = {enabled: boolean, serverIds?: Array | undefined}; export type NotificationRuleDestinationInput = {email?: NotificationRuleDestinationEmailInput | undefined, webhook?: NotificationRuleDestinationWebhookInput | undefined, syslog?: NotificationRuleDestinationSyslogInput | undefined}; export type NotificationRuleUpdateInput = {description?: string | undefined, enabled?: boolean | undefined, sourceTypes?: Array<'ALERTS'> | undefined, source?: NotificationRuleSourceInput | undefined, destination?: NotificationRuleDestinationInput | undefined}; export type PatchApiNetworkNotificationsV1NotificationRulesIdArgs = {id: string, body: {notificationRuleInput?: NotificationRuleUpdateInput | undefined}}; export type PatchApiNetworkNotificationsV1NotificationRulesIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRuleSourceAlerts = z.object({enabled: z.boolean().optional(), alertNames: z.array(z.string().max(256)).optional(), minAlertSeverity: z.enum(["CRITICAL", "MAJOR", "MINOR", "INFO"]).optional()}); export const notificationRuleSource = z.object({alerts: notificationRuleSourceAlerts.optional()}); export const notificationRuleDestinationEmail = z.object({enabled: z.boolean().optional(), emailIds: z.array(z.string().max(256)).optional()}); export const notificationRuleDestinationWebhook = z.object({enabled: z.boolean().optional(), webhookIds: z.array(z.string().max(512)).optional()}); export const notificationRuleDestinationSyslog = z.object({enabled: z.boolean().optional(), serverIds: z.array(z.string().max(256)).optional()}); export const notificationRuleDestination = z.object({email: notificationRuleDestinationEmail.optional(), webhook: notificationRuleDestinationWebhook.optional(), syslog: notificationRuleDestinationSyslog.optional()}); export const notificationRule = z.object({id: z.string().max(128), type: z.string(), name: z.string().max(128), description: z.string().max(512).optional(), enabled: z.boolean().optional(), sourceTypes: z.array(z.literal("ALERTS")).optional(), source: notificationRuleSource.optional(), destination: notificationRuleDestination.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 }; /** * Update a notification rule * * PATCH /network-notifications/v1/notification-rules/{id} */ export const patchApiNetworkNotificationsV1NotificationRulesId = async (args: PatchApiNetworkNotificationsV1NotificationRulesIdArgs, options?: PatchApiNetworkNotificationsV1NotificationRulesIdOptions) => { const {baseUrl = "https://sso.common.cloud.hpe.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/merge-patch+json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/network-notifications/v1/notification-rules/${encodeURIComponent(String(args.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(notificationRule, await res.json()) };