import {z} from 'zod' 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 ConfluentLikeSchemaRegistryRequest = {url: string, security?: ConfluentLikeSchemaRegistrySecurity | undefined, properties?: string | undefined, ignoreUntrustedCertificate?: boolean | undefined, schemaRegistryProxyClusterName?: string | undefined, type: 'ConfluentLikeSchemaRegistryRequest'}; 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 GlueSchemaRegistryRequest = {registryName?: string | undefined, region: string, security: AmazonSecurity, type: 'GlueSchemaRegistryRequest'}; export type SchemaRegistryRequest = ConfluentLikeSchemaRegistryRequest | GlueSchemaRegistryRequest; export type KafkaConnectBasicAuth = {username: string, password: string, type: 'KafkaConnectBasicAuth'}; export type KafkaConnectBearerToken = {token: string, type: 'KafkaConnectBearerToken'}; export type KafkaConnectNoSecurity = {type: 'KafkaConnectNoSecurity'}; export type KafkaConnectSSLAuth = {key: string, certificateChain: string, type: 'KafkaConnectSSLAuth'}; export type KafkaConnectSecurity = KafkaConnectBasicAuth | KafkaConnectBearerToken | KafkaConnectNoSecurity | KafkaConnectSSLAuth; export type KafkaConnectUpsert = {url: string, slug?: string | undefined, name: string, security?: KafkaConnectSecurity | undefined, headers?: string | undefined, ignoreUntrustedCertificate?: boolean | undefined, policiesRef?: Array | undefined}; export type AivenCreate = {apiToken: string, project: string, serviceName: string, type: 'Aiven'}; export type ConfluentCreate = {key: string, secret: string, confluentEnvironmentId: string, confluentClusterId: string, organizationId?: string | undefined, schemaRegistryId?: string | undefined, enableRbacRoleBindings?: boolean | undefined, type: 'Confluent'}; export type ConfluentPlatformAuthenticationCreateRequest = {username: string, password: string, type: 'LDAP'}; export type ConfluentPlatformMTLSCreateRequest = {clientCertificateId: string, clientKeyId: string, caCertificateIds?: Array | undefined}; export type ConfluentPlatformCreate = {kafkaClusterId: string, mdsUrl?: string | undefined, authentication?: ConfluentPlatformAuthenticationCreateRequest | undefined, mtls?: ConfluentPlatformMTLSCreateRequest | undefined, schemaRegistryClusterId?: string | undefined, enableRbacRoleBindings?: boolean | undefined, ignoreUntrustedCertificate?: boolean | undefined, type: 'ConfluentPlatform'}; export type GatewayCreate = {url: string, user: string, password: string, virtualCluster: string, ignoreUntrustedCertificate?: boolean | undefined, type: 'Gateway'}; export type KafkaFlavorCreateRequest = AivenCreate | ConfluentCreate | ConfluentPlatformCreate | GatewayCreate; export type KsqlDBBasicAuth = {username: string, password: string, type: 'KsqlDBBasicAuth'}; export type KsqlDBBearerToken = {token: string, type: 'KsqlDBBearerToken'}; export type KsqlDBNoSecurity = {type: 'KsqlDBNoSecurity'}; export type KsqlDBSSLAuth = {key: string, certificateChain: string, type: 'KsqlDBSSLAuth'}; export type KsqlDBSecurity = KsqlDBBasicAuth | KsqlDBBearerToken | KsqlDBNoSecurity | KsqlDBSSLAuth; export type KsqlDBUpsert = {url: string, slug?: string | undefined, name: string, security?: KsqlDBSecurity | undefined, headers?: string | undefined, ignoreUntrustedCertificate?: boolean | undefined}; export type UpsertKafkaClusterRequest = {name: string, bootstrapServers: string, properties?: string | undefined, color?: string | undefined, icon?: string | undefined, schemaRegistry?: SchemaRegistryRequest | undefined, kafkaConnects?: Array | undefined, ignoreUntrustedCertificate?: boolean | undefined, amazonSecurity?: AmazonSecurity | undefined, kafkaFlavor?: KafkaFlavorCreateRequest | undefined, ksqlDBs?: Array | undefined, policiesRef?: Array | undefined}; export type UpdateApiPublicV1ClustersClusterNameArgs = {clusterName: string, body: UpsertKafkaClusterRequest}; export type UpdateApiPublicV1ClustersClusterNameOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 confluentLikeSchemaRegistryResponse = z.object({id: z.string(), url: z.string(), security: confluentLikeSchemaRegistrySecurity, properties: z.string().optional(), ignoreUntrustedCertificate: z.boolean(), schemaRegistryProxyClusterName: z.string().optional(), schemaRegistryProxyId: z.string().optional(), type: z.literal("ConfluentLikeSchemaRegistryResponse")}); 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 glueSchemaRegistryResponse = z.object({id: z.string(), registryName: z.string().optional(), region: z.string(), security: amazonSecurity, type: z.literal("GlueSchemaRegistryResponse")}); export const sharedSchemaRegistryResponseV2 = z.discriminatedUnion("type", [confluentLikeSchemaRegistryResponse, glueSchemaRegistryResponse]); export const kafkaConnectBasicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("KafkaConnectBasicAuth")}); export const kafkaConnectBearerToken = z.object({token: z.string(), type: z.literal("KafkaConnectBearerToken")}); export const kafkaConnectNoSecurity = z.object({type: z.literal("KafkaConnectNoSecurity")}); export const kafkaConnectSSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("KafkaConnectSSLAuth")}); export const kafkaConnectSecurity = z.discriminatedUnion("type", [kafkaConnectBasicAuth, kafkaConnectBearerToken, kafkaConnectNoSecurity, kafkaConnectSSLAuth]); export const mapLabelKeyString = z.record(z.string(), z.string()); export const kafkaConnectWithId = z.object({id: z.string(), slug: z.string(), url: z.string(), name: z.string(), security: kafkaConnectSecurity, headers: z.string().optional(), ignoreUntrustedCertificate: z.boolean(), labels: mapLabelKeyString, policiesRef: z.array(z.string()).optional()}); export const aivenResponse = z.object({project: z.string(), serviceName: z.string(), type: z.literal("Aiven")}); export const confluentPlatformAuthenticationResponse = z.object({username: z.string(), type: z.literal("LDAP")}); export const confluentPlatformMTLSResponse = z.object({clientCertificateId: z.string(), clientKeyId: z.string(), caCertificateIds: z.array(z.string()).optional()}); export const confluentPlatformResponse = z.object({kafkaClusterId: z.string(), mdsUrl: z.string().optional(), authentication: confluentPlatformAuthenticationResponse.optional(), mtls: confluentPlatformMTLSResponse.optional(), schemaRegistryClusterId: z.string().optional(), enableRbacRoleBindings: z.boolean().optional(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("ConfluentPlatform")}); export const confluentResponse = z.object({key: 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 gatewayResponse = z.object({url: z.string(), user: z.string(), virtualCluster: z.string(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("Gateway")}); export const kafkaFlavorResponse = z.discriminatedUnion("type", [aivenResponse, confluentPlatformResponse, confluentResponse, gatewayResponse]); export const ksqlDBBasicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("KsqlDBBasicAuth")}); export const ksqlDBBearerToken = z.object({token: z.string(), type: z.literal("KsqlDBBearerToken")}); export const ksqlDBNoSecurity = z.object({type: z.literal("KsqlDBNoSecurity")}); export const ksqlDBSSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("KsqlDBSSLAuth")}); export const ksqlDBSecurity = z.discriminatedUnion("type", [ksqlDBBasicAuth, ksqlDBBearerToken, ksqlDBNoSecurity, ksqlDBSSLAuth]); export const ksqlDBWithId = z.object({id: z.string(), slug: z.string(), url: z.string(), name: z.string(), security: ksqlDBSecurity, headers: z.string().optional(), ignoreUntrustedCertificate: z.boolean()}); export const publicKafkaClusterResponse = z.object({clusterSlug: z.string(), name: z.string(), bootstrapServers: z.string(), properties: z.string().optional(), color: z.string().optional(), icon: z.string().optional(), schemaRegistry: sharedSchemaRegistryResponseV2.optional(), kafkaConnects: z.array(kafkaConnectWithId).optional(), ignoreUntrustedCertificate: z.boolean(), accessCert: z.string().optional(), accessKey: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), amazonSecurity: amazonSecurity.optional(), kafkaFlavor: kafkaFlavorResponse.optional(), ksqlDBs: z.array(ksqlDBWithId).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 }; /** * PUT /public/v1/clusters/{clusterName} */ export const updateApiPublicV1ClustersClusterName = async (args: UpdateApiPublicV1ClustersClusterNameArgs, options: UpdateApiPublicV1ClustersClusterNameOptions) => { 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 (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/clusters/${encodeURIComponent(String(args.clusterName))}${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(publicKafkaClusterResponse, await res.json()) };