The best way to control Claude Code, Codex and more from any computer or phone

main
ts 369 lines 12.6 KB
Raw
1 import * as netapp from "@distilled.cloud/gcp/netapp_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 Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { stripInternalLabels } from "../Labels.ts";
9 import { waitForOperation as waitForLongRunning, type LongRunningOperation } from "../Operation.ts";
10
11 export const MAX_NAME_LENGTH = 63;
12
13 export class ResourceNotResolved extends Data.TaggedError("GCP.NetApp.ResourceNotResolved")<{
14 resourceName: string;
15 }> {}
16
17 export class ResourceStillExists extends Data.TaggedError("GCP.NetApp.ResourceStillExists")<{
18 resourceName: string;
19 }> {}
20
21 export class ResourceNotReady extends Data.TaggedError("GCP.NetApp.ResourceNotReady")<{
22 resourceName: string;
23 state: string;
24 }> {}
25
26 export class ResourceFailed extends Data.TaggedError("GCP.NetApp.ResourceFailed")<{
27 resourceName: string;
28 state: string;
29 details: string | undefined;
30 }> {}
31
32 export const lastSegment = (value: string) => {
33 const trimmed = value.replace(/\/+$/, "");
34 const parts = trimmed.split("/");
35 return parts[parts.length - 1] || trimmed;
36 };
37
38 export const rfc1035 = (name: string, fallback = "netapp"): string => {
39 let next = name
40 .toLowerCase()
41 .replace(/[^a-z0-9-]/g, "-")
42 .replace(/-+/g, "-")
43 .replace(/^-+|-+$/g, "");
44 if (!/^[a-z]/.test(next)) next = `n${next}`;
45 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
46 if (next.length === 0) return fallback;
47 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
48 return next.slice(0, MAX_NAME_LENGTH);
49 };
50
51 export const normalizeLocation = (location: string | undefined, fallback: string) =>
52 lastSegment(location ?? fallback).toLowerCase();
53
54 export const parentOf = (project: string, location: string) =>
55 `projects/${project}/locations/${lastSegment(location).toLowerCase()}`;
56
57 export const toPhysicalId = (
58 id: string,
59 explicit: string | undefined,
60 existing: string | undefined,
61 fallback = "netapp",
62 ) =>
63 Effect.gen(function* () {
64 if (explicit !== undefined) return rfc1035(explicit, fallback);
65 if (existing !== undefined) return existing;
66 return rfc1035(
67 yield* createPhysicalName({
68 id,
69 maxLength: MAX_NAME_LENGTH,
70 lowercase: true,
71 }),
72 fallback,
73 );
74 });
75
76 export const parseName = (name: string, collection: string, fallbackLocation: string) => {
77 const parts = name.split("/").filter((part) => part.length > 0);
78 const collectionAt = parts.lastIndexOf(collection);
79 const locationsAt = parts.lastIndexOf("locations");
80 const projectsAt = parts.lastIndexOf("projects");
81 return {
82 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
83 location:
84 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation,
85 id:
86 collectionAt >= 0 && parts[collectionAt + 1]
87 ? parts.slice(collectionAt + 1).join("/")
88 : lastSegment(name),
89 parent:
90 collectionAt > 0
91 ? parts.slice(0, collectionAt).join("/")
92 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
93 };
94 };
95
96 export const expandParent = (
97 value: string,
98 project: string,
99 location: string,
100 collection: string,
101 ) => {
102 if (value.includes("/")) return value.replace(/\/+$/, "");
103 return `projects/${project}/locations/${location}/${collection}/${value}`;
104 };
105
106 export const networkName = (project: string, network: string | undefined) => {
107 const value = network ?? "default";
108 if (value.includes("/")) return value;
109 return `projects/${project}/global/networks/${value}`;
110 };
111
112 export const gibOf = (value: number | string | undefined): string | undefined =>
113 value === undefined ? undefined : String(value);
114
115 export const userLabels = (
116 labels: Record<string, string | undefined> | null | undefined,
117 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
118
119 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
120 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
121
122 export const sortedStrings = (values: ReadonlyArray<string> | undefined) =>
123 [...(values ?? [])].map((value) => value).sort();
124
125 export const sameStringList = (
126 left: readonly string[] | undefined,
127 right: readonly string[] | undefined,
128 ) => JSON.stringify(sortedStrings(left)) === JSON.stringify(sortedStrings(right));
129
130 export const canonical = (value: unknown): unknown => {
131 if (value === undefined || value === null) return undefined;
132 if (typeof value === "boolean" || typeof value === "number") return value;
133 if (typeof value === "string") {
134 return value.length === 0 ? undefined : value;
135 }
136 if (Array.isArray(value)) {
137 const items = value.map(canonical).filter((item) => item !== undefined);
138 return items.length === 0 ? undefined : items;
139 }
140 if (typeof value === "object") {
141 const entries = Object.entries(value as Record<string, unknown>)
142 .map(([key, item]) => [key, canonical(item)] as const)
143 .filter(([, item]) => item !== undefined)
144 .sort(([left], [right]) => left.localeCompare(right));
145 if (entries.length === 0) return undefined;
146 return Object.fromEntries(entries);
147 }
148 return undefined;
149 };
150
151 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
152
153 export const fieldMask = (fields: Array<string | false | undefined>) =>
154 fields.filter((field): field is string => typeof field === "string").join(",");
155
156 export const replaceOnIdentity = (input: {
157 previousId: string | undefined;
158 nextId: string | undefined;
159 previousLocation: string;
160 nextLocation: string;
161 extra?: boolean;
162 previousParent?: string;
163 nextParent?: string;
164 }) => {
165 const parentChanged =
166 (input.previousParent ?? "") !== "" &&
167 (input.nextParent ?? "") !== "" &&
168 (input.previousParent ?? "") !== (input.nextParent ?? "");
169 const replace =
170 (input.extra ?? false) ||
171 parentChanged ||
172 (input.previousId !== undefined &&
173 input.nextId !== undefined &&
174 input.nextId !== input.previousId) ||
175 input.previousLocation !== input.nextLocation;
176 if (!replace) return undefined;
177 const samePhysical =
178 input.previousLocation === input.nextLocation &&
179 !parentChanged &&
180 input.previousId !== undefined &&
181 input.nextId === input.previousId;
182 return {
183 action: "replace" as const,
184 deleteFirst: samePhysical,
185 };
186 };
187
188 // A KMS config parks in KEY_CHECK_PENDING until the NetApp service agent is
189 // granted the key and `verify` is called — a settled state, not provisioning.
190 const READY_STATES = new Set(["READY", "IN_USE", "READ_ONLY", "KEY_CHECK_PENDING"]);
191 const FAILED_STATES = new Set(["ERROR", "KEY_NOT_REACHABLE"]);
192
193 export const isReadyState = (state: string | undefined) =>
194 READY_STATES.has((state ?? "").toUpperCase());
195
196 export const isFailedState = (state: string | undefined) =>
197 FAILED_STATES.has((state ?? "").toUpperCase());
198
199 // NetApp pool, volume, backup, and replication operations routinely run for
200 // several minutes; poll for up to ~10 minutes before giving up.
201 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
202 get: Effect.Effect<A, E, R>,
203 name: string,
204 ): Effect.Effect<A & {}, E | ResourceNotResolved, R> =>
205 get.pipe(
206 Effect.filterOrFail(
207 (value): value is A & {} => value !== undefined,
208 () => new ResourceNotResolved({ resourceName: name }),
209 ),
210 Effect.retry({
211 while: (error) => error._tag === "GCP.NetApp.ResourceNotResolved",
212 times: 8,
213 schedule: Schedule.spaced("1 second"),
214 }),
215 );
216
217 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
218 get: Effect.Effect<A, E, R>,
219 name: string,
220 ) =>
221 get.pipe(
222 Effect.filterOrFail(
223 (value) => value === undefined,
224 () => new ResourceStillExists({ resourceName: name }),
225 ),
226 Effect.retry({
227 while: (error) => error._tag === "GCP.NetApp.ResourceStillExists",
228 times: OPERATION_POLLS,
229 schedule: Schedule.spaced("5 seconds"),
230 }),
231 Effect.asVoid,
232 );
233
234 export const waitUntilReady = <A, E extends { readonly _tag: string }, R>(
235 get: Effect.Effect<A, E, R>,
236 name: string,
237 stateOf: (value: A & {}) => string | undefined,
238 detailsOf?: (value: A & {}) => string | undefined,
239 options?: {
240 times?: number;
241 interval?: `${number} seconds`;
242 },
243 ): Effect.Effect<A & {}, E | ResourceNotResolved | ResourceNotReady | ResourceFailed, R> =>
244 get.pipe(
245 Effect.filterOrFail(
246 (value): value is A & {} => value !== undefined,
247 () => new ResourceNotResolved({ resourceName: name }),
248 ),
249 Effect.filterOrFail(
250 (value) => !isFailedState(stateOf(value)),
251 (value) =>
252 new ResourceFailed({
253 resourceName: name,
254 state: stateOf(value) ?? "",
255 details: detailsOf?.(value),
256 }),
257 ),
258 Effect.filterOrFail(
259 (value) => {
260 const state = stateOf(value) ?? "";
261 return isReadyState(state) || state.length === 0;
262 },
263 (value) => new ResourceNotReady({ resourceName: name, state: stateOf(value) ?? "" }),
264 ),
265 Effect.retry({
266 while: (error) =>
267 error._tag === "GCP.NetApp.ResourceNotReady" ||
268 error._tag === "GCP.NetApp.ResourceNotResolved",
269 times: options?.times ?? OPERATION_POLLS,
270 schedule: Schedule.spaced(options?.interval ?? "5 seconds"),
271 }),
272 );
273
274 export const collectPages = <Page, A, E, R>(
275 pages: Stream.Stream<Page, E, R>,
276 items: (page: Page) => readonly A[] | undefined,
277 ) =>
278 pages.pipe(
279 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
280 Stream.runCollect,
281 Effect.map((chunk) => Array.from(chunk)),
282 );
283
284 /** Every location at once: the API accepts the `locations/-` wildcard. */
285 export const listAtLocation = <A, E, R>(
286 project: string,
287 list: (parent: string) => Effect.Effect<A[], E, R>,
288 ) => list(`projects/${project}/locations/-`);
289
290 export const volumeOf = (value: string, project: string, location: string) =>
291 expandParent(value, project, location, "volumes");
292
293 export const storagePoolOf = (value: string, project: string, location: string) =>
294 expandParent(value, project, location, "storagePools");
295
296 export const listVolumes = (project: string) =>
297 listAtLocation(project, (parent) =>
298 collectPages(
299 netapp.listProjectsLocationsVolumes.pages({ parent, pageSize: 1000 }),
300 (page) => page.volumes,
301 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as netapp.Volume[]))),
302 );
303
304 export const listVolumeChildren = <A, E, R>(
305 project: string,
306 region: string,
307 list: (volumeName: string) => Effect.Effect<A[], E, R>,
308 ) =>
309 listVolumes(project).pipe(
310 Effect.flatMap((volumes) =>
311 Effect.forEach(
312 volumes.filter((volume) => (volume.name ?? "").length > 0),
313 (volume) => list(volume.name!),
314 { concurrency: 4 },
315 ).pipe(Effect.map((groups) => groups.flat())),
316 ),
317 );
318
319 /** Children of every parent in every location (`locations/-/{nested}`). */
320 export const listAtNested = <A, E, R>(
321 project: string,
322 nested: string,
323 list: (parent: string) => Effect.Effect<A[], E, R>,
324 ) => list(`projects/${project}/locations/-/${nested}`);
325
326 export const listLabeledPages = <Page, A, E, R>(
327 pages: Stream.Stream<Page, E, R>,
328 items: (page: Page) => readonly A[] | undefined,
329 labelsOf: (item: A) => Record<string, string | undefined> | null | undefined,
330 ) =>
331 pages.pipe(
332 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
333 Stream.filter((item) => hasAlchemyLabelMap(labelsOf(item))),
334 Stream.runCollect,
335 Effect.map((chunk) => Array.from(chunk)),
336 );
337
338 /** Readiness / gone polls: 360 × 5s = 30 minutes. */
339 const OPERATION_POLLS = 360;
340
341 /** Storage pool, volume and replication operations take up to ~20 minutes. */
342 const OPERATION_BUDGET = "30 minutes";
343
344 /**
345 * Wait for a long-running operation. ALREADY_EXISTS (code 6) means a
346 * concurrent create won the race; reconcile observes the resource next.
347 */
348 export const waitForOperation = (operation: LongRunningOperation) =>
349 waitForLongRunning(operation, (name) => netapp.getProjectsLocationsOperations({ name }), {
350 budget: OPERATION_BUDGET,
351 }).pipe(
352 Effect.catchIf(
353 (error) => error._tag === "GCP.OperationFailed" && error.code === 6,
354 () => Effect.succeed(operation),
355 ),
356 );
357
358 /**
359 * Wait for a delete operation. A vanished operation or NOT_FOUND (code 5)
360 * means the resource is already gone.
361 */
362 export const waitForDeleteOperation = (operation: LongRunningOperation) =>
363 waitForOperation(operation).pipe(
364 Effect.catchIf(
365 (error) =>
366 error._tag === "NotFound" || (error._tag === "GCP.OperationFailed" && error.code === 5),
367 () => Effect.succeed(operation),
368 ),
369 );