import {z} from 'zod' export type KafkaClusterKind = 'KafkaCluster'; export type MapLabelKeyString = {[key: string]: string}; export type KafkaClusterMetadata = {name: string, labels?: MapLabelKeyString | undefined}; export type MapString = {[key: string]: string}; export type BasicAuth = {username: string, password: string, type: 'BasicAuth'}; export type BearerToken = {token: string, type: 'BearerToken'}; export type NoSecurity = {type: 'NoSecurity'}; export type SSLAuth = {key: string, certificateChain: string, type: 'SSLAuth'}; export type ConfluentLikeSchemaRegistrySecurity = BasicAuth | BearerToken | NoSecurity | SSLAuth; export type ConfluentLike = {url: string, security: ConfluentLikeSchemaRegistrySecurity, properties?: string | undefined, ignoreUntrustedCertificate: boolean, schemaRegistryProxyClusterName?: string | undefined, schemaRegistryProxyId?: string | undefined, type: 'ConfluentLike'}; export type Credentials = {accessKeyId: string, secretKey: string, type: 'Credentials'}; export type FromContext = {profile?: string | undefined, type: 'FromContext'}; export type FromRole = {role: string, type: 'FromRole'}; export type IAMAnywhere = {trustAnchorArn: string, profileArn: string, roleArn: string, certificate: string, privateKey: string, type: 'IAMAnywhere'}; export type AmazonSecurity = Credentials | FromContext | FromRole | IAMAnywhere; export type Glue = {registryName?: string | undefined, region: string, security: AmazonSecurity, type: 'Glue'}; export type SchemaRegistry = ConfluentLike | Glue; export type Aiven = {apiToken: string, project: string, serviceName: string, type: 'Aiven'}; export type Confluent = {key: string, secret: string, confluentEnvironmentId: string, confluentClusterId: string, organizationId?: string | undefined, schemaRegistryId?: string | undefined, enableRbacRoleBindings?: boolean | undefined, type: 'Confluent'}; export type ConfluentPlatform = {kafkaClusterId: string, mdsUrl?: string | undefined, ldapUsername?: string | undefined, ldapPassword?: string | undefined, mtlsClientCertificateId?: string | undefined, mtlsClientKeyId?: string | undefined, mtlsCaCertificateIds?: Array | undefined, schemaRegistryClusterId?: string | undefined, enableRbacRoleBindings?: boolean | undefined, ignoreUntrustedCertificate?: boolean | undefined, type: 'ConfluentPlatform'}; export type Gateway = {url: string, user: string, password: string, virtualCluster: string, ignoreUntrustedCertificate?: boolean | undefined, type: 'Gateway'}; export type KafkaFlavor = Aiven | Confluent | ConfluentPlatform | Gateway; export type KafkaClusterSpec = {displayName: string, bootstrapServers: string, properties?: MapString | undefined, color?: string | undefined, icon?: string | undefined, schemaRegistry?: SchemaRegistry | undefined, ignoreUntrustedCertificate?: boolean | undefined, kafkaFlavor?: KafkaFlavor | undefined, policiesRef?: Array | undefined}; export type KafkaClusterResourceV2 = {apiVersion: string, kind: KafkaClusterKind, metadata: KafkaClusterMetadata, spec: KafkaClusterSpec}; export type UpdateApiPublicConsoleV2KafkaClusterArgs = {dryMode?: boolean | undefined, body: KafkaClusterResourceV2}; export type UpdateApiPublicConsoleV2KafkaClusterOptions = {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 upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const kafkaClusterApplyResultV2 = z.object({resource: kafkaClusterResourceV2, upsertResult: upsertResult}); 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 or update a KafkaCluster * * PUT /public/console/v2/kafka-cluster */ export const updateApiPublicConsoleV2KafkaCluster = async (args: UpdateApiPublicConsoleV2KafkaClusterArgs, options: UpdateApiPublicConsoleV2KafkaClusterOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryMode != null) query.append("dryMode", String(args.dryMode)) 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: "PUT", 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(kafkaClusterApplyResultV2, await res.json()) };