The best way to control Claude Code, Codex and more from any computer or phone
| 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 { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | getSchemaRegistry, |
| 13 | hasAlchemyLabelMap, |
| 14 | hasSchemaRegistryOwnership, |
| 15 | listSchemaRegistries, |
| 16 | locationParent, |
| 17 | normalizeLocation, |
| 18 | parseName, |
| 19 | replaceOnIdentity, |
| 20 | retryTransient, |
| 21 | schemaRegistryIdOf, |
| 22 | schemaRegistryOwnership, |
| 23 | stampSchemaRegistryOwnership, |
| 24 | toSchemaRegistryId, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type SchemaRegistryProps = { |
| 28 | /** |
| 29 | * Schema registry id. Letters, numbers, and underscores; max 63 |
| 30 | * characters; must not start with a number. If omitted, a unique id is |
| 31 | * generated. Immutable — changing it replaces the registry. |
| 32 | */ |
| 33 | schemaRegistryId?: string; |
| 34 | /** |
| 35 | * Region (`us-central1`, …). Immutable. |
| 36 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 37 | */ |
| 38 | location?: string; |
| 39 | }; |
| 40 | |
| 41 | export type SchemaRegistry = Resource< |
| 42 | "GCP.ManagedKafka.SchemaRegistry", |
| 43 | SchemaRegistryProps, |
| 44 | { |
| 45 | /** Full resource name `.../schemaRegistries/{schemaRegistry}`. */ |
| 46 | name: string; |
| 47 | /** Schema registry id. */ |
| 48 | schemaRegistryId: string; |
| 49 | /** Project id. */ |
| 50 | project: string; |
| 51 | /** Location id. */ |
| 52 | location: string; |
| 53 | /** Contexts currently present. */ |
| 54 | contexts: string[]; |
| 55 | }, |
| 56 | never, |
| 57 | Providers |
| 58 | >; |
| 59 | |
| 60 | /** |
| 61 | * A Managed Kafka schema registry instance. |
| 62 | * |
| 63 | * Schema registries have no labels field, so Alchemy stamps ownership on |
| 64 | * a reserved `alchemy_ownership` subject. Name and location are identity |
| 65 | * — changing either replaces the registry. |
| 66 | * |
| 67 | * ### Creating a Schema Registry |
| 68 | * **Example:** Generated name |
| 69 | * ```typescript |
| 70 | * const registry = yield* GCP.ManagedKafka.SchemaRegistry("Schemas", {}); |
| 71 | * ``` |
| 72 | * |
| 73 | * **Example:** Explicit id |
| 74 | * ```typescript |
| 75 | * const registry = yield* GCP.ManagedKafka.SchemaRegistry("Schemas", { |
| 76 | * schemaRegistryId: "app_schemas", |
| 77 | * location: "us-central1", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * @resource |
| 82 | * @category ManagedKafka |
| 83 | */ |
| 84 | export const SchemaRegistry = Resource<SchemaRegistry>("GCP.ManagedKafka.SchemaRegistry"); |
| 85 | |
| 86 | export class SchemaRegistryNotResolved extends Data.TaggedError( |
| 87 | "GCP.ManagedKafka.SchemaRegistryNotResolved", |
| 88 | )<{ |
| 89 | resourceName: string; |
| 90 | }> {} |
| 91 | |
| 92 | const resourceName = (project: string, location: string, schemaRegistryId: string) => |
| 93 | `projects/${project}/locations/${location}/schemaRegistries/${schemaRegistryId}`; |
| 94 | |
| 95 | const toAttrs = (registry: kafka.SchemaRegistry, project: string) => { |
| 96 | const name = registry.name ?? ""; |
| 97 | const parsed = parseName(name, "schemaRegistries"); |
| 98 | return { |
| 99 | name, |
| 100 | schemaRegistryId: parsed.id, |
| 101 | project: parsed.project || project, |
| 102 | location: parsed.location, |
| 103 | contexts: registry.contexts ?? [], |
| 104 | }; |
| 105 | }; |
| 106 | |
| 107 | export const SchemaRegistryProvider = () => |
| 108 | Provider.succeed(SchemaRegistry, { |
| 109 | stables: ["name", "schemaRegistryId", "project", "location"], |
| 110 | |
| 111 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 112 | if (!isResolved(news)) return undefined; |
| 113 | const env = yield* GcpEnvironment.current; |
| 114 | return replaceOnIdentity({ |
| 115 | previousId: olds?.schemaRegistryId ?? output?.schemaRegistryId, |
| 116 | nextId: news.schemaRegistryId |
| 117 | ? schemaRegistryIdOf(news.schemaRegistryId) |
| 118 | : (olds?.schemaRegistryId ?? output?.schemaRegistryId), |
| 119 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 120 | nextLocation: normalizeLocation(news.location ?? output?.location, env.region), |
| 121 | }); |
| 122 | }), |
| 123 | |
| 124 | read: Effect.fn(function* ({ id, olds, output }) { |
| 125 | const env = yield* GcpEnvironment.current; |
| 126 | const schemaRegistryId = yield* toSchemaRegistryId( |
| 127 | id, |
| 128 | olds?.schemaRegistryId, |
| 129 | output?.schemaRegistryId, |
| 130 | ); |
| 131 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 132 | const name = output?.name ?? resourceName(env.project, location, schemaRegistryId); |
| 133 | const existing = yield* getSchemaRegistry(name); |
| 134 | if (existing === undefined) return undefined; |
| 135 | const attrs = toAttrs(existing, env.project); |
| 136 | return (yield* hasSchemaRegistryOwnership(id, name)) ? attrs : Unowned(attrs); |
| 137 | }), |
| 138 | |
| 139 | list: () => |
| 140 | Effect.gen(function* () { |
| 141 | const env = yield* GcpEnvironment.current; |
| 142 | const registries = yield* listSchemaRegistries(env.project, env.region); |
| 143 | const owned = yield* Effect.forEach( |
| 144 | registries.filter((registry) => (registry.name ?? "").length > 0), |
| 145 | (registry) => |
| 146 | schemaRegistryOwnership(registry.name!).pipe( |
| 147 | Effect.map((labels) => |
| 148 | hasAlchemyLabelMap(labels) ? toAttrs(registry, env.project) : undefined, |
| 149 | ), |
| 150 | ), |
| 151 | { concurrency: 4 }, |
| 152 | ); |
| 153 | return owned.filter((attrs): attrs is NonNullable<typeof attrs> => attrs !== undefined); |
| 154 | }), |
| 155 | |
| 156 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 157 | const env = yield* GcpEnvironment.current; |
| 158 | const schemaRegistryId = yield* toSchemaRegistryId( |
| 159 | id, |
| 160 | news.schemaRegistryId, |
| 161 | output?.schemaRegistryId, |
| 162 | ); |
| 163 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 164 | const name = output?.name ?? resourceName(env.project, location, schemaRegistryId); |
| 165 | const labels = yield* createInternalLabels(id); |
| 166 | |
| 167 | let current = yield* getSchemaRegistry(name); |
| 168 | |
| 169 | if (current === undefined) { |
| 170 | const created = yield* retryTransient( |
| 171 | kafka |
| 172 | .createProjectsLocationsSchemaRegistries({ |
| 173 | parent: locationParent(env.project, location), |
| 174 | body: { |
| 175 | schemaRegistryId, |
| 176 | schemaRegistry: {}, |
| 177 | }, |
| 178 | }) |
| 179 | .pipe(Effect.catchTag("Conflict", () => getSchemaRegistry(name))), |
| 180 | ); |
| 181 | current = created ?? undefined; |
| 182 | } |
| 183 | |
| 184 | if (current === undefined) { |
| 185 | return yield* new SchemaRegistryNotResolved({ resourceName: name }); |
| 186 | } |
| 187 | |
| 188 | yield* stampSchemaRegistryOwnership(current.name ?? name, labels); |
| 189 | return toAttrs(current, env.project); |
| 190 | }), |
| 191 | |
| 192 | delete: Effect.fn(function* ({ output }) { |
| 193 | yield* kafka |
| 194 | .deleteProjectsLocationsSchemaRegistries({ name: output.name }) |
| 195 | .pipe(Effect.catchTag(["NotFound", "SchemaRegistryRequiresCluster"], () => Effect.void)); |
| 196 | }), |
| 197 | }); |