@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 301 lines 10.1 KB
Raw
1 import * as dataplex from "@distilled.cloud/gcp/dataplex_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 { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 DataplexNotResolved,
14 collectPages,
15 hasAlchemyLabelMap,
16 listAtLocation,
17 normalizeLocation,
18 parentOf,
19 parseName,
20 replaceOnIdentity,
21 retryQuota,
22 toPhysicalId,
23 userLabels,
24 waitForOperation,
25 waitUntilExists,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type GlossaryProps = {
30 /**
31 * Glossary id (the `{glossary}` segment of
32 * `projects/{project}/locations/{location}/glossaries/{glossary}`).
33 * If omitted, a unique name is generated from the stack, stage, and
34 * logical id. Must be 1-63 characters, start with a letter, and match
35 * `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable — changing it replaces the
36 * glossary.
37 */
38 glossaryId?: string;
39 /**
40 * Region (`us-central1`, `us-east1`, …). Immutable — changing it
41 * replaces the glossary. `US-CENTRAL1` is accepted and normalized to
42 * `us-central1`.
43 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
44 */
45 location?: string;
46 /**
47 * Human-readable description.
48 */
49 description?: string;
50 /**
51 * User-friendly display name.
52 */
53 displayName?: string;
54 /**
55 * User labels. Alchemy ownership labels are merged in automatically.
56 */
57 labels?: Record<string, string>;
58 };
59
60 export type Glossary = Resource<
61 "GCP.Dataplex.Glossary",
62 GlossaryProps,
63 {
64 /** Full resource name. */
65 name: string;
66 /** Glossary id (last path segment). */
67 glossaryId: string;
68 /** Project id. */
69 project: string;
70 /** Location id. */
71 location: string;
72 /** User-provided description. */
73 description: string | undefined;
74 /** User-friendly display name. */
75 displayName: string | undefined;
76 /** User labels (Alchemy ownership labels stripped). */
77 labels: Record<string, string>;
78 /** Number of terms. */
79 termCount: number | undefined;
80 /** Number of categories. */
81 categoryCount: number | undefined;
82 /** Server etag. */
83 etag: string | undefined;
84 /** System-generated uid. */
85 uid: string | undefined;
86 /** RFC3339 creation timestamp. */
87 createTime: string | undefined;
88 /** RFC3339 last-update timestamp. */
89 updateTime: string | undefined;
90 },
91 never,
92 Providers
93 >;
94
95 /**
96 * A Dataplex Glossary — a collection of GlossaryCategories and
97 * GlossaryTerms.
98 *
99 * Changing `glossaryId` or `location` replaces the glossary. Description,
100 * display name, and labels update in place. Nested categories and terms
101 * must be deleted before the glossary.
102 *
103 * ### Creating a Glossary
104 * **Example:** Generated name
105 * ```typescript
106 * const glossary = yield* GCP.Dataplex.Glossary("BusinessTerms", {});
107 * ```
108 *
109 * **Example:** Explicit id and labels
110 * ```typescript
111 * const glossary = yield* GCP.Dataplex.Glossary("BusinessTerms", {
112 * glossaryId: "app-glossary",
113 * displayName: "App glossary",
114 * labels: { env: "prod" },
115 * });
116 * ```
117 *
118 * ### Updating a Glossary
119 * **Example:** Description and labels
120 * ```typescript
121 * // Same logical id as before; only the changed props differ.
122 * const glossary = yield* GCP.Dataplex.Glossary("BusinessTerms", {
123 * glossaryId: "app-glossary",
124 * description: "glossary v2",
125 * labels: { env: "prod", team: "data" },
126 * });
127 * ```
128 *
129 * @resource
130 * @category Dataplex
131 */
132 export const Glossary = Resource<Glossary>("GCP.Dataplex.Glossary");
133
134 const resourceName = (project: string, location: string, glossaryId: string) =>
135 `projects/${project}/locations/${location}/glossaries/${glossaryId}`;
136
137 const toAttrs = (glossary: dataplex.GoogleCloudDataplexV1Glossary, project: string) => {
138 const name = glossary.name ?? "";
139 const parsed = parseName(name, "glossaries");
140 return {
141 name,
142 glossaryId: parsed.id,
143 project: parsed.project || project,
144 location: parsed.location,
145 description: glossary.description,
146 displayName: glossary.displayName,
147 labels: userLabels(glossary.labels),
148 termCount: glossary.termCount,
149 categoryCount: glossary.categoryCount,
150 etag: glossary.etag,
151 uid: glossary.uid,
152 createTime: glossary.createTime,
153 updateTime: glossary.updateTime,
154 };
155 };
156
157 const getByName = (name: string) =>
158 retryQuota(dataplex.getProjectsLocationsGlossaries({ name })).pipe(
159 Effect.catchTag("NotFound", () => Effect.undefined),
160 );
161
162 const listGlossaries = (project: string, region: string) => {
163 const collect = (parent: string) =>
164 collectPages(
165 dataplex.listProjectsLocationsGlossaries.pages({
166 parent,
167 pageSize: 1000,
168 }),
169 (page) => page.glossaries,
170 ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels))));
171 return listAtLocation(project, region, collect);
172 };
173
174 export const listAlchemyGlossaries = (project: string, region: string) =>
175 listGlossaries(project, region);
176
177 export const GlossaryProvider = () =>
178 Provider.succeed(Glossary, {
179 stables: ["name", "glossaryId", "project", "location", "createTime"],
180
181 diff: Effect.fn(function* ({ news, olds, output }) {
182 if (!isResolved(news)) return undefined;
183 const env = yield* GcpEnvironment.current;
184 return replaceOnIdentity({
185 previousId: olds?.glossaryId ?? output?.glossaryId,
186 nextId: news.glossaryId ?? olds?.glossaryId ?? output?.glossaryId,
187 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
188 nextLocation: normalizeLocation(
189 news.location ?? olds?.location ?? output?.location,
190 env.region,
191 ),
192 });
193 }),
194
195 read: Effect.fn(function* ({ id, olds, output }) {
196 const env = yield* GcpEnvironment.current;
197 const glossaryId = yield* toPhysicalId(id, olds?.glossaryId, output?.glossaryId, "glossary");
198 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
199 const name = output?.name ?? resourceName(env.project, location, glossaryId);
200 const existing = yield* getByName(name);
201 if (existing === undefined) return undefined;
202 const attrs = toAttrs(existing, env.project);
203 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
204 }),
205
206 list: () =>
207 Effect.gen(function* () {
208 const env = yield* GcpEnvironment.current;
209 const items = yield* listGlossaries(env.project, env.region);
210 return items.map((item) => toAttrs(item, env.project));
211 }),
212
213 reconcile: Effect.fn(function* ({ id, news, output }) {
214 const env = yield* GcpEnvironment.current;
215 const glossaryId = yield* toPhysicalId(id, news.glossaryId, output?.glossaryId, "glossary");
216 const location = normalizeLocation(news.location ?? output?.location, env.region);
217 const name = resourceName(env.project, location, glossaryId);
218 const desiredLabels = {
219 ...toLabels(news.labels),
220 ...(yield* createInternalLabels(id)),
221 };
222
223 let current = yield* getByName(output?.name ?? name);
224
225 if (current === undefined) {
226 const created = yield* dataplex
227 .createProjectsLocationsGlossaries({
228 parent: parentOf(env.project, location),
229 glossaryId,
230 body: {
231 description: news.description,
232 displayName: news.displayName,
233 labels: desiredLabels,
234 },
235 })
236 .pipe(
237 retryQuota,
238 Effect.catchTag("Conflict", () => Effect.undefined),
239 );
240 if (created !== undefined) {
241 yield* waitForOperation(created);
242 }
243 current = yield* waitUntilExists(getByName(name), name);
244 }
245
246 if (current === undefined) {
247 return yield* new DataplexNotResolved({ resourceName: name });
248 }
249
250 const observedLabels = tagRecord(current.labels);
251 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
252 const labelsChanged = upsert.length > 0 || removed.length > 0;
253 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
254 const displayNameChanged = (current.displayName ?? "") !== (news.displayName ?? "");
255
256 if (labelsChanged || descriptionChanged || displayNameChanged) {
257 const operation = yield* retryQuota(
258 dataplex.patchProjectsLocationsGlossaries({
259 name: current.name ?? name,
260 updateMask: [
261 labelsChanged ? "labels" : undefined,
262 descriptionChanged ? "description" : undefined,
263 displayNameChanged ? "displayName" : undefined,
264 ]
265 .filter((field): field is string => field !== undefined)
266 .join(","),
267 body: {
268 name: current.name ?? name,
269 etag: current.etag,
270 labels: desiredLabels,
271 description: news.description,
272 displayName: news.displayName,
273 },
274 }),
275 );
276 yield* waitForOperation(operation);
277 current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name);
278 }
279
280 return toAttrs(current, env.project);
281 }),
282
283 delete: Effect.fn(function* ({ output }) {
284 const operation = yield* dataplex
285 .deleteProjectsLocationsGlossaries({
286 name: output.name,
287 })
288 .pipe(
289 Effect.retry({
290 while: (error) => error._tag === "Conflict" || error._tag === "TooManyRequests",
291 times: 8,
292 schedule: Schedule.spaced("2 seconds"),
293 }),
294 Effect.catchTag("NotFound", () => Effect.undefined),
295 );
296 if (operation !== undefined) {
297 yield* waitForOperation(operation, { notFoundOk: true });
298 }
299 yield* waitUntilGone(getByName(output.name), output.name);
300 }),
301 });