The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as vm from "@distilled.cloud/gcp/vmmigration_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 | collectPages, |
| 14 | fieldMask, |
| 15 | fingerprint, |
| 16 | forEachSource, |
| 17 | hasAlchemyLabelMap, |
| 18 | normalizeLocation, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | sourceOf, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type SchedulePolicy = vm.SchedulePolicy; |
| 31 | export type ComputeEngineTargetDefaults = vm.ComputeEngineTargetDefaults; |
| 32 | export type ComputeEngineDisksTargetDefaults = vm.ComputeEngineDisksTargetDefaults; |
| 33 | export type MigratingVmState = vm.MigratingVmStateEnum | (string & {}); |
| 34 | |
| 35 | export type SourcesMigratingVmProps = { |
| 36 | /** |
| 37 | * Parent source. Full name |
| 38 | * `projects/{project}/locations/{location}/sources/{source}` or the |
| 39 | * source id (combined with `location`). Immutable — changing it |
| 40 | * replaces the migrating VM. |
| 41 | */ |
| 42 | source: string; |
| 43 | /** |
| 44 | * Region used when `source` is a bare id. |
| 45 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 46 | */ |
| 47 | location?: string; |
| 48 | /** |
| 49 | * Migrating VM id. If omitted, a unique RFC1035 name is generated. |
| 50 | * Immutable — changing it replaces the migrating VM. |
| 51 | */ |
| 52 | migratingVmId?: string; |
| 53 | /** |
| 54 | * Unique VM id in the source (vSphere moRef `vm-…`, AWS instance id, |
| 55 | * Azure VM id). Immutable — changing it replaces the migrating VM. |
| 56 | */ |
| 57 | sourceVmId: string; |
| 58 | /** |
| 59 | * User-friendly display name. |
| 60 | */ |
| 61 | displayName?: string; |
| 62 | /** |
| 63 | * Free-text description. |
| 64 | */ |
| 65 | description?: string; |
| 66 | /** |
| 67 | * User labels. Alchemy ownership labels are merged in automatically. |
| 68 | */ |
| 69 | labels?: Record<string, string>; |
| 70 | /** |
| 71 | * Replication schedule policy. |
| 72 | */ |
| 73 | policy?: SchedulePolicy; |
| 74 | /** |
| 75 | * Target VM defaults in Compute Engine. Mutually exclusive with |
| 76 | * `computeEngineDisksTargetDefaults`. |
| 77 | */ |
| 78 | computeEngineTargetDefaults?: ComputeEngineTargetDefaults; |
| 79 | /** |
| 80 | * Target persistent-disk defaults. Mutually exclusive with |
| 81 | * `computeEngineTargetDefaults`. |
| 82 | */ |
| 83 | computeEngineDisksTargetDefaults?: ComputeEngineDisksTargetDefaults; |
| 84 | }; |
| 85 | |
| 86 | export type SourcesMigratingVm = Resource< |
| 87 | "GCP.VMMigration.SourcesMigratingVm", |
| 88 | SourcesMigratingVmProps, |
| 89 | { |
| 90 | /** Full resource name. */ |
| 91 | name: string; |
| 92 | /** Migrating VM id (last path segment). */ |
| 93 | migratingVmId: string; |
| 94 | /** Parent source resource name. */ |
| 95 | source: string; |
| 96 | /** Project id. */ |
| 97 | project: string; |
| 98 | /** Location id. */ |
| 99 | location: string; |
| 100 | /** Unique VM id in the source. */ |
| 101 | sourceVmId: string | undefined; |
| 102 | /** User-friendly display name. */ |
| 103 | displayName: string | undefined; |
| 104 | /** Free-text description. */ |
| 105 | description: string | undefined; |
| 106 | /** User labels (Alchemy ownership labels stripped). */ |
| 107 | labels: Record<string, string>; |
| 108 | /** Replication schedule policy. */ |
| 109 | policy: SchedulePolicy | undefined; |
| 110 | /** Compute Engine VM target defaults. */ |
| 111 | computeEngineTargetDefaults: ComputeEngineTargetDefaults | undefined; |
| 112 | /** Compute Engine disk target defaults. */ |
| 113 | computeEngineDisksTargetDefaults: ComputeEngineDisksTargetDefaults | undefined; |
| 114 | /** Group this migrating VM belongs to. */ |
| 115 | group: string | undefined; |
| 116 | /** Replication state. */ |
| 117 | state: string | undefined; |
| 118 | /** RFC3339 creation timestamp. */ |
| 119 | createTime: string | undefined; |
| 120 | /** RFC3339 last-update timestamp. */ |
| 121 | updateTime: string | undefined; |
| 122 | }, |
| 123 | never, |
| 124 | Providers |
| 125 | >; |
| 126 | |
| 127 | /** |
| 128 | * A VM Migration migrating VM: a source VM plus the replication and |
| 129 | * target defaults used to clone or cut over into Compute Engine. |
| 130 | * |
| 131 | * Source, migrating VM id, and `sourceVmId` are immutable. Display |
| 132 | * name, description, labels, policy, and target defaults update in |
| 133 | * place. |
| 134 | * |
| 135 | * ### Creating a Migrating VM |
| 136 | * **Example:** Compute Engine VM target |
| 137 | * ```typescript |
| 138 | * const vm = yield* GCP.VMMigration.SourcesMigratingVm("Web", { |
| 139 | * source: source.name, |
| 140 | * sourceVmId: "vm-123", |
| 141 | * displayName: "web-1", |
| 142 | * computeEngineTargetDefaults: { |
| 143 | * vmName: "web-1", |
| 144 | * machineType: "n2-standard-4", |
| 145 | * zone: "us-central1-a", |
| 146 | * targetProject: target.name, |
| 147 | * }, |
| 148 | * }); |
| 149 | * ``` |
| 150 | * |
| 151 | * @resource |
| 152 | * @category VMMigration |
| 153 | */ |
| 154 | export const SourcesMigratingVm = Resource<SourcesMigratingVm>( |
| 155 | "GCP.VMMigration.SourcesMigratingVm", |
| 156 | ); |
| 157 | |
| 158 | const resourceName = (source: string, migratingVmId: string) => |
| 159 | `${source}/migratingVms/${migratingVmId}`; |
| 160 | |
| 161 | const userVmTarget = (target: ComputeEngineTargetDefaults | undefined) => { |
| 162 | if (target === undefined) return undefined; |
| 163 | const { appliedLicense: _applied, bootOption: _boot, ...rest } = target; |
| 164 | return rest; |
| 165 | }; |
| 166 | |
| 167 | const userDisksTarget = (target: ComputeEngineDisksTargetDefaults | undefined) => { |
| 168 | if (target === undefined) return undefined; |
| 169 | return { |
| 170 | targetProject: target.targetProject, |
| 171 | zone: target.zone, |
| 172 | disks: target.disks, |
| 173 | vmTargetDefaults: target.vmTargetDefaults, |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | const toAttrs = (migrating: vm.MigratingVm, project: string, region: string) => { |
| 178 | const name = migrating.name ?? ""; |
| 179 | const parsed = parseName(name, "migratingVms", region); |
| 180 | return { |
| 181 | name, |
| 182 | migratingVmId: parsed.id, |
| 183 | source: parsed.parent, |
| 184 | project: parsed.project || project, |
| 185 | location: parsed.location, |
| 186 | sourceVmId: migrating.sourceVmId, |
| 187 | displayName: migrating.displayName, |
| 188 | description: migrating.description, |
| 189 | labels: userLabels(migrating.labels), |
| 190 | policy: migrating.policy, |
| 191 | computeEngineTargetDefaults: migrating.computeEngineTargetDefaults, |
| 192 | computeEngineDisksTargetDefaults: migrating.computeEngineDisksTargetDefaults, |
| 193 | group: migrating.group, |
| 194 | state: migrating.state, |
| 195 | createTime: migrating.createTime, |
| 196 | updateTime: migrating.updateTime, |
| 197 | }; |
| 198 | }; |
| 199 | |
| 200 | const getByName = (name: string) => |
| 201 | name.length === 0 |
| 202 | ? Effect.undefined |
| 203 | : vm |
| 204 | .getProjectsLocationsSourcesMigratingVms({ name }) |
| 205 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 206 | |
| 207 | const listChildren = (parent: string) => |
| 208 | collectPages( |
| 209 | vm.listProjectsLocationsSourcesMigratingVms.pages({ |
| 210 | parent, |
| 211 | pageSize: 1000, |
| 212 | view: "MIGRATING_VM_VIEW_BASIC", |
| 213 | }), |
| 214 | (page) => page.migratingVms, |
| 215 | ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as vm.MigratingVm[]))); |
| 216 | |
| 217 | export const SourcesMigratingVmProvider = () => |
| 218 | Provider.succeed(SourcesMigratingVm, { |
| 219 | stables: ["name", "migratingVmId", "source", "project", "location", "createTime"], |
| 220 | |
| 221 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 222 | if (!isResolved(news)) return undefined; |
| 223 | const env = yield* GcpEnvironment.current; |
| 224 | const previousSource = olds?.source ?? output?.source; |
| 225 | const previousVm = olds?.sourceVmId ?? output?.sourceVmId; |
| 226 | const nextVm = news.sourceVmId ?? previousVm; |
| 227 | return replaceOnIdentity({ |
| 228 | previousId: olds?.migratingVmId ?? output?.migratingVmId, |
| 229 | nextId: news.migratingVmId ?? olds?.migratingVmId ?? output?.migratingVmId, |
| 230 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 231 | nextLocation: normalizeLocation( |
| 232 | news.location ?? olds?.location ?? output?.location, |
| 233 | env.region, |
| 234 | ), |
| 235 | previousParent: previousSource, |
| 236 | nextParent: news.source ?? previousSource, |
| 237 | extra: previousVm !== nextVm, |
| 238 | }); |
| 239 | }), |
| 240 | |
| 241 | read: Effect.fn(function* ({ id, olds, output }) { |
| 242 | const env = yield* GcpEnvironment.current; |
| 243 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 244 | const migratingVmId = yield* toPhysicalId( |
| 245 | id, |
| 246 | olds?.migratingVmId, |
| 247 | output?.migratingVmId, |
| 248 | "migratingvm", |
| 249 | ); |
| 250 | const source = |
| 251 | olds?.source !== undefined |
| 252 | ? sourceOf(olds.source, env.project, location) |
| 253 | : (output?.source ?? ""); |
| 254 | const name = output?.name ?? (source.length > 0 ? resourceName(source, migratingVmId) : ""); |
| 255 | const existing = yield* getByName(name); |
| 256 | if (existing === undefined) return undefined; |
| 257 | const attrs = toAttrs(existing, env.project, env.region); |
| 258 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 259 | }), |
| 260 | |
| 261 | list: () => |
| 262 | Effect.gen(function* () { |
| 263 | const env = yield* GcpEnvironment.current; |
| 264 | const items = yield* forEachSource(env.project, listChildren, env.region); |
| 265 | return items |
| 266 | .filter((item) => hasAlchemyLabelMap(item.labels)) |
| 267 | .map((item) => toAttrs(item, env.project, env.region)); |
| 268 | }), |
| 269 | |
| 270 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 271 | const env = yield* GcpEnvironment.current; |
| 272 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 273 | const source = sourceOf(news.source, env.project, location); |
| 274 | const migratingVmId = yield* toPhysicalId( |
| 275 | id, |
| 276 | news.migratingVmId, |
| 277 | output?.migratingVmId, |
| 278 | "migratingvm", |
| 279 | ); |
| 280 | const name = resourceName(source, migratingVmId); |
| 281 | const desiredLabels = { |
| 282 | ...toLabels(news.labels), |
| 283 | ...(yield* createInternalLabels(id)), |
| 284 | }; |
| 285 | const displayName = news.displayName ?? migratingVmId; |
| 286 | |
| 287 | let current = yield* getByName(output?.name ?? name); |
| 288 | |
| 289 | if (current === undefined) { |
| 290 | const created = yield* vm |
| 291 | .createProjectsLocationsSourcesMigratingVms({ |
| 292 | parent: source, |
| 293 | migratingVmId, |
| 294 | body: { |
| 295 | sourceVmId: news.sourceVmId, |
| 296 | displayName, |
| 297 | description: news.description, |
| 298 | labels: desiredLabels, |
| 299 | policy: news.policy, |
| 300 | computeEngineTargetDefaults: news.computeEngineTargetDefaults, |
| 301 | computeEngineDisksTargetDefaults: news.computeEngineDisksTargetDefaults, |
| 302 | }, |
| 303 | }) |
| 304 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 305 | if (created !== undefined) { |
| 306 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 307 | yield* waitForOperation(created).pipe( |
| 308 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 309 | error.code === 6 ? Effect.void : Effect.fail(error), |
| 310 | ), |
| 311 | ); |
| 312 | } |
| 313 | current = yield* waitUntilExists(getByName(name), name); |
| 314 | } |
| 315 | |
| 316 | if (current === undefined) { |
| 317 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 318 | } |
| 319 | |
| 320 | const observedLabels = tagRecord(current.labels); |
| 321 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 322 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 323 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 324 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 325 | const policyChanged = fingerprint(current.policy) !== fingerprint(news.policy); |
| 326 | const vmTargetChanged = |
| 327 | fingerprint(userVmTarget(current.computeEngineTargetDefaults)) !== |
| 328 | fingerprint(userVmTarget(news.computeEngineTargetDefaults)); |
| 329 | const disksTargetChanged = |
| 330 | fingerprint(userDisksTarget(current.computeEngineDisksTargetDefaults)) !== |
| 331 | fingerprint(userDisksTarget(news.computeEngineDisksTargetDefaults)); |
| 332 | const mask = fieldMask([ |
| 333 | labelsChanged && "labels", |
| 334 | displayNameChanged && "displayName", |
| 335 | descriptionChanged && "description", |
| 336 | policyChanged && "policy", |
| 337 | vmTargetChanged && "computeEngineTargetDefaults", |
| 338 | disksTargetChanged && "computeEngineDisksTargetDefaults", |
| 339 | ]); |
| 340 | |
| 341 | if (mask.length > 0) { |
| 342 | const operation = yield* vm.patchProjectsLocationsSourcesMigratingVms({ |
| 343 | name: current.name ?? name, |
| 344 | updateMask: mask, |
| 345 | body: { |
| 346 | name: current.name ?? name, |
| 347 | displayName, |
| 348 | description: news.description, |
| 349 | labels: desiredLabels, |
| 350 | policy: news.policy, |
| 351 | computeEngineTargetDefaults: news.computeEngineTargetDefaults, |
| 352 | computeEngineDisksTargetDefaults: news.computeEngineDisksTargetDefaults, |
| 353 | }, |
| 354 | }); |
| 355 | yield* waitForOperation(operation); |
| 356 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 357 | } |
| 358 | |
| 359 | return toAttrs(current, env.project, env.region); |
| 360 | }), |
| 361 | |
| 362 | delete: Effect.fn(function* ({ output }) { |
| 363 | const operation = yield* vm |
| 364 | .deleteProjectsLocationsSourcesMigratingVms({ name: output.name }) |
| 365 | .pipe( |
| 366 | Effect.retry({ |
| 367 | while: (error) => error._tag === "Conflict", |
| 368 | times: 8, |
| 369 | schedule: Schedule.spaced("2 seconds"), |
| 370 | }), |
| 371 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 372 | ); |
| 373 | if (operation !== undefined) { |
| 374 | // NOT_FOUND (5): already gone. |
| 375 | yield* waitForOperation(operation).pipe( |
| 376 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 377 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 378 | ), |
| 379 | ); |
| 380 | } |
| 381 | yield* waitUntilGone(getByName(output.name), output.name); |
| 382 | }), |
| 383 | }); |