The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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_GLOBAL, |
| 12 | VmwareengineNotResolved, |
| 13 | canonicalizeLink, |
| 14 | changedFields, |
| 15 | collectPages, |
| 16 | createInternalLabels, |
| 17 | encodeOwnership, |
| 18 | expandName, |
| 19 | hasAlchemyLabels, |
| 20 | hasOwnershipMarker, |
| 21 | listAcrossLocations, |
| 22 | normalizeLocation, |
| 23 | parentOf, |
| 24 | parseName, |
| 25 | parseOwnership, |
| 26 | replaceOnIdentity, |
| 27 | rfc1035, |
| 28 | toPhysicalId, |
| 29 | waitForOperation, |
| 30 | waitUntilGone, |
| 31 | waitUntilPresent, |
| 32 | waitUntilReady, |
| 33 | } from "./internal.ts"; |
| 34 | |
| 35 | const COLLECTION = "privateConnections"; |
| 36 | const VEN_COLLECTION = "vmwareEngineNetworks"; |
| 37 | const DEFAULT_ROUTING_MODE = "GLOBAL" satisfies vmwareengine.PrivateConnectionRoutingModeEnum; |
| 38 | |
| 39 | export type PrivateConnectionType = vmwareengine.PrivateConnectionTypeEnum | (string & {}); |
| 40 | |
| 41 | export type PrivateConnectionRoutingMode = |
| 42 | | vmwareengine.PrivateConnectionRoutingModeEnum |
| 43 | | (string & {}); |
| 44 | |
| 45 | export type PrivateConnectionProps = { |
| 46 | /** |
| 47 | * Connection id (the `{privateConnection}` segment of |
| 48 | * `projects/{project}/locations/{location}/privateConnections/{id}`). |
| 49 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 50 | * it replaces the connection. |
| 51 | */ |
| 52 | privateConnectionId?: string; |
| 53 | /** |
| 54 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 55 | * connection. `US-CENTRAL1` is accepted and normalized. |
| 56 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 57 | * region, else `us-central1`) |
| 58 | */ |
| 59 | location?: string; |
| 60 | /** |
| 61 | * Connection type (`PRIVATE_SERVICE_ACCESS`, `NETAPP_CLOUD_VOLUMES`, |
| 62 | * `DELL_POWERSCALE`, `THIRD_PARTY_SERVICE`). Immutable — changing it |
| 63 | * replaces the connection. |
| 64 | */ |
| 65 | type: PrivateConnectionType; |
| 66 | /** |
| 67 | * VMware Engine network to attach |
| 68 | * (`projects/{project}/locations/{location}/vmwareEngineNetworks/{id}`). |
| 69 | * Immutable — changing it replaces the connection. |
| 70 | */ |
| 71 | vmwareEngineNetwork: string; |
| 72 | /** |
| 73 | * Service network to peer |
| 74 | * (`projects/{project}/global/networks/{network}`). For |
| 75 | * `PRIVATE_SERVICE_ACCESS` this is the Service Networking VPC. For |
| 76 | * `THIRD_PARTY_SERVICE` it may be a consumer or producer VPC. |
| 77 | * Immutable — changing it replaces the connection. |
| 78 | */ |
| 79 | serviceNetwork: string; |
| 80 | /** |
| 81 | * Routing mode. `PRIVATE_SERVICE_ACCESS` accepts `GLOBAL` or |
| 82 | * `REGIONAL`; other types only support `GLOBAL`. |
| 83 | * @default "GLOBAL" |
| 84 | */ |
| 85 | routingMode?: PrivateConnectionRoutingMode; |
| 86 | /** |
| 87 | * Human-readable description. Private connections have no labels |
| 88 | * field, so Alchemy ownership is stored in a `[alchemy …]` prefix and |
| 89 | * stripped from attributes. |
| 90 | */ |
| 91 | description?: string; |
| 92 | }; |
| 93 | |
| 94 | export type PrivateConnection = Resource< |
| 95 | "GCP.VMwareEngine.PrivateConnection", |
| 96 | PrivateConnectionProps, |
| 97 | { |
| 98 | /** Full resource name `projects/{project}/locations/{location}/privateConnections/{id}`. */ |
| 99 | name: string; |
| 100 | /** Connection id (last path segment). */ |
| 101 | privateConnectionId: string; |
| 102 | /** Project id. */ |
| 103 | project: string; |
| 104 | /** Region id. */ |
| 105 | location: string; |
| 106 | /** Connection type. */ |
| 107 | type: string; |
| 108 | /** Attached VMware Engine network. */ |
| 109 | vmwareEngineNetwork: string | undefined; |
| 110 | /** Canonical VMware Engine network name (project number). */ |
| 111 | vmwareEngineNetworkCanonical: string | undefined; |
| 112 | /** Peered service network. */ |
| 113 | serviceNetwork: string | undefined; |
| 114 | /** Routing mode. */ |
| 115 | routingMode: string | undefined; |
| 116 | /** User description with the Alchemy ownership prefix stripped. */ |
| 117 | description: string | undefined; |
| 118 | /** Server-reported state (`ACTIVE`, `CREATING`, …). */ |
| 119 | state: string | undefined; |
| 120 | /** VPC peering state (`PEERING_ACTIVE`, `PEERING_INACTIVE`). */ |
| 121 | peeringState: string | undefined; |
| 122 | /** VPC network peering id. */ |
| 123 | peeringId: string | undefined; |
| 124 | /** Server-generated uid. */ |
| 125 | uid: string | undefined; |
| 126 | /** RFC3339 creation timestamp. */ |
| 127 | createTime: string | undefined; |
| 128 | /** RFC3339 last-update timestamp. */ |
| 129 | updateTime: string | undefined; |
| 130 | }, |
| 131 | never, |
| 132 | Providers |
| 133 | >; |
| 134 | |
| 135 | /** |
| 136 | * A private connection that peers a service VPC with a VMware Engine |
| 137 | * network so private clouds can reach Google Cloud services, NetApp, |
| 138 | * Dell PowerScale, or a third-party VPC. |
| 139 | * |
| 140 | * Changing id, location, type, `vmwareEngineNetwork`, or |
| 141 | * `serviceNetwork` replaces the connection. Description and routing |
| 142 | * mode update in place. |
| 143 | * |
| 144 | * ### Creating a Connection |
| 145 | * **Example:** Third-party VPC |
| 146 | * ```typescript |
| 147 | * const connection = yield* GCP.VMwareEngine.PrivateConnection("Peer", { |
| 148 | * type: "THIRD_PARTY_SERVICE", |
| 149 | * vmwareEngineNetwork: ven.name, |
| 150 | * serviceNetwork: "projects/my-project/global/networks/default", |
| 151 | * description: "consumer vpc", |
| 152 | * }); |
| 153 | * ``` |
| 154 | * |
| 155 | * **Example:** Private Service Access |
| 156 | * ```typescript |
| 157 | * const connection = yield* GCP.VMwareEngine.PrivateConnection("Psa", { |
| 158 | * location: "us-central1", |
| 159 | * type: "PRIVATE_SERVICE_ACCESS", |
| 160 | * vmwareEngineNetwork: ven.name, |
| 161 | * serviceNetwork: |
| 162 | * "projects/my-project/global/networks/servicenetworking", |
| 163 | * routingMode: "GLOBAL", |
| 164 | * }); |
| 165 | * ``` |
| 166 | * |
| 167 | * ### Updating a Connection |
| 168 | * Re-declare the same logical id with changed props; the engine keeps the |
| 169 | * physical resource and updates it in place. |
| 170 | * |
| 171 | * **Example:** Description and regional routing |
| 172 | * ```typescript |
| 173 | * const connection = yield* GCP.VMwareEngine.PrivateConnection("Psa", { |
| 174 | * location: "us-central1", |
| 175 | * type: "PRIVATE_SERVICE_ACCESS", |
| 176 | * vmwareEngineNetwork: ven.name, |
| 177 | * serviceNetwork: |
| 178 | * "projects/my-project/global/networks/servicenetworking", |
| 179 | * routingMode: "REGIONAL", |
| 180 | * description: "psa v2", |
| 181 | * }); |
| 182 | * ``` |
| 183 | * |
| 184 | * @resource |
| 185 | * @category VMwareEngine |
| 186 | */ |
| 187 | export const PrivateConnection = Resource<PrivateConnection>("GCP.VMwareEngine.PrivateConnection"); |
| 188 | |
| 189 | const resourceName = (project: string, location: string, connectionId: string) => |
| 190 | `${parentOf(project, location)}/${COLLECTION}/${connectionId}`; |
| 191 | |
| 192 | const expandVen = (project: string, value: string) => |
| 193 | expandName(value, project, DEFAULT_GLOBAL, VEN_COLLECTION); |
| 194 | |
| 195 | const expandVpc = (project: string, value: string) => { |
| 196 | const canonical = canonicalizeLink(value); |
| 197 | if (canonical.includes("/")) return canonical; |
| 198 | return `projects/${project}/global/networks/${rfc1035(canonical, "network")}`; |
| 199 | }; |
| 200 | |
| 201 | const routingOf = (value: string | undefined) => (value ?? DEFAULT_ROUTING_MODE).toUpperCase(); |
| 202 | |
| 203 | const toAttrs = (connection: vmwareengine.PrivateConnection, project: string, region: string) => { |
| 204 | const name = connection.name ?? ""; |
| 205 | const parsed = parseName(name, COLLECTION, region); |
| 206 | const ownership = parseOwnership(connection.description); |
| 207 | return { |
| 208 | name, |
| 209 | privateConnectionId: parsed.id, |
| 210 | project: parsed.project || project, |
| 211 | location: parsed.location, |
| 212 | type: (connection.type ?? "").toUpperCase(), |
| 213 | vmwareEngineNetwork: connection.vmwareEngineNetwork, |
| 214 | vmwareEngineNetworkCanonical: connection.vmwareEngineNetworkCanonical, |
| 215 | serviceNetwork: connection.serviceNetwork, |
| 216 | routingMode: connection.routingMode, |
| 217 | description: ownership.text, |
| 218 | state: connection.state, |
| 219 | peeringState: connection.peeringState, |
| 220 | peeringId: connection.peeringId, |
| 221 | uid: connection.uid, |
| 222 | createTime: connection.createTime, |
| 223 | updateTime: connection.updateTime, |
| 224 | }; |
| 225 | }; |
| 226 | |
| 227 | const getByName = (name: string) => |
| 228 | name.length === 0 |
| 229 | ? Effect.undefined |
| 230 | : vmwareengine |
| 231 | .getProjectsLocationsPrivateConnections({ name }) |
| 232 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 233 | |
| 234 | export const PrivateConnectionProvider = () => |
| 235 | Provider.succeed(PrivateConnection, { |
| 236 | stables: ["name", "privateConnectionId", "project", "location", "type", "uid", "createTime"], |
| 237 | |
| 238 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 239 | if (!isResolved(news)) return undefined; |
| 240 | const env = yield* GcpEnvironment.current; |
| 241 | const previousVen = canonicalizeLink( |
| 242 | olds?.vmwareEngineNetwork ?? output?.vmwareEngineNetwork, |
| 243 | ); |
| 244 | const nextVen = canonicalizeLink(news.vmwareEngineNetwork); |
| 245 | const previousService = canonicalizeLink(olds?.serviceNetwork ?? output?.serviceNetwork); |
| 246 | const nextService = canonicalizeLink(news.serviceNetwork); |
| 247 | const previousType = (olds?.type ?? output?.type ?? "").toUpperCase(); |
| 248 | const nextType = news.type.toUpperCase(); |
| 249 | return replaceOnIdentity({ |
| 250 | previousId: olds?.privateConnectionId ?? output?.privateConnectionId, |
| 251 | nextId: news.privateConnectionId |
| 252 | ? rfc1035(news.privateConnectionId, "connection") |
| 253 | : (olds?.privateConnectionId ?? output?.privateConnectionId), |
| 254 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 255 | nextLocation: normalizeLocation( |
| 256 | news.location ?? olds?.location ?? output?.location, |
| 257 | env.region, |
| 258 | ), |
| 259 | extra: |
| 260 | (previousType.length > 0 && previousType !== nextType) || |
| 261 | (previousVen.length > 0 && previousVen !== nextVen) || |
| 262 | (previousService.length > 0 && previousService !== nextService), |
| 263 | }); |
| 264 | }), |
| 265 | |
| 266 | read: Effect.fn(function* ({ id, olds, output }) { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 269 | const connectionId = yield* toPhysicalId( |
| 270 | id, |
| 271 | olds?.privateConnectionId, |
| 272 | output?.privateConnectionId, |
| 273 | "connection", |
| 274 | ); |
| 275 | const name = output?.name ?? resourceName(env.project, location, connectionId); |
| 276 | const existing = yield* getByName(name); |
| 277 | if (existing === undefined) return undefined; |
| 278 | const attrs = toAttrs(existing, env.project, env.region); |
| 279 | const { labels } = parseOwnership(existing.description); |
| 280 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 281 | }), |
| 282 | |
| 283 | list: () => |
| 284 | Effect.gen(function* () { |
| 285 | const env = yield* GcpEnvironment.current; |
| 286 | const items = yield* listAcrossLocations(env.project, env.region, (parent) => |
| 287 | collectPages( |
| 288 | vmwareengine.listProjectsLocationsPrivateConnections.pages({ |
| 289 | parent, |
| 290 | pageSize: 1000, |
| 291 | }), |
| 292 | (page) => page.privateConnections, |
| 293 | ), |
| 294 | ); |
| 295 | return items |
| 296 | .filter((item) => hasOwnershipMarker(item.description)) |
| 297 | .map((item) => toAttrs(item, env.project, env.region)); |
| 298 | }), |
| 299 | |
| 300 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 301 | const env = yield* GcpEnvironment.current; |
| 302 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 303 | const connectionId = yield* toPhysicalId( |
| 304 | id, |
| 305 | news.privateConnectionId, |
| 306 | output?.privateConnectionId, |
| 307 | "connection", |
| 308 | ); |
| 309 | const name = resourceName(env.project, location, connectionId); |
| 310 | const ownership = yield* createInternalLabels(id); |
| 311 | const desiredDescription = encodeOwnership(ownership, news.description); |
| 312 | const type = news.type.toUpperCase(); |
| 313 | const routingMode = routingOf(news.routingMode); |
| 314 | const vmwareEngineNetwork = expandVen(env.project, news.vmwareEngineNetwork); |
| 315 | const serviceNetwork = expandVpc(env.project, news.serviceNetwork); |
| 316 | |
| 317 | let current = yield* getByName(output?.name ?? name); |
| 318 | |
| 319 | if (current === undefined) { |
| 320 | const created = yield* vmwareengine |
| 321 | .createProjectsLocationsPrivateConnections({ |
| 322 | parent: parentOf(env.project, location), |
| 323 | privateConnectionId: connectionId, |
| 324 | body: { |
| 325 | type, |
| 326 | vmwareEngineNetwork, |
| 327 | serviceNetwork, |
| 328 | routingMode, |
| 329 | description: desiredDescription, |
| 330 | }, |
| 331 | }) |
| 332 | .pipe( |
| 333 | Effect.retry({ |
| 334 | while: (error) => error._tag === "Conflict", |
| 335 | times: 5, |
| 336 | schedule: Schedule.spaced("2 seconds"), |
| 337 | }), |
| 338 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 339 | ); |
| 340 | if (created !== undefined) { |
| 341 | yield* waitForOperation(created); |
| 342 | } |
| 343 | current = yield* waitUntilPresent(getByName(name), name); |
| 344 | } |
| 345 | |
| 346 | if (current === undefined) { |
| 347 | return yield* new VmwareengineNotResolved({ resourceName: name }); |
| 348 | } |
| 349 | |
| 350 | current = yield* waitUntilReady( |
| 351 | getByName(current.name ?? name), |
| 352 | current.name ?? name, |
| 353 | (item) => item.state, |
| 354 | ); |
| 355 | |
| 356 | const updateMask = changedFields([ |
| 357 | ["description", (current.description ?? "") !== desiredDescription], |
| 358 | ["routingMode", routingOf(current.routingMode) !== routingMode], |
| 359 | ]); |
| 360 | if (updateMask.length > 0) { |
| 361 | const operation = yield* vmwareengine.patchProjectsLocationsPrivateConnections({ |
| 362 | name: current.name ?? name, |
| 363 | updateMask: updateMask.join(","), |
| 364 | body: { |
| 365 | name: current.name ?? name, |
| 366 | description: desiredDescription, |
| 367 | routingMode, |
| 368 | }, |
| 369 | }); |
| 370 | yield* waitForOperation(operation); |
| 371 | current = yield* waitUntilReady( |
| 372 | getByName(current.name ?? name), |
| 373 | current.name ?? name, |
| 374 | (item) => item.state, |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | return toAttrs(current, env.project, env.region); |
| 379 | }), |
| 380 | |
| 381 | delete: Effect.fn(function* ({ output }) { |
| 382 | const operation = yield* vmwareengine |
| 383 | .deleteProjectsLocationsPrivateConnections({ name: output.name }) |
| 384 | .pipe( |
| 385 | Effect.retry({ |
| 386 | while: (error) => error._tag === "Conflict", |
| 387 | times: 8, |
| 388 | schedule: Schedule.spaced("2 seconds"), |
| 389 | }), |
| 390 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 391 | ); |
| 392 | if (operation !== undefined) { |
| 393 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 394 | } |
| 395 | yield* waitUntilGone(getByName(output.name), output.name); |
| 396 | }), |
| 397 | }); |