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

main
ts 323 lines 10.4 KB
Raw
1 import * as biglake from "@distilled.cloud/gcp/biglake_v1";
2 import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 BiglakeNotResolved,
12 listLocations,
13 createInternalLabels,
14 expandCatalog,
15 hasAlchemyLabelMap,
16 hasAlchemyLabels,
17 ignoreGone,
18 listCatalogs,
19 listChildResources,
20 listDatabases,
21 listTables,
22 locationParent,
23 mergeParameters,
24 missingGet,
25 namedOf,
26 normalizeLocation,
27 parseResourceName,
28 replaceOnIdentity,
29 retryTransient,
30 sameJson,
31 sameText,
32 toPhysicalId,
33 updateMaskOf,
34 userLabels,
35 waitUntilGone,
36 } from "./internal.ts";
37
38 export type HiveDatabaseOptions = {
39 /**
40 * Cloud Storage folder URI where the database data is stored, starting
41 * with `gs://`.
42 */
43 locationUri?: string;
44 /**
45 * User-supplied Hive database parameters. Alchemy ownership keys
46 * (`alchemy-stack`, `alchemy-stage`, `alchemy-id`) are merged in
47 * automatically — BigLake databases have no labels field.
48 */
49 parameters?: Record<string, string>;
50 };
51
52 export type CatalogsDatabaseProps = {
53 /**
54 * Parent catalog. Full name
55 * `projects/{project}/locations/{location}/catalogs/{catalog}` or the
56 * catalog id (combined with `location`). Immutable — changing it
57 * replaces the database.
58 */
59 catalog: string;
60 /**
61 * Database id (the `{database}` segment). If omitted, a unique id is
62 * generated. Immutable — changing it replaces the database.
63 */
64 databaseId?: string;
65 /**
66 * Location used when `catalog` is a bare id.
67 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
68 */
69 location?: string;
70 /**
71 * Database type.
72 * @default "HIVE"
73 */
74 type?: biglake.DatabaseTypeEnum | (string & {});
75 /**
76 * Hive database options. Required when `type` is `HIVE`.
77 */
78 hiveOptions?: HiveDatabaseOptions;
79 /**
80 * Shortcut for `hiveOptions.locationUri`.
81 */
82 locationUri?: string;
83 /**
84 * Shortcut for `hiveOptions.parameters`. Alchemy ownership keys are
85 * merged in automatically.
86 */
87 parameters?: Record<string, string>;
88 };
89
90 export type CatalogsDatabase = Resource<
91 "GCP.BigLake.CatalogsDatabase",
92 CatalogsDatabaseProps,
93 {
94 /** Full resource name. */
95 name: string;
96 /** Database id (last path segment). */
97 databaseId: string;
98 /** Parent catalog resource name. */
99 catalog: string;
100 /** Project id. */
101 project: string;
102 /** Location id. */
103 location: string;
104 /** Database type. */
105 type: string | undefined;
106 /** Hive location URI. */
107 locationUri: string | undefined;
108 /** User Hive parameters (Alchemy ownership keys stripped). */
109 parameters: Record<string, string>;
110 /** RFC3339 creation timestamp. */
111 createTime: string | undefined;
112 /** RFC3339 last-update timestamp. */
113 updateTime: string | undefined;
114 /** RFC3339 deletion timestamp, if soft-deleted. */
115 deleteTime: string | undefined;
116 /** RFC3339 expire timestamp after delete. */
117 expireTime: string | undefined;
118 },
119 never,
120 Providers
121 >;
122
123 /**
124 * A Hive database inside a BigLake Metastore catalog.
125 *
126 * Databases have no labels field — Alchemy stamps ownership into
127 * `hiveOptions.parameters` so `list` / nuke can find them. Catalog,
128 * database id, and type are immutable. Hive location URI and parameters
129 * update in place.
130 *
131 * ### Creating a Database
132 * **Example:** Hive database on a GCS prefix
133 * ```typescript
134 * const catalog = yield* GCP.BigLake.Catalog("Lake", {});
135 * const database = yield* GCP.BigLake.CatalogsDatabase("Warehouse", {
136 * catalog: catalog.name,
137 * locationUri: `gs://${bucket.bucketName}/warehouse`,
138 * parameters: { owner: "analytics" },
139 * });
140 * ```
141 *
142 * **Example:** Named database
143 * ```typescript
144 * const database = yield* GCP.BigLake.CatalogsDatabase("Warehouse", {
145 * catalog: catalog.name,
146 * databaseId: "sales",
147 * type: "HIVE",
148 * hiveOptions: {
149 * locationUri: "gs://my-bucket/sales",
150 * },
151 * });
152 * ```
153 *
154 * @resource
155 * @category BigLake
156 */
157 export const CatalogsDatabase = Resource<CatalogsDatabase>("GCP.BigLake.CatalogsDatabase");
158
159 const catalogOf = (catalog: string, project: string, location: string) =>
160 expandCatalog(catalog, project, location);
161
162 const resourceName = (catalog: string, databaseId: string) => `${catalog}/databases/${databaseId}`;
163
164 const parametersOf = (database: biglake.Database) => userLabels(database.hiveOptions?.parameters);
165
166 const toAttrs = (database: biglake.Database, project: string) => {
167 const name = database.name ?? "";
168 const parsed = parseResourceName(name, "databases");
169 return {
170 name,
171 databaseId: parsed.id,
172 catalog: parsed.parent,
173 project,
174 location: parsed.location,
175 type: database.type,
176 locationUri: database.hiveOptions?.locationUri,
177 parameters: parametersOf(database),
178 createTime: database.createTime,
179 updateTime: database.updateTime,
180 deleteTime: database.deleteTime,
181 expireTime: database.expireTime,
182 };
183 };
184
185 const getByName = missingGet(biglake.getProjectsLocationsCatalogsDatabases);
186
187 const desiredHiveOptions = (
188 news: CatalogsDatabaseProps,
189 parameters: Record<string, string>,
190 ): biglake.HiveDatabaseOptions => ({
191 locationUri: news.hiveOptions?.locationUri ?? news.locationUri,
192 parameters,
193 });
194
195 const emptyAndDeleteDatabase = (name: string) =>
196 Effect.gen(function* () {
197 const tables = yield* listTables(name);
198 yield* Effect.forEach(
199 namedOf(tables),
200 (table) =>
201 ignoreGone(
202 biglake.deleteProjectsLocationsCatalogsDatabasesTables({
203 name: table.name!,
204 }),
205 ),
206 { concurrency: 4 },
207 );
208 yield* ignoreGone(biglake.deleteProjectsLocationsCatalogsDatabases({ name }));
209 });
210
211 export const CatalogsDatabaseProvider = () =>
212 Provider.succeed(CatalogsDatabase, {
213 stables: ["name", "databaseId", "catalog", "project", "location", "createTime"],
214
215 diff: Effect.fn(function* ({ news, olds, output }) {
216 if (!isResolved(news)) return undefined;
217 const env = yield* GcpEnvironment.current;
218 const location = normalizeLocation(
219 news.location ?? olds?.location ?? output?.location,
220 env.region,
221 );
222 const typeChanged =
223 (olds?.type ?? output?.type) !== undefined &&
224 (news.type ?? "HIVE") !== (olds?.type ?? output?.type);
225 if (typeChanged) {
226 return { action: "replace" as const, deleteFirst: true };
227 }
228 return replaceOnIdentity({
229 previousId: olds?.databaseId ?? output?.databaseId,
230 nextId: news.databaseId,
231 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
232 nextLocation: location,
233 previousParent: olds?.catalog ?? output?.catalog,
234 nextParent: catalogOf(news.catalog, env.project, location),
235 });
236 }),
237
238 read: Effect.fn(function* ({ id, olds, output, previous }) {
239 const env = yield* GcpEnvironment.current;
240 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
241 const databaseId = yield* toPhysicalId(id, olds?.databaseId, output?.databaseId);
242 const catalog =
243 olds?.catalog !== undefined
244 ? catalogOf(olds.catalog, env.project, location)
245 : (output?.catalog ?? "");
246 const name = output?.name ?? (catalog.length > 0 ? resourceName(catalog, databaseId) : "");
247 const existing = yield* getByName(name);
248 if (existing === undefined) return undefined;
249 const attrs = toAttrs(existing, env.project);
250 return (yield* hasAlchemyLabels(id, tagRecord(existing.hiveOptions?.parameters)))
251 ? attrs
252 : unownedUnlessPrevious(attrs, previous, "name");
253 }),
254
255 list: () =>
256 Effect.gen(function* () {
257 const env = yield* GcpEnvironment.current;
258 const catalogs = (yield* Effect.forEach(listLocations(env.region), (location) =>
259 listCatalogs(locationParent(env.project, location)),
260 )).flat();
261 const databases = yield* listChildResources(namedOf(catalogs), listDatabases);
262 return databases
263 .filter((item) => hasAlchemyLabelMap(item.hiveOptions?.parameters))
264 .map((item) => toAttrs(item, env.project));
265 }),
266
267 reconcile: Effect.fn(function* ({ id, news, output }) {
268 const env = yield* GcpEnvironment.current;
269 const location = normalizeLocation(news.location ?? output?.location, env.region);
270 const catalog = catalogOf(news.catalog, env.project, location);
271 const databaseId = yield* toPhysicalId(id, news.databaseId, output?.databaseId);
272 const name = output?.name ?? resourceName(catalog, databaseId);
273 const type = news.type ?? "HIVE";
274 const parameters = mergeParameters(
275 news.parameters ?? news.hiveOptions?.parameters,
276 yield* createInternalLabels(id),
277 );
278 const hiveOptions = desiredHiveOptions(news, parameters);
279 const body: biglake.Database = { type, hiveOptions };
280
281 let current = yield* getByName(name);
282
283 if (current === undefined) {
284 const created = yield* retryTransient(
285 biglake.createProjectsLocationsCatalogsDatabases({
286 parent: catalog,
287 databaseId,
288 body,
289 }),
290 ).pipe(Effect.catchTag("Conflict", () => getByName(name)));
291 current = created ?? undefined;
292 }
293
294 if (current === undefined) {
295 return yield* new BiglakeNotResolved({ resourceName: name });
296 }
297
298 const currentName = current.name ?? name;
299 const typeChanged = !sameText(current.type, type);
300 const hiveChanged = !sameJson(current.hiveOptions, hiveOptions);
301
302 if (typeChanged || hiveChanged) {
303 current = yield* retryTransient(
304 biglake.patchProjectsLocationsCatalogsDatabases({
305 name: currentName,
306 updateMask: updateMaskOf(
307 typeChanged ? "type" : undefined,
308 hiveChanged ? "hiveOptions" : undefined,
309 ),
310 body: { name: currentName, ...body },
311 }),
312 );
313 }
314
315 return toAttrs(current, env.project);
316 }),
317
318 delete: Effect.fn(function* ({ output }) {
319 if (!output.name) return;
320 yield* emptyAndDeleteDatabase(output.name);
321 yield* waitUntilGone(getByName(output.name));
322 }),
323 });