The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilGone = (name: string) => |
| 15 | networkconnectivity.getProjectsLocationsSpokes({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("2 seconds"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider.skipIf(!!process.env.FAST)( |
| 26 | "create, update, and delete a vpc spoke", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | yield* stack.destroy(); |
| 30 | |
| 31 | const created = yield* stack.deploy( |
| 32 | Effect.gen(function* () { |
| 33 | const network = yield* GCP.Compute.Network("SpokeVpc", { |
| 34 | autoCreateSubnetworks: false, |
| 35 | }); |
| 36 | const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", { |
| 37 | description: "ncc hub for spoke", |
| 38 | }); |
| 39 | const spoke = yield* GCP.NetworkConnectivity.Spoke("VpcSpoke", { |
| 40 | hub: hub.name, |
| 41 | description: "spoke a", |
| 42 | labels: { env: "test" }, |
| 43 | linkedVpcNetwork: { |
| 44 | uri: network.selfLink.as<string>(), |
| 45 | }, |
| 46 | }); |
| 47 | return { network, hub, spoke }; |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | expect(created.spoke.name).toContain("/spokes/"); |
| 52 | expect(created.spoke.name).toContain("/locations/global/"); |
| 53 | expect(created.spoke.spokeId).toEqual(expect.any(String)); |
| 54 | expect(created.spoke.location).toEqual("global"); |
| 55 | expect(created.spoke.hub).toEqual(created.hub.name); |
| 56 | expect(created.spoke.description).toEqual("spoke a"); |
| 57 | expect(created.spoke.labels).toMatchObject({ env: "test" }); |
| 58 | expect(created.spoke.spokeType).toEqual("VPC_NETWORK"); |
| 59 | expect(created.spoke.state).toEqual("ACTIVE"); |
| 60 | expect(created.spoke.uniqueId).toEqual(expect.any(String)); |
| 61 | expect(created.spoke.createTime).toEqual(expect.any(String)); |
| 62 | expect(created.spoke.linkedVpcNetwork?.uri).toContain( |
| 63 | `networks/${created.network.networkName}`, |
| 64 | ); |
| 65 | |
| 66 | const fetched = yield* networkconnectivity.getProjectsLocationsSpokes({ |
| 67 | name: created.spoke.name, |
| 68 | }); |
| 69 | expect(fetched.name).toEqual(created.spoke.name); |
| 70 | expect(fetched.hub).toEqual(created.hub.name); |
| 71 | expect(fetched.description).toEqual("spoke a"); |
| 72 | expect(fetched.labels?.env).toEqual("test"); |
| 73 | expect(fetched.spokeType).toEqual("VPC_NETWORK"); |
| 74 | expect(fetched.state).toEqual("ACTIVE"); |
| 75 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 76 | true, |
| 77 | ); |
| 78 | |
| 79 | const updated = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | const network = yield* GCP.Compute.Network("SpokeVpc", { |
| 82 | networkName: created.network.networkName, |
| 83 | autoCreateSubnetworks: false, |
| 84 | }); |
| 85 | const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", { |
| 86 | hubId: created.hub.hubId, |
| 87 | description: "ncc hub for spoke", |
| 88 | }); |
| 89 | const spoke = yield* GCP.NetworkConnectivity.Spoke("VpcSpoke", { |
| 90 | spokeId: created.spoke.spokeId, |
| 91 | hub: hub.name, |
| 92 | description: "spoke b", |
| 93 | labels: { env: "prod", role: "spoke" }, |
| 94 | linkedVpcNetwork: { |
| 95 | uri: network.selfLink.as<string>(), |
| 96 | }, |
| 97 | }); |
| 98 | return { network, hub, spoke }; |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(updated.spoke.name).toEqual(created.spoke.name); |
| 103 | expect(updated.spoke.uniqueId).toEqual(created.spoke.uniqueId); |
| 104 | expect(updated.spoke.description).toEqual("spoke b"); |
| 105 | expect(updated.spoke.labels).toMatchObject({ |
| 106 | env: "prod", |
| 107 | role: "spoke", |
| 108 | }); |
| 109 | expect(updated.spoke.state).toEqual("ACTIVE"); |
| 110 | |
| 111 | const refetched = yield* networkconnectivity.getProjectsLocationsSpokes({ |
| 112 | name: created.spoke.name, |
| 113 | }); |
| 114 | expect(refetched.description).toEqual("spoke b"); |
| 115 | expect(refetched.labels?.env).toEqual("prod"); |
| 116 | expect(refetched.labels?.role).toEqual("spoke"); |
| 117 | expect(refetched.hub).toEqual(created.hub.name); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const gone = yield* waitUntilGone(created.spoke.name); |
| 122 | expect(gone).toEqual("gone"); |
| 123 | }).pipe(logLevel, withNetworkSlot), |
| 124 | { |
| 125 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 126 | timeout: 600_000, |
| 127 | }, |
| 128 | ); |