The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as apphub from "@distilled.cloud/gcp/apphub_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 * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | type Attributes, |
| 13 | type WorkloadProperties, |
| 14 | type WorkloadReference, |
| 15 | encodeOwnership, |
| 16 | expandApplication, |
| 17 | fieldMask, |
| 18 | listNestedOwned, |
| 19 | listOwnedPages, |
| 20 | locationParent, |
| 21 | normalizeLocation, |
| 22 | ownedByAlchemy, |
| 23 | parseName, |
| 24 | parseOwnership, |
| 25 | replaceOnIdentity, |
| 26 | resolveDiscoveredWorkload, |
| 27 | ResourceNotResolved, |
| 28 | sameJson, |
| 29 | sameText, |
| 30 | toPhysicalId, |
| 31 | waitForOperation, |
| 32 | waitUntilExists, |
| 33 | waitUntilGone, |
| 34 | waitUntilReady, |
| 35 | } from "./internal.ts"; |
| 36 | |
| 37 | export type ApplicationsWorkloadProps = { |
| 38 | /** |
| 39 | * Parent Application. Full name |
| 40 | * `projects/{project}/locations/{location}/applications/{application}` |
| 41 | * or the application id (combined with `location`). Immutable — |
| 42 | * changing it replaces the workload. |
| 43 | */ |
| 44 | application: string; |
| 45 | /** |
| 46 | * Region used when `application` is a bare id. |
| 47 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 48 | * region, else `us-central1`) |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * Workload id (the `{workload}` segment). Must be RFC1035 (63 chars). |
| 53 | * If omitted, a unique name is generated. Immutable — changing it |
| 54 | * replaces the workload. |
| 55 | */ |
| 56 | workloadId?: string; |
| 57 | /** |
| 58 | * Resource name of the original discovered workload, or the underlying |
| 59 | * resource URI (looked up via `discoveredWorkloads:lookup`). Immutable |
| 60 | * — changing it replaces the workload. |
| 61 | */ |
| 62 | discoveredWorkload: string; |
| 63 | /** |
| 64 | * User-defined display name. Maximum length is 63 characters. Defaults |
| 65 | * to the workload id. |
| 66 | */ |
| 67 | displayName?: string; |
| 68 | /** |
| 69 | * User-defined description. Maximum length is 2048 characters. |
| 70 | * Workloads have no labels field, so Alchemy stamps ownership into a |
| 71 | * `[alchemy …]` prefix and strips it from attributes. |
| 72 | */ |
| 73 | description?: string; |
| 74 | /** |
| 75 | * Consumer-provided attributes (criticality, environment, owners). |
| 76 | */ |
| 77 | attributes?: Attributes; |
| 78 | }; |
| 79 | |
| 80 | export type ApplicationsWorkload = Resource< |
| 81 | "GCP.AppHub.ApplicationsWorkload", |
| 82 | ApplicationsWorkloadProps, |
| 83 | { |
| 84 | /** Full resource name. */ |
| 85 | name: string; |
| 86 | /** Workload id (last path segment). */ |
| 87 | workloadId: string; |
| 88 | /** Parent Application resource name. */ |
| 89 | application: string; |
| 90 | /** Project id. */ |
| 91 | project: string; |
| 92 | /** Location id. */ |
| 93 | location: string; |
| 94 | /** Discovered workload resource name. */ |
| 95 | discoveredWorkload: string | undefined; |
| 96 | /** User-defined display name. */ |
| 97 | displayName: string | undefined; |
| 98 | /** User description with the Alchemy ownership prefix stripped. */ |
| 99 | description: string | undefined; |
| 100 | /** Consumer-provided attributes. */ |
| 101 | attributes: Attributes | undefined; |
| 102 | /** Reference to the underlying compute resource. */ |
| 103 | workloadReference: WorkloadReference | undefined; |
| 104 | /** Properties of the underlying compute resource. */ |
| 105 | workloadProperties: WorkloadProperties | undefined; |
| 106 | /** Server-reported state. */ |
| 107 | state: string | undefined; |
| 108 | /** Server-generated resource uid. */ |
| 109 | uid: string | undefined; |
| 110 | /** RFC3339 creation timestamp. */ |
| 111 | createTime: string | undefined; |
| 112 | /** RFC3339 last-update timestamp. */ |
| 113 | updateTime: string | undefined; |
| 114 | }, |
| 115 | never, |
| 116 | Providers |
| 117 | >; |
| 118 | |
| 119 | /** |
| 120 | * An App Hub workload registered onto an Application. A workload is a |
| 121 | * discovered binary deployment (managed instance group, GKE deployment, |
| 122 | * …) that performs the smallest logical subset of business |
| 123 | * functionality. |
| 124 | * |
| 125 | * Workloads have no labels field, so Alchemy stamps ownership into the |
| 126 | * description for `list` / nuke. Application, workload id, and |
| 127 | * `discoveredWorkload` are immutable. Display name, description, and |
| 128 | * attributes update in place. |
| 129 | * |
| 130 | * ### Creating a Workload |
| 131 | * **Example:** Register a discovered workload |
| 132 | * ```typescript |
| 133 | * const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", { |
| 134 | * application: app.name, |
| 135 | * discoveredWorkload: discovered.name, |
| 136 | * }); |
| 137 | * ``` |
| 138 | * |
| 139 | * **Example:** Lookup from an underlying resource URI |
| 140 | * ```typescript |
| 141 | * const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", { |
| 142 | * application: app.name, |
| 143 | * discoveredWorkload: instanceGroup.instanceGroup, |
| 144 | * displayName: "api", |
| 145 | * description: "checkout mig", |
| 146 | * }); |
| 147 | * ``` |
| 148 | * |
| 149 | * ### Updating a Workload |
| 150 | * Change props on the same logical id; the engine keeps the physical id. |
| 151 | * |
| 152 | * **Example:** Display name and attributes |
| 153 | * ```typescript |
| 154 | * const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", { |
| 155 | * application: app.name, |
| 156 | * discoveredWorkload: discovered.name, |
| 157 | * displayName: "api-v2", |
| 158 | * attributes: { criticality: { type: "HIGH" } }, |
| 159 | * }); |
| 160 | * ``` |
| 161 | * |
| 162 | * @resource |
| 163 | * @category AppHub |
| 164 | */ |
| 165 | export const ApplicationsWorkload = Resource<ApplicationsWorkload>( |
| 166 | "GCP.AppHub.ApplicationsWorkload", |
| 167 | ); |
| 168 | |
| 169 | const resourceName = (application: string, workloadId: string) => |
| 170 | `${application}/workloads/${workloadId}`; |
| 171 | |
| 172 | const toAttrs = (item: apphub.Workload, project: string, region: string) => { |
| 173 | const name = item.name ?? ""; |
| 174 | const parsed = parseName(name, "workloads", region); |
| 175 | const ownership = parseOwnership(item.description); |
| 176 | return { |
| 177 | name, |
| 178 | workloadId: parsed.id, |
| 179 | application: parsed.application, |
| 180 | project: parsed.project || project, |
| 181 | location: parsed.location, |
| 182 | discoveredWorkload: item.discoveredWorkload, |
| 183 | displayName: item.displayName, |
| 184 | description: ownership.text, |
| 185 | attributes: item.attributes, |
| 186 | workloadReference: item.workloadReference, |
| 187 | workloadProperties: item.workloadProperties, |
| 188 | state: item.state, |
| 189 | uid: item.uid, |
| 190 | createTime: item.createTime, |
| 191 | updateTime: item.updateTime, |
| 192 | }; |
| 193 | }; |
| 194 | |
| 195 | const getByName = (name: string) => |
| 196 | name.length === 0 |
| 197 | ? Effect.undefined |
| 198 | : apphub |
| 199 | .getProjectsLocationsApplicationsWorkloads({ name }) |
| 200 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 201 | |
| 202 | const listOwned = (project: string, region: string) => |
| 203 | listNestedOwned(project, region, (application) => |
| 204 | listOwnedPages( |
| 205 | apphub.listProjectsLocationsApplicationsWorkloads.pages({ |
| 206 | parent: application, |
| 207 | pageSize: 1000, |
| 208 | }), |
| 209 | (page) => page.workloads, |
| 210 | (item) => item.description, |
| 211 | ), |
| 212 | ); |
| 213 | |
| 214 | export const ApplicationsWorkloadProvider = () => |
| 215 | Provider.succeed(ApplicationsWorkload, { |
| 216 | stables: [ |
| 217 | "name", |
| 218 | "workloadId", |
| 219 | "application", |
| 220 | "project", |
| 221 | "location", |
| 222 | "discoveredWorkload", |
| 223 | "uid", |
| 224 | "createTime", |
| 225 | ], |
| 226 | |
| 227 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 228 | if (!isResolved(news)) return undefined; |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const previousDiscovered = olds?.discoveredWorkload ?? output?.discoveredWorkload; |
| 231 | return replaceOnIdentity({ |
| 232 | previousId: olds?.workloadId ?? output?.workloadId, |
| 233 | nextId: news.workloadId ?? olds?.workloadId ?? output?.workloadId, |
| 234 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 235 | nextLocation: normalizeLocation( |
| 236 | news.location ?? olds?.location ?? output?.location, |
| 237 | env.region, |
| 238 | ), |
| 239 | previousParent: olds?.application ?? output?.application, |
| 240 | nextParent: news.application, |
| 241 | extra: |
| 242 | previousDiscovered !== undefined && |
| 243 | news.discoveredWorkload !== undefined && |
| 244 | previousDiscovered !== news.discoveredWorkload && |
| 245 | !previousDiscovered.endsWith(`/${news.discoveredWorkload}`) && |
| 246 | !news.discoveredWorkload.endsWith(`/${previousDiscovered}`), |
| 247 | }); |
| 248 | }), |
| 249 | |
| 250 | read: Effect.fn(function* ({ id, olds, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const workloadId = yield* toPhysicalId(id, olds?.workloadId, output?.workloadId, "workload"); |
| 253 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 254 | const application = expandApplication( |
| 255 | olds?.application ?? output?.application ?? "", |
| 256 | env.project, |
| 257 | location, |
| 258 | ); |
| 259 | const name = output?.name ?? resourceName(application, workloadId); |
| 260 | const existing = yield* getByName(name); |
| 261 | if (existing === undefined) return undefined; |
| 262 | const attrs = toAttrs(existing, env.project, env.region); |
| 263 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 264 | }), |
| 265 | |
| 266 | list: () => |
| 267 | Effect.gen(function* () { |
| 268 | const env = yield* GcpEnvironment.current; |
| 269 | const items = yield* listOwned(env.project, env.region); |
| 270 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 271 | }), |
| 272 | |
| 273 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 274 | const env = yield* GcpEnvironment.current; |
| 275 | const workloadId = yield* toPhysicalId(id, news.workloadId, output?.workloadId, "workload"); |
| 276 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 277 | const application = expandApplication(news.application, env.project, location); |
| 278 | const name = resourceName(application, workloadId); |
| 279 | const ownership = yield* createInternalLabels(id); |
| 280 | const description = encodeOwnership(ownership, news.description); |
| 281 | const displayName = news.displayName ?? workloadId; |
| 282 | const discoveredWorkload = yield* resolveDiscoveredWorkload( |
| 283 | locationParent(env.project, location), |
| 284 | news.discoveredWorkload, |
| 285 | env.project, |
| 286 | location, |
| 287 | ); |
| 288 | |
| 289 | let current = yield* getByName(output?.name ?? name); |
| 290 | |
| 291 | if (current === undefined) { |
| 292 | const created = yield* apphub |
| 293 | .createProjectsLocationsApplicationsWorkloads({ |
| 294 | parent: application, |
| 295 | workloadId, |
| 296 | body: { |
| 297 | discoveredWorkload, |
| 298 | displayName, |
| 299 | description, |
| 300 | attributes: news.attributes, |
| 301 | }, |
| 302 | }) |
| 303 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 304 | if (created !== undefined) { |
| 305 | yield* waitForOperation(created); |
| 306 | } |
| 307 | current = yield* waitUntilExists(getByName(name), name); |
| 308 | } |
| 309 | |
| 310 | if (current === undefined) { |
| 311 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 312 | } |
| 313 | |
| 314 | current = yield* waitUntilReady( |
| 315 | getByName(current.name ?? name), |
| 316 | current.name ?? name, |
| 317 | (item) => item.state, |
| 318 | ); |
| 319 | |
| 320 | const mask = fieldMask([ |
| 321 | !sameText(current.description, description) && "description", |
| 322 | !sameText(current.displayName, displayName) && "displayName", |
| 323 | news.attributes !== undefined && |
| 324 | !sameJson(current.attributes, news.attributes) && |
| 325 | "attributes", |
| 326 | ]); |
| 327 | |
| 328 | if (mask.length > 0) { |
| 329 | const operation = yield* apphub.patchProjectsLocationsApplicationsWorkloads({ |
| 330 | name: current.name ?? name, |
| 331 | updateMask: mask, |
| 332 | body: { |
| 333 | displayName, |
| 334 | description, |
| 335 | attributes: news.attributes, |
| 336 | }, |
| 337 | }); |
| 338 | yield* waitForOperation(operation); |
| 339 | current = yield* waitUntilReady( |
| 340 | getByName(current.name ?? name), |
| 341 | current.name ?? name, |
| 342 | (item) => item.state, |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | return toAttrs(current, env.project, env.region); |
| 347 | }), |
| 348 | |
| 349 | delete: Effect.fn(function* ({ output }) { |
| 350 | const operation = yield* apphub |
| 351 | .deleteProjectsLocationsApplicationsWorkloads({ name: output.name }) |
| 352 | .pipe( |
| 353 | Effect.retry({ |
| 354 | while: (error) => error._tag === "Conflict", |
| 355 | times: 8, |
| 356 | schedule: Schedule.spaced("2 seconds"), |
| 357 | }), |
| 358 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 359 | ); |
| 360 | if (operation !== undefined) { |
| 361 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 362 | } |
| 363 | yield* waitUntilGone(getByName(output.name), output.name); |
| 364 | }), |
| 365 | }); |