@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 469 lines 15.2 KB
Raw
1 import * as workloadmanager from "@distilled.cloud/gcp/workloadmanager_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 { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 fieldMask,
14 fingerprint,
15 listAtLocation,
16 listLabeledPages,
17 normalizeLocation,
18 parentOf,
19 parseName,
20 replaceOnIdentity,
21 ResourceNotResolved,
22 sameStringList,
23 sameText,
24 stringMap,
25 toPhysicalId,
26 userLabels,
27 waitForOperation,
28 waitUntilExists,
29 waitUntilGone,
30 waitUntilReady,
31 } from "./internal.ts";
32
33 export type GceInstanceFilter = {
34 /**
35 * Service accounts whose associated Compute Engine instances are
36 * included in the evaluation.
37 */
38 serviceAccounts?: string[];
39 };
40
41 export type ResourceFilter = {
42 /**
43 * Evaluation scopes. Format `projects/{project}`, `folders/{folder}`,
44 * or `organizations/{organization}`. Bare project ids are expanded.
45 * Defaults to the current project.
46 */
47 scopes?: string[];
48 /**
49 * Labels that must exist on a resource for it to be included
50 * (for example VM instance labels).
51 */
52 inclusionLabels?: Record<string, string>;
53 /**
54 * Filter Compute Engine instances by service account.
55 */
56 gceInstanceFilter?: GceInstanceFilter;
57 /**
58 * Resource-id patterns. A pattern of `prod-cluster` matches resources
59 * whose id contains that substring.
60 */
61 resourceIdPatterns?: string[];
62 };
63
64 export type BigQueryDestination = {
65 /**
66 * Destination dataset for evaluation results.
67 */
68 destinationDataset?: string;
69 /**
70 * When true, a new results table is created for each Execution.
71 */
72 createNewResultsTable?: boolean;
73 };
74
75 export type EvaluationType = workloadmanager.EvaluationEvaluationTypeEnum | (string & {});
76
77 export type EvaluationProps = {
78 /**
79 * Evaluation id (the `{evaluation}` segment of
80 * `projects/{project}/locations/{location}/evaluations/{evaluation}`).
81 * If omitted, a unique RFC1035 name is generated from the stack, stage,
82 * and logical id. Immutable — changing it replaces the evaluation.
83 */
84 evaluationId?: string;
85 /**
86 * Region (`us-central1`, `us-east1`, …). Immutable — changing it
87 * replaces the evaluation. `US-CENTRAL1` is accepted and normalized
88 * to `us-central1`.
89 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
90 */
91 location?: string;
92 /**
93 * Human-readable description.
94 */
95 description?: string;
96 /**
97 * Scope of Cloud resources to evaluate. When omitted, the current
98 * project is used as the sole scope.
99 */
100 resourceFilter?: ResourceFilter;
101 /**
102 * Rule names applied by this evaluation. List available names with
103 * `listProjectsLocationsRules`.
104 */
105 ruleNames?: string[];
106 /**
107 * Customer-managed encryption key
108 * `projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`.
109 * Immutable — changing it replaces the evaluation.
110 */
111 kmsKey?: string;
112 /**
113 * BigQuery destination for detailed evaluation results.
114 */
115 bigQueryDestination?: BigQueryDestination;
116 /**
117 * Workload type (`SAP`, `SQL_SERVER`, `OTHER`, …).
118 */
119 evaluationType?: EvaluationType;
120 /**
121 * User labels. Alchemy ownership labels are merged in automatically.
122 */
123 labels?: Record<string, string>;
124 /**
125 * Crontab schedule. Workload Manager only accepts a fixed set of
126 * cadences (hourly, every 6 or 12 hours, daily, weekly, every 14
127 * days, monthly).
128 */
129 schedule?: string;
130 /**
131 * Cloud Storage bucket that holds custom rules.
132 */
133 customRulesBucket?: string;
134 };
135
136 export type Evaluation = Resource<
137 "GCP.WorkloadManager.Evaluation",
138 EvaluationProps,
139 {
140 /** Full resource name. */
141 name: string;
142 /** Evaluation id (last path segment). */
143 evaluationId: string;
144 /** Project id. */
145 project: string;
146 /** Location id. */
147 location: string;
148 /** Human-readable description. */
149 description: string | undefined;
150 /** Resource filter. */
151 resourceFilter: ResourceFilter | undefined;
152 /** Applied rule names. */
153 ruleNames: string[];
154 /** Customer-managed encryption key. */
155 kmsKey: string | undefined;
156 /** Lifecycle state of the evaluation resource. */
157 resourceStatus: string | undefined;
158 /** BigQuery destination. */
159 bigQueryDestination: BigQueryDestination | undefined;
160 /** Workload type. */
161 evaluationType: string | undefined;
162 /** User labels (Alchemy ownership labels stripped). */
163 labels: Record<string, string>;
164 /** Crontab schedule. */
165 schedule: string | undefined;
166 /** Custom-rules bucket. */
167 customRulesBucket: string | undefined;
168 /** RFC3339 creation timestamp. */
169 createTime: string | undefined;
170 /** RFC3339 last-update timestamp. */
171 updateTime: string | undefined;
172 },
173 never,
174 Providers
175 >;
176
177 /**
178 * A Workload Manager evaluation — a named set of best-practice rules
179 * validated against a scope of Cloud resources (SAP, SQL Server, or
180 * custom).
181 *
182 * Changing `evaluationId`, `location`, or `kmsKey` replaces the
183 * evaluation. Description, labels, resource filter, rule names,
184 * schedule, custom-rules bucket, BigQuery destination, and
185 * `evaluationType` update in place.
186 *
187 * ### Creating an Evaluation
188 * **Example:** Generated name
189 * ```typescript
190 * const evaluation = yield* GCP.WorkloadManager.Evaluation("SapBest", {
191 * evaluationType: "SAP",
192 * ruleNames: ["sap-hana"],
193 * resourceFilter: { scopes: ["projects/my-project"] },
194 * labels: { env: "test" },
195 * });
196 * ```
197 *
198 * **Example:** Explicit id
199 * ```typescript
200 * const evaluation = yield* GCP.WorkloadManager.Evaluation("SapBest", {
201 * evaluationId: "nightly-sap",
202 * evaluationType: "SAP",
203 * ruleNames: ["sap-hana"],
204 * description: "nightly SAP scan",
205 * });
206 * ```
207 *
208 * ### Updating an Evaluation
209 * **Example:** Description and labels
210 * ```typescript
211 * // Same logical id, changed props: the engine updates it in place.
212 * const evaluation = yield* GCP.WorkloadManager.Evaluation("SapBest", {
213 * evaluationType: "SAP",
214 * ruleNames: ["sap-hana"],
215 * description: "nightly SAP scan v2",
216 * labels: { env: "prod", team: "platform" },
217 * });
218 * ```
219 *
220 * @resource
221 * @category WorkloadManager
222 */
223 export const Evaluation = Resource<Evaluation>("GCP.WorkloadManager.Evaluation");
224
225 const resourceName = (project: string, location: string, evaluationId: string) =>
226 `projects/${project}/locations/${location}/evaluations/${evaluationId}`;
227
228 const expandScope = (value: string, project: string) => {
229 const trimmed = value.replace(/\/+$/, "");
230 if (trimmed.includes("/")) return trimmed;
231 return `projects/${trimmed.length > 0 ? trimmed : project}`;
232 };
233
234 const toFilter = (
235 filter: workloadmanager.ResourceFilter | undefined,
236 ): ResourceFilter | undefined =>
237 filter === undefined
238 ? undefined
239 : {
240 scopes: filter.scopes,
241 inclusionLabels: stringMap(filter.inclusionLabels),
242 gceInstanceFilter: filter.gceInstanceFilter,
243 resourceIdPatterns: filter.resourceIdPatterns,
244 };
245
246 const toBigQuery = (
247 destination: workloadmanager.BigQueryDestination | undefined,
248 ): BigQueryDestination | undefined =>
249 destination === undefined
250 ? undefined
251 : {
252 destinationDataset: destination.destinationDataset,
253 createNewResultsTable: destination.createNewResultsTable,
254 };
255
256 const desiredFilter = (
257 filter: ResourceFilter | undefined,
258 project: string,
259 ): workloadmanager.ResourceFilter => ({
260 scopes: (filter?.scopes === undefined || filter.scopes.length === 0
261 ? [`projects/${project}`]
262 : filter.scopes
263 ).map((scope) => expandScope(scope, project)),
264 inclusionLabels: filter?.inclusionLabels,
265 gceInstanceFilter: filter?.gceInstanceFilter,
266 resourceIdPatterns: filter?.resourceIdPatterns,
267 });
268
269 const toAttrs = (item: workloadmanager.Evaluation, project: string, region: string) => {
270 const name = item.name ?? "";
271 const parsed = parseName(name, "evaluations", region);
272 return {
273 name,
274 evaluationId: parsed.id,
275 project: parsed.project || project,
276 location: parsed.location,
277 description: item.description,
278 resourceFilter: toFilter(item.resourceFilter),
279 ruleNames: item.ruleNames ?? [],
280 kmsKey: item.kmsKey,
281 resourceStatus: item.resourceStatus?.state,
282 bigQueryDestination: toBigQuery(item.bigQueryDestination),
283 evaluationType: item.evaluationType,
284 labels: userLabels(item.labels),
285 schedule: item.schedule,
286 customRulesBucket: item.customRulesBucket,
287 createTime: item.createTime,
288 updateTime: item.updateTime,
289 };
290 };
291
292 const getByName = (name: string) =>
293 workloadmanager
294 .getProjectsLocationsEvaluations({ name })
295 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
296
297 const listOwned = (project: string, region: string) =>
298 listAtLocation(project, region, (parent) =>
299 listLabeledPages(
300 workloadmanager.listProjectsLocationsEvaluations.pages({
301 parent,
302 pageSize: 1000,
303 }),
304 (page) => page.evaluations,
305 (item) => item.labels,
306 ),
307 );
308
309 export const EvaluationProvider = () =>
310 Provider.succeed(Evaluation, {
311 stables: ["name", "evaluationId", "project", "location", "kmsKey", "createTime"],
312
313 diff: Effect.fn(function* ({ news, olds, output }) {
314 if (!isResolved(news)) return undefined;
315 const env = yield* GcpEnvironment.current;
316 return replaceOnIdentity({
317 previousId: olds?.evaluationId ?? output?.evaluationId,
318 nextId: news.evaluationId ?? olds?.evaluationId ?? output?.evaluationId,
319 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
320 nextLocation: normalizeLocation(
321 news.location ?? olds?.location ?? output?.location,
322 env.region,
323 ),
324 extra:
325 (olds?.kmsKey ?? output?.kmsKey) !== undefined &&
326 news.kmsKey !== undefined &&
327 (olds?.kmsKey ?? output?.kmsKey) !== news.kmsKey,
328 });
329 }),
330
331 read: Effect.fn(function* ({ id, olds, output }) {
332 const env = yield* GcpEnvironment.current;
333 const evaluationId = yield* toPhysicalId(
334 id,
335 olds?.evaluationId,
336 output?.evaluationId,
337 "evaluation",
338 );
339 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
340 const name = output?.name ?? resourceName(env.project, location, evaluationId);
341 const existing = yield* getByName(name);
342 if (existing === undefined) return undefined;
343 const attrs = toAttrs(existing, env.project, env.region);
344 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
345 }),
346
347 list: () =>
348 Effect.gen(function* () {
349 const env = yield* GcpEnvironment.current;
350 const items = yield* listOwned(env.project, env.region);
351 return items.map((item) => toAttrs(item, env.project, env.region));
352 }),
353
354 reconcile: Effect.fn(function* ({ id, news, output }) {
355 const env = yield* GcpEnvironment.current;
356 const evaluationId = yield* toPhysicalId(
357 id,
358 news.evaluationId,
359 output?.evaluationId,
360 "evaluation",
361 );
362 const location = normalizeLocation(news.location ?? output?.location, env.region);
363 const name = resourceName(env.project, location, evaluationId);
364 const desiredLabels = {
365 ...toLabels(news.labels),
366 ...(yield* createInternalLabels(id)),
367 };
368 const resourceFilter = desiredFilter(news.resourceFilter, env.project);
369 const ruleNames = news.ruleNames;
370 const body: workloadmanager.Evaluation = {
371 description: news.description,
372 resourceFilter,
373 ruleNames,
374 kmsKey: news.kmsKey,
375 bigQueryDestination: news.bigQueryDestination,
376 evaluationType: news.evaluationType,
377 labels: desiredLabels,
378 schedule: news.schedule,
379 customRulesBucket: news.customRulesBucket,
380 };
381
382 let current = yield* getByName(output?.name ?? name);
383
384 if (current === undefined) {
385 const created = yield* workloadmanager
386 .createProjectsLocationsEvaluations({
387 parent: parentOf(env.project, location),
388 evaluationId,
389 body,
390 })
391 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
392 if (created !== undefined) {
393 // ALREADY_EXISTS (6): a concurrent create won the race.
394 yield* waitForOperation(created).pipe(
395 Effect.catchTag("GCP.OperationFailed", (error) =>
396 error.code === 6 ? Effect.void : Effect.fail(error),
397 ),
398 );
399 }
400 current = yield* waitUntilExists(getByName(name), name);
401 }
402
403 if (current === undefined) {
404 return yield* new ResourceNotResolved({ resourceName: name });
405 }
406
407 current = yield* waitUntilReady(
408 getByName(current.name ?? name),
409 current.name ?? name,
410 (item) => item.resourceStatus?.state,
411 );
412
413 const observedLabels = tagRecord(current.labels);
414 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
415 const mask = fieldMask([
416 (upsert.length > 0 || removed.length > 0) && "labels",
417 !sameText(current.description, news.description) && "description",
418 fingerprint(toFilter(current.resourceFilter)) !== fingerprint(toFilter(resourceFilter)) &&
419 "resourceFilter",
420 !sameStringList(current.ruleNames, ruleNames) && "ruleNames",
421 fingerprint(toBigQuery(current.bigQueryDestination)) !==
422 fingerprint(news.bigQueryDestination) && "bigQueryDestination",
423 !sameText(current.evaluationType, news.evaluationType) && "evaluationType",
424 !sameText(current.schedule, news.schedule) && "schedule",
425 !sameText(current.customRulesBucket, news.customRulesBucket) && "customRulesBucket",
426 ]);
427
428 if (mask.length > 0) {
429 const operation = yield* workloadmanager.patchProjectsLocationsEvaluations({
430 name: current.name ?? name,
431 updateMask: mask,
432 body,
433 });
434 yield* waitForOperation(operation);
435 current = yield* waitUntilReady(
436 getByName(current.name ?? name),
437 current.name ?? name,
438 (item) => item.resourceStatus?.state,
439 );
440 }
441
442 return toAttrs(current, env.project, env.region);
443 }),
444
445 delete: Effect.fn(function* ({ output }) {
446 const operation = yield* workloadmanager
447 .deleteProjectsLocationsEvaluations({
448 name: output.name,
449 force: true,
450 })
451 .pipe(
452 Effect.retry({
453 while: (error) => error._tag === "Conflict",
454 times: 8,
455 schedule: Schedule.spaced("2 seconds"),
456 }),
457 Effect.catchTag("NotFound", () => Effect.undefined),
458 );
459 if (operation !== undefined) {
460 // NOT_FOUND (5): already gone.
461 yield* waitForOperation(operation).pipe(
462 Effect.catchTag("GCP.OperationFailed", (error) =>
463 error.code === 5 ? Effect.void : Effect.fail(error),
464 ),
465 );
466 }
467 yield* waitUntilGone(getByName(output.name), output.name);
468 }),
469 });