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 * 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 | collectPages, |
| 13 | encodeOwnershipLine, |
| 14 | forEachSource, |
| 15 | hasOwnershipMarker, |
| 16 | normalizeLocation, |
| 17 | ownedByAlchemy, |
| 18 | parseName, |
| 19 | parseOwnership, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | sourceOf, |
| 23 | toPhysicalId, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type DatacenterConnectorState = vm.DatacenterConnectorStateEnum | (string & {}); |
| 30 | |
| 31 | export type SourcesDatacenterConnectorProps = { |
| 32 | /** |
| 33 | * Parent source. Full name |
| 34 | * `projects/{project}/locations/{location}/sources/{source}` or the |
| 35 | * source id (combined with `location`). Immutable — changing it |
| 36 | * replaces the connector. |
| 37 | */ |
| 38 | source: string; |
| 39 | /** |
| 40 | * Region used when `source` is a bare id. |
| 41 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 42 | */ |
| 43 | location?: string; |
| 44 | /** |
| 45 | * Datacenter connector id. If omitted, a unique RFC1035 name is |
| 46 | * generated. Immutable — changing it replaces the connector. |
| 47 | */ |
| 48 | datacenterConnectorId?: string; |
| 49 | /** |
| 50 | * Unique registration key supplied by the OVA connector. Immutable — |
| 51 | * changing it replaces the connector. Defaults to the connector id. |
| 52 | */ |
| 53 | registrationId?: string; |
| 54 | /** |
| 55 | * Appliance version. Connectors have no labels or description field, |
| 56 | * so Alchemy ownership is stored in a `[alchemy …]` prefix and |
| 57 | * stripped from attributes. |
| 58 | */ |
| 59 | version?: string; |
| 60 | /** |
| 61 | * Service account the connector uses when talking to Google Cloud. |
| 62 | */ |
| 63 | serviceAccount?: string; |
| 64 | }; |
| 65 | |
| 66 | export type SourcesDatacenterConnector = Resource< |
| 67 | "GCP.VMMigration.SourcesDatacenterConnector", |
| 68 | SourcesDatacenterConnectorProps, |
| 69 | { |
| 70 | /** Full resource name. */ |
| 71 | name: string; |
| 72 | /** Datacenter connector id (last path segment). */ |
| 73 | datacenterConnectorId: string; |
| 74 | /** Parent source resource name. */ |
| 75 | source: string; |
| 76 | /** Project id. */ |
| 77 | project: string; |
| 78 | /** Location id. */ |
| 79 | location: string; |
| 80 | /** Registration key. */ |
| 81 | registrationId: string | undefined; |
| 82 | /** Appliance version with the Alchemy ownership prefix stripped. */ |
| 83 | version: string | undefined; |
| 84 | /** Service account used by the connector. */ |
| 85 | serviceAccount: string | undefined; |
| 86 | /** Communication bucket. */ |
| 87 | bucket: string | undefined; |
| 88 | /** Health state. */ |
| 89 | state: string | undefined; |
| 90 | /** RFC3339 creation timestamp. */ |
| 91 | createTime: string | undefined; |
| 92 | /** RFC3339 last-update timestamp. */ |
| 93 | updateTime: string | undefined; |
| 94 | }, |
| 95 | never, |
| 96 | Providers |
| 97 | >; |
| 98 | |
| 99 | /** |
| 100 | * A VM Migration datacenter connector registered against a VMware |
| 101 | * source. The on-prem OVA appliance uses the registration id to bind to |
| 102 | * this resource. |
| 103 | * |
| 104 | * Connectors have no labels or description — Alchemy stamps ownership |
| 105 | * into `version` so `list` / nuke can find them. The API has no patch |
| 106 | * method; changing identity or registration replaces the connector. |
| 107 | * |
| 108 | * ### Creating a Datacenter Connector |
| 109 | * **Example:** Register against a source |
| 110 | * ```typescript |
| 111 | * const source = yield* GCP.VMMigration.Source("Vcenter", { |
| 112 | * vmware: { vcenterIp: "10.0.0.4", username: "admin" }, |
| 113 | * }); |
| 114 | * const connector = yield* GCP.VMMigration.SourcesDatacenterConnector( |
| 115 | * "Appliance", |
| 116 | * { |
| 117 | * source: source.name, |
| 118 | * version: "1.0.0", |
| 119 | * }, |
| 120 | * ); |
| 121 | * ``` |
| 122 | * |
| 123 | * @resource |
| 124 | * @category VMMigration |
| 125 | */ |
| 126 | export const SourcesDatacenterConnector = Resource<SourcesDatacenterConnector>( |
| 127 | "GCP.VMMigration.SourcesDatacenterConnector", |
| 128 | ); |
| 129 | |
| 130 | const resourceName = (source: string, datacenterConnectorId: string) => |
| 131 | `${source}/datacenterConnectors/${datacenterConnectorId}`; |
| 132 | |
| 133 | const toAttrs = (connector: vm.DatacenterConnector, project: string, region: string) => { |
| 134 | const name = connector.name ?? ""; |
| 135 | const parsed = parseName(name, "datacenterConnectors", region); |
| 136 | const ownership = parseOwnership(connector.version); |
| 137 | return { |
| 138 | name, |
| 139 | datacenterConnectorId: parsed.id, |
| 140 | source: parsed.parent, |
| 141 | project: parsed.project || project, |
| 142 | location: parsed.location, |
| 143 | registrationId: connector.registrationId, |
| 144 | version: ownership.text, |
| 145 | serviceAccount: connector.serviceAccount, |
| 146 | bucket: connector.bucket, |
| 147 | state: connector.state, |
| 148 | createTime: connector.createTime, |
| 149 | updateTime: connector.updateTime, |
| 150 | }; |
| 151 | }; |
| 152 | |
| 153 | const getByName = (name: string) => |
| 154 | name.length === 0 |
| 155 | ? Effect.undefined |
| 156 | : vm |
| 157 | .getProjectsLocationsSourcesDatacenterConnectors({ name }) |
| 158 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 159 | |
| 160 | const listChildren = (parent: string) => |
| 161 | collectPages( |
| 162 | vm.listProjectsLocationsSourcesDatacenterConnectors.pages({ |
| 163 | parent, |
| 164 | pageSize: 1000, |
| 165 | }), |
| 166 | (page) => page.datacenterConnectors, |
| 167 | ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as vm.DatacenterConnector[]))); |
| 168 | |
| 169 | export const SourcesDatacenterConnectorProvider = () => |
| 170 | Provider.succeed(SourcesDatacenterConnector, { |
| 171 | stables: ["name", "datacenterConnectorId", "source", "project", "location", "createTime"], |
| 172 | |
| 173 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 174 | if (!isResolved(news)) return undefined; |
| 175 | const env = yield* GcpEnvironment.current; |
| 176 | const previousSource = olds?.source ?? output?.source; |
| 177 | const extra = |
| 178 | (news.registrationId !== undefined && |
| 179 | (olds?.registrationId ?? output?.registrationId) !== undefined && |
| 180 | news.registrationId !== (olds?.registrationId ?? output?.registrationId)) || |
| 181 | (news.serviceAccount !== undefined && |
| 182 | (olds?.serviceAccount ?? output?.serviceAccount) !== undefined && |
| 183 | news.serviceAccount !== (olds?.serviceAccount ?? output?.serviceAccount)); |
| 184 | return replaceOnIdentity({ |
| 185 | previousId: olds?.datacenterConnectorId ?? output?.datacenterConnectorId, |
| 186 | nextId: |
| 187 | news.datacenterConnectorId ?? |
| 188 | olds?.datacenterConnectorId ?? |
| 189 | output?.datacenterConnectorId, |
| 190 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 191 | nextLocation: normalizeLocation( |
| 192 | news.location ?? olds?.location ?? output?.location, |
| 193 | env.region, |
| 194 | ), |
| 195 | previousParent: previousSource, |
| 196 | nextParent: news.source ?? previousSource, |
| 197 | extra, |
| 198 | }); |
| 199 | }), |
| 200 | |
| 201 | read: Effect.fn(function* ({ id, olds, output }) { |
| 202 | const env = yield* GcpEnvironment.current; |
| 203 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 204 | const datacenterConnectorId = yield* toPhysicalId( |
| 205 | id, |
| 206 | olds?.datacenterConnectorId, |
| 207 | output?.datacenterConnectorId, |
| 208 | "connector", |
| 209 | ); |
| 210 | const source = |
| 211 | olds?.source !== undefined |
| 212 | ? sourceOf(olds.source, env.project, location) |
| 213 | : (output?.source ?? ""); |
| 214 | const name = |
| 215 | output?.name ?? (source.length > 0 ? resourceName(source, datacenterConnectorId) : ""); |
| 216 | const existing = yield* getByName(name); |
| 217 | if (existing === undefined) return undefined; |
| 218 | const attrs = toAttrs(existing, env.project, env.region); |
| 219 | return (yield* ownedByAlchemy(id, existing.version)) ? attrs : Unowned(attrs); |
| 220 | }), |
| 221 | |
| 222 | list: () => |
| 223 | Effect.gen(function* () { |
| 224 | const env = yield* GcpEnvironment.current; |
| 225 | const items = yield* forEachSource(env.project, listChildren, env.region); |
| 226 | return items |
| 227 | .filter((item) => hasOwnershipMarker(item.version)) |
| 228 | .map((item) => toAttrs(item, env.project, env.region)); |
| 229 | }), |
| 230 | |
| 231 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 232 | const env = yield* GcpEnvironment.current; |
| 233 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 234 | const source = sourceOf(news.source, env.project, location); |
| 235 | const datacenterConnectorId = yield* toPhysicalId( |
| 236 | id, |
| 237 | news.datacenterConnectorId, |
| 238 | output?.datacenterConnectorId, |
| 239 | "connector", |
| 240 | ); |
| 241 | const name = resourceName(source, datacenterConnectorId); |
| 242 | const ownership = yield* createInternalLabels(id); |
| 243 | const version = encodeOwnershipLine(ownership, news.version, 128); |
| 244 | const registrationId = news.registrationId ?? datacenterConnectorId; |
| 245 | |
| 246 | let current = yield* getByName(output?.name ?? name); |
| 247 | |
| 248 | if (current === undefined) { |
| 249 | const created = yield* vm |
| 250 | .createProjectsLocationsSourcesDatacenterConnectors({ |
| 251 | parent: source, |
| 252 | datacenterConnectorId, |
| 253 | body: { |
| 254 | registrationId, |
| 255 | version, |
| 256 | serviceAccount: news.serviceAccount, |
| 257 | }, |
| 258 | }) |
| 259 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 260 | if (created !== undefined) { |
| 261 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 262 | yield* waitForOperation(created).pipe( |
| 263 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 264 | error.code === 6 ? Effect.void : Effect.fail(error), |
| 265 | ), |
| 266 | ); |
| 267 | } |
| 268 | current = yield* waitUntilExists(getByName(name), name); |
| 269 | } |
| 270 | |
| 271 | if (current === undefined) { |
| 272 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 273 | } |
| 274 | |
| 275 | return toAttrs(current, env.project, env.region); |
| 276 | }), |
| 277 | |
| 278 | delete: Effect.fn(function* ({ output }) { |
| 279 | const operation = yield* vm |
| 280 | .deleteProjectsLocationsSourcesDatacenterConnectors({ |
| 281 | name: output.name, |
| 282 | }) |
| 283 | .pipe( |
| 284 | Effect.retry({ |
| 285 | while: (error) => error._tag === "Conflict", |
| 286 | times: 8, |
| 287 | schedule: Schedule.spaced("2 seconds"), |
| 288 | }), |
| 289 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 290 | ); |
| 291 | if (operation !== undefined) { |
| 292 | // NOT_FOUND (5): already gone. |
| 293 | yield* waitForOperation(operation).pipe( |
| 294 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 295 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 296 | ), |
| 297 | ); |
| 298 | } |
| 299 | yield* waitUntilGone(getByName(output.name), output.name); |
| 300 | }), |
| 301 | }); |