The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as vmwareengine from "@distilled.cloud/gcp/vmwareengine_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 type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | DEFAULT_ZONE, |
| 12 | VmwareengineNotResolved, |
| 13 | changedFields, |
| 14 | collectPages, |
| 15 | createInternalLabels, |
| 16 | encodeOwnership, |
| 17 | expandName, |
| 18 | hasAlchemyLabels, |
| 19 | hasOwnershipMarker, |
| 20 | listAcrossLocations, |
| 21 | locationFromName, |
| 22 | normalizeLocation, |
| 23 | parseName, |
| 24 | parseOwnership, |
| 25 | replaceOnIdentity, |
| 26 | rfc1035, |
| 27 | toPhysicalId, |
| 28 | waitForOperation, |
| 29 | waitUntilGone, |
| 30 | waitUntilPresent, |
| 31 | waitUntilReady, |
| 32 | } from "./internal.ts"; |
| 33 | |
| 34 | const COLLECTION = "externalAddresses"; |
| 35 | const PARENT_COLLECTION = "privateClouds"; |
| 36 | |
| 37 | export type PrivateCloudsExternalAddressProps = { |
| 38 | /** |
| 39 | * Parent PrivateCloud resource name |
| 40 | * (`projects/{project}/locations/{location}/privateClouds/{privateCloud}`) |
| 41 | * or the cloud id. Immutable — changing it replaces the address. |
| 42 | */ |
| 43 | privateCloud: string; |
| 44 | /** |
| 45 | * External address id (the `{externalAddress}` segment of |
| 46 | * `.../privateClouds/{privateCloud}/externalAddresses/{externalAddress}`). |
| 47 | * If omitted, a unique RFC1035 name is generated. Immutable. |
| 48 | */ |
| 49 | externalAddressId?: string; |
| 50 | /** |
| 51 | * Location of the parent private cloud. Inferred from `privateCloud` |
| 52 | * when that value is a full resource name. Immutable. |
| 53 | * @default "us-central1-a" |
| 54 | */ |
| 55 | location?: string; |
| 56 | /** |
| 57 | * Internal IP address of the workload VM this mapping points at. |
| 58 | */ |
| 59 | internalIp: string; |
| 60 | /** |
| 61 | * Human-readable description. External addresses have no labels field, |
| 62 | * so Alchemy stamps ownership into a `[alchemy …]` prefix and strips |
| 63 | * it from attributes. |
| 64 | */ |
| 65 | description?: string; |
| 66 | }; |
| 67 | |
| 68 | export type PrivateCloudsExternalAddress = Resource< |
| 69 | "GCP.VMwareEngine.PrivateCloudsExternalAddress", |
| 70 | PrivateCloudsExternalAddressProps, |
| 71 | { |
| 72 | /** Full resource name. */ |
| 73 | name: string; |
| 74 | /** External address id (last path segment). */ |
| 75 | externalAddressId: string; |
| 76 | /** Parent PrivateCloud resource name. */ |
| 77 | privateCloud: string; |
| 78 | /** Project id. */ |
| 79 | project: string; |
| 80 | /** Location id. */ |
| 81 | location: string; |
| 82 | /** Internal IP of the workload VM. */ |
| 83 | internalIp: string | undefined; |
| 84 | /** Allocated external IP. */ |
| 85 | externalIp: string | undefined; |
| 86 | /** User description with the Alchemy ownership prefix stripped. */ |
| 87 | description: string | undefined; |
| 88 | /** Server-reported state. */ |
| 89 | state: string | undefined; |
| 90 | /** System-generated unique identifier. */ |
| 91 | uid: string | undefined; |
| 92 | /** RFC3339 creation timestamp. */ |
| 93 | createTime: string | undefined; |
| 94 | /** RFC3339 last-update timestamp. */ |
| 95 | updateTime: string | undefined; |
| 96 | }, |
| 97 | never, |
| 98 | Providers |
| 99 | >; |
| 100 | |
| 101 | /** |
| 102 | * An allocated external IP address mapped to a workload VM inside a |
| 103 | * VMware Engine private cloud. The corresponding network policy must have |
| 104 | * `externalIp` enabled. |
| 105 | * |
| 106 | * External addresses have no labels field, so Alchemy stamps ownership |
| 107 | * into the description for `list` / nuke. Changing the parent cloud, |
| 108 | * address id, or location replaces the address. Internal IP and |
| 109 | * description update in place. |
| 110 | * |
| 111 | * ### Creating a PrivateCloudsExternalAddress |
| 112 | * **Example:** Map a workload VM |
| 113 | * ```typescript |
| 114 | * const address = yield* GCP.VMwareEngine.PrivateCloudsExternalAddress( |
| 115 | * "Web", |
| 116 | * { |
| 117 | * privateCloud: cloud.name, |
| 118 | * internalIp: "192.168.1.10", |
| 119 | * description: "web vip", |
| 120 | * }, |
| 121 | * ); |
| 122 | * ``` |
| 123 | * |
| 124 | * @resource |
| 125 | * @category VMwareEngine |
| 126 | */ |
| 127 | export const PrivateCloudsExternalAddress = Resource<PrivateCloudsExternalAddress>( |
| 128 | "GCP.VMwareEngine.PrivateCloudsExternalAddress", |
| 129 | ); |
| 130 | |
| 131 | const parentCloudName = (project: string, location: string, privateCloud: string) => |
| 132 | expandName(privateCloud, project, location, PARENT_COLLECTION); |
| 133 | |
| 134 | const resourceNameOf = (parent: string, externalAddressId: string) => |
| 135 | `${parent}/${COLLECTION}/${externalAddressId}`; |
| 136 | |
| 137 | const toAttrs = (item: vmwareengine.ExternalAddress, project: string) => { |
| 138 | const name = item.name ?? ""; |
| 139 | const parsed = parseName(name, COLLECTION, DEFAULT_ZONE); |
| 140 | const ownership = parseOwnership(item.description); |
| 141 | return { |
| 142 | name, |
| 143 | externalAddressId: parsed.id, |
| 144 | privateCloud: parsed.parent, |
| 145 | project: parsed.project || project, |
| 146 | location: parsed.location, |
| 147 | internalIp: item.internalIp, |
| 148 | externalIp: item.externalIp, |
| 149 | description: ownership.text, |
| 150 | state: item.state, |
| 151 | uid: item.uid, |
| 152 | createTime: item.createTime, |
| 153 | updateTime: item.updateTime, |
| 154 | }; |
| 155 | }; |
| 156 | |
| 157 | const getByName = (name: string) => |
| 158 | vmwareengine |
| 159 | .getProjectsLocationsPrivateCloudsExternalAddresses({ name }) |
| 160 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 161 | |
| 162 | export const PrivateCloudsExternalAddressProvider = () => |
| 163 | Provider.succeed(PrivateCloudsExternalAddress, { |
| 164 | stables: [ |
| 165 | "name", |
| 166 | "externalAddressId", |
| 167 | "privateCloud", |
| 168 | "project", |
| 169 | "location", |
| 170 | "uid", |
| 171 | "createTime", |
| 172 | ], |
| 173 | |
| 174 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 175 | if (!isResolved(news)) return undefined; |
| 176 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 177 | return replaceOnIdentity({ |
| 178 | previousId: olds?.externalAddressId ?? output?.externalAddressId, |
| 179 | nextId: news.externalAddressId |
| 180 | ? rfc1035(news.externalAddressId, "address") |
| 181 | : (olds?.externalAddressId ?? output?.externalAddressId), |
| 182 | previousLocation, |
| 183 | nextLocation: normalizeLocation( |
| 184 | news.location ?? locationFromName(news.privateCloud, previousLocation), |
| 185 | DEFAULT_ZONE, |
| 186 | ), |
| 187 | previousParent: olds?.privateCloud ?? output?.privateCloud, |
| 188 | nextParent: news.privateCloud, |
| 189 | }); |
| 190 | }), |
| 191 | |
| 192 | read: Effect.fn(function* ({ id, olds, output }) { |
| 193 | const env = yield* GcpEnvironment.current; |
| 194 | const location = normalizeLocation( |
| 195 | olds?.location ?? |
| 196 | output?.location ?? |
| 197 | (olds?.privateCloud ? locationFromName(olds.privateCloud, DEFAULT_ZONE) : undefined), |
| 198 | DEFAULT_ZONE, |
| 199 | ); |
| 200 | const parent = parentCloudName( |
| 201 | env.project, |
| 202 | location, |
| 203 | olds?.privateCloud ?? output?.privateCloud ?? "", |
| 204 | ); |
| 205 | const externalAddressId = yield* toPhysicalId( |
| 206 | id, |
| 207 | olds?.externalAddressId, |
| 208 | output?.externalAddressId, |
| 209 | "address", |
| 210 | ); |
| 211 | const name = output?.name ?? resourceNameOf(parent, externalAddressId); |
| 212 | const existing = yield* getByName(name); |
| 213 | if (existing === undefined) return undefined; |
| 214 | const attrs = toAttrs(existing, env.project); |
| 215 | const { labels } = parseOwnership(existing.description); |
| 216 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 217 | }), |
| 218 | |
| 219 | list: () => |
| 220 | Effect.gen(function* () { |
| 221 | const env = yield* GcpEnvironment.current; |
| 222 | const clouds = yield* listAcrossLocations(env.project, env.region, (parent) => |
| 223 | collectPages( |
| 224 | vmwareengine.listProjectsLocationsPrivateClouds.pages({ |
| 225 | parent, |
| 226 | pageSize: 1000, |
| 227 | }), |
| 228 | (page) => page.privateClouds, |
| 229 | ), |
| 230 | ); |
| 231 | const nested = yield* Effect.forEach( |
| 232 | clouds.filter((cloud) => (cloud.name ?? "").length > 0), |
| 233 | (cloud) => |
| 234 | collectPages( |
| 235 | vmwareengine.listProjectsLocationsPrivateCloudsExternalAddresses.pages({ |
| 236 | parent: cloud.name ?? "", |
| 237 | pageSize: 1000, |
| 238 | }), |
| 239 | (page) => page.externalAddresses, |
| 240 | ), |
| 241 | { concurrency: 4 }, |
| 242 | ); |
| 243 | return nested |
| 244 | .flat() |
| 245 | .filter((item) => hasOwnershipMarker(item.description)) |
| 246 | .map((item) => toAttrs(item, env.project)); |
| 247 | }), |
| 248 | |
| 249 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 250 | const env = yield* GcpEnvironment.current; |
| 251 | const location = normalizeLocation( |
| 252 | news.location ?? output?.location ?? locationFromName(news.privateCloud, DEFAULT_ZONE), |
| 253 | DEFAULT_ZONE, |
| 254 | ); |
| 255 | const parent = parentCloudName(env.project, location, news.privateCloud); |
| 256 | const externalAddressId = yield* toPhysicalId( |
| 257 | id, |
| 258 | news.externalAddressId, |
| 259 | output?.externalAddressId, |
| 260 | "address", |
| 261 | ); |
| 262 | const name = resourceNameOf(parent, externalAddressId); |
| 263 | const ownership = yield* createInternalLabels(id); |
| 264 | const desiredDescription = encodeOwnership(ownership, news.description); |
| 265 | |
| 266 | let current = yield* getByName(output?.name ?? name); |
| 267 | |
| 268 | if (current === undefined) { |
| 269 | const created = yield* vmwareengine |
| 270 | .createProjectsLocationsPrivateCloudsExternalAddresses({ |
| 271 | parent, |
| 272 | externalAddressId, |
| 273 | body: { |
| 274 | internalIp: news.internalIp, |
| 275 | description: desiredDescription, |
| 276 | }, |
| 277 | }) |
| 278 | .pipe( |
| 279 | Effect.retry({ |
| 280 | while: (error) => error._tag === "Conflict", |
| 281 | times: 5, |
| 282 | schedule: Schedule.spaced("2 seconds"), |
| 283 | }), |
| 284 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 285 | ); |
| 286 | if (created !== undefined) { |
| 287 | yield* waitForOperation(created); |
| 288 | } |
| 289 | current = yield* waitUntilPresent(getByName(name), name); |
| 290 | } |
| 291 | |
| 292 | if (current === undefined) { |
| 293 | return yield* new VmwareengineNotResolved({ resourceName: name }); |
| 294 | } |
| 295 | |
| 296 | current = yield* waitUntilReady( |
| 297 | getByName(current.name ?? name), |
| 298 | current.name ?? name, |
| 299 | (item) => item.state, |
| 300 | ); |
| 301 | |
| 302 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 303 | const ipChanged = (current.internalIp ?? "") !== news.internalIp; |
| 304 | const updateMask = changedFields([ |
| 305 | ["description", descriptionChanged], |
| 306 | ["internalIp", ipChanged], |
| 307 | ]); |
| 308 | |
| 309 | if (updateMask.length > 0) { |
| 310 | const operation = yield* vmwareengine.patchProjectsLocationsPrivateCloudsExternalAddresses({ |
| 311 | name: current.name ?? name, |
| 312 | updateMask: updateMask.join(","), |
| 313 | body: { |
| 314 | name: current.name ?? name, |
| 315 | description: desiredDescription, |
| 316 | internalIp: news.internalIp, |
| 317 | }, |
| 318 | }); |
| 319 | yield* waitForOperation(operation); |
| 320 | current = yield* waitUntilReady( |
| 321 | getByName(current.name ?? name), |
| 322 | current.name ?? name, |
| 323 | (item) => item.state, |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | return toAttrs(current, env.project); |
| 328 | }), |
| 329 | |
| 330 | delete: Effect.fn(function* ({ output }) { |
| 331 | const operation = yield* vmwareengine |
| 332 | .deleteProjectsLocationsPrivateCloudsExternalAddresses({ |
| 333 | name: output.name, |
| 334 | }) |
| 335 | .pipe( |
| 336 | Effect.retry({ |
| 337 | while: (error) => error._tag === "Conflict", |
| 338 | times: 8, |
| 339 | schedule: Schedule.spaced("2 seconds"), |
| 340 | }), |
| 341 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 342 | ); |
| 343 | if (operation !== undefined) { |
| 344 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 345 | } |
| 346 | yield* waitUntilGone(getByName(output.name), output.name); |
| 347 | }), |
| 348 | }); |