@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 294 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 Data from "effect/Data";
8 import * as Effect from "effect/Effect";
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 expandParent,
14 hasAlchemyLabelMap,
15 listGlossaries,
16 listTerms,
17 normalizeLocation,
18 parseResourceName,
19 replaceIfChanged,
20 toPhysicalRfc1035,
21 userLabels,
22 } from "./shared.ts";
23
24 export type GlossariesTermProps = {
25 /**
26 * Parent glossary. Full name
27 * `projects/{project}/locations/{location}/glossaries/{glossary}` or the
28 * glossary id (combined with `location`). Immutable — changing it
29 * replaces the term.
30 */
31 glossary: string;
32 /**
33 * Immediate parent in the glossary tree — the glossary itself or a
34 * category
35 * `.../glossaries/{glossary}/categories/{category}`. Defaults to
36 * `glossary`.
37 */
38 parent?: string;
39 /**
40 * Region used when `glossary` is a bare id.
41 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
42 */
43 location?: string;
44 /**
45 * Term id (the `{term}` segment of
46 * `.../glossaries/{glossary}/terms/{term}`). If omitted, a unique name
47 * is generated from the stack, stage, and logical id. Immutable —
48 * changing it replaces the term.
49 */
50 termId?: string;
51 /**
52 * User-friendly display name. Defaults to the term id.
53 */
54 displayName?: string;
55 /**
56 * Rich-text description attached to catalog entries.
57 */
58 description?: string;
59 /**
60 * User labels. Alchemy ownership labels are merged in automatically.
61 */
62 labels?: Record<string, string>;
63 };
64
65 export type GlossariesTerm = Resource<
66 "GCP.Dataplex.GlossariesTerm",
67 GlossariesTermProps,
68 {
69 /** Full resource name `.../glossaries/{glossary}/terms/{term}`. */
70 name: string;
71 /** Term id (last path segment). */
72 termId: string;
73 /** Parent glossary resource name. */
74 glossary: string;
75 /** Immediate parent (glossary or category). */
76 parent: string;
77 /** Project id. */
78 project: string;
79 /** Location id. */
80 location: string;
81 /** User-friendly display name. */
82 displayName: string | undefined;
83 /** Description. */
84 description: string | undefined;
85 /** User labels (Alchemy ownership labels stripped). */
86 labels: Record<string, string>;
87 /** Server-assigned uid. */
88 uid: string | undefined;
89 /** RFC3339 creation timestamp. */
90 createTime: string | undefined;
91 /** RFC3339 last-update timestamp. */
92 updateTime: string | undefined;
93 },
94 never,
95 Providers
96 >;
97
98 /**
99 * A Dataplex glossary term — a defined business concept that can be
100 * attached to catalog entries.
101 *
102 * Changing `glossary`, `termId`, or `location` replaces the term. Display
103 * name, description, labels, and immediate parent update in place.
104 *
105 * ### Creating a Term
106 * **Example:** Term under a glossary
107 * ```typescript
108 * const term = yield* GCP.Dataplex.GlossariesTerm("Customer", {
109 * glossary: glossary.name,
110 * displayName: "Customer",
111 * description: "a paying account",
112 * labels: { env: "dev" },
113 * });
114 * ```
115 *
116 * **Example:** Named term
117 * ```typescript
118 * const term = yield* GCP.Dataplex.GlossariesTerm("Customer", {
119 * glossary: glossary.name,
120 * termId: "customer",
121 * labels: { env: "prod" },
122 * });
123 * ```
124 *
125 * @resource
126 * @category Dataplex
127 */
128 export const GlossariesTerm = Resource<GlossariesTerm>("GCP.Dataplex.GlossariesTerm");
129
130 export class GlossariesTermNotResolved extends Data.TaggedError(
131 "GCP.Dataplex.GlossariesTermNotResolved",
132 )<{
133 resourceName: string;
134 }> {}
135
136 const glossaryOf = (glossary: string, project: string, location: string) =>
137 expandParent(glossary, project, location, "glossaries");
138
139 const resourceName = (glossary: string, termId: string) => `${glossary}/terms/${termId}`;
140
141 const toAttrs = (term: dataplex.GoogleCloudDataplexV1GlossaryTerm, project: string) => {
142 const name = term.name ?? "";
143 const parsed = parseResourceName(name, "terms");
144 const glossary = parseResourceName(name, "glossaries");
145 return {
146 name,
147 termId: parsed.id,
148 glossary: glossary.parent ? `${glossary.parent}/glossaries/${glossary.id}` : parsed.parent,
149 parent: term.parent ?? parsed.parent,
150 project: parsed.project || project,
151 location: parsed.location,
152 displayName: term.displayName,
153 description: term.description,
154 labels: userLabels(term.labels),
155 uid: term.uid,
156 createTime: term.createTime,
157 updateTime: term.updateTime,
158 };
159 };
160
161 const getByName = (name: string) =>
162 dataplex
163 .getProjectsLocationsGlossariesTerms({ name })
164 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
165
166 export const GlossariesTermProvider = () =>
167 Provider.succeed(GlossariesTerm, {
168 stables: ["name", "termId", "glossary", "project", "location", "uid", "createTime"],
169
170 diff: Effect.fn(function* ({ news, olds, output }) {
171 if (!isResolved(news)) return undefined;
172 const env = yield* GcpEnvironment.current;
173 const previousId = olds?.termId ?? output?.termId;
174 const nextId = news.termId ?? previousId;
175 const previousGlossary = olds?.glossary ?? output?.glossary;
176 const nextGlossary = news.glossary ?? previousGlossary;
177 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
178 const nextLocation = normalizeLocation(
179 news.location ?? olds?.location ?? output?.location,
180 env.region,
181 );
182 if (
183 replaceIfChanged(previousId, nextId) ||
184 replaceIfChanged(previousGlossary, nextGlossary) ||
185 (output !== undefined && previousLocation !== nextLocation)
186 ) {
187 return {
188 action: "replace" as const,
189 deleteFirst:
190 previousGlossary === nextGlossary &&
191 previousLocation === nextLocation &&
192 previousId !== undefined &&
193 nextId === previousId,
194 };
195 }
196 return undefined;
197 }),
198
199 read: Effect.fn(function* ({ id, olds, output }) {
200 const env = yield* GcpEnvironment.current;
201 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
202 const glossary = glossaryOf(olds?.glossary ?? output?.glossary ?? "", env.project, location);
203 const termId = yield* toPhysicalRfc1035(id, olds?.termId, output?.termId);
204 const name = output?.name ?? resourceName(glossary, termId);
205 const existing = yield* getByName(name);
206 if (existing === undefined) return undefined;
207 const attrs = toAttrs(existing, env.project);
208 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
209 }),
210
211 list: () =>
212 Effect.gen(function* () {
213 const env = yield* GcpEnvironment.current;
214 const glossaries = yield* listGlossaries(env.project, env.region);
215 const terms = yield* Effect.forEach(
216 glossaries.filter((glossary) => (glossary.name ?? "").length > 0),
217 (glossary) => listTerms(glossary.name!),
218 { concurrency: 4 },
219 );
220 return terms
221 .flat()
222 .filter((term) => hasAlchemyLabelMap(term.labels))
223 .map((term) => toAttrs(term, env.project));
224 }),
225
226 reconcile: Effect.fn(function* ({ id, news, output }) {
227 const env = yield* GcpEnvironment.current;
228 const location = normalizeLocation(news.location ?? output?.location, env.region);
229 const glossary = glossaryOf(news.glossary, env.project, location);
230 const termId = yield* toPhysicalRfc1035(id, news.termId, output?.termId);
231 const name = output?.name ?? resourceName(glossary, termId);
232 const immediateParent = news.parent ?? glossary;
233 const desiredLabels = {
234 ...toLabels(news.labels),
235 ...(yield* createInternalLabels(id)),
236 };
237
238 let current = yield* getByName(name);
239
240 if (current === undefined) {
241 const created = yield* dataplex
242 .createProjectsLocationsGlossariesTerms({
243 parent: glossary,
244 termId,
245 body: {
246 parent: immediateParent,
247 displayName: news.displayName,
248 description: news.description,
249 labels: desiredLabels,
250 },
251 })
252 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
253 current = created ?? undefined;
254 }
255
256 if (current === undefined) {
257 return yield* new GlossariesTermNotResolved({ resourceName: name });
258 }
259
260 const observedLabels = tagRecord(current.labels);
261 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
262 const labelsChanged = upsert.length > 0 || removed.length > 0;
263 const displayNameChanged = (current.displayName ?? "") !== (news.displayName ?? "");
264 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
265 const parentChanged = (current.parent ?? "") !== immediateParent;
266
267 if (labelsChanged || displayNameChanged || descriptionChanged || parentChanged) {
268 const updateMask = [
269 labelsChanged ? "labels" : undefined,
270 displayNameChanged ? "display_name" : undefined,
271 descriptionChanged ? "description" : undefined,
272 parentChanged ? "parent" : undefined,
273 ].filter((field): field is string => field !== undefined);
274 current = yield* dataplex.patchProjectsLocationsGlossariesTerms({
275 name: current.name ?? name,
276 updateMask: updateMask.join(","),
277 body: {
278 parent: immediateParent,
279 displayName: news.displayName,
280 description: news.description,
281 labels: desiredLabels,
282 },
283 });
284 }
285
286 return toAttrs(current, env.project);
287 }),
288
289 delete: Effect.fn(function* ({ output }) {
290 yield* dataplex
291 .deleteProjectsLocationsGlossariesTerms({ name: output.name })
292 .pipe(Effect.catchTag("NotFound", () => Effect.void));
293 }),
294 });