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_MANAGED_CLUSTER_PREFIX_LENGTH, |
| 16 | MAX_POLICY_ID_LENGTH, |
| 17 | defaultWorkflowJobs, |
| 18 | defaultWorkflowPlacement, |
| 19 | emptyOnMissing, |
| 20 | fingerprint, |
| 21 | hasAlchemyLabelMap, |
| 22 | normalizeLocation, |
| 23 | parseResourceName, |
| 24 | regionParent, |
| 25 | rfc1035, |
| 26 | toPhysicalId, |
| 27 | userLabels, |
| 28 | waitUntilGone, |
| 29 | } from "./internal.ts"; |
| 30 | import type { WorkflowTemplateProps } from "./WorkflowTemplate.ts"; |
| 31 | |
| 32 | export type RegionsWorkflowTemplateProps = Omit<WorkflowTemplateProps, "location"> & { |
| 33 | /** |
| 34 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 35 | * template. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 36 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 37 | */ |
| 38 | region?: string; |
| 39 | }; |
| 40 | |
| 41 | export type RegionsWorkflowTemplate = Resource< |
| 42 | "GCP.Dataproc.RegionsWorkflowTemplate", |
| 43 | RegionsWorkflowTemplateProps, |
| 44 | { |
| 45 | /** Full resource name `projects/{project}/regions/{region}/workflowTemplates/{template}`. */ |
| 46 | name: string; |
| 47 | /** Template id (last path segment). */ |
| 48 | templateId: string; |
| 49 | /** Project id. */ |
| 50 | project: string; |
| 51 | /** Region id. */ |
| 52 | region: string; |
| 53 | /** User labels (Alchemy ownership labels stripped). */ |
| 54 | labels: Record<string, string>; |
| 55 | /** Server version used for optimistic updates. */ |
| 56 | version: number | undefined; |
| 57 | /** DAG timeout. */ |
| 58 | dagTimeout: string | undefined; |
| 59 | /** RFC3339 creation timestamp. */ |
| 60 | createTime: string | undefined; |
| 61 | /** RFC3339 last-update timestamp. */ |
| 62 | updateTime: string | undefined; |
| 63 | }, |
| 64 | never, |
| 65 | Providers |
| 66 | >; |
| 67 | |
| 68 | /** |
| 69 | * A Dataproc workflow template (regions API). |
| 70 | * |
| 71 | * Same resource as {@link WorkflowTemplate} addressed via |
| 72 | * `projects/{project}/regions/{region}/workflowTemplates/{template}`. |
| 73 | * |
| 74 | * ### Creating a Template |
| 75 | * **Example:** Generated name |
| 76 | * ```typescript |
| 77 | * const template = yield* GCP.Dataproc.RegionsWorkflowTemplate("Nightly", {}); |
| 78 | * ``` |
| 79 | * |
| 80 | * **Example:** Explicit id and labels |
| 81 | * ```typescript |
| 82 | * const template = yield* GCP.Dataproc.RegionsWorkflowTemplate("Nightly", { |
| 83 | * templateId: "nightly-etl-reg", |
| 84 | * region: "us-central1", |
| 85 | * labels: { env: "prod" }, |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * @resource |
| 90 | * @category Dataproc |
| 91 | */ |
| 92 | export const RegionsWorkflowTemplate = Resource<RegionsWorkflowTemplate>( |
| 93 | "GCP.Dataproc.RegionsWorkflowTemplate", |
| 94 | ); |
| 95 | |
| 96 | export class RegionsWorkflowTemplateNotResolved extends Data.TaggedError( |
| 97 | "GCP.Dataproc.RegionsWorkflowTemplateNotResolved", |
| 98 | )<{ |
| 99 | resourceName: string; |
| 100 | }> {} |
| 101 | |
| 102 | const resourceName = (project: string, region: string, templateId: string) => |
| 103 | `${regionParent(project, region)}/workflowTemplates/${templateId}`; |
| 104 | |
| 105 | const toAttrs = (template: dataproc.WorkflowTemplate, project: string, region: string) => { |
| 106 | const name = template.name ?? ""; |
| 107 | const parsed = parseResourceName(name, "workflowTemplates", region); |
| 108 | return { |
| 109 | name, |
| 110 | templateId: template.id ?? parsed.id, |
| 111 | project: parsed.project || project, |
| 112 | region: parsed.location || region, |
| 113 | labels: userLabels(template.labels), |
| 114 | version: template.version, |
| 115 | dagTimeout: template.dagTimeout, |
| 116 | createTime: template.createTime, |
| 117 | updateTime: template.updateTime, |
| 118 | }; |
| 119 | }; |
| 120 | |
| 121 | const desiredBody = ( |
| 122 | news: RegionsWorkflowTemplateProps, |
| 123 | templateId: string, |
| 124 | name: string, |
| 125 | desiredLabels: Record<string, string>, |
| 126 | version?: number, |
| 127 | ): dataproc.WorkflowTemplate => ({ |
| 128 | id: templateId, |
| 129 | name, |
| 130 | labels: desiredLabels, |
| 131 | version, |
| 132 | jobs: news.jobs ?? defaultWorkflowJobs(), |
| 133 | placement: |
| 134 | news.placement ?? |
| 135 | defaultWorkflowPlacement( |
| 136 | rfc1035(`${templateId}-c`, MAX_MANAGED_CLUSTER_PREFIX_LENGTH, "cluster"), |
| 137 | ), |
| 138 | dagTimeout: news.dagTimeout, |
| 139 | parameters: news.parameters, |
| 140 | encryptionConfig: news.kmsKey ? { kmsKey: news.kmsKey } : undefined, |
| 141 | }); |
| 142 | |
| 143 | const getByName = (name: string) => |
| 144 | dataproc |
| 145 | .getProjectsRegionsWorkflowTemplates({ name }) |
| 146 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 147 | |
| 148 | const listRegion = (project: string, region: string) => |
| 149 | emptyOnMissing( |
| 150 | collectPages( |
| 151 | dataproc.listProjectsRegionsWorkflowTemplates.pages({ |
| 152 | parent: regionParent(project, region), |
| 153 | pageSize: 1000, |
| 154 | }), |
| 155 | (page) => page.templates, |
| 156 | ).pipe( |
| 157 | Effect.map((items) => |
| 158 | items |
| 159 | .filter((template) => hasAlchemyLabelMap(template.labels)) |
| 160 | .map((template) => toAttrs(template, project, region)), |
| 161 | ), |
| 162 | ), |
| 163 | ); |
| 164 | |
| 165 | const templateChanged = (current: dataproc.WorkflowTemplate, desired: dataproc.WorkflowTemplate) => |
| 166 | fingerprint(current.jobs) !== fingerprint(desired.jobs) || |
| 167 | fingerprint(current.placement) !== fingerprint(desired.placement) || |
| 168 | (current.dagTimeout ?? "") !== (desired.dagTimeout ?? "") || |
| 169 | fingerprint(current.parameters) !== fingerprint(desired.parameters) || |
| 170 | (current.encryptionConfig?.kmsKey ?? "") !== (desired.encryptionConfig?.kmsKey ?? ""); |
| 171 | |
| 172 | export const RegionsWorkflowTemplateProvider = () => |
| 173 | Provider.succeed(RegionsWorkflowTemplate, { |
| 174 | stables: ["name", "templateId", "project", "region", "createTime"], |
| 175 | |
| 176 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 177 | if (!isResolved(news)) return undefined; |
| 178 | const env = yield* GcpEnvironment.current; |
| 179 | const previousId = olds?.templateId ?? output?.templateId; |
| 180 | const nextId = news.templateId ?? previousId; |
| 181 | const previousRegion = normalizeLocation(olds?.region ?? output?.region, env.region); |
| 182 | const nextRegion = normalizeLocation( |
| 183 | news.region ?? olds?.region ?? output?.region, |
| 184 | env.region, |
| 185 | ); |
| 186 | if ( |
| 187 | (previousId !== undefined && nextId !== undefined && previousId !== nextId) || |
| 188 | (output !== undefined && previousRegion !== nextRegion) |
| 189 | ) { |
| 190 | return { |
| 191 | action: "replace" as const, |
| 192 | deleteFirst: |
| 193 | previousRegion === nextRegion && previousId !== undefined && nextId === previousId, |
| 194 | }; |
| 195 | } |
| 196 | return undefined; |
| 197 | }), |
| 198 | |
| 199 | read: Effect.fn(function* ({ id, olds, output }) { |
| 200 | const env = yield* GcpEnvironment.current; |
| 201 | const region = normalizeLocation(olds?.region ?? output?.region, env.region); |
| 202 | const templateId = yield* toPhysicalId( |
| 203 | id, |
| 204 | olds?.templateId, |
| 205 | output?.templateId, |
| 206 | MAX_POLICY_ID_LENGTH, |
| 207 | "template", |
| 208 | ); |
| 209 | const name = output?.name ?? resourceName(env.project, region, templateId); |
| 210 | const existing = yield* getByName(name); |
| 211 | if (existing === undefined) return undefined; |
| 212 | const attrs = toAttrs(existing, env.project, region); |
| 213 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 214 | }), |
| 215 | |
| 216 | list: () => |
| 217 | Effect.gen(function* () { |
| 218 | const env = yield* GcpEnvironment.current; |
| 219 | const pages = yield* Effect.forEach( |
| 220 | [...new Set([env.region, ...LIST_LOCATIONS])], |
| 221 | (region) => listRegion(env.project, region), |
| 222 | { concurrency: 4 }, |
| 223 | ); |
| 224 | return pages.flat(); |
| 225 | }), |
| 226 | |
| 227 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 228 | const env = yield* GcpEnvironment.current; |
| 229 | const region = normalizeLocation(news.region ?? output?.region, env.region); |
| 230 | const templateId = yield* toPhysicalId( |
| 231 | id, |
| 232 | news.templateId, |
| 233 | output?.templateId, |
| 234 | MAX_POLICY_ID_LENGTH, |
| 235 | "template", |
| 236 | ); |
| 237 | const name = resourceName(env.project, region, templateId); |
| 238 | const desiredLabels = { |
| 239 | ...toLabels(news.labels), |
| 240 | ...(yield* createInternalLabels(id)), |
| 241 | }; |
| 242 | |
| 243 | let current = yield* getByName(name); |
| 244 | |
| 245 | if (current === undefined) { |
| 246 | const created = yield* dataproc |
| 247 | .createProjectsRegionsWorkflowTemplates({ |
| 248 | parent: regionParent(env.project, region), |
| 249 | body: desiredBody(news, templateId, name, desiredLabels), |
| 250 | }) |
| 251 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 252 | current = created ?? undefined; |
| 253 | } |
| 254 | |
| 255 | if (current === undefined) { |
| 256 | return yield* new RegionsWorkflowTemplateNotResolved({ resourceName: name }); |
| 257 | } |
| 258 | |
| 259 | const desired = desiredBody( |
| 260 | news, |
| 261 | templateId, |
| 262 | current.name ?? name, |
| 263 | desiredLabels, |
| 264 | current.version, |
| 265 | ); |
| 266 | const observedLabels = tagRecord(current.labels); |
| 267 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 268 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 269 | |
| 270 | if (labelsChanged || templateChanged(current, desired)) { |
| 271 | current = yield* dataproc.updateProjectsRegionsWorkflowTemplates({ |
| 272 | name: current.name ?? name, |
| 273 | body: desired, |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | return toAttrs(current, env.project, region); |
| 278 | }), |
| 279 | |
| 280 | delete: Effect.fn(function* ({ output }) { |
| 281 | if (!output.name) return; |
| 282 | yield* dataproc |
| 283 | .deleteProjectsRegionsWorkflowTemplates({ name: output.name }) |
| 284 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 285 | yield* waitUntilGone(getByName(output.name), output.name); |
| 286 | }), |
| 287 | }); |