The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as kafka from "@distilled.cloud/gcp/managedkafka_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | expandParent, |
| 12 | getSchemaVersion, |
| 13 | hasAlchemyLabelMap, |
| 14 | listSchemaRegistries, |
| 15 | normalizeLocation, |
| 16 | OWNERSHIP_SUBJECT, |
| 17 | parseHttpJson, |
| 18 | parseName, |
| 19 | replaceOnIdentity, |
| 20 | retryTransient, |
| 21 | schemaRegistryOwnership, |
| 22 | subjectParent, |
| 23 | toSubjectId, |
| 24 | versionName, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type SchemaReference = { |
| 28 | /** Name of the referenced schema. */ |
| 29 | name?: string; |
| 30 | /** Subject of the referenced schema. */ |
| 31 | subject?: string; |
| 32 | /** Version of the referenced schema. */ |
| 33 | version?: number; |
| 34 | }; |
| 35 | |
| 36 | export type SchemaRegistriesSubjectsVersionProps = { |
| 37 | /** |
| 38 | * Parent schema registry. Full name |
| 39 | * `projects/{project}/locations/{location}/schemaRegistries/{id}` or |
| 40 | * the registry id (combined with `location`). Immutable — changing it |
| 41 | * replaces the version. |
| 42 | */ |
| 43 | schemaRegistry: string; |
| 44 | /** |
| 45 | * Region used when `schemaRegistry` is a bare id. |
| 46 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * Subject name. If omitted, a unique name is generated. Immutable — |
| 51 | * changing it replaces the version. |
| 52 | */ |
| 53 | subject?: string; |
| 54 | /** |
| 55 | * Version number. If omitted, the server assigns the next id. |
| 56 | * Immutable — changing it replaces the version. |
| 57 | */ |
| 58 | version?: number; |
| 59 | /** |
| 60 | * Schema payload. Immutable — changing it replaces the version. |
| 61 | */ |
| 62 | schema: string; |
| 63 | /** |
| 64 | * Schema type. |
| 65 | * @default "AVRO" |
| 66 | */ |
| 67 | schemaType?: kafka.CreateVersionRequestSchemaTypeEnum | (string & {}); |
| 68 | /** |
| 69 | * Normalize the schema before storing. |
| 70 | * @default false |
| 71 | */ |
| 72 | normalize?: boolean; |
| 73 | /** |
| 74 | * Explicit schema id. Must not collide with a different schema. |
| 75 | */ |
| 76 | id?: number; |
| 77 | /** |
| 78 | * Schema references. |
| 79 | */ |
| 80 | references?: SchemaReference[]; |
| 81 | }; |
| 82 | |
| 83 | export type SchemaRegistriesSubjectsVersion = Resource< |
| 84 | "GCP.ManagedKafka.SchemaRegistriesSubjectsVersion", |
| 85 | SchemaRegistriesSubjectsVersionProps, |
| 86 | { |
| 87 | /** Full resource name `.../subjects/{subject}/versions/{version}`. */ |
| 88 | name: string; |
| 89 | /** Parent schema registry resource name. */ |
| 90 | schemaRegistry: string; |
| 91 | /** Project id. */ |
| 92 | project: string; |
| 93 | /** Location id. */ |
| 94 | location: string; |
| 95 | /** Subject name. */ |
| 96 | subject: string; |
| 97 | /** Version number. */ |
| 98 | version: number; |
| 99 | /** Schema id. */ |
| 100 | schemaId: number | undefined; |
| 101 | /** Schema payload. */ |
| 102 | schema: string | undefined; |
| 103 | /** Schema type. */ |
| 104 | schemaType: string | undefined; |
| 105 | /** Schema references. */ |
| 106 | references: SchemaReference[]; |
| 107 | }, |
| 108 | never, |
| 109 | Providers |
| 110 | >; |
| 111 | |
| 112 | /** |
| 113 | * A schema version under a subject in a Managed Kafka schema registry |
| 114 | * (default context). |
| 115 | * |
| 116 | * Versions are immutable. Changing subject, registry, schema, type, or |
| 117 | * version replaces the resource. |
| 118 | * |
| 119 | * ### Creating a Version |
| 120 | * **Example:** Avro record |
| 121 | * ```typescript |
| 122 | * const version = yield* GCP.ManagedKafka.SchemaRegistriesSubjectsVersion( |
| 123 | * "OrderSchema", |
| 124 | * { |
| 125 | * schemaRegistry: registry.name, |
| 126 | * subject: "orders", |
| 127 | * schemaType: "AVRO", |
| 128 | * schema: JSON.stringify({ |
| 129 | * type: "record", |
| 130 | * name: "Order", |
| 131 | * fields: [{ name: "id", type: "string" }], |
| 132 | * }), |
| 133 | * }, |
| 134 | * ); |
| 135 | * ``` |
| 136 | * |
| 137 | * @resource |
| 138 | * @category ManagedKafka |
| 139 | */ |
| 140 | export const SchemaRegistriesSubjectsVersion = Resource<SchemaRegistriesSubjectsVersion>( |
| 141 | "GCP.ManagedKafka.SchemaRegistriesSubjectsVersion", |
| 142 | ); |
| 143 | |
| 144 | export class SchemaRegistriesSubjectsVersionNotResolved extends Data.TaggedError( |
| 145 | "GCP.ManagedKafka.SchemaRegistriesSubjectsVersionNotResolved", |
| 146 | )<{ |
| 147 | resourceName: string; |
| 148 | }> {} |
| 149 | |
| 150 | const registryOf = (value: string, project: string, location: string) => |
| 151 | expandParent(value, project, location, "schemaRegistries"); |
| 152 | |
| 153 | const toAttrs = ( |
| 154 | version: kafka.SchemaVersion, |
| 155 | schemaRegistry: string, |
| 156 | project: string, |
| 157 | location: string, |
| 158 | name: string, |
| 159 | ) => { |
| 160 | const parsed = parseName(schemaRegistry, "schemaRegistries"); |
| 161 | return { |
| 162 | name, |
| 163 | schemaRegistry, |
| 164 | project: parsed.project || project, |
| 165 | location: parsed.location || location, |
| 166 | subject: version.subject ?? "", |
| 167 | version: version.version ?? 0, |
| 168 | schemaId: version.id, |
| 169 | schema: version.schema, |
| 170 | schemaType: version.schemaType, |
| 171 | references: version.references ?? [], |
| 172 | }; |
| 173 | }; |
| 174 | |
| 175 | const parseVersionList = (value: unknown): number[] => { |
| 176 | if (!Array.isArray(value)) return []; |
| 177 | return value |
| 178 | .map((item) => { |
| 179 | if (typeof item === "number") return item; |
| 180 | if (typeof item === "string") return Number(item); |
| 181 | if (item && typeof item === "object" && "version" in item) { |
| 182 | return Number((item as { version?: unknown }).version); |
| 183 | } |
| 184 | return Number.NaN; |
| 185 | }) |
| 186 | .filter((item) => Number.isFinite(item)); |
| 187 | }; |
| 188 | |
| 189 | export const SchemaRegistriesSubjectsVersionProvider = () => |
| 190 | Provider.succeed(SchemaRegistriesSubjectsVersion, { |
| 191 | stables: ["name", "schemaRegistry", "project", "location", "subject", "version", "schemaId"], |
| 192 | |
| 193 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 194 | if (!isResolved(news)) return undefined; |
| 195 | const env = yield* GcpEnvironment.current; |
| 196 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 197 | const schemaChanged = |
| 198 | (olds?.schema ?? output?.schema ?? "") !== news.schema || |
| 199 | (olds?.schemaType ?? output?.schemaType ?? "") !== |
| 200 | (news.schemaType ?? olds?.schemaType ?? output?.schemaType ?? ""); |
| 201 | return replaceOnIdentity({ |
| 202 | previousId: |
| 203 | olds?.subject !== undefined && olds.version !== undefined |
| 204 | ? `${olds.subject}/${olds.version}` |
| 205 | : output !== undefined |
| 206 | ? `${output.subject}/${output.version}` |
| 207 | : undefined, |
| 208 | nextId: |
| 209 | news.subject !== undefined && news.version !== undefined |
| 210 | ? `${news.subject}/${news.version}` |
| 211 | : undefined, |
| 212 | previousParent: olds?.schemaRegistry ?? output?.schemaRegistry, |
| 213 | nextParent: registryOf(news.schemaRegistry, env.project, location), |
| 214 | extra: schemaChanged, |
| 215 | }); |
| 216 | }), |
| 217 | |
| 218 | read: Effect.fn(function* ({ id, olds, output }) { |
| 219 | const env = yield* GcpEnvironment.current; |
| 220 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 221 | const subject = yield* toSubjectId(id, olds?.subject, output?.subject); |
| 222 | const schemaRegistry = |
| 223 | olds?.schemaRegistry !== undefined |
| 224 | ? registryOf(olds.schemaRegistry, env.project, location) |
| 225 | : (output?.schemaRegistry ?? ""); |
| 226 | const version = olds?.version ?? output?.version ?? "latest"; |
| 227 | const name = |
| 228 | output?.name ?? |
| 229 | (schemaRegistry.length > 0 |
| 230 | ? versionName(subjectParent(schemaRegistry, subject), version) |
| 231 | : ""); |
| 232 | const existing = yield* getSchemaVersion(name); |
| 233 | if (existing === undefined) return undefined; |
| 234 | const resolvedName = versionName( |
| 235 | subjectParent(schemaRegistry, existing.subject ?? subject), |
| 236 | existing.version ?? version, |
| 237 | ); |
| 238 | const attrs = toAttrs(existing, schemaRegistry, env.project, location, resolvedName); |
| 239 | return output !== undefined || olds !== undefined ? attrs : Unowned(attrs); |
| 240 | }), |
| 241 | |
| 242 | list: () => |
| 243 | Effect.gen(function* () { |
| 244 | const env = yield* GcpEnvironment.current; |
| 245 | const registries = yield* listSchemaRegistries(env.project, env.region); |
| 246 | const owned = yield* Effect.forEach( |
| 247 | registries.filter((registry) => (registry.name ?? "").length > 0), |
| 248 | (registry) => |
| 249 | schemaRegistryOwnership(registry.name!).pipe( |
| 250 | Effect.map((labels) => (hasAlchemyLabelMap(labels) ? registry : undefined)), |
| 251 | ), |
| 252 | { concurrency: 4 }, |
| 253 | ); |
| 254 | const versions = yield* Effect.forEach( |
| 255 | owned.filter((registry): registry is kafka.SchemaRegistry => registry !== undefined), |
| 256 | (registry) => |
| 257 | Effect.gen(function* () { |
| 258 | const body = yield* kafka |
| 259 | .listProjectsLocationsSchemaRegistriesSubjects({ |
| 260 | parent: registry.name!, |
| 261 | }) |
| 262 | .pipe( |
| 263 | Effect.catchTag(["NotFound", "SchemaRegistryRequiresCluster"], () => |
| 264 | Effect.succeed<kafka.HttpBody>({}), |
| 265 | ), |
| 266 | ); |
| 267 | const subjects = (yield* parseHttpJson(body)) as unknown; |
| 268 | const names = Array.isArray(subjects) |
| 269 | ? subjects.filter( |
| 270 | (item): item is string => |
| 271 | typeof item === "string" && item !== OWNERSHIP_SUBJECT, |
| 272 | ) |
| 273 | : []; |
| 274 | return yield* Effect.forEach( |
| 275 | names, |
| 276 | (subject) => |
| 277 | Effect.gen(function* () { |
| 278 | const listed = yield* kafka |
| 279 | .listProjectsLocationsSchemaRegistriesSubjectsVersions({ |
| 280 | parent: subjectParent(registry.name!, subject), |
| 281 | }) |
| 282 | .pipe( |
| 283 | Effect.catchTag(["NotFound", "SchemaRegistryRequiresCluster"], () => |
| 284 | Effect.succeed<kafka.HttpBody>({}), |
| 285 | ), |
| 286 | ); |
| 287 | const ids = parseVersionList(yield* parseHttpJson(listed)); |
| 288 | return yield* Effect.forEach( |
| 289 | ids, |
| 290 | (version) => |
| 291 | getSchemaVersion( |
| 292 | versionName(subjectParent(registry.name!, subject), version), |
| 293 | ).pipe( |
| 294 | Effect.map((item) => |
| 295 | item |
| 296 | ? toAttrs( |
| 297 | item, |
| 298 | registry.name!, |
| 299 | env.project, |
| 300 | parseName(registry.name!, "schemaRegistries").location, |
| 301 | versionName(subjectParent(registry.name!, subject), version), |
| 302 | ) |
| 303 | : undefined, |
| 304 | ), |
| 305 | ), |
| 306 | { concurrency: 4 }, |
| 307 | ); |
| 308 | }), |
| 309 | { concurrency: 4 }, |
| 310 | ); |
| 311 | }), |
| 312 | { concurrency: 2 }, |
| 313 | ); |
| 314 | return versions |
| 315 | .flat(2) |
| 316 | .filter((item): item is NonNullable<typeof item> => item !== undefined); |
| 317 | }), |
| 318 | |
| 319 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 320 | const env = yield* GcpEnvironment.current; |
| 321 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 322 | const schemaRegistry = registryOf(news.schemaRegistry, env.project, location); |
| 323 | const subject = yield* toSubjectId(id, news.subject, output?.subject); |
| 324 | const parent = subjectParent(schemaRegistry, subject); |
| 325 | const versionHint = news.version ?? output?.version; |
| 326 | const name = output?.name ?? versionName(parent, versionHint ?? "latest"); |
| 327 | |
| 328 | let current = yield* getSchemaVersion(name); |
| 329 | |
| 330 | if (current === undefined) { |
| 331 | const created = yield* retryTransient( |
| 332 | kafka |
| 333 | .createProjectsLocationsSchemaRegistriesSubjectsVersions({ |
| 334 | parent, |
| 335 | body: { |
| 336 | schemaType: news.schemaType, |
| 337 | version: news.version, |
| 338 | normalize: news.normalize, |
| 339 | id: news.id, |
| 340 | schema: news.schema, |
| 341 | references: news.references, |
| 342 | }, |
| 343 | }) |
| 344 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)), |
| 345 | ); |
| 346 | const lookedUp = yield* kafka |
| 347 | .lookupVersionProjectsLocationsSchemaRegistriesSubjects({ |
| 348 | parent, |
| 349 | body: { |
| 350 | schema: news.schema, |
| 351 | schemaType: news.schemaType, |
| 352 | normalize: news.normalize, |
| 353 | references: news.references, |
| 354 | }, |
| 355 | }) |
| 356 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 357 | current = |
| 358 | lookedUp ?? (yield* getSchemaVersion(versionName(parent, news.version ?? "latest"))); |
| 359 | void created; |
| 360 | } |
| 361 | |
| 362 | if (current === undefined) { |
| 363 | return yield* new SchemaRegistriesSubjectsVersionNotResolved({ |
| 364 | resourceName: name, |
| 365 | }); |
| 366 | } |
| 367 | |
| 368 | const resolved = versionName( |
| 369 | subjectParent(schemaRegistry, current.subject ?? subject), |
| 370 | current.version ?? versionHint ?? "latest", |
| 371 | ); |
| 372 | return toAttrs(current, schemaRegistry, env.project, location, resolved); |
| 373 | }), |
| 374 | |
| 375 | delete: Effect.fn(function* ({ output }) { |
| 376 | yield* kafka |
| 377 | .deleteProjectsLocationsSchemaRegistriesSubjectsVersions({ |
| 378 | name: output.name, |
| 379 | permanent: false, |
| 380 | }) |
| 381 | .pipe( |
| 382 | Effect.catchTag( |
| 383 | ["NotFound", "SchemaRegistryRequiresCluster", "SchemaRegistryPathNotFound"], |
| 384 | () => Effect.void, |
| 385 | ), |
| 386 | ); |
| 387 | yield* kafka |
| 388 | .deleteProjectsLocationsSchemaRegistriesSubjectsVersions({ |
| 389 | name: output.name, |
| 390 | permanent: true, |
| 391 | }) |
| 392 | .pipe( |
| 393 | Effect.catchTag( |
| 394 | ["NotFound", "SchemaRegistryRequiresCluster", "SchemaRegistryPathNotFound"], |
| 395 | () => Effect.void, |
| 396 | ), |
| 397 | ); |
| 398 | }), |
| 399 | }); |