import {z} from 'zod' export type DbaasServiceName = string; export type JsonSchemaGrafana = {allow_embedding?: boolean | undefined, cookie_samesite?: ('lax' | 'strict' | 'none') | undefined, dashboard_previews_enabled?: boolean | undefined, metrics_enabled?: boolean | undefined, auth_azuread?: ({allow_sign_up?: boolean | undefined, allowed_domains?: Array | undefined, allowed_groups?: Array | undefined, auth_url: string, client_id: string, client_secret: string, token_url: string}) | undefined, alerting_enabled?: boolean | undefined, wal?: boolean | undefined, unified_alerting_enabled?: boolean | undefined, auth_github?: ({allow_sign_up?: boolean | undefined, allowed_organizations?: Array | undefined, auto_login?: boolean | undefined, client_id: string, client_secret: string, skip_org_role_sync?: boolean | undefined, team_ids?: Array | undefined}) | undefined, user_auto_assign_org?: boolean | undefined, dataproxy_send_user_header?: boolean | undefined, google_analytics_ua_id?: string | undefined, dashboards_versions_to_keep?: number | undefined, editors_can_admin?: boolean | undefined, smtp_server?: ({from_address: string, from_name?: (string | null) | undefined, host: string, password?: (string | null) | undefined, port: number, skip_verify?: boolean | undefined, starttls_policy?: ('OpportunisticStartTLS' | 'MandatoryStartTLS' | 'NoStartTLS') | undefined, username?: (string | null) | undefined}) | undefined, auth_gitlab?: ({allow_sign_up?: boolean | undefined, allowed_groups: Array, api_url?: string | undefined, auth_url?: string | undefined, client_id: string, client_secret: string, token_url?: string | undefined}) | undefined, alerting_nodata_or_nullvalues?: ('alerting' | 'no_data' | 'keep_state' | 'ok') | undefined, auth_basic_enabled?: boolean | undefined, date_formats?: ({default_timezone?: string | undefined, full_date?: string | undefined, interval_day?: string | undefined, interval_hour?: string | undefined, interval_minute?: string | undefined, interval_month?: string | undefined, interval_second?: string | undefined, interval_year?: string | undefined}) | undefined, service_log?: (boolean | null) | undefined, disable_gravatar?: boolean | undefined, user_auto_assign_org_role?: ('Viewer' | 'Admin' | 'Editor') | undefined, dataproxy_timeout?: number | undefined, viewers_can_edit?: boolean | undefined, dashboards_min_refresh_interval?: string | undefined, auth_google?: ({allow_sign_up?: boolean | undefined, allowed_domains: Array, client_id: string, client_secret: string}) | undefined, oauth_allow_insecure_email_lookup?: boolean | undefined, alerting_max_annotations_to_keep?: number | undefined, auth_generic_oauth?: ({scopes?: Array | undefined, allowed_domains?: Array | undefined, allowed_organizations?: Array | undefined, token_url: string, name?: string | undefined, auth_url: string, api_url: string, auto_login?: boolean | undefined, client_id: string, client_secret: string, allow_sign_up?: boolean | undefined}) | undefined, custom_domain?: (string | null) | undefined, alerting_error_or_timeout?: ('alerting' | 'keep_state') | undefined}; export type CreateApiDbaasGrafanaNameArgs = {name: DbaasServiceName, body: {maintenance?: ({dow: 'saturday' | 'tuesday' | 'never' | 'wednesday' | 'sunday' | 'friday' | 'monday' | 'thursday', time: string}) | undefined, plan: string, 'termination-protection'?: boolean | undefined, 'fork-from-service'?: DbaasServiceName | undefined, 'grafana-settings'?: JsonSchemaGrafana | undefined, 'ip-filter'?: Array | undefined}}; export type CreateApiDbaasGrafanaNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const operation = z.object({id: z.string().optional(), reason: z.enum(["incorrect", "unknown", "unavailable", "forbidden", "busy", "fault", "partial", "not-found", "interrupted", "unsupported", "conflict"]).optional(), reference: z.object({id: z.string().optional(), link: z.string().optional(), command: z.string().optional()}).optional(), message: z.string().optional(), state: z.enum(["failure", "pending", "success", "timeout"]).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 }; /** * POST /dbaas-grafana/{name} */ export const createApiDbaasGrafanaName = async (args: CreateApiDbaasGrafanaNameArgs, options?: CreateApiDbaasGrafanaNameOptions) => { const {baseUrl = "https://api-ch-gva-2.exoscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/dbaas-grafana/${encodeURIComponent(String(args.name))}${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(operation, await res.json()) };