The best way to control Claude Code, Codex and more from any computer or phone

main
ts 455 lines 15.8 KB
Raw
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 getContextSchemaVersion,
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 ContextSchemaReference = {
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 SchemaRegistriesContextsSubjectsVersionProps = {
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 * Schema context. Immutable — changing it replaces the version.
46 */
47 context: string;
48 /**
49 * Region used when `schemaRegistry` is a bare id.
50 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
51 */
52 location?: string;
53 /**
54 * Subject name. If omitted, a unique name is generated. Immutable —
55 * changing it replaces the version.
56 */
57 subject?: string;
58 /**
59 * Version number. If omitted, the server assigns the next id.
60 * Immutable — changing it replaces the version.
61 */
62 version?: number;
63 /**
64 * Schema payload. Immutable — changing it replaces the version.
65 */
66 schema: string;
67 /**
68 * Schema type.
69 * @default "AVRO"
70 */
71 schemaType?: kafka.CreateVersionRequestSchemaTypeEnum | (string & {});
72 /**
73 * Normalize the schema before storing.
74 * @default false
75 */
76 normalize?: boolean;
77 /**
78 * Explicit schema id. Must not collide with a different schema.
79 */
80 id?: number;
81 /**
82 * Schema references.
83 */
84 references?: ContextSchemaReference[];
85 };
86
87 export type SchemaRegistriesContextsSubjectsVersion = Resource<
88 "GCP.ManagedKafka.SchemaRegistriesContextsSubjectsVersion",
89 SchemaRegistriesContextsSubjectsVersionProps,
90 {
91 /** Full resource name `.../contexts/{context}/subjects/{subject}/versions/{version}`. */
92 name: string;
93 /** Parent schema registry resource name. */
94 schemaRegistry: string;
95 /** Context id. */
96 context: string;
97 /** Project id. */
98 project: string;
99 /** Location id. */
100 location: string;
101 /** Subject name. */
102 subject: string;
103 /** Version number. */
104 version: number;
105 /** Schema id. */
106 schemaId: number | undefined;
107 /** Schema payload. */
108 schema: string | undefined;
109 /** Schema type. */
110 schemaType: string | undefined;
111 /** Schema references. */
112 references: ContextSchemaReference[];
113 },
114 never,
115 Providers
116 >;
117
118 /**
119 * A schema version under a subject in a named context of a Managed Kafka
120 * schema registry.
121 *
122 * Versions are immutable. Changing context, subject, registry, schema,
123 * type, or version replaces the resource.
124 *
125 * ### Creating a Version
126 * **Example:** Avro record in a context
127 * ```typescript
128 * const version =
129 * yield* GCP.ManagedKafka.SchemaRegistriesContextsSubjectsVersion(
130 * "OrderSchema",
131 * {
132 * schemaRegistry: registry.name,
133 * context: "prod",
134 * subject: "orders",
135 * schemaType: "AVRO",
136 * schema: JSON.stringify({
137 * type: "record",
138 * name: "Order",
139 * fields: [{ name: "id", type: "string" }],
140 * }),
141 * },
142 * );
143 * ```
144 *
145 * @resource
146 * @category ManagedKafka
147 */
148 export const SchemaRegistriesContextsSubjectsVersion =
149 Resource<SchemaRegistriesContextsSubjectsVersion>(
150 "GCP.ManagedKafka.SchemaRegistriesContextsSubjectsVersion",
151 );
152
153 export class SchemaRegistriesContextsSubjectsVersionNotResolved extends Data.TaggedError(
154 "GCP.ManagedKafka.SchemaRegistriesContextsSubjectsVersionNotResolved",
155 )<{
156 resourceName: string;
157 }> {}
158
159 const registryOf = (value: string, project: string, location: string) =>
160 expandParent(value, project, location, "schemaRegistries");
161
162 /**
163 * Context-scoped subjects come back qualified as `:.{context}:{subject}`;
164 * resource paths and attributes use the bare subject.
165 */
166 const unqualifySubject = (subject: string | undefined, context: string) =>
167 subject?.replace(`:.${context}:`, "");
168
169 const toAttrs = (
170 version: kafka.SchemaVersion,
171 schemaRegistry: string,
172 context: string,
173 project: string,
174 location: string,
175 name: string,
176 ) => {
177 const parsed = parseName(schemaRegistry, "schemaRegistries");
178 return {
179 name,
180 schemaRegistry,
181 context,
182 project: parsed.project || project,
183 location: parsed.location || location,
184 subject: unqualifySubject(version.subject, context) ?? "",
185 version: version.version ?? 0,
186 schemaId: version.id,
187 schema: version.schema,
188 schemaType: version.schemaType,
189 references: version.references ?? [],
190 };
191 };
192
193 const parseVersionList = (value: unknown): number[] => {
194 if (!Array.isArray(value)) return [];
195 return value
196 .map((item) => {
197 if (typeof item === "number") return item;
198 if (typeof item === "string") return Number(item);
199 if (item && typeof item === "object" && "version" in item) {
200 return Number((item as { version?: unknown }).version);
201 }
202 return Number.NaN;
203 })
204 .filter((item) => Number.isFinite(item));
205 };
206
207 export const SchemaRegistriesContextsSubjectsVersionProvider = () =>
208 Provider.succeed(SchemaRegistriesContextsSubjectsVersion, {
209 stables: [
210 "name",
211 "schemaRegistry",
212 "context",
213 "project",
214 "location",
215 "subject",
216 "version",
217 "schemaId",
218 ],
219
220 diff: Effect.fn(function* ({ news, olds, output }) {
221 if (!isResolved(news)) return undefined;
222 const env = yield* GcpEnvironment.current;
223 const location = normalizeLocation(news.location ?? output?.location, env.region);
224 const schemaChanged =
225 (olds?.schema ?? output?.schema ?? "") !== news.schema ||
226 (olds?.schemaType ?? output?.schemaType ?? "") !==
227 (news.schemaType ?? olds?.schemaType ?? output?.schemaType ?? "") ||
228 (olds?.context ?? output?.context ?? "") !== news.context;
229 return replaceOnIdentity({
230 previousId:
231 olds?.subject !== undefined && olds.version !== undefined
232 ? `${olds.context}/${olds.subject}/${olds.version}`
233 : output !== undefined
234 ? `${output.context}/${output.subject}/${output.version}`
235 : undefined,
236 nextId:
237 news.subject !== undefined && news.version !== undefined
238 ? `${news.context}/${news.subject}/${news.version}`
239 : undefined,
240 previousParent: olds?.schemaRegistry ?? output?.schemaRegistry,
241 nextParent: registryOf(news.schemaRegistry, env.project, location),
242 extra: schemaChanged,
243 });
244 }),
245
246 read: Effect.fn(function* ({ id, olds, output }) {
247 const env = yield* GcpEnvironment.current;
248 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
249 const subject = yield* toSubjectId(id, olds?.subject, output?.subject);
250 const context = olds?.context ?? output?.context ?? "";
251 const schemaRegistry =
252 olds?.schemaRegistry !== undefined
253 ? registryOf(olds.schemaRegistry, env.project, location)
254 : (output?.schemaRegistry ?? "");
255 const version = olds?.version ?? output?.version ?? "latest";
256 const name =
257 output?.name ??
258 (schemaRegistry.length > 0 && context.length > 0
259 ? versionName(subjectParent(schemaRegistry, subject, context), version)
260 : "");
261 const existing = yield* getContextSchemaVersion(name);
262 if (existing === undefined) return undefined;
263 const resolvedName = versionName(
264 subjectParent(schemaRegistry, existing.subject ?? subject, context),
265 existing.version ?? version,
266 );
267 const attrs = toAttrs(existing, schemaRegistry, context, env.project, location, resolvedName);
268 return output !== undefined || olds !== undefined ? attrs : Unowned(attrs);
269 }),
270
271 list: () =>
272 Effect.gen(function* () {
273 const env = yield* GcpEnvironment.current;
274 const registries = yield* listSchemaRegistries(env.project, env.region);
275 const owned = yield* Effect.forEach(
276 registries.filter((registry) => (registry.name ?? "").length > 0),
277 (registry) =>
278 schemaRegistryOwnership(registry.name!).pipe(
279 Effect.map((labels) => (hasAlchemyLabelMap(labels) ? registry : undefined)),
280 ),
281 { concurrency: 4 },
282 );
283 const versions = yield* Effect.forEach(
284 owned.filter((registry): registry is kafka.SchemaRegistry => registry !== undefined),
285 (registry) =>
286 Effect.gen(function* () {
287 const contexts = (registry.contexts ?? []).filter(
288 (context) => context.length > 0 && context !== ".",
289 );
290 return yield* Effect.forEach(
291 contexts,
292 (context) =>
293 Effect.gen(function* () {
294 const contextName = context.includes("/")
295 ? context
296 : `${registry.name}/contexts/${context}`;
297 const body = yield* kafka
298 .listProjectsLocationsSchemaRegistriesContextsSubjects({
299 parent: contextName,
300 })
301 .pipe(
302 Effect.catchTag(["NotFound", "SchemaRegistryRequiresCluster"], () =>
303 Effect.succeed<kafka.HttpBody>({}),
304 ),
305 );
306 const subjects = (yield* parseHttpJson(body)) as unknown;
307 const names = Array.isArray(subjects)
308 ? subjects.filter(
309 (item): item is string =>
310 typeof item === "string" && item !== OWNERSHIP_SUBJECT,
311 )
312 : [];
313 const contextId = parseName(contextName, "contexts").id;
314 return yield* Effect.forEach(
315 names,
316 (subject) =>
317 Effect.gen(function* () {
318 const listed = yield* kafka
319 .listProjectsLocationsSchemaRegistriesContextsSubjectsVersions({
320 parent: subjectParent(registry.name!, subject, contextId),
321 })
322 .pipe(
323 Effect.catchTag(["NotFound", "SchemaRegistryRequiresCluster"], () =>
324 Effect.succeed<kafka.HttpBody>({}),
325 ),
326 );
327 const ids = parseVersionList(yield* parseHttpJson(listed));
328 return yield* Effect.forEach(
329 ids,
330 (version) =>
331 getContextSchemaVersion(
332 versionName(
333 subjectParent(registry.name!, subject, contextId),
334 version,
335 ),
336 ).pipe(
337 Effect.map((item) =>
338 item
339 ? toAttrs(
340 item,
341 registry.name!,
342 contextId,
343 env.project,
344 parseName(registry.name!, "schemaRegistries").location,
345 versionName(
346 subjectParent(registry.name!, subject, contextId),
347 version,
348 ),
349 )
350 : undefined,
351 ),
352 ),
353 { concurrency: 4 },
354 );
355 }),
356 { concurrency: 4 },
357 );
358 }),
359 { concurrency: 2 },
360 );
361 }),
362 { concurrency: 2 },
363 );
364 return versions
365 .flat(3)
366 .filter((item): item is NonNullable<typeof item> => item !== undefined);
367 }),
368
369 reconcile: Effect.fn(function* ({ id, news, output }) {
370 const env = yield* GcpEnvironment.current;
371 const location = normalizeLocation(news.location ?? output?.location, env.region);
372 const schemaRegistry = registryOf(news.schemaRegistry, env.project, location);
373 const subject = yield* toSubjectId(id, news.subject, output?.subject);
374 const context = news.context;
375 const parent = subjectParent(schemaRegistry, subject, context);
376 const versionHint = news.version ?? output?.version;
377 const name = output?.name ?? versionName(parent, versionHint ?? "latest");
378
379 let current = yield* getContextSchemaVersion(name);
380
381 if (current === undefined) {
382 const created = yield* retryTransient(
383 kafka
384 .createProjectsLocationsSchemaRegistriesContextsSubjectsVersions({
385 parent,
386 body: {
387 schemaType: news.schemaType,
388 version: news.version,
389 normalize: news.normalize,
390 id: news.id,
391 schema: news.schema,
392 references: news.references,
393 },
394 })
395 .pipe(Effect.catchTag("Conflict", () => Effect.undefined)),
396 );
397 const lookedUp = yield* kafka
398 .lookupVersionProjectsLocationsSchemaRegistriesContextsSubjects({
399 parent,
400 body: {
401 schema: news.schema,
402 schemaType: news.schemaType,
403 normalize: news.normalize,
404 references: news.references,
405 },
406 })
407 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
408 current =
409 lookedUp ??
410 (yield* getContextSchemaVersion(versionName(parent, news.version ?? "latest")));
411 void created;
412 }
413
414 if (current === undefined) {
415 return yield* new SchemaRegistriesContextsSubjectsVersionNotResolved({
416 resourceName: name,
417 });
418 }
419
420 const resolved = versionName(
421 subjectParent(
422 schemaRegistry,
423 unqualifySubject(current.subject, context) ?? subject,
424 context,
425 ),
426 current.version ?? versionHint ?? "latest",
427 );
428 return toAttrs(current, schemaRegistry, context, env.project, location, resolved);
429 }),
430
431 delete: Effect.fn(function* ({ output }) {
432 yield* kafka
433 .deleteProjectsLocationsSchemaRegistriesContextsSubjectsVersions({
434 name: output.name,
435 permanent: false,
436 })
437 .pipe(
438 Effect.catchTag(
439 ["NotFound", "SchemaRegistryRequiresCluster", "SchemaRegistryPathNotFound"],
440 () => Effect.void,
441 ),
442 );
443 yield* kafka
444 .deleteProjectsLocationsSchemaRegistriesContextsSubjectsVersions({
445 name: output.name,
446 permanent: true,
447 })
448 .pipe(
449 Effect.catchTag(
450 ["NotFound", "SchemaRegistryRequiresCluster", "SchemaRegistryPathNotFound"],
451 () => Effect.void,
452 ),
453 );
454 }),
455 });