import {z} from 'zod' export type ServiceAccountKind = 'ServiceAccount'; export type MapLabelKeyString = {[key: string]: string}; export type ServiceAccountMetadata = {appInstance?: string | undefined, cluster: string, labels?: MapLabelKeyString | undefined, name: string}; export type AivenResourceType = 'SCHEMA' | 'TOPIC'; export type AivenPermission = 'admin' | 'read' | 'readwrite' | 'schema_registry_read' | 'schema_registry_write' | 'write'; export type AivenServiceAccountACL = {resourceType: AivenResourceType, name: string, permission: AivenPermission}; export type AIVENACL = {acls?: Array | undefined, type: 'AIVEN_ACL'}; export type AclResourceType = 'CLUSTER' | 'CONSUMER_GROUP' | 'DELEGATION_TOKEN' | 'TOPIC' | 'TRANSACTIONAL_ID' | 'UNKNOWN' | 'USER'; export type ResourcePatternType = 'LITERAL' | 'PREFIXED'; export type Operation = 'All' | 'Alter' | 'AlterConfigs' | 'ClusterAction' | 'Create' | 'CreateTokens' | 'Delete' | 'Describe' | 'DescribeConfigs' | 'DescribeTokens' | 'IdempotentWrite' | 'Read' | 'Unknown' | 'Write'; export type AclPermissionTypeForAccessControlEntry = 'Deny' | 'Allow'; export type KafkaServiceAccountACL = {type: AclResourceType, name: string, patternType: ResourcePatternType, connectCluster?: string | undefined, operations?: Array | undefined, host?: string | undefined, permission?: AclPermissionTypeForAccessControlEntry | undefined}; export type KAFKAACL = {acls?: Array | undefined, type: 'KAFKA_ACL'}; export type ServiceAccountAuthorization = AIVENACL | KAFKAACL; export type SchemaRegistryOperation = 'Read' | 'Write'; export type SchemaRegistryAcl = {name: string, patternType: ResourcePatternType, operations?: Array | undefined}; export type SchemaRegistryAuthorization = {acls?: Array | undefined}; export type ServiceAccountSpec = {authorization: ServiceAccountAuthorization, schemaRegistryAuthorization?: SchemaRegistryAuthorization | undefined}; export type ServiceAccountResourceV1 = {apiVersion: string, kind: ServiceAccountKind, metadata: ServiceAccountMetadata, spec: ServiceAccountSpec}; export type UpdateApiPublicSelfServeV1ClusterClusterServiceAccountArgs = {cluster: string, dryMode?: boolean | undefined, body: ServiceAccountResourceV1}; export type UpdateApiPublicSelfServeV1ClusterClusterServiceAccountOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceAccountKind = z.literal("ServiceAccount"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const serviceAccountMetadata = z.object({appInstance: z.string().optional(), cluster: z.string(), labels: mapLabelKeyString.optional(), name: z.string()}); export const aivenResourceType = z.enum(["SCHEMA", "TOPIC"]); export const aivenPermission = z.enum(["admin", "read", "readwrite", "schema_registry_read", "schema_registry_write", "write"]); export const aivenServiceAccountACL = z.object({resourceType: aivenResourceType, name: z.string(), permission: aivenPermission}); export const aIVENACL = z.object({acls: z.array(aivenServiceAccountACL).optional(), type: z.literal("AIVEN_ACL")}); export const aclResourceType = z.enum(["CLUSTER", "CONSUMER_GROUP", "DELEGATION_TOKEN", "TOPIC", "TRANSACTIONAL_ID", "UNKNOWN", "USER"]); export const resourcePatternType = z.enum(["LITERAL", "PREFIXED"]); export const operation = z.enum(["All", "Alter", "AlterConfigs", "ClusterAction", "Create", "CreateTokens", "Delete", "Describe", "DescribeConfigs", "DescribeTokens", "IdempotentWrite", "Read", "Unknown", "Write"]); export const aclPermissionTypeForAccessControlEntry = z.enum(["Deny", "Allow"]); export const kafkaServiceAccountACL = z.object({type: aclResourceType, name: z.string(), patternType: resourcePatternType, connectCluster: z.string().optional(), operations: z.array(operation).optional(), host: z.string().optional(), permission: aclPermissionTypeForAccessControlEntry.optional()}); export const kAFKAACL = z.object({acls: z.array(kafkaServiceAccountACL).optional(), type: z.literal("KAFKA_ACL")}); export const serviceAccountAuthorization = z.discriminatedUnion("type", [aIVENACL, kAFKAACL]); export const schemaRegistryOperation = z.enum(["Read", "Write"]); export const schemaRegistryAcl = z.object({name: z.string(), patternType: resourcePatternType, operations: z.array(schemaRegistryOperation).optional()}); export const schemaRegistryAuthorization = z.object({acls: z.array(schemaRegistryAcl).optional()}); export const serviceAccountSpec = z.object({authorization: serviceAccountAuthorization, schemaRegistryAuthorization: schemaRegistryAuthorization.optional()}); export const serviceAccountResourceV1 = z.object({apiVersion: z.string(), kind: serviceAccountKind, metadata: serviceAccountMetadata, spec: serviceAccountSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const serviceAccountApplyResultV1 = z.object({resource: serviceAccountResourceV1, 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 ServiceAccount * * PUT /public/self-serve/v1/cluster/{cluster}/service-account */ export const updateApiPublicSelfServeV1ClusterClusterServiceAccount = async (args: UpdateApiPublicSelfServeV1ClusterClusterServiceAccountArgs, options: UpdateApiPublicSelfServeV1ClusterClusterServiceAccountOptions) => { 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/self-serve/v1/cluster/${encodeURIComponent(String(args.cluster))}/service-account${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(serviceAccountApplyResultV1, await res.json()) };