@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 316 lines 9.8 KB
Raw
1 import * as dataplex from "@distilled.cloud/gcp/dataplex_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import { tagRecord } from "alchemy/Tags";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
7 import { createInternalLabels, hasAlchemyLabels, stripInternalLabels } from "../Labels.ts";
8
9 export const MAX_ID_LENGTH = 63;
10 export const MAX_ENTITY_ID_LENGTH = 256;
11
12 export class DataplexNotResolved extends Data.TaggedError("GCP.Dataplex.NotResolved")<{
13 resourceName: string;
14 }> {}
15
16 export class DataplexStillExists extends Data.TaggedError("GCP.Dataplex.StillExists")<{
17 resourceName: string;
18 }> {}
19
20 export const lastSegment = (value: string) => {
21 const trimmed = value.replace(/\/+$/, "");
22 const parts = trimmed.split("/");
23 return parts[parts.length - 1] || trimmed;
24 };
25
26 export const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
27 lastSegment(location ?? defaultLocation).toLowerCase();
28
29 export const locationParent = (project: string, location: string) =>
30 `projects/${project}/locations/${location}`;
31
32 export const parseResourceName = (name: string, collection: string) => {
33 const parts = name.split("/").filter((part) => part.length > 0);
34 const collectionAt = parts.lastIndexOf(collection);
35 const locationsAt = parts.lastIndexOf("locations");
36 const projectsAt = parts.lastIndexOf("projects");
37 return {
38 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
39 // API resource names always carry a `locations/{location}` segment.
40 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
41 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
42 parent:
43 collectionAt > 0
44 ? parts.slice(0, collectionAt).join("/")
45 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
46 };
47 };
48
49 export const expandParent = (
50 value: string,
51 project: string,
52 location: string,
53 collection: string,
54 ) => {
55 if (value.includes("/")) return value;
56 return `projects/${project}/locations/${location}/${collection}/${value}`;
57 };
58
59 export const rfc1035 = (name: string, maxLength = MAX_ID_LENGTH): string => {
60 let next = name
61 .toLowerCase()
62 .replace(/[^a-z0-9-]/g, "-")
63 .replace(/-+/g, "-")
64 .replace(/^-+|-+$/g, "");
65 if (!/^[a-z]/.test(next)) next = `a${next}`;
66 next = next.slice(0, maxLength).replace(/-+$/g, "");
67 if (next.length === 0) return "dataplex";
68 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
69 return next.slice(0, maxLength);
70 };
71
72 export const snakeId = (name: string, maxLength = MAX_ENTITY_ID_LENGTH): string => {
73 let next = name
74 .toLowerCase()
75 .replace(/[^a-z0-9_]/g, "_")
76 .replace(/_+/g, "_")
77 .replace(/^_+|_+$/g, "");
78 if (!/^[a-z]/.test(next)) next = `e${next}`;
79 next = next.slice(0, maxLength).replace(/_+$/g, "");
80 if (next.length === 0) return "entity";
81 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
82 return next.slice(0, maxLength);
83 };
84
85 export const toPhysicalRfc1035 = (
86 id: string,
87 explicit: string | undefined,
88 existing: string | undefined,
89 maxLength = MAX_ID_LENGTH,
90 ) =>
91 Effect.gen(function* () {
92 if (explicit !== undefined) return rfc1035(explicit, maxLength);
93 if (existing !== undefined) return existing;
94 return rfc1035(yield* createPhysicalName({ id, maxLength, lowercase: true }), maxLength);
95 });
96
97 export const toPhysicalSnake = (
98 id: string,
99 explicit: string | undefined,
100 existing: string | undefined,
101 maxLength = MAX_ENTITY_ID_LENGTH,
102 ) =>
103 Effect.gen(function* () {
104 if (explicit !== undefined) return snakeId(explicit, maxLength);
105 if (existing !== undefined) return existing;
106 return snakeId(
107 yield* createPhysicalName({
108 id,
109 maxLength,
110 lowercase: true,
111 delimiter: "_",
112 }),
113 maxLength,
114 );
115 });
116
117 export const userLabels = (
118 labels: Record<string, string | undefined> | null | undefined,
119 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
120
121 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
122 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
123
124 export const ownedLabels = (id: string, labels: Record<string, string>) =>
125 hasAlchemyLabels(id, labels);
126
127 export const createOwnership = (id: string) => createInternalLabels(id);
128
129 export const canonical = (value: unknown): unknown => {
130 if (value === undefined || value === null) return undefined;
131 if (typeof value === "boolean" || typeof value === "number") return value;
132 if (typeof value === "string") {
133 return value.length === 0 ? undefined : value;
134 }
135 if (Array.isArray(value)) {
136 const items = value.map(canonical).filter((item) => item !== undefined);
137 return items.length === 0 ? undefined : items;
138 }
139 if (typeof value === "object") {
140 const entries = Object.entries(value as Record<string, unknown>)
141 .map(([key, item]) => [key, canonical(item)] as const)
142 .filter(([, item]) => item !== undefined)
143 .sort(([left], [right]) => left.localeCompare(right));
144 if (entries.length === 0) return undefined;
145 return Object.fromEntries(entries);
146 }
147 return undefined;
148 };
149
150 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
151
152 export const sameJson = (left: unknown, right: unknown) => fingerprint(left) === fingerprint(right);
153
154 export const sameStringList = (
155 left: readonly string[] | undefined,
156 right: readonly string[] | undefined,
157 ) => JSON.stringify([...(left ?? [])].sort()) === JSON.stringify([...(right ?? [])].sort());
158
159 export const isPendingState = (state: string | undefined) =>
160 state === "CREATING" ||
161 state === "DELETING" ||
162 state === "UPDATING" ||
163 state === "STATE_UNSPECIFIED" ||
164 state === undefined ||
165 state === "";
166
167 export const collectPages = <Page, Item, E, R>(
168 stream: Stream.Stream<Page, E, R>,
169 pick: (page: Page) => readonly Item[] | undefined,
170 ) =>
171 stream.pipe(
172 Stream.flatMap((page) => Stream.fromIterable(pick(page) ?? [])),
173 Stream.runCollect,
174 Effect.map((chunk) => Array.from(chunk)),
175 );
176
177 const emptyOnMissing = <A, E extends { readonly _tag: string }, R>(
178 effect: Effect.Effect<A[], E, R>,
179 ) =>
180 effect.pipe(
181 Effect.catchIf(
182 (error) => error._tag === "NotFound",
183 () => Effect.succeed([] as A[]),
184 ),
185 );
186
187 // `us-central1` was the fixed default before `GCP.Region`; list paths keep
188 // it alongside the stack region so resources created under it are found.
189 const listLocations = (region: string) => [...new Set([region, "us-central1"])];
190
191 const acrossLocations = <A, E, R>(
192 region: string,
193 list: (location: string) => Effect.Effect<A[], E, R>,
194 ) => Effect.forEach(listLocations(region), list).pipe(Effect.map((groups) => groups.flat()));
195
196 export const listLakes = (project: string, region: string) =>
197 acrossLocations(region, (location) =>
198 emptyOnMissing(
199 collectPages(
200 dataplex.listProjectsLocationsLakes.pages({
201 parent: locationParent(project, location),
202 pageSize: 1000,
203 }),
204 (page) => page.lakes,
205 ),
206 ),
207 );
208
209 export const listGlossaries = (project: string, region: string) =>
210 acrossLocations(region, (location) =>
211 emptyOnMissing(
212 collectPages(
213 dataplex.listProjectsLocationsGlossaries.pages({
214 parent: locationParent(project, location),
215 pageSize: 1000,
216 }),
217 (page) => page.glossaries,
218 ),
219 ),
220 );
221
222 export const listTerms = (glossary: string) =>
223 emptyOnMissing(
224 collectPages(
225 dataplex.listProjectsLocationsGlossariesTerms.pages({
226 parent: glossary,
227 pageSize: 1000,
228 }),
229 (page) => page.terms,
230 ),
231 );
232
233 export const listTasks = (lake: string) =>
234 emptyOnMissing(
235 collectPages(
236 dataplex.listProjectsLocationsLakesTasks.pages({
237 parent: lake,
238 pageSize: 1000,
239 }),
240 (page) => page.tasks,
241 ),
242 );
243
244 export const listZones = (lake: string) =>
245 emptyOnMissing(
246 collectPages(
247 dataplex.listProjectsLocationsLakesZones.pages({
248 parent: lake,
249 pageSize: 1000,
250 }),
251 (page) => page.zones,
252 ),
253 );
254
255 export const listAssets = (zone: string) =>
256 emptyOnMissing(
257 collectPages(
258 dataplex.listProjectsLocationsLakesZonesAssets.pages({
259 parent: zone,
260 pageSize: 1000,
261 }),
262 (page) => page.assets,
263 ),
264 );
265
266 export const listEntities = (
267 zone: string,
268 view: dataplex.ListProjectsLocationsLakesZonesEntitiesViewEnum,
269 ) =>
270 emptyOnMissing(
271 collectPages(
272 dataplex.listProjectsLocationsLakesZonesEntities.pages({
273 parent: zone,
274 view,
275 pageSize: 500,
276 }),
277 (page) => page.entities,
278 ),
279 );
280
281 export const listPartitions = (entity: string) =>
282 emptyOnMissing(
283 collectPages(
284 dataplex.listProjectsLocationsLakesZonesEntitiesPartitions.pages({
285 parent: entity,
286 pageSize: 500,
287 }),
288 (page) => page.partitions,
289 ),
290 );
291
292 export const listMetadataFeeds = (project: string, region: string) =>
293 acrossLocations(region, (location) =>
294 emptyOnMissing(
295 collectPages(
296 dataplex.listProjectsLocationsMetadataFeeds.pages({
297 parent: locationParent(project, location),
298 pageSize: 1000,
299 }),
300 (page) => page.metadataFeeds,
301 ),
302 ),
303 );
304
305 export const listChildResources = <A, E, R>(
306 lakes: readonly dataplex.GoogleCloudDataplexV1Lake[],
307 list: (lakeName: string) => Effect.Effect<A[], E, R>,
308 ) =>
309 Effect.forEach(
310 lakes.filter((lake) => (lake.name ?? "").length > 0),
311 (lake) => list(lake.name!),
312 { concurrency: 4 },
313 ).pipe(Effect.map((groups) => groups.flat()));
314
315 export const replaceIfChanged = (previous: string | undefined, next: string | undefined) =>
316 previous !== undefined && next !== undefined && previous !== next;