@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 404 lines 13.1 KB
Raw
1 import * as managedidentities from "@distilled.cloud/gcp/managedidentities_v1";
2 import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import { tagRecord } from "alchemy/Tags";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import { stripInternalLabels } from "../Labels.ts";
10 import { waitForOperation as waitForLongRunningOperation } from "../Operation.ts";
11
12 export const GLOBAL_LOCATION = "global";
13 export const DEFAULT_NETWORK = "default";
14 export const DEFAULT_ADMIN = "setupadmin";
15 export const DOMAIN_SUFFIX = ".alch.test";
16 export const FIRST_SEGMENT_MAX = 15;
17 export const MAX_NAME_LENGTH = 63;
18 export const PAGE_SIZE = 100;
19
20 export class ResourceNotResolved extends Data.TaggedError(
21 "GCP.ManagedIdentities.ResourceNotResolved",
22 )<{
23 resourceName: string;
24 }> {}
25
26 export class ResourceStillExists extends Data.TaggedError(
27 "GCP.ManagedIdentities.ResourceStillExists",
28 )<{
29 resourceName: string;
30 }> {}
31
32 export class ResourceNotReady extends Data.TaggedError("GCP.ManagedIdentities.ResourceNotReady")<{
33 resourceName: string;
34 state: string;
35 }> {}
36
37 export class ResourceFailed extends Data.TaggedError("GCP.ManagedIdentities.ResourceFailed")<{
38 resourceName: string;
39 state: string;
40 details: string | undefined;
41 }> {}
42
43 export const lastSegment = (value: string) => {
44 const trimmed = value.replace(/\/+$/, "");
45 const parts = trimmed.split("/");
46 return parts[parts.length - 1] || trimmed;
47 };
48
49 export const rfc1035 = (name: string, fallback = "ad", maxLength = MAX_NAME_LENGTH): string => {
50 let next = name
51 .toLowerCase()
52 .replace(/[^a-z0-9-]/g, "-")
53 .replace(/-+/g, "-")
54 .replace(/^-+|-+$/g, "");
55 if (!/^[a-z]/.test(next)) next = `${fallback[0] ?? "a"}${next}`;
56 next = next.slice(0, maxLength).replace(/-+$/g, "");
57 if (next.length === 0) return fallback.slice(0, maxLength);
58 if (!/[a-z0-9]$/.test(next)) {
59 next = `${next.slice(0, maxLength - 1)}0`;
60 }
61 return next.slice(0, maxLength);
62 };
63
64 export const normalizeLocation = (location: string) => lastSegment(location).toLowerCase();
65
66 export const globalParent = (project: string) => `projects/${project}/locations/${GLOBAL_LOCATION}`;
67
68 export const parseName = (name: string, collection: string) => {
69 const parts = name.split("/").filter((part) => part.length > 0);
70 const collectionAt = parts.lastIndexOf(collection);
71 const locationsAt = parts.lastIndexOf("locations");
72 const projectsAt = parts.lastIndexOf("projects");
73 return {
74 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
75 location:
76 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : GLOBAL_LOCATION,
77 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
78 parent:
79 collectionAt > 0
80 ? parts.slice(0, collectionAt).join("/")
81 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
82 };
83 };
84
85 export const domainResourceOf = (value: string, project: string) => {
86 const trimmed = value.replace(/\/+$/, "");
87 if (trimmed.includes("/")) return trimmed;
88 return `${globalParent(project)}/domains/${trimmed}`;
89 };
90
91 export const domainNameOf = (value: string) => {
92 const trimmed = value.replace(/\/+$/, "");
93 if (trimmed.includes("/domains/")) return parseName(trimmed, "domains").id;
94 return trimmed;
95 };
96
97 export const networkOf = (value: string, project: string) => {
98 const trimmed = value.replace(/\/+$/, "");
99 if (trimmed.length === 0) {
100 return `projects/${project}/global/networks/${DEFAULT_NETWORK}`;
101 }
102 if (trimmed.includes("/")) return trimmed;
103 return `projects/${project}/global/networks/${trimmed}`;
104 };
105
106 export const toPhysicalId = (
107 id: string,
108 explicit: string | undefined,
109 existing: string | undefined,
110 fallback = "ad",
111 ) =>
112 Effect.gen(function* () {
113 if (explicit !== undefined) return rfc1035(explicit, fallback);
114 if (existing !== undefined) return existing;
115 return rfc1035(
116 yield* createPhysicalName({
117 id,
118 maxLength: MAX_NAME_LENGTH,
119 lowercase: true,
120 }),
121 fallback,
122 );
123 });
124
125 export const toDomainName = (
126 id: string,
127 explicit: string | undefined,
128 existing: string | undefined,
129 ) =>
130 Effect.gen(function* () {
131 if (explicit !== undefined) return explicit.toLowerCase();
132 if (existing !== undefined) return domainNameOf(existing);
133 const first = rfc1035(
134 yield* createPhysicalName({
135 id,
136 maxLength: FIRST_SEGMENT_MAX,
137 lowercase: true,
138 }),
139 "ad",
140 FIRST_SEGMENT_MAX,
141 );
142 return `${first}${DOMAIN_SUFFIX}`;
143 });
144
145 export const userLabels = (
146 labels: Record<string, string | undefined> | null | undefined,
147 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
148
149 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
150 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
151
152 export const fieldMask = (fields: Array<string | false | undefined>) =>
153 fields.filter((field): field is string => typeof field === "string").join(",");
154
155 export const sameText = (left: string | undefined, right: string | undefined) =>
156 (left ?? "") === (right ?? "");
157
158 export const sameStringList = (
159 left: readonly string[] | undefined,
160 right: readonly string[] | undefined,
161 ) =>
162 JSON.stringify([...(left ?? [])].map((value) => value.toLowerCase()).sort()) ===
163 JSON.stringify([...(right ?? [])].map((value) => value.toLowerCase()).sort());
164
165 export const replaceOnIdentity = (input: {
166 previousId: string | undefined;
167 nextId: string | undefined;
168 extra?: boolean;
169 previousParent?: string;
170 nextParent?: string;
171 }) => {
172 const parentChanged =
173 (input.previousParent ?? "") !== "" &&
174 (input.nextParent ?? "") !== "" &&
175 (input.previousParent ?? "") !== (input.nextParent ?? "");
176 const replace =
177 (input.extra ?? false) ||
178 parentChanged ||
179 (input.previousId !== undefined &&
180 input.nextId !== undefined &&
181 input.nextId !== input.previousId);
182 if (!replace) return undefined;
183 const samePhysical =
184 !parentChanged && input.previousId !== undefined && input.nextId === input.previousId;
185 return {
186 action: "replace" as const,
187 deleteFirst: samePhysical,
188 };
189 };
190
191 const READY_STATES = new Set(["READY", "ACTIVE", "CONNECTED", "PERFORMING_MAINTENANCE"]);
192 const FAILED_STATES = new Set(["ERROR", "FAILED", "UNAVAILABLE"]);
193
194 export const isReadyState = (state: string | undefined) =>
195 READY_STATES.has((state ?? "").toUpperCase());
196
197 export const isFailedState = (state: string | undefined) =>
198 FAILED_STATES.has((state ?? "").toUpperCase());
199
200 /**
201 * Wait on a Managed Microsoft AD long-running operation. Domain creation takes up to ~60 minutes.
202 * `ALREADY_EXISTS` (a create race) counts as success; so does `NOT_FOUND`
203 * when `notFoundOk` (deletes). Returns the final operation.
204 */
205 export const waitForOperation = (
206 operation: managedidentities.Operation,
207 options?: { notFoundOk?: boolean },
208 ) =>
209 Effect.suspend(() => {
210 let latest = operation;
211 return waitForLongRunningOperation(
212 operation,
213 (name) => {
214 const get = managedidentities.getProjectsLocationsGlobalOperations({ name }).pipe(
215 Effect.tap((current) =>
216 Effect.sync(() => {
217 latest = current;
218 }),
219 ),
220 );
221 const observe: Effect.Effect<
222 managedidentities.Operation,
223 managedidentities.GetProjectsLocationsGlobalOperationsError,
224 GcpOpContext
225 > =
226 options?.notFoundOk === true
227 ? get.pipe(
228 Effect.catchTag("NotFound", () =>
229 Effect.succeed<managedidentities.Operation>({
230 name,
231 done: true,
232 }),
233 ),
234 )
235 : get.pipe(
236 // A just-returned operation can briefly 404 on read.
237 Effect.retry({
238 while: (error) => error._tag === "NotFound",
239 times: 5,
240 schedule: Schedule.exponential("250 millis"),
241 }),
242 );
243 return observe;
244 },
245 { budget: "90 minutes", interval: "30 seconds" },
246 ).pipe(
247 Effect.map(() => latest),
248 // google.rpc.Code ALREADY_EXISTS = 6, NOT_FOUND = 5.
249 Effect.catchTag("GCP.OperationFailed", (error) =>
250 error.code === 6 || (options?.notFoundOk === true && error.code === 5)
251 ? Effect.succeed(latest)
252 : Effect.fail(error),
253 ),
254 );
255 });
256
257 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
258 get: Effect.Effect<A | undefined, E, R>,
259 name: string,
260 ) =>
261 get.pipe(
262 Effect.filterOrFail(
263 (value): value is A => value !== undefined,
264 () => new ResourceNotResolved({ resourceName: name }),
265 ),
266 Effect.retry({
267 while: (error) => error._tag === "GCP.ManagedIdentities.ResourceNotResolved",
268 times: 8,
269 schedule: Schedule.spaced("1 second"),
270 }),
271 );
272
273 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
274 get: Effect.Effect<A | undefined, E, R>,
275 name: string,
276 ) =>
277 get.pipe(
278 Effect.filterOrFail(
279 (value) => value === undefined,
280 () => new ResourceStillExists({ resourceName: name }),
281 ),
282 Effect.retry({
283 while: (error) => error._tag === "GCP.ManagedIdentities.ResourceStillExists",
284 times: 10,
285 schedule: Schedule.spaced("5 seconds"),
286 }),
287 Effect.asVoid,
288 );
289
290 export const waitUntilReady = <
291 A extends { readonly state?: string; readonly statusMessage?: string },
292 E extends { readonly _tag: string },
293 R,
294 >(
295 get: Effect.Effect<A | undefined, E, R>,
296 name: string,
297 stateOf: (value: A) => string | undefined,
298 detailsOf?: (value: A) => string | undefined,
299 options?: {
300 times?: number;
301 interval?: `${number} seconds`;
302 },
303 ): Effect.Effect<A, E | ResourceNotResolved | ResourceNotReady | ResourceFailed, R> =>
304 get.pipe(
305 Effect.filterOrFail(
306 (value): value is A => value !== undefined,
307 () => new ResourceNotResolved({ resourceName: name }),
308 ),
309 Effect.filterOrFail(
310 (value) => {
311 const state = stateOf(value) ?? "";
312 return isReadyState(state) || isFailedState(state) || state.length === 0;
313 },
314 (value) => new ResourceNotReady({ resourceName: name, state: stateOf(value) ?? "" }),
315 ),
316 Effect.filterOrFail(
317 (value) => !isFailedState(stateOf(value)),
318 (value) =>
319 new ResourceFailed({
320 resourceName: name,
321 state: stateOf(value) ?? "",
322 details: detailsOf?.(value),
323 }),
324 ),
325 Effect.retry({
326 while: (error) =>
327 error._tag === "GCP.ManagedIdentities.ResourceNotReady" ||
328 error._tag === "GCP.ManagedIdentities.ResourceNotResolved",
329 // Domains take up to ~60 minutes to become READY.
330 times: options?.times ?? 180,
331 schedule: Schedule.spaced(options?.interval ?? "30 seconds"),
332 }),
333 );
334
335 export const listLabeledPages = <Page, A, E, R>(
336 pages: Stream.Stream<Page, E, R>,
337 items: (page: Page) => readonly A[] | undefined,
338 labelsOf: (item: A) => Record<string, string | undefined> | null | undefined,
339 ) =>
340 pages.pipe(
341 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
342 Stream.filter((item) => hasAlchemyLabelMap(labelsOf(item))),
343 Stream.runCollect,
344 Effect.map((chunk) => Array.from(chunk)),
345 );
346
347 export const listDomains = (project: string) =>
348 listLabeledPages(
349 managedidentities.listProjectsLocationsGlobalDomains.pages({
350 parent: globalParent(project),
351 pageSize: PAGE_SIZE,
352 }),
353 (page) => page.domains,
354 (item) => item.labels,
355 );
356
357 export const listPeerings = (project: string) =>
358 listLabeledPages(
359 managedidentities.listProjectsLocationsGlobalPeerings.pages({
360 parent: globalParent(project),
361 pageSize: PAGE_SIZE,
362 }),
363 (page) => page.peerings,
364 (item) => item.labels,
365 );
366
367 export const listBackups = (project: string) =>
368 Effect.gen(function* () {
369 const wildcard = yield* listLabeledPages(
370 managedidentities.listProjectsLocationsGlobalDomainsBackups.pages({
371 parent: `${globalParent(project)}/domains/-`,
372 pageSize: PAGE_SIZE,
373 }),
374 (page) => page.backups,
375 (item) => item.labels,
376 );
377 if (wildcard.length > 0) {
378 return wildcard;
379 }
380 const domains = yield* managedidentities.listProjectsLocationsGlobalDomains
381 .pages({
382 parent: globalParent(project),
383 pageSize: PAGE_SIZE,
384 })
385 .pipe(
386 Stream.flatMap((page) => Stream.fromIterable(page.domains ?? [])),
387 Stream.runCollect,
388 Effect.map((chunk) => Array.from(chunk)),
389 );
390 const groups = yield* Effect.forEach(
391 domains.filter((domain) => (domain.name ?? "").length > 0),
392 (domain) =>
393 listLabeledPages(
394 managedidentities.listProjectsLocationsGlobalDomainsBackups.pages({
395 parent: domain.name!,
396 pageSize: PAGE_SIZE,
397 }),
398 (page) => page.backups,
399 (item) => item.labels,
400 ),
401 { concurrency: 4 },
402 );
403 return groups.flat();
404 });