@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 280 lines 9.27 KB
Raw
1 import * as dataproc from "@distilled.cloud/gcp/dataproc_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 Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import type { AutoscalingPolicyProps } from "./AutoscalingPolicy.ts";
13 import {
14 collectPages,
15 LIST_LOCATIONS,
16 MAX_POLICY_ID_LENGTH,
17 defaultWorkerConfig,
18 defaultYarnConfig,
19 emptyOnMissing,
20 fingerprint,
21 hasAlchemyLabelMap,
22 normalizeLocation,
23 parseResourceName,
24 regionParent,
25 sameJson,
26 toPhysicalId,
27 userLabels,
28 waitUntilGone,
29 } from "./internal.ts";
30
31 export type RegionsAutoscalingPolicyProps = Omit<AutoscalingPolicyProps, "location"> & {
32 /**
33 * Region (`us-central1`, …). Immutable — changing it replaces the
34 * policy. `US-CENTRAL1` is accepted and normalized to `us-central1`.
35 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
36 */
37 region?: string;
38 };
39
40 export type RegionsAutoscalingPolicy = Resource<
41 "GCP.Dataproc.RegionsAutoscalingPolicy",
42 RegionsAutoscalingPolicyProps,
43 {
44 /** Full resource name `projects/{project}/regions/{region}/autoscalingPolicies/{policy}`. */
45 name: string;
46 /** Policy id (last path segment). */
47 policyId: string;
48 /** Project id. */
49 project: string;
50 /** Region id. */
51 region: string;
52 /** User labels (Alchemy ownership labels stripped). */
53 labels: Record<string, string>;
54 /** Cluster topology this policy applies to. */
55 clusterType: string | undefined;
56 /** Primary worker min instances. */
57 workerMinInstances: number | undefined;
58 /** Primary worker max instances. */
59 workerMaxInstances: number | undefined;
60 /** Cooldown period. */
61 cooldownPeriod: string | undefined;
62 },
63 never,
64 Providers
65 >;
66
67 /**
68 * A Dataproc autoscaling policy (regions API).
69 *
70 * Same resource as {@link AutoscalingPolicy} addressed via
71 * `projects/{project}/regions/{region}/autoscalingPolicies/{policy}`.
72 *
73 * ### Creating a Policy
74 * **Example:** Generated name
75 * ```typescript
76 * const policy = yield* GCP.Dataproc.RegionsAutoscalingPolicy("SparkScale", {});
77 * ```
78 *
79 * **Example:** Explicit bounds
80 * ```typescript
81 * const policy = yield* GCP.Dataproc.RegionsAutoscalingPolicy("SparkScale", {
82 * policyId: "spark-scale-reg",
83 * region: "us-central1",
84 * workerConfig: { minInstances: 2, maxInstances: 6 },
85 * labels: { env: "prod" },
86 * });
87 * ```
88 *
89 * @resource
90 * @category Dataproc
91 */
92 export const RegionsAutoscalingPolicy = Resource<RegionsAutoscalingPolicy>(
93 "GCP.Dataproc.RegionsAutoscalingPolicy",
94 );
95
96 export class RegionsAutoscalingPolicyNotResolved extends Data.TaggedError(
97 "GCP.Dataproc.RegionsAutoscalingPolicyNotResolved",
98 )<{
99 resourceName: string;
100 }> {}
101
102 const resourceName = (project: string, region: string, policyId: string) =>
103 `${regionParent(project, region)}/autoscalingPolicies/${policyId}`;
104
105 const toAttrs = (policy: dataproc.AutoscalingPolicy, project: string, region: string) => {
106 const name = policy.name ?? "";
107 const parsed = parseResourceName(name, "autoscalingPolicies", region);
108 return {
109 name,
110 policyId: policy.id ?? parsed.id,
111 project: parsed.project || project,
112 region: parsed.location || region,
113 labels: userLabels(policy.labels),
114 clusterType: policy.clusterType,
115 workerMinInstances: policy.workerConfig?.minInstances,
116 workerMaxInstances: policy.workerConfig?.maxInstances,
117 cooldownPeriod: policy.basicAlgorithm?.cooldownPeriod,
118 };
119 };
120
121 const desiredBody = (
122 news: RegionsAutoscalingPolicyProps,
123 policyId: string,
124 name: string,
125 desiredLabels: Record<string, string>,
126 ): dataproc.AutoscalingPolicy => ({
127 id: policyId,
128 name,
129 labels: desiredLabels,
130 clusterType: news.clusterType,
131 workerConfig: news.workerConfig ?? defaultWorkerConfig(),
132 secondaryWorkerConfig: news.secondaryWorkerConfig,
133 basicAlgorithm: {
134 cooldownPeriod: news.cooldownPeriod,
135 yarnConfig: news.yarnConfig ?? defaultYarnConfig(),
136 sparkStandaloneConfig: news.sparkStandaloneConfig,
137 },
138 });
139
140 const getByName = (name: string) =>
141 dataproc
142 .getProjectsRegionsAutoscalingPolicies({ name })
143 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
144
145 const listRegion = (project: string, region: string) =>
146 emptyOnMissing(
147 collectPages(
148 dataproc.listProjectsRegionsAutoscalingPolicies.pages({
149 parent: regionParent(project, region),
150 pageSize: 1000,
151 }),
152 (page) => page.policies,
153 ).pipe(
154 Effect.map((items) =>
155 items
156 .filter((policy) => hasAlchemyLabelMap(policy.labels))
157 .map((policy) => toAttrs(policy, project, region)),
158 ),
159 ),
160 );
161
162 const policyChanged = (current: dataproc.AutoscalingPolicy, desired: dataproc.AutoscalingPolicy) =>
163 !sameJson(current.workerConfig, desired.workerConfig) ||
164 !sameJson(current.secondaryWorkerConfig, desired.secondaryWorkerConfig) ||
165 (current.clusterType ?? "") !== (desired.clusterType ?? "") ||
166 fingerprint(current.basicAlgorithm) !== fingerprint(desired.basicAlgorithm);
167
168 export const RegionsAutoscalingPolicyProvider = () =>
169 Provider.succeed(RegionsAutoscalingPolicy, {
170 stables: ["name", "policyId", "project", "region"],
171
172 diff: Effect.fn(function* ({ news, olds, output }) {
173 if (!isResolved(news)) return undefined;
174 const env = yield* GcpEnvironment.current;
175 const previousId = olds?.policyId ?? output?.policyId;
176 const nextId = news.policyId ?? previousId;
177 const previousRegion = normalizeLocation(olds?.region ?? output?.region, env.region);
178 const nextRegion = normalizeLocation(
179 news.region ?? olds?.region ?? output?.region,
180 env.region,
181 );
182 if (
183 (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||
184 (output !== undefined && previousRegion !== nextRegion)
185 ) {
186 return {
187 action: "replace" as const,
188 deleteFirst:
189 previousRegion === nextRegion && previousId !== undefined && nextId === previousId,
190 };
191 }
192 return undefined;
193 }),
194
195 read: Effect.fn(function* ({ id, olds, output }) {
196 const env = yield* GcpEnvironment.current;
197 const region = normalizeLocation(olds?.region ?? output?.region, env.region);
198 const policyId = yield* toPhysicalId(
199 id,
200 olds?.policyId,
201 output?.policyId,
202 MAX_POLICY_ID_LENGTH,
203 "policy",
204 );
205 const name = output?.name ?? resourceName(env.project, region, policyId);
206 const existing = yield* getByName(name);
207 if (existing === undefined) return undefined;
208 const attrs = toAttrs(existing, env.project, region);
209 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
210 }),
211
212 list: () =>
213 Effect.gen(function* () {
214 const env = yield* GcpEnvironment.current;
215 const pages = yield* Effect.forEach(
216 [...new Set([env.region, ...LIST_LOCATIONS])],
217 (region) => listRegion(env.project, region),
218 { concurrency: 4 },
219 );
220 return pages.flat();
221 }),
222
223 reconcile: Effect.fn(function* ({ id, news, output }) {
224 const env = yield* GcpEnvironment.current;
225 const region = normalizeLocation(news.region ?? output?.region, env.region);
226 const policyId = yield* toPhysicalId(
227 id,
228 news.policyId,
229 output?.policyId,
230 MAX_POLICY_ID_LENGTH,
231 "policy",
232 );
233 const name = resourceName(env.project, region, policyId);
234 const desiredLabels = {
235 ...toLabels(news.labels),
236 ...(yield* createInternalLabels(id)),
237 };
238 const desired = desiredBody(news, policyId, name, desiredLabels);
239
240 let current = yield* getByName(name);
241
242 if (current === undefined) {
243 const created = yield* dataproc
244 .createProjectsRegionsAutoscalingPolicies({
245 parent: regionParent(env.project, region),
246 body: desired,
247 })
248 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
249 current = created ?? undefined;
250 }
251
252 if (current === undefined) {
253 return yield* new RegionsAutoscalingPolicyNotResolved({ resourceName: name });
254 }
255
256 const observedLabels = tagRecord(current.labels);
257 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
258 const labelsChanged = upsert.length > 0 || removed.length > 0;
259
260 if (labelsChanged || policyChanged(current, desired)) {
261 current = yield* dataproc.updateProjectsRegionsAutoscalingPolicies({
262 name: current.name ?? name,
263 body: {
264 ...desired,
265 name: current.name ?? name,
266 },
267 });
268 }
269
270 return toAttrs(current, env.project, region);
271 }),
272
273 delete: Effect.fn(function* ({ output }) {
274 if (!output.name) return;
275 yield* dataproc
276 .deleteProjectsRegionsAutoscalingPolicies({ name: output.name })
277 .pipe(Effect.catchTag("NotFound", () => Effect.void));
278 yield* waitUntilGone(getByName(output.name), output.name);
279 }),
280 });