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 { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (name: string) => |
| 16 | networksecurity.getProjectsLocationsInterceptDeployments({ name }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("2 seconds"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | test.provider( |
| 27 | "getProjectsLocationsInterceptDeployments on a missing deployment fails with a typed tag", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | const error = yield* Effect.flip( |
| 34 | networksecurity.getProjectsLocationsInterceptDeployments({ |
| 35 | name: `projects/${project}/locations/us-central1-a/interceptDeployments/alchemy-missing`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toEqual("NotFound"); |
| 39 | |
| 40 | yield* stack.destroy(); |
| 41 | }).pipe(logLevel), |
| 42 | { |
| 43 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 44 | timeout: 90_000, |
| 45 | }, |
| 46 | ); |
| 47 | |
| 48 | test.provider.skipIf(!!process.env.FAST)( |
| 49 | "create, update, and delete an intercept deployment", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const created = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 57 | autoCreateSubnetworks: false, |
| 58 | description: "intercept deployment vpc", |
| 59 | }); |
| 60 | const subnet = yield* GCP.Compute.Subnetwork("Subnet", { |
| 61 | region: "us-central1", |
| 62 | network: vpc.networkName, |
| 63 | ipCidrRange: "10.90.0.0/24", |
| 64 | description: "intercept deployment subnet", |
| 65 | }); |
| 66 | const healthCheck = yield* GCP.Compute.RegionHealthCheck("Hc", { |
| 67 | region: "us-central1", |
| 68 | type: "TCP", |
| 69 | tcpHealthCheck: { port: 80 }, |
| 70 | description: "intercept deployment hc", |
| 71 | }); |
| 72 | const backend = yield* GCP.Compute.RegionBackendService("Backend", { |
| 73 | region: "us-central1", |
| 74 | protocol: "UDP", |
| 75 | loadBalancingScheme: "INTERNAL", |
| 76 | network: vpc.selfLink, |
| 77 | healthChecks: [healthCheck.selfLink.as<string>()], |
| 78 | description: "intercept deployment backend", |
| 79 | }); |
| 80 | const rule = yield* GCP.Compute.ForwardingRule("Rule", { |
| 81 | region: "us-central1", |
| 82 | loadBalancingScheme: "INTERNAL", |
| 83 | ipProtocol: "UDP", |
| 84 | ports: ["6081"], |
| 85 | network: vpc.selfLink, |
| 86 | subnetwork: subnet.selfLink, |
| 87 | backendService: backend.selfLink, |
| 88 | description: "intercept deployment rule", |
| 89 | }); |
| 90 | const group = yield* GCP.NetworkSecurity.InterceptDeploymentGroup("Inspect", { |
| 91 | location: "global", |
| 92 | network: vpc.selfLink.as<string>(), |
| 93 | description: "intercept deployment group", |
| 94 | labels: { env: "test" }, |
| 95 | }); |
| 96 | const deployment = yield* GCP.NetworkSecurity.InterceptDeployment("ZoneA", { |
| 97 | location: "us-central1-a", |
| 98 | interceptDeploymentGroup: group.name, |
| 99 | forwardingRule: rule.selfLink.as<string>(), |
| 100 | description: "intercept deployment a", |
| 101 | labels: { env: "test" }, |
| 102 | }); |
| 103 | return { vpc, subnet, healthCheck, backend, rule, group, deployment }; |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | expect(created.deployment.name).toContain("/interceptDeployments/"); |
| 108 | expect(created.deployment.location).toEqual("us-central1-a"); |
| 109 | expect(created.deployment.description).toEqual("intercept deployment a"); |
| 110 | expect(created.deployment.labels).toMatchObject({ env: "test" }); |
| 111 | expect(created.deployment.interceptDeploymentGroup).toEqual(created.group.name); |
| 112 | |
| 113 | const fetched = yield* networksecurity.getProjectsLocationsInterceptDeployments({ |
| 114 | name: created.deployment.name, |
| 115 | }); |
| 116 | expect(fetched.name).toEqual(created.deployment.name); |
| 117 | expect(fetched.description).toEqual("intercept deployment a"); |
| 118 | expect(fetched.labels?.env).toEqual("test"); |
| 119 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 120 | true, |
| 121 | ); |
| 122 | |
| 123 | const updated = yield* stack.deploy( |
| 124 | Effect.gen(function* () { |
| 125 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 126 | networkName: created.vpc.networkName, |
| 127 | autoCreateSubnetworks: false, |
| 128 | description: "intercept deployment vpc", |
| 129 | }); |
| 130 | const subnet = yield* GCP.Compute.Subnetwork("Subnet", { |
| 131 | subnetworkName: created.subnet.subnetworkName, |
| 132 | region: "us-central1", |
| 133 | network: vpc.networkName, |
| 134 | ipCidrRange: "10.90.0.0/24", |
| 135 | description: "intercept deployment subnet", |
| 136 | }); |
| 137 | const healthCheck = yield* GCP.Compute.RegionHealthCheck("Hc", { |
| 138 | healthCheckName: created.healthCheck.healthCheckName, |
| 139 | region: "us-central1", |
| 140 | type: "TCP", |
| 141 | tcpHealthCheck: { port: 80 }, |
| 142 | description: "intercept deployment hc", |
| 143 | }); |
| 144 | const backend = yield* GCP.Compute.RegionBackendService("Backend", { |
| 145 | name: created.backend.name, |
| 146 | region: "us-central1", |
| 147 | protocol: "UDP", |
| 148 | loadBalancingScheme: "INTERNAL", |
| 149 | network: vpc.selfLink, |
| 150 | healthChecks: [healthCheck.selfLink.as<string>()], |
| 151 | description: "intercept deployment backend", |
| 152 | }); |
| 153 | const rule = yield* GCP.Compute.ForwardingRule("Rule", { |
| 154 | forwardingRuleName: created.rule.forwardingRuleName, |
| 155 | region: "us-central1", |
| 156 | loadBalancingScheme: "INTERNAL", |
| 157 | ipProtocol: "UDP", |
| 158 | ports: ["6081"], |
| 159 | network: vpc.selfLink, |
| 160 | subnetwork: subnet.selfLink, |
| 161 | backendService: backend.selfLink, |
| 162 | description: "intercept deployment rule", |
| 163 | }); |
| 164 | const group = yield* GCP.NetworkSecurity.InterceptDeploymentGroup("Inspect", { |
| 165 | interceptDeploymentGroupId: created.group.interceptDeploymentGroupId, |
| 166 | location: "global", |
| 167 | network: vpc.selfLink.as<string>(), |
| 168 | description: "intercept deployment group", |
| 169 | labels: { env: "test" }, |
| 170 | }); |
| 171 | const deployment = yield* GCP.NetworkSecurity.InterceptDeployment("ZoneA", { |
| 172 | interceptDeploymentId: created.deployment.interceptDeploymentId, |
| 173 | location: "us-central1-a", |
| 174 | interceptDeploymentGroup: group.name, |
| 175 | forwardingRule: rule.selfLink.as<string>(), |
| 176 | description: "intercept deployment b", |
| 177 | labels: { env: "prod", role: "intercept" }, |
| 178 | }); |
| 179 | return { vpc, subnet, healthCheck, backend, rule, group, deployment }; |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | expect(updated.deployment.name).toEqual(created.deployment.name); |
| 184 | expect(updated.deployment.description).toEqual("intercept deployment b"); |
| 185 | expect(updated.deployment.labels).toMatchObject({ |
| 186 | env: "prod", |
| 187 | role: "intercept", |
| 188 | }); |
| 189 | |
| 190 | const refetched = yield* networksecurity.getProjectsLocationsInterceptDeployments({ |
| 191 | name: created.deployment.name, |
| 192 | }); |
| 193 | expect(refetched.description).toEqual("intercept deployment b"); |
| 194 | expect(refetched.labels?.env).toEqual("prod"); |
| 195 | expect(refetched.labels?.role).toEqual("intercept"); |
| 196 | |
| 197 | yield* stack.destroy(); |
| 198 | |
| 199 | const gone = yield* waitUntilGone(created.deployment.name); |
| 200 | expect(gone).toEqual("gone"); |
| 201 | }).pipe(logLevel, withNetworkSlot), |
| 202 | { |
| 203 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 204 | timeout: 600_000, |
| 205 | }, |
| 206 | ); |