The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as rma from "@distilled.cloud/gcp/rapidmigrationassessment_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 | expectedAssetCountOf, |
| 14 | fieldMask, |
| 15 | isGoneState, |
| 16 | isPausedState, |
| 17 | isRegisteredState, |
| 18 | listOwnedCollectors, |
| 19 | locationParent, |
| 20 | normalizeLocation, |
| 21 | parseName, |
| 22 | replaceOnIdentity, |
| 23 | ResourceNotResolved, |
| 24 | sameNumber, |
| 25 | sameText, |
| 26 | toPhysicalId, |
| 27 | userLabels, |
| 28 | waitForOperation, |
| 29 | waitUntilExists, |
| 30 | waitUntilGone, |
| 31 | waitUntilReady, |
| 32 | } from "./internal.ts"; |
| 33 | |
| 34 | export type CollectorState = rma.CollectorStateEnum | (string & {}); |
| 35 | |
| 36 | export type CollectorProps = { |
| 37 | /** |
| 38 | * Collector id (the `{collector}` segment of |
| 39 | * `projects/{project}/locations/{location}/collectors/{collector}`). |
| 40 | * If omitted, a unique RFC1035 name is generated. Immutable — |
| 41 | * changing it replaces the collector. |
| 42 | */ |
| 43 | collectorId?: string; |
| 44 | /** |
| 45 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 46 | * collector. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 47 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 48 | */ |
| 49 | location?: string; |
| 50 | /** |
| 51 | * User-facing display name. Alchemy falls back to the generated |
| 52 | * collector id. |
| 53 | */ |
| 54 | displayName?: string; |
| 55 | /** |
| 56 | * User-specified description of the collector. |
| 57 | */ |
| 58 | description?: string; |
| 59 | /** |
| 60 | * How many days to collect data. |
| 61 | */ |
| 62 | collectionDays?: number; |
| 63 | /** |
| 64 | * Service account email used to ingest data to this collector. |
| 65 | */ |
| 66 | serviceAccount?: string; |
| 67 | /** |
| 68 | * User-specified expected asset count. |
| 69 | */ |
| 70 | expectedAssetCount?: number | string; |
| 71 | /** |
| 72 | * URI for the End User License Agreement accepted by the customer. |
| 73 | */ |
| 74 | eulaUri?: string; |
| 75 | /** |
| 76 | * When true, pause an active collector. When false, resume a paused |
| 77 | * collector. Omitted leaves pause state unchanged. |
| 78 | */ |
| 79 | paused?: boolean; |
| 80 | /** |
| 81 | * When true, register a collector that is ready to use. Registration |
| 82 | * is one-way; omitted or false leaves the collector unregistered. |
| 83 | */ |
| 84 | registered?: boolean; |
| 85 | /** |
| 86 | * User labels. Alchemy ownership labels are merged in automatically. |
| 87 | */ |
| 88 | labels?: Record<string, string>; |
| 89 | }; |
| 90 | |
| 91 | export type Collector = Resource< |
| 92 | "GCP.RapidMigrationAssessment.Collector", |
| 93 | CollectorProps, |
| 94 | { |
| 95 | /** Full resource name. */ |
| 96 | name: string; |
| 97 | /** Collector id (last path segment). */ |
| 98 | collectorId: string; |
| 99 | /** Project id. */ |
| 100 | project: string; |
| 101 | /** Location id. */ |
| 102 | location: string; |
| 103 | /** User-facing display name. */ |
| 104 | displayName: string | undefined; |
| 105 | /** User-specified description. */ |
| 106 | description: string | undefined; |
| 107 | /** How many days to collect data. */ |
| 108 | collectionDays: number | undefined; |
| 109 | /** Service account email used to ingest data. */ |
| 110 | serviceAccount: string | undefined; |
| 111 | /** Expected asset count. */ |
| 112 | expectedAssetCount: string | undefined; |
| 113 | /** EULA URI. */ |
| 114 | eulaUri: string | undefined; |
| 115 | /** User labels (Alchemy ownership labels stripped). */ |
| 116 | labels: Record<string, string>; |
| 117 | /** Server-reported collector state. */ |
| 118 | state: string | undefined; |
| 119 | /** Whether the collector is paused. */ |
| 120 | paused: boolean; |
| 121 | /** Whether the collector has been registered. */ |
| 122 | registered: boolean; |
| 123 | /** Client version reported by the appliance. */ |
| 124 | clientVersion: string | undefined; |
| 125 | /** Cloud Storage bucket created with this collector. */ |
| 126 | bucket: string | undefined; |
| 127 | /** Migration Center guest-OS scan reference. */ |
| 128 | guestOsScan: rma.GuestOsScan | undefined; |
| 129 | /** Migration Center vSphere scan reference. */ |
| 130 | vsphereScan: rma.VSphereScan | undefined; |
| 131 | /** RFC3339 creation timestamp. */ |
| 132 | createTime: string | undefined; |
| 133 | /** RFC3339 last-update timestamp. */ |
| 134 | updateTime: string | undefined; |
| 135 | }, |
| 136 | never, |
| 137 | Providers |
| 138 | >; |
| 139 | |
| 140 | /** |
| 141 | * A Rapid Migration Assessment collector that manages the on-prem |
| 142 | * appliance gathering customer asset information. |
| 143 | * |
| 144 | * `collectorId` and `location` are immutable. Display name, description, |
| 145 | * collection days, service account, expected asset count, EULA URI, and |
| 146 | * labels update in place. Set `paused` to pause or resume, and |
| 147 | * `registered` to register a collector that is ready to use. |
| 148 | * |
| 149 | * ### Creating a Collector |
| 150 | * **Example:** Generated name |
| 151 | * ```typescript |
| 152 | * const collector = yield* GCP.RapidMigrationAssessment.Collector("OnPrem", { |
| 153 | * collectionDays: 30, |
| 154 | * expectedAssetCount: 100, |
| 155 | * labels: { env: "test" }, |
| 156 | * }); |
| 157 | * ``` |
| 158 | * |
| 159 | * **Example:** Explicit id and service account |
| 160 | * ```typescript |
| 161 | * const collector = yield* GCP.RapidMigrationAssessment.Collector("OnPrem", { |
| 162 | * collectorId: "dc-east", |
| 163 | * location: "us-central1", |
| 164 | * displayName: "east datacenter", |
| 165 | * serviceAccount: "rma@my-project.iam.gserviceaccount.com", |
| 166 | * collectionDays: 14, |
| 167 | * }); |
| 168 | * ``` |
| 169 | * |
| 170 | * ### Updating a Collector |
| 171 | * **Example:** Description, labels, and pause |
| 172 | * ```typescript |
| 173 | * const collector = yield* GCP.RapidMigrationAssessment.Collector("OnPrem", { |
| 174 | * displayName: "east datacenter v2", |
| 175 | * description: "paused for maintenance", |
| 176 | * paused: true, |
| 177 | * labels: { env: "prod", team: "migration" }, |
| 178 | * }); |
| 179 | * ``` |
| 180 | * |
| 181 | * @resource |
| 182 | * @category RapidMigrationAssessment |
| 183 | */ |
| 184 | export const Collector = Resource<Collector>("GCP.RapidMigrationAssessment.Collector"); |
| 185 | |
| 186 | const resourceName = (project: string, location: string, collectorId: string) => |
| 187 | `${locationParent(project, location)}/collectors/${collectorId}`; |
| 188 | |
| 189 | const toAttrs = (item: rma.Collector, project: string) => { |
| 190 | const name = item.name ?? ""; |
| 191 | const parsed = parseName(name); |
| 192 | return { |
| 193 | name, |
| 194 | collectorId: parsed.id, |
| 195 | project: parsed.project || project, |
| 196 | location: parsed.location, |
| 197 | displayName: item.displayName, |
| 198 | description: item.description, |
| 199 | collectionDays: item.collectionDays, |
| 200 | serviceAccount: item.serviceAccount, |
| 201 | expectedAssetCount: item.expectedAssetCount, |
| 202 | eulaUri: item.eulaUri, |
| 203 | labels: userLabels(item.labels), |
| 204 | state: item.state, |
| 205 | paused: isPausedState(item.state), |
| 206 | registered: isRegisteredState(item.state), |
| 207 | clientVersion: item.clientVersion, |
| 208 | bucket: item.bucket, |
| 209 | guestOsScan: item.guestOsScan, |
| 210 | vsphereScan: item.vsphereScan, |
| 211 | createTime: item.createTime, |
| 212 | updateTime: item.updateTime, |
| 213 | }; |
| 214 | }; |
| 215 | |
| 216 | const getByName = (name: string) => |
| 217 | name.length === 0 |
| 218 | ? Effect.undefined |
| 219 | : rma.getProjectsLocationsCollectors({ name }).pipe( |
| 220 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 221 | Effect.map((item) => (item === undefined || isGoneState(item.state) ? undefined : item)), |
| 222 | ); |
| 223 | |
| 224 | const ignoreConflict = <A, R>( |
| 225 | effect: Effect.Effect< |
| 226 | A, |
| 227 | | rma.PauseProjectsLocationsCollectorsError |
| 228 | | rma.RegisterProjectsLocationsCollectorsError |
| 229 | | rma.ResumeProjectsLocationsCollectorsError, |
| 230 | R |
| 231 | >, |
| 232 | ) => effect.pipe(Effect.catchTag(["Conflict", "BadRequest"], () => Effect.undefined)); |
| 233 | |
| 234 | const applyDesiredState = (name: string, current: rma.Collector, news: CollectorProps) => |
| 235 | Effect.gen(function* () { |
| 236 | let next = current; |
| 237 | const state = next.state; |
| 238 | |
| 239 | if (news.registered === true && !isRegisteredState(state)) { |
| 240 | const operation = yield* ignoreConflict(rma.registerProjectsLocationsCollectors({ name })); |
| 241 | if (operation !== undefined) { |
| 242 | yield* waitForOperation(operation); |
| 243 | next = yield* waitUntilExists(getByName(name), name); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (news.paused === true && !isPausedState(next.state)) { |
| 248 | const operation = yield* ignoreConflict(rma.pauseProjectsLocationsCollectors({ name })); |
| 249 | if (operation !== undefined) { |
| 250 | yield* waitForOperation(operation); |
| 251 | next = yield* waitUntilExists(getByName(name), name); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (news.paused === false && isPausedState(next.state)) { |
| 256 | const operation = yield* ignoreConflict(rma.resumeProjectsLocationsCollectors({ name })); |
| 257 | if (operation !== undefined) { |
| 258 | yield* waitForOperation(operation); |
| 259 | next = yield* waitUntilExists(getByName(name), name); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return next; |
| 264 | }); |
| 265 | |
| 266 | export const CollectorProvider = () => |
| 267 | Provider.succeed(Collector, { |
| 268 | stables: ["name", "collectorId", "project", "location", "createTime"], |
| 269 | |
| 270 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 271 | if (!isResolved(news)) return undefined; |
| 272 | const env = yield* GcpEnvironment.current; |
| 273 | return replaceOnIdentity({ |
| 274 | previousId: olds?.collectorId ?? output?.collectorId, |
| 275 | nextId: news.collectorId ?? olds?.collectorId ?? output?.collectorId, |
| 276 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 277 | nextLocation: normalizeLocation( |
| 278 | news.location ?? olds?.location ?? output?.location, |
| 279 | env.region, |
| 280 | ), |
| 281 | }); |
| 282 | }), |
| 283 | |
| 284 | read: Effect.fn(function* ({ id, olds, output }) { |
| 285 | const env = yield* GcpEnvironment.current; |
| 286 | const collectorId = yield* toPhysicalId(id, olds?.collectorId, output?.collectorId); |
| 287 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 288 | const name = output?.name ?? resourceName(env.project, location, collectorId); |
| 289 | const existing = yield* getByName(name); |
| 290 | if (existing === undefined) return undefined; |
| 291 | const attrs = toAttrs(existing, env.project); |
| 292 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 293 | }), |
| 294 | |
| 295 | list: () => |
| 296 | Effect.gen(function* () { |
| 297 | const env = yield* GcpEnvironment.current; |
| 298 | const items = yield* listOwnedCollectors(env.project, env.region); |
| 299 | return items |
| 300 | .filter((item) => !isGoneState(item.state)) |
| 301 | .map((item) => toAttrs(item, env.project)); |
| 302 | }), |
| 303 | |
| 304 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 305 | const env = yield* GcpEnvironment.current; |
| 306 | const collectorId = yield* toPhysicalId(id, news.collectorId, output?.collectorId); |
| 307 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 308 | const name = resourceName(env.project, location, collectorId); |
| 309 | const displayName = news.displayName ?? collectorId; |
| 310 | const expectedAssetCount = expectedAssetCountOf(news.expectedAssetCount); |
| 311 | const desiredLabels = { |
| 312 | ...toLabels(news.labels), |
| 313 | ...(yield* createInternalLabels(id)), |
| 314 | }; |
| 315 | |
| 316 | let observed = yield* getByName(output?.name ?? name); |
| 317 | |
| 318 | if (observed === undefined) { |
| 319 | const created = yield* rma |
| 320 | .createProjectsLocationsCollectors({ |
| 321 | parent: locationParent(env.project, location), |
| 322 | collectorId, |
| 323 | body: { |
| 324 | displayName, |
| 325 | description: news.description, |
| 326 | collectionDays: news.collectionDays, |
| 327 | serviceAccount: news.serviceAccount, |
| 328 | expectedAssetCount, |
| 329 | eulaUri: news.eulaUri, |
| 330 | labels: desiredLabels, |
| 331 | }, |
| 332 | }) |
| 333 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 334 | if (created !== undefined) { |
| 335 | yield* waitForOperation(created); |
| 336 | } |
| 337 | observed = yield* waitUntilExists(getByName(name), name); |
| 338 | } |
| 339 | |
| 340 | let current = observed ?? (yield* new ResourceNotResolved({ resourceName: name })); |
| 341 | current = yield* waitUntilReady( |
| 342 | getByName(current.name ?? name), |
| 343 | current.name ?? name, |
| 344 | (item) => item.state, |
| 345 | ).pipe( |
| 346 | Effect.catchTag("GCP.RapidMigrationAssessment.ResourceNotReady", () => |
| 347 | Effect.succeed(current), |
| 348 | ), |
| 349 | ); |
| 350 | |
| 351 | const observedLabels = tagRecord(current.labels); |
| 352 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 353 | const mask = fieldMask([ |
| 354 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 355 | !sameText(current.displayName, displayName) && "displayName", |
| 356 | news.description !== undefined && |
| 357 | !sameText(current.description, news.description) && |
| 358 | "description", |
| 359 | news.collectionDays !== undefined && |
| 360 | !sameNumber(current.collectionDays, news.collectionDays) && |
| 361 | "collectionDays", |
| 362 | news.serviceAccount !== undefined && |
| 363 | !sameText(current.serviceAccount, news.serviceAccount) && |
| 364 | "serviceAccount", |
| 365 | expectedAssetCount !== undefined && |
| 366 | !sameText(current.expectedAssetCount, expectedAssetCount) && |
| 367 | "expectedAssetCount", |
| 368 | news.eulaUri !== undefined && !sameText(current.eulaUri, news.eulaUri) && "eulaUri", |
| 369 | ]); |
| 370 | |
| 371 | if (mask.length > 0) { |
| 372 | const operation = yield* rma.patchProjectsLocationsCollectors({ |
| 373 | name: current.name ?? name, |
| 374 | updateMask: mask, |
| 375 | body: { |
| 376 | name: current.name ?? name, |
| 377 | displayName, |
| 378 | description: news.description, |
| 379 | collectionDays: news.collectionDays, |
| 380 | serviceAccount: news.serviceAccount, |
| 381 | expectedAssetCount, |
| 382 | eulaUri: news.eulaUri, |
| 383 | labels: desiredLabels, |
| 384 | }, |
| 385 | }); |
| 386 | yield* waitForOperation(operation); |
| 387 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 388 | } |
| 389 | |
| 390 | current = yield* applyDesiredState(current.name ?? name, current, news); |
| 391 | return toAttrs(current, env.project); |
| 392 | }), |
| 393 | |
| 394 | delete: Effect.fn(function* ({ output }) { |
| 395 | const operation = yield* rma.deleteProjectsLocationsCollectors({ name: output.name }).pipe( |
| 396 | Effect.retry({ |
| 397 | while: (error) => error._tag === "Conflict", |
| 398 | times: 8, |
| 399 | schedule: Schedule.spaced("2 seconds"), |
| 400 | }), |
| 401 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 402 | ); |
| 403 | if (operation !== undefined) { |
| 404 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 405 | } |
| 406 | yield* waitUntilGone(getByName(output.name), output.name); |
| 407 | }), |
| 408 | }); |