import {z} from 'zod' export type GetApiPublicConsoleV2KafkaClusterOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const kafkaClusterKind = z.literal("KafkaCluster"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const kafkaClusterMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const mapString = z.record(z.string(), z.string()); export const basicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("BasicAuth")}); export const bearerToken = z.object({token: z.string(), type: z.literal("BearerToken")}); export const noSecurity = z.object({type: z.literal("NoSecurity")}); export const sSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("SSLAuth")}); export const confluentLikeSchemaRegistrySecurity = z.discriminatedUnion("type", [basicAuth, bearerToken, noSecurity, sSLAuth]); export const confluentLike = z.object({url: z.string(), security: confluentLikeSchemaRegistrySecurity, properties: z.string().optional(), ignoreUntrustedCertificate: z.boolean(), schemaRegistryProxyClusterName: z.string().optional(), schemaRegistryProxyId: z.string().optional(), type: z.literal("ConfluentLike")}); export const credentials = z.object({accessKeyId: z.string(), secretKey: z.string(), type: z.literal("Credentials")}); export const fromContext = z.object({profile: z.string().optional(), type: z.literal("FromContext")}); export const fromRole = z.object({role: z.string(), type: z.literal("FromRole")}); export const iAMAnywhere = z.object({trustAnchorArn: z.string(), profileArn: z.string(), roleArn: z.string(), certificate: z.string(), privateKey: z.string(), type: z.literal("IAMAnywhere")}); export const amazonSecurity = z.discriminatedUnion("type", [credentials, fromContext, fromRole, iAMAnywhere]); export const glue = z.object({registryName: z.string().optional(), region: z.string(), security: amazonSecurity, type: z.literal("Glue")}); export const schemaRegistry = z.discriminatedUnion("type", [confluentLike, glue]); export const aiven = z.object({apiToken: z.string(), project: z.string(), serviceName: z.string(), type: z.literal("Aiven")}); export const confluent = z.object({key: z.string(), secret: z.string(), confluentEnvironmentId: z.string(), confluentClusterId: z.string(), organizationId: z.string().optional(), schemaRegistryId: z.string().optional(), enableRbacRoleBindings: z.boolean().optional(), type: z.literal("Confluent")}); export const confluentPlatform = z.object({kafkaClusterId: z.string(), mdsUrl: z.string().optional(), ldapUsername: z.string().optional(), ldapPassword: z.string().optional(), mtlsClientCertificateId: z.string().optional(), mtlsClientKeyId: z.string().optional(), mtlsCaCertificateIds: z.array(z.string()).optional(), schemaRegistryClusterId: z.string().optional(), enableRbacRoleBindings: z.boolean().optional(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("ConfluentPlatform")}); export const gateway = z.object({url: z.string(), user: z.string(), password: z.string(), virtualCluster: z.string(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("Gateway")}); export const kafkaFlavor = z.discriminatedUnion("type", [aiven, confluent, confluentPlatform, gateway]); export const kafkaClusterSpec = z.object({displayName: z.string(), bootstrapServers: z.string(), properties: mapString.optional(), color: z.string().optional(), icon: z.string().optional(), schemaRegistry: schemaRegistry.optional(), ignoreUntrustedCertificate: z.boolean().optional(), kafkaFlavor: kafkaFlavor.optional(), policiesRef: z.array(z.string()).optional()}); export const kafkaClusterResourceV2 = z.object({apiVersion: z.string(), kind: kafkaClusterKind, metadata: kafkaClusterMetadata, spec: kafkaClusterSpec}); export const getApiPublicConsoleV2KafkaClusterResponse = z.array(kafkaClusterResourceV2); 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 }; /** * List KafkaCluster resources * * GET /public/console/v2/kafka-cluster */ export const getApiPublicConsoleV2KafkaCluster = async (options: GetApiPublicConsoleV2KafkaClusterOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/console/v2/kafka-cluster${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(getApiPublicConsoleV2KafkaClusterResponse, await res.json()) };