import {z} from 'zod' export type SksClusterLabels = {[key: string]: string}; export type Networking = {'cluster-cidr'?: string | undefined, 'service-cluster-ip-range'?: string | undefined, 'node-cidr-mask-size-ipv4'?: number | undefined, 'node-cidr-mask-size-ipv6'?: number | undefined}; export type SksOidc = {'client-id': string, 'issuer-url': string, 'username-claim'?: string | undefined, 'username-prefix'?: string | undefined, 'groups-claim'?: string | undefined, 'groups-prefix'?: string | undefined, 'required-claim'?: {[key: string]: string} | undefined}; export type SksAuditEndpoint = string; export type SksAuditBearerToken = string; export type SksAuditInitialBackoff = string; export type SksAuditCreate = {endpoint: SksAuditEndpoint, 'bearer-token': SksAuditBearerToken, 'initial-backoff'?: SksAuditInitialBackoff | undefined}; export type CreateApiSksClusterArgs = {body: {description?: (string | null) | undefined, labels?: SksClusterLabels | undefined, cni?: ('calico' | 'cilium') | undefined, 'auto-upgrade'?: boolean | undefined, networking?: Networking | undefined, oidc?: SksOidc | undefined, name: string, 'create-default-security-group'?: (boolean | null) | undefined, 'enable-kube-proxy'?: boolean | undefined, level: 'starter' | 'pro', 'feature-gates'?: Array | undefined, addons?: (Array<'exoscale-cloud-controller' | 'exoscale-container-storage-interface' | 'metrics-server' | 'karpenter'>) | undefined, audit?: SksAuditCreate | undefined, version: string}}; export type CreateApiSksClusterOptions = {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 }; /** * Create an SKS cluster * * POST /sks-cluster */ export const createApiSksCluster = async (args: CreateApiSksClusterArgs, options?: CreateApiSksClusterOptions) => { 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(/\/+$/, '')}/sks-cluster${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()) };