@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 406 lines 14.5 KB
Raw
1 import * as bigqueryreservation from "@distilled.cloud/gcp/bigqueryreservation_v1";
2 import { unownedUnlessPrevious } 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 Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 compact,
13 hasOwnershipMarker,
14 lastSegment,
15 listLocations,
16 normalizeLocation,
17 ownedByAlchemy,
18 parentOf,
19 toResourceId,
20 } from "./internal.ts";
21
22 export type AssignmentJobType = bigqueryreservation.AssignmentJobTypeEnum | (string & {});
23
24 export type AssignmentSchedulingPolicy = {
25 /**
26 * Soft per-project slot cap for jobs in this assignment. Preview.
27 */
28 maxSlots?: string;
29 /**
30 * Soft per-project job concurrency cap. Preview.
31 */
32 concurrency?: string;
33 };
34
35 export type AssignmentProps = {
36 /**
37 * Parent reservation resource name
38 * `projects/{project}/locations/{location}/reservations/{reservation}`
39 * or a bare reservation id (resolved in the current project and
40 * `location`). Immutable — changing it replaces the assignment.
41 */
42 reservation: string;
43 /**
44 * Resource that consumes the reservation: `projects/{project}`,
45 * `folders/{folder}`, or `organizations/{org}`. Defaults to the
46 * current GCP project. Immutable — changing it replaces the
47 * assignment. One assignment per `(assignee, jobType, location)`.
48 */
49 assignee?: string;
50 /**
51 * Job type that uses the reservation (`QUERY`, `PIPELINE`,
52 * `ML_EXTERNAL`, `BACKGROUND`, `CONTINUOUS`, …). Immutable —
53 * changing it replaces the assignment.
54 * @default "QUERY"
55 */
56 jobType?: AssignmentJobType;
57 /**
58 * Assignment id (the `{assignment}` segment). If omitted, a unique
59 * name is generated. Assignments have no labels field, so Alchemy
60 * stamps ownership into this id (`alch---…`) for `list` / nuke.
61 * Immutable — changing it replaces the assignment. Max 64
62 * characters, lowercase letters, digits, or dashes.
63 */
64 assignmentId?: string;
65 /**
66 * Location of the parent reservation. Used when `reservation` is a
67 * bare id. Immutable — changing it replaces the assignment.
68 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
69 */
70 location?: string;
71 /**
72 * Principal that must run jobs to use this assignment. When empty,
73 * any job of `jobType` on `assignee` uses the reservation.
74 */
75 principal?: string;
76 /**
77 * Per-assignment scheduling policy. Preview.
78 */
79 schedulingPolicy?: AssignmentSchedulingPolicy;
80 };
81
82 export type Assignment = Resource<
83 "GCP.BigQueryReservation.Assignment",
84 AssignmentProps,
85 {
86 /** Full resource name `…/reservations/{reservation}/assignments/{assignment}`. */
87 name: string;
88 /** Assignment id (last path segment). */
89 assignmentId: string;
90 /** Parent reservation resource name. */
91 reservation: string;
92 /** Reservation id. */
93 reservationId: string;
94 /** Project that owns the reservation. */
95 project: string;
96 /** Location id. */
97 location: string;
98 /** Assignee resource (`projects/…`, `folders/…`, `organizations/…`). */
99 assignee: string | undefined;
100 /** Job type. */
101 jobType: string | undefined;
102 /** Assignment state (`PENDING`, `ACTIVE`). */
103 state: string | undefined;
104 /** Principal, if set. */
105 principal: string | undefined;
106 /** Scheduling policy, if set. */
107 schedulingPolicy: bigqueryreservation.SchedulingPolicy | undefined;
108 },
109 never,
110 Providers
111 >;
112
113 /**
114 * A BigQuery reservation assignment — grants a project, folder, or
115 * organization slots from a {@link Reservation} for one job type.
116 *
117 * Assignments have no labels. Alchemy stamps ownership into
118 * `assignmentId` so `list` / `pnpm nuke:gcp` can find them. Changing
119 * `reservation`, `assignee`, `jobType`, `location`, or `assignmentId`
120 * replaces the assignment. `principal` and `schedulingPolicy` update
121 * in place.
122 *
123 * ### Creating an Assignment
124 * **Example:** Assign QUERY jobs in this project
125 * ```typescript
126 * const slots = yield* GCP.BigQueryReservation.Reservation("Slots", {});
127 * const assignment = yield* GCP.BigQueryReservation.Assignment("Query", {
128 * reservation: slots.name,
129 * jobType: "QUERY",
130 * });
131 * ```
132 *
133 * **Example:** None assignment (on-demand)
134 * ```typescript
135 * const none = yield* GCP.BigQueryReservation.Assignment("OnDemand", {
136 * reservation: "none",
137 * location: "US",
138 * jobType: "QUERY",
139 * });
140 * ```
141 *
142 * @resource
143 * @category BigQueryReservation
144 */
145 export const Assignment = Resource<Assignment>("GCP.BigQueryReservation.Assignment");
146
147 export class AssignmentNotResolved extends Data.TaggedError(
148 "GCP.BigQueryReservation.AssignmentNotResolved",
149 )<{
150 resourceName: string;
151 }> {}
152
153 const DEFAULT_JOB_TYPE = "QUERY";
154
155 const parseName = (name: string) => {
156 const parts = name.split("/").filter((part) => part.length > 0);
157 const after = (segment: string) => {
158 const at = parts.lastIndexOf(segment);
159 return at >= 0 && parts[at + 1] ? parts[at + 1]! : "";
160 };
161 const reservationId = after("reservations");
162 const location = after("locations");
163 const project = after("projects");
164 return {
165 project,
166 location,
167 reservationId,
168 assignmentId: after("assignments") || lastSegment(name),
169 reservation:
170 project && location && reservationId
171 ? `projects/${project}/locations/${location}/reservations/${reservationId}`
172 : "",
173 };
174 };
175
176 const reservationParent = (project: string, location: string, reservation: string) => {
177 if (reservation.includes("/reservations/")) {
178 const parsed = parseName(
179 reservation.endsWith("/assignments") ? reservation : `${reservation}/assignments/_`,
180 );
181 return `projects/${parsed.project || project}/locations/${parsed.location || location}/reservations/${parsed.reservationId || lastSegment(reservation)}`;
182 }
183 return `projects/${project}/locations/${location}/reservations/${lastSegment(reservation)}`;
184 };
185
186 const assigneeOf = (project: string, assignee: string | undefined) =>
187 assignee && assignee.length > 0 ? assignee : `projects/${project}`;
188
189 const jobTypeOf = (jobType: string | undefined) => (jobType ?? DEFAULT_JOB_TYPE).toUpperCase();
190
191 const jsonOf = (value: unknown) => JSON.stringify(value ?? null);
192
193 const desiredSchedulingPolicy = (
194 news: AssignmentProps,
195 ): bigqueryreservation.SchedulingPolicy | undefined =>
196 news.schedulingPolicy === undefined
197 ? undefined
198 : compact({
199 maxSlots: news.schedulingPolicy.maxSlots,
200 concurrency: news.schedulingPolicy.concurrency,
201 });
202
203 const toAttrs = (current: bigqueryreservation.Assignment, fallbackProject: string) => {
204 const name = current.name ?? "";
205 const parsed = parseName(name);
206 return {
207 name,
208 assignmentId: parsed.assignmentId,
209 reservation: parsed.reservation,
210 reservationId: parsed.reservationId,
211 project: parsed.project || fallbackProject,
212 location: parsed.location,
213 assignee: current.assignee,
214 jobType: current.jobType,
215 state: current.state,
216 principal: current.principal,
217 schedulingPolicy: current.schedulingPolicy,
218 };
219 };
220
221 const listAt = (parent: string) =>
222 parent.length === 0
223 ? Effect.succeed([] as bigqueryreservation.Assignment[])
224 : bigqueryreservation.listProjectsLocationsReservationsAssignments
225 .pages({ parent, pageSize: 1000 })
226 .pipe(
227 Stream.flatMap((page) => Stream.fromIterable(page.assignments ?? [])),
228 Stream.runCollect,
229 Effect.map((chunk) => Array.from(chunk)),
230 Effect.catchTag("NotFound", () => Effect.succeed([] as bigqueryreservation.Assignment[])),
231 );
232
233 const getByName = (name: string) => {
234 if (name.length === 0) return Effect.undefined;
235 const parent = parseName(name).reservation;
236 return listAt(parent).pipe(
237 Effect.map((assignments) => assignments.find((item) => item.name === name)),
238 );
239 };
240
241 const findExisting = (parent: string, assignee: string, jobType: string, assignmentId?: string) =>
242 listAt(parent).pipe(
243 Effect.map((assignments) =>
244 assignments.find((item) => {
245 if (assignmentId && lastSegment(item.name ?? "") === assignmentId) {
246 return true;
247 }
248 return (item.assignee ?? "") === assignee && jobTypeOf(item.jobType) === jobType;
249 }),
250 ),
251 );
252
253 export const AssignmentProvider = () =>
254 Provider.succeed(Assignment, {
255 stables: ["name", "assignmentId", "reservation", "reservationId", "project", "location"],
256
257 diff: Effect.fn(function* ({ news, olds, output }) {
258 if (!isResolved(news)) return undefined;
259 const env = yield* GcpEnvironment.current;
260 const previousReservation = olds?.reservation ?? output?.reservation;
261 const previousAssignee = olds?.assignee ?? output?.assignee;
262 const previousJobType = jobTypeOf(olds?.jobType ?? output?.jobType);
263 const previousLocation = normalizeLocation(olds?.location ?? output?.location ?? env.region);
264 const previousId = olds?.assignmentId ?? output?.assignmentId;
265 const nextJobType = jobTypeOf(news.jobType ?? previousJobType);
266 const nextLocation = normalizeLocation(news.location ?? output?.location ?? env.region);
267 if (
268 (previousReservation !== undefined && news.reservation !== previousReservation) ||
269 (previousAssignee !== undefined &&
270 news.assignee !== undefined &&
271 news.assignee !== previousAssignee) ||
272 previousJobType !== nextJobType ||
273 previousLocation !== nextLocation ||
274 (previousId !== undefined &&
275 news.assignmentId !== undefined &&
276 news.assignmentId !== previousId)
277 ) {
278 return { action: "replace" as const, deleteFirst: true };
279 }
280 return undefined;
281 }),
282
283 read: Effect.fn(function* ({ id, olds, output, previous }) {
284 const env = yield* GcpEnvironment.current;
285 const parentRef = olds?.reservation ?? output?.reservation;
286 // A recovery read of an interrupted create gets persisted props whose
287 // unresolved parent reference was stripped; without the parent there
288 // is no name to look up, so report nothing found rather than calling
289 // the API with an empty path segment.
290 if (output?.name === undefined && (typeof parentRef !== "string" || parentRef.length === 0)) {
291 return undefined;
292 }
293 const location = normalizeLocation(olds?.location ?? output?.location ?? env.region);
294 const parent = reservationParent(
295 env.project,
296 location,
297 olds?.reservation ?? output?.reservation ?? "",
298 );
299 const assignmentId = yield* toResourceId(id, olds?.assignmentId, output?.assignmentId);
300 const name = output?.name ?? (parent ? `${parent}/assignments/${assignmentId}` : "");
301 const existing =
302 (yield* getByName(name)) ??
303 (yield* findExisting(
304 parent,
305 assigneeOf(env.project, olds?.assignee ?? output?.assignee),
306 jobTypeOf(olds?.jobType ?? output?.jobType),
307 assignmentId,
308 ));
309 if (existing === undefined) return undefined;
310 const attrs = toAttrs(existing, env.project);
311 return (yield* ownedByAlchemy(id, attrs.assignmentId))
312 ? attrs
313 : unownedUnlessPrevious(attrs, previous, "name");
314 }),
315
316 list: () =>
317 Effect.gen(function* () {
318 const env = yield* GcpEnvironment.current;
319 const pages = yield* Effect.forEach(
320 listLocations(env.region),
321 (location) =>
322 listAt(`${parentOf(env.project, location)}/reservations/-`).pipe(
323 Effect.map((assignments) =>
324 assignments
325 .filter((item) => hasOwnershipMarker(lastSegment(item.name ?? "")))
326 .map((item) => toAttrs(item, env.project)),
327 ),
328 ),
329 { concurrency: 4 },
330 );
331 const byName = new Map<string, ReturnType<typeof toAttrs>>();
332 for (const item of pages.flat()) {
333 byName.set(item.name, item);
334 }
335 return Array.from(byName.values());
336 }),
337
338 reconcile: Effect.fn(function* ({ id, news, output }) {
339 const env = yield* GcpEnvironment.current;
340 const location = normalizeLocation(news.location ?? output?.location ?? env.region);
341 const parent = reservationParent(env.project, location, news.reservation);
342 const assignmentId = yield* toResourceId(id, news.assignmentId, output?.assignmentId);
343 const name = `${parent}/assignments/${assignmentId}`;
344 const assignee = assigneeOf(env.project, news.assignee);
345 const jobType = jobTypeOf(news.jobType);
346 const body = compact({
347 assignee,
348 jobType,
349 principal: news.principal,
350 schedulingPolicy: desiredSchedulingPolicy(news),
351 });
352
353 let current =
354 (yield* getByName(output?.name ?? name)) ??
355 (yield* findExisting(parent, assignee, jobType, assignmentId));
356
357 if (current === undefined) {
358 const created = yield* bigqueryreservation
359 .createProjectsLocationsReservationsAssignments({
360 parent,
361 assignmentId,
362 body,
363 })
364 .pipe(
365 Effect.catchTag("Conflict", () =>
366 findExisting(parent, assignee, jobType, assignmentId),
367 ),
368 );
369 current = created ?? undefined;
370 }
371
372 if (current === undefined) {
373 return yield* new AssignmentNotResolved({ resourceName: name });
374 }
375
376 const currentName = current.name ?? name;
377 const principalChanged =
378 news.principal !== undefined && (current.principal ?? "") !== news.principal;
379 const schedulingChanged =
380 news.schedulingPolicy !== undefined &&
381 jsonOf(current.schedulingPolicy) !== jsonOf(desiredSchedulingPolicy(news));
382 const updateMask = [
383 principalChanged ? "principal" : undefined,
384 schedulingChanged ? "schedulingPolicy" : undefined,
385 ].filter((field): field is string => field !== undefined);
386
387 if (updateMask.length > 0) {
388 current = yield* bigqueryreservation.patchProjectsLocationsReservationsAssignments({
389 name: currentName,
390 updateMask: updateMask.join(","),
391 body,
392 });
393 }
394
395 return toAttrs(current, env.project);
396 }),
397
398 delete: Effect.fn(function* ({ output }) {
399 if (!output.name) return;
400 yield* bigqueryreservation
401 .deleteProjectsLocationsReservationsAssignments({
402 name: output.name,
403 })
404 .pipe(Effect.catchTag("NotFound", () => Effect.void));
405 }),
406 });