The best way to control Claude Code, Codex and more from any computer or phone
| 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 { |
| 13 | collectPages, |
| 14 | LIST_LOCATIONS, |
| 15 | MAX_POLICY_ID_LENGTH, |
| 16 | defaultWorkerConfig, |
| 17 | defaultYarnConfig, |
| 18 | emptyOnMissing, |
| 19 | fingerprint, |
| 20 | hasAlchemyLabelMap, |
| 21 | locationParent, |
| 22 | normalizeLocation, |
| 23 | parseResourceName, |
| 24 | sameJson, |
| 25 | toPhysicalId, |
| 26 | userLabels, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type AutoscalingPolicyProps = { |
| 31 | /** |
| 32 | * Policy id (the `{policy}` segment of |
| 33 | * `projects/{project}/locations/{location}/autoscalingPolicies/{policy}`). |
| 34 | * If omitted, a unique RFC1035 name is generated. 3-50 characters; |
| 35 | * letters, numbers, hyphens, underscores. Immutable — changing it |
| 36 | * replaces the policy. |
| 37 | */ |
| 38 | policyId?: string; |
| 39 | /** |
| 40 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 41 | * policy. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 42 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 43 | */ |
| 44 | location?: string; |
| 45 | /** |
| 46 | * User labels. Alchemy ownership labels are merged in automatically. |
| 47 | */ |
| 48 | labels?: Record<string, string>; |
| 49 | /** |
| 50 | * Cluster topology this policy applies to (`STANDARD`, `ZERO_SCALE`). |
| 51 | */ |
| 52 | clusterType?: dataproc.AutoscalingPolicyClusterTypeEnum | (string & {}); |
| 53 | /** |
| 54 | * Primary worker bounds. Defaults to min 2, max 3, weight 1. |
| 55 | */ |
| 56 | workerConfig?: dataproc.InstanceGroupAutoscalingPolicyConfig; |
| 57 | /** |
| 58 | * Secondary worker bounds. Optional; omitted means no secondary workers. |
| 59 | */ |
| 60 | secondaryWorkerConfig?: dataproc.InstanceGroupAutoscalingPolicyConfig; |
| 61 | /** |
| 62 | * Duration between scaling events (JSON Duration). Bounds: 2m-1d. |
| 63 | * @default "2m" |
| 64 | */ |
| 65 | cooldownPeriod?: string; |
| 66 | /** |
| 67 | * YARN autoscaling configuration. Defaults to scale-up 0.5, no |
| 68 | * scale-down, 3600s graceful decommission. |
| 69 | */ |
| 70 | yarnConfig?: dataproc.BasicYarnAutoscalingConfig; |
| 71 | /** |
| 72 | * Spark Standalone autoscaling configuration (Spark Standalone clusters). |
| 73 | */ |
| 74 | sparkStandaloneConfig?: dataproc.SparkStandaloneAutoscalingConfig; |
| 75 | }; |
| 76 | |
| 77 | export type AutoscalingPolicy = Resource< |
| 78 | "GCP.Dataproc.AutoscalingPolicy", |
| 79 | AutoscalingPolicyProps, |
| 80 | { |
| 81 | /** Full resource name `projects/{project}/locations/{location}/autoscalingPolicies/{policy}`. */ |
| 82 | name: string; |
| 83 | /** Policy id (last path segment). */ |
| 84 | policyId: string; |
| 85 | /** Project id. */ |
| 86 | project: string; |
| 87 | /** Location id. */ |
| 88 | location: string; |
| 89 | /** User labels (Alchemy ownership labels stripped). */ |
| 90 | labels: Record<string, string>; |
| 91 | /** Cluster topology this policy applies to. */ |
| 92 | clusterType: string | undefined; |
| 93 | /** Primary worker min instances. */ |
| 94 | workerMinInstances: number | undefined; |
| 95 | /** Primary worker max instances. */ |
| 96 | workerMaxInstances: number | undefined; |
| 97 | /** Cooldown period. */ |
| 98 | cooldownPeriod: string | undefined; |
| 99 | }, |
| 100 | never, |
| 101 | Providers |
| 102 | >; |
| 103 | |
| 104 | /** |
| 105 | * A Dataproc autoscaling policy (locations API). |
| 106 | * |
| 107 | * Changing `policyId` or `location` replaces the policy. Worker bounds, |
| 108 | * algorithm, and labels update in place via a full-resource replace. |
| 109 | * |
| 110 | * ### Creating a Policy |
| 111 | * **Example:** Generated name |
| 112 | * ```typescript |
| 113 | * const policy = yield* GCP.Dataproc.AutoscalingPolicy("SparkScale", {}); |
| 114 | * ``` |
| 115 | * |
| 116 | * **Example:** Explicit bounds |
| 117 | * ```typescript |
| 118 | * const policy = yield* GCP.Dataproc.AutoscalingPolicy("SparkScale", { |
| 119 | * policyId: "spark-scale", |
| 120 | * location: "us-central1", |
| 121 | * workerConfig: { minInstances: 2, maxInstances: 6 }, |
| 122 | * labels: { env: "prod" }, |
| 123 | * }); |
| 124 | * ``` |
| 125 | * |
| 126 | * @resource |
| 127 | * @category Dataproc |
| 128 | */ |
| 129 | export const AutoscalingPolicy = Resource<AutoscalingPolicy>("GCP.Dataproc.AutoscalingPolicy"); |
| 130 | |
| 131 | export class AutoscalingPolicyNotResolved extends Data.TaggedError( |
| 132 | "GCP.Dataproc.AutoscalingPolicyNotResolved", |
| 133 | )<{ |
| 134 | resourceName: string; |
| 135 | }> {} |
| 136 | |
| 137 | const resourceName = (project: string, location: string, policyId: string) => |
| 138 | `${locationParent(project, location)}/autoscalingPolicies/${policyId}`; |
| 139 | |
| 140 | const toAttrs = (policy: dataproc.AutoscalingPolicy, project: string, location: string) => { |
| 141 | const name = policy.name ?? ""; |
| 142 | const parsed = parseResourceName(name, "autoscalingPolicies", location); |
| 143 | return { |
| 144 | name, |
| 145 | policyId: policy.id ?? parsed.id, |
| 146 | project: parsed.project || project, |
| 147 | location: parsed.location || location, |
| 148 | labels: userLabels(policy.labels), |
| 149 | clusterType: policy.clusterType, |
| 150 | workerMinInstances: policy.workerConfig?.minInstances, |
| 151 | workerMaxInstances: policy.workerConfig?.maxInstances, |
| 152 | cooldownPeriod: policy.basicAlgorithm?.cooldownPeriod, |
| 153 | }; |
| 154 | }; |
| 155 | |
| 156 | const desiredBody = ( |
| 157 | news: AutoscalingPolicyProps, |
| 158 | policyId: string, |
| 159 | name: string, |
| 160 | desiredLabels: Record<string, string>, |
| 161 | ): dataproc.AutoscalingPolicy => ({ |
| 162 | id: policyId, |
| 163 | name, |
| 164 | labels: desiredLabels, |
| 165 | clusterType: news.clusterType, |
| 166 | workerConfig: news.workerConfig ?? defaultWorkerConfig(), |
| 167 | secondaryWorkerConfig: news.secondaryWorkerConfig, |
| 168 | basicAlgorithm: { |
| 169 | cooldownPeriod: news.cooldownPeriod, |
| 170 | yarnConfig: news.yarnConfig ?? defaultYarnConfig(), |
| 171 | sparkStandaloneConfig: news.sparkStandaloneConfig, |
| 172 | }, |
| 173 | }); |
| 174 | |
| 175 | const getByName = (name: string) => |
| 176 | dataproc |
| 177 | .getProjectsLocationsAutoscalingPolicies({ name }) |
| 178 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 179 | |
| 180 | const listLocation = (project: string, location: string) => |
| 181 | emptyOnMissing( |
| 182 | collectPages( |
| 183 | dataproc.listProjectsLocationsAutoscalingPolicies.pages({ |
| 184 | parent: locationParent(project, location), |
| 185 | pageSize: 1000, |
| 186 | }), |
| 187 | (page) => page.policies, |
| 188 | ).pipe( |
| 189 | Effect.map((items) => |
| 190 | items |
| 191 | .filter((policy) => hasAlchemyLabelMap(policy.labels)) |
| 192 | .map((policy) => toAttrs(policy, project, location)), |
| 193 | ), |
| 194 | ), |
| 195 | ); |
| 196 | |
| 197 | const policyChanged = (current: dataproc.AutoscalingPolicy, desired: dataproc.AutoscalingPolicy) => |
| 198 | !sameJson(current.workerConfig, desired.workerConfig) || |
| 199 | !sameJson(current.secondaryWorkerConfig, desired.secondaryWorkerConfig) || |
| 200 | (current.clusterType ?? "") !== (desired.clusterType ?? "") || |
| 201 | fingerprint(current.basicAlgorithm) !== fingerprint(desired.basicAlgorithm); |
| 202 | |
| 203 | export const AutoscalingPolicyProvider = () => |
| 204 | Provider.succeed(AutoscalingPolicy, { |
| 205 | stables: ["name", "policyId", "project", "location"], |
| 206 | |
| 207 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 208 | if (!isResolved(news)) return undefined; |
| 209 | const env = yield* GcpEnvironment.current; |
| 210 | const previousId = olds?.policyId ?? output?.policyId; |
| 211 | const nextId = news.policyId ?? previousId; |
| 212 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 213 | const nextLocation = normalizeLocation( |
| 214 | news.location ?? olds?.location ?? output?.location, |
| 215 | env.region, |
| 216 | ); |
| 217 | if ( |
| 218 | (previousId !== undefined && nextId !== undefined && previousId !== nextId) || |
| 219 | (output !== undefined && previousLocation !== nextLocation) |
| 220 | ) { |
| 221 | return { |
| 222 | action: "replace" as const, |
| 223 | deleteFirst: |
| 224 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 225 | }; |
| 226 | } |
| 227 | return undefined; |
| 228 | }), |
| 229 | |
| 230 | read: Effect.fn(function* ({ id, olds, output }) { |
| 231 | const env = yield* GcpEnvironment.current; |
| 232 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 233 | const policyId = yield* toPhysicalId( |
| 234 | id, |
| 235 | olds?.policyId, |
| 236 | output?.policyId, |
| 237 | MAX_POLICY_ID_LENGTH, |
| 238 | "policy", |
| 239 | ); |
| 240 | const name = output?.name ?? resourceName(env.project, location, policyId); |
| 241 | const existing = yield* getByName(name); |
| 242 | if (existing === undefined) return undefined; |
| 243 | const attrs = toAttrs(existing, env.project, location); |
| 244 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 245 | }), |
| 246 | |
| 247 | list: () => |
| 248 | Effect.gen(function* () { |
| 249 | const env = yield* GcpEnvironment.current; |
| 250 | const pages = yield* Effect.forEach( |
| 251 | [...new Set([env.region, ...LIST_LOCATIONS])], |
| 252 | (location) => listLocation(env.project, location), |
| 253 | { concurrency: 4 }, |
| 254 | ); |
| 255 | return pages.flat(); |
| 256 | }), |
| 257 | |
| 258 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 259 | const env = yield* GcpEnvironment.current; |
| 260 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 261 | const policyId = yield* toPhysicalId( |
| 262 | id, |
| 263 | news.policyId, |
| 264 | output?.policyId, |
| 265 | MAX_POLICY_ID_LENGTH, |
| 266 | "policy", |
| 267 | ); |
| 268 | const name = resourceName(env.project, location, policyId); |
| 269 | const desiredLabels = { |
| 270 | ...toLabels(news.labels), |
| 271 | ...(yield* createInternalLabels(id)), |
| 272 | }; |
| 273 | const desired = desiredBody(news, policyId, name, desiredLabels); |
| 274 | |
| 275 | let current = yield* getByName(name); |
| 276 | |
| 277 | if (current === undefined) { |
| 278 | const created = yield* dataproc |
| 279 | .createProjectsLocationsAutoscalingPolicies({ |
| 280 | parent: locationParent(env.project, location), |
| 281 | body: desired, |
| 282 | }) |
| 283 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 284 | current = created ?? undefined; |
| 285 | } |
| 286 | |
| 287 | if (current === undefined) { |
| 288 | return yield* new AutoscalingPolicyNotResolved({ resourceName: name }); |
| 289 | } |
| 290 | |
| 291 | const observedLabels = tagRecord(current.labels); |
| 292 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 293 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 294 | |
| 295 | if (labelsChanged || policyChanged(current, desired)) { |
| 296 | current = yield* dataproc.updateProjectsLocationsAutoscalingPolicies({ |
| 297 | name: current.name ?? name, |
| 298 | body: { |
| 299 | ...desired, |
| 300 | name: current.name ?? name, |
| 301 | }, |
| 302 | }); |
| 303 | } |
| 304 | |
| 305 | return toAttrs(current, env.project, location); |
| 306 | }), |
| 307 | |
| 308 | delete: Effect.fn(function* ({ output }) { |
| 309 | if (!output.name) return; |
| 310 | yield* dataproc |
| 311 | .deleteProjectsLocationsAutoscalingPolicies({ name: output.name }) |
| 312 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 313 | yield* waitUntilGone(getByName(output.name), output.name); |
| 314 | }), |
| 315 | }); |