@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, 04:45 UTC

main
ts 412 lines 12.8 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 * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import {
10 DataplexNotResolved,
11 RELATED_ENTRY_LINK_TYPE,
12 collectPages,
13 expandParent,
14 fingerprint,
15 hasAlchemyLabelMap,
16 lastSegment,
17 listAtLocation,
18 normalizeLocation,
19 parseName,
20 replaceOnIdentity,
21 retryQuota,
22 toPhysicalId,
23 waitUntilExists,
24 waitUntilGone,
25 } from "./internal.ts";
26
27 export type EntryLinkReference = {
28 /**
29 * Referenced Entry resource name
30 * `projects/{project}/locations/{location}/entryGroups/{entryGroup}/entries/{entry}`.
31 * Immutable.
32 */
33 name: string;
34 /**
35 * Path in the Entry. Empty means the Entry itself. Immutable.
36 */
37 path?: string;
38 /**
39 * Reference type (`SOURCE` or `TARGET`). Immutable.
40 */
41 type?: dataplex.GoogleCloudDataplexV1EntryLinkEntryReferenceTypeEnum | (string & {});
42 };
43
44 export type EntryGroupsEntryLinkProps = {
45 /**
46 * Parent Entry Group. Full name
47 * `projects/{project}/locations/{location}/entryGroups/{entryGroup}`
48 * or the entry-group id (combined with `location`). Immutable —
49 * changing it replaces the link.
50 */
51 entryGroup: string;
52 /**
53 * Entry link id. If omitted, a unique name is generated from the
54 * stack, stage, and logical id. Must be 1-63 characters, start with
55 * a letter, and match `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable —
56 * changing it replaces the link.
57 */
58 entryLinkId?: string;
59 /**
60 * Region used when `entryGroup` is a bare id. Immutable — changing it
61 * replaces the link.
62 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
63 */
64 location?: string;
65 /**
66 * Entry link type resource name. Immutable — changing it replaces
67 * the link.
68 * @default "projects/dataplex-types/locations/global/entryLinkTypes/related"
69 */
70 entryLinkType?: string;
71 /**
72 * Exactly two entry references (`SOURCE` and `TARGET`). Immutable —
73 * changing them replaces the link.
74 */
75 entryReferences: EntryLinkReference[];
76 /**
77 * Aspects attached to the link, keyed by
78 * `{project}.{location}.{aspectType}`.
79 */
80 aspects?: dataplex.GoogleCloudDataplexV1AspectMap;
81 };
82
83 export type EntryGroupsEntryLink = Resource<
84 "GCP.Dataplex.EntryGroupsEntryLink",
85 EntryGroupsEntryLinkProps,
86 {
87 /** Full resource name. */
88 name: string;
89 /** Entry link id (last path segment). */
90 entryLinkId: string;
91 /** Parent entry group resource name. */
92 entryGroup: string;
93 /** Parent entry group id. */
94 entryGroupId: string;
95 /** Project id. */
96 project: string;
97 /** Location id. */
98 location: string;
99 /** Entry link type resource name. */
100 entryLinkType: string | undefined;
101 /** Referenced entries. */
102 entryReferences: EntryLinkReference[];
103 /** Aspects attached to the link. */
104 aspects: dataplex.GoogleCloudDataplexV1AspectMap | undefined;
105 /** RFC3339 creation timestamp. */
106 createTime: string | undefined;
107 /** RFC3339 last-update timestamp. */
108 updateTime: string | undefined;
109 },
110 never,
111 Providers
112 >;
113
114 /**
115 * A Dataplex Entry Link between two Catalog Entries.
116 *
117 * Entry links have no labels field. `list` finds links by looking up
118 * entries in alchemy-labeled entry groups. Changing `entryLinkId`,
119 * `entryGroup`, `location`, `entryLinkType`, or `entryReferences`
120 * replaces the link. Aspects update in place.
121 *
122 * ### Creating an Entry Link
123 * **Example:** Related entries
124 * ```typescript
125 * const link = yield* GCP.Dataplex.EntryGroupsEntryLink("Related", {
126 * entryGroup: group.name,
127 * entryReferences: [
128 * { name: left.name, type: "SOURCE" },
129 * { name: right.name, type: "TARGET" },
130 * ],
131 * });
132 * ```
133 *
134 * @resource
135 * @category Dataplex
136 */
137 export const EntryGroupsEntryLink = Resource<EntryGroupsEntryLink>(
138 "GCP.Dataplex.EntryGroupsEntryLink",
139 );
140
141 const resolveParent = (
142 project: string,
143 entryGroup: string,
144 location: string | undefined,
145 defaultLocation: string,
146 ) => {
147 const parent = expandParent(
148 entryGroup,
149 project,
150 normalizeLocation(location, defaultLocation),
151 "entryGroups",
152 );
153 const parsed = parseName(`${parent}/entryLinks/_`, "entryLinks");
154 return {
155 parent: parsed.parent,
156 location: parsed.location,
157 project: parsed.project || project,
158 entryGroupId: lastSegment(parsed.parent),
159 };
160 };
161
162 const resourceName = (parent: string, entryLinkId: string) => `${parent}/entryLinks/${entryLinkId}`;
163
164 const linkTypeOf = (value: string | undefined) =>
165 value && value.length > 0 ? value : RELATED_ENTRY_LINK_TYPE;
166
167 const refsOf = (
168 refs:
169 | readonly dataplex.GoogleCloudDataplexV1EntryLinkEntryReference[]
170 | readonly EntryLinkReference[]
171 | undefined,
172 ): EntryLinkReference[] =>
173 (refs ?? []).flatMap((ref) =>
174 ref.name === undefined
175 ? []
176 : [
177 {
178 name: ref.name,
179 path: ref.path,
180 type: ref.type,
181 },
182 ],
183 );
184
185 const refsKey = (refs: readonly EntryLinkReference[] | undefined) =>
186 fingerprint(
187 refsOf(refs)
188 .map((ref) => ({
189 name: ref.name,
190 path: ref.path ?? "",
191 type: (ref.type ?? "").toUpperCase(),
192 }))
193 .sort((left, right) => left.name.localeCompare(right.name)),
194 );
195
196 const toAttrs = (link: dataplex.GoogleCloudDataplexV1EntryLink, project: string) => {
197 const name = link.name ?? "";
198 const parsed = parseName(name, "entryLinks");
199 const group = parseName(parsed.parent, "entryGroups");
200 return {
201 name,
202 entryLinkId: parsed.id,
203 entryGroup: parsed.parent,
204 entryGroupId: group.id,
205 project: parsed.project || project,
206 location: parsed.location,
207 entryLinkType: link.entryLinkType,
208 entryReferences: refsOf(link.entryReferences),
209 aspects: link.aspects,
210 createTime: link.createTime,
211 updateTime: link.updateTime,
212 };
213 };
214
215 const getByName = (name: string) =>
216 retryQuota(dataplex.getProjectsLocationsEntryGroupsEntryLinks({ name })).pipe(
217 Effect.catchTag("NotFound", () => Effect.undefined),
218 );
219
220 const getGroup = (name: string) =>
221 dataplex
222 .getProjectsLocationsEntryGroups({ name })
223 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
224
225 const lookupLinks = (locationParent: string, entry: string, project: string) =>
226 collectPages(
227 dataplex.lookupEntryLinksProjectsLocations.pages({
228 name: locationParent,
229 entry,
230 pageSize: 10,
231 }),
232 (page) => page.entryLinks,
233 ).pipe(
234 Effect.map((items) => items.map((item) => toAttrs(item, project))),
235 Effect.catchTag("NotFound", () => Effect.succeed([])),
236 );
237
238 export const EntryGroupsEntryLinkProvider = () =>
239 Provider.succeed(EntryGroupsEntryLink, {
240 stables: [
241 "name",
242 "entryLinkId",
243 "entryGroup",
244 "entryGroupId",
245 "project",
246 "location",
247 "entryLinkType",
248 "createTime",
249 ],
250
251 diff: Effect.fn(function* ({ news, olds, output }) {
252 if (!isResolved(news)) return undefined;
253 const env = yield* GcpEnvironment.current;
254 const previousType = linkTypeOf(olds?.entryLinkType ?? output?.entryLinkType);
255 const nextType = linkTypeOf(news.entryLinkType ?? previousType);
256 const previousRefs = refsKey(olds?.entryReferences ?? output?.entryReferences);
257 const nextRefs = refsKey(news.entryReferences ?? olds?.entryReferences);
258 return replaceOnIdentity({
259 previousId: olds?.entryLinkId ?? output?.entryLinkId,
260 nextId: news.entryLinkId ?? olds?.entryLinkId ?? output?.entryLinkId,
261 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
262 nextLocation: normalizeLocation(
263 news.location ?? olds?.location ?? output?.location,
264 env.region,
265 ),
266 previousParent: olds?.entryGroup ?? output?.entryGroup,
267 nextParent: news.entryGroup ?? olds?.entryGroup ?? output?.entryGroup,
268 extra: nextType !== previousType || nextRefs !== previousRefs,
269 });
270 }),
271
272 read: Effect.fn(function* ({ id, olds, output }) {
273 const env = yield* GcpEnvironment.current;
274 const resolved = resolveParent(
275 env.project,
276 olds?.entryGroup ?? output?.entryGroup ?? "",
277 olds?.location ?? output?.location,
278 env.region,
279 );
280 const entryLinkId = yield* toPhysicalId(
281 id,
282 olds?.entryLinkId,
283 output?.entryLinkId,
284 "entrylink",
285 );
286 const name = output?.name ?? resourceName(resolved.parent, entryLinkId);
287 const existing = yield* getByName(name);
288 if (existing === undefined) return undefined;
289 const attrs = toAttrs(existing, env.project);
290 const group = yield* getGroup(attrs.entryGroup);
291 if (group === undefined) return Unowned(attrs);
292 return hasAlchemyLabelMap(group.labels) ? attrs : Unowned(attrs);
293 }),
294
295 list: () =>
296 Effect.gen(function* () {
297 const env = yield* GcpEnvironment.current;
298 const groups = yield* listAtLocation(env.project, env.region, (parent) =>
299 collectPages(
300 dataplex.listProjectsLocationsEntryGroups.pages({
301 parent,
302 pageSize: 1000,
303 }),
304 (page) => page.entryGroups,
305 ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))),
306 );
307 const seen = new Set<string>();
308 const found: ReturnType<typeof toAttrs>[] = [];
309 yield* Effect.forEach(
310 groups,
311 (group) =>
312 Effect.gen(function* () {
313 if (group.name === undefined) return;
314 const entries = yield* collectPages(
315 dataplex.listProjectsLocationsEntryGroupsEntries.pages({
316 parent: group.name,
317 pageSize: 100,
318 }),
319 (page) => page.entries,
320 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([])));
321 const locationParent = `projects/${env.project}/locations/${parseName(group.name, "entryGroups").location}`;
322 const pages = yield* Effect.forEach(
323 entries,
324 (entry) =>
325 entry.name
326 ? lookupLinks(locationParent, entry.name, env.project)
327 : Effect.succeed([]),
328 { concurrency: 4 },
329 );
330 for (const link of pages.flat()) {
331 if (seen.has(link.name)) continue;
332 seen.add(link.name);
333 found.push(link);
334 }
335 }),
336 { concurrency: 2 },
337 );
338 return found;
339 }),
340
341 reconcile: Effect.fn(function* ({ id, news, output }) {
342 const env = yield* GcpEnvironment.current;
343 const resolved = resolveParent(
344 env.project,
345 news.entryGroup,
346 news.location ?? output?.location,
347 env.region,
348 );
349 const entryLinkId = yield* toPhysicalId(
350 id,
351 news.entryLinkId,
352 output?.entryLinkId,
353 "entrylink",
354 );
355 const name = resourceName(resolved.parent, entryLinkId);
356 const entryLinkType = linkTypeOf(news.entryLinkType);
357 const entryReferences = refsOf(news.entryReferences);
358
359 let current = yield* getByName(output?.name ?? name);
360
361 if (current === undefined) {
362 const created = yield* dataplex
363 .createProjectsLocationsEntryGroupsEntryLinks({
364 parent: resolved.parent,
365 entryLinkId,
366 body: {
367 entryLinkType,
368 entryReferences,
369 aspects: news.aspects,
370 },
371 })
372 .pipe(
373 retryQuota,
374 Effect.catchTag("Conflict", () => getByName(name)),
375 );
376 current = created ?? undefined;
377 if (current === undefined) {
378 current = yield* waitUntilExists(getByName(name), name);
379 }
380 }
381
382 if (current === undefined) {
383 return yield* new DataplexNotResolved({ resourceName: name });
384 }
385
386 const aspectsChanged =
387 news.aspects !== undefined && fingerprint(news.aspects) !== fingerprint(current.aspects);
388
389 if (aspectsChanged) {
390 current = yield* retryQuota(
391 dataplex.patchProjectsLocationsEntryGroupsEntryLinks({
392 name: current.name ?? name,
393 body: {
394 name: current.name ?? name,
395 aspects: news.aspects,
396 },
397 }),
398 );
399 }
400
401 return toAttrs(current, env.project);
402 }),
403
404 delete: Effect.fn(function* ({ output }) {
405 yield* retryQuota(
406 dataplex.deleteProjectsLocationsEntryGroupsEntryLinks({
407 name: output.name,
408 }),
409 ).pipe(Effect.catchTag("NotFound", () => Effect.void));
410 yield* waitUntilGone(getByName(output.name), output.name);
411 }),
412 });