The best way to control Claude Code, Codex and more from any computer or phone

main
ts 183 lines 7.21 KB
Raw
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.getProjectsLocationsMirroringDeployments({ 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 "getProjectsLocationsMirroringDeployments 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.getProjectsLocationsMirroringDeployments({
35 name: `projects/${project}/locations/us-central1-a/mirroringDeployments/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 a mirroring 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: "mirroring deployment vpc",
59 });
60 const subnet = yield* GCP.Compute.Subnetwork("Subnet", {
61 region: "us-central1",
62 network: vpc.selfLink.as<string>(),
63 ipCidrRange: "10.10.0.0/24",
64 });
65 const health = yield* GCP.Compute.HealthCheck("Tcp", {
66 type: "TCP",
67 tcpHealthCheck: { port: 80 },
68 });
69 const backend = yield* GCP.Compute.RegionBackendService("Collector", {
70 region: "us-central1",
71 protocol: "UDP",
72 loadBalancingScheme: "INTERNAL",
73 healthChecks: [health.selfLink.as<string>()],
74 });
75 const rule = yield* GCP.Compute.ForwardingRule("CollectorRule", {
76 region: "us-central1",
77 loadBalancingScheme: "INTERNAL",
78 ipProtocol: "UDP",
79 allPorts: true,
80 network: vpc.selfLink.as<string>(),
81 subnetwork: subnet.selfLink.as<string>(),
82 backendService: backend.selfLink.as<string>(),
83 isMirroringCollector: true,
84 });
85 const collectors = yield* GCP.NetworkSecurity.MirroringDeploymentGroup("Collectors", {
86 network: vpc.selfLink.as<string>(),
87 });
88 const deployment = yield* GCP.NetworkSecurity.MirroringDeployment("ZoneA", {
89 location: "us-central1-a",
90 mirroringDeploymentGroup: collectors.name,
91 forwardingRule: rule.selfLink.as<string>(),
92 description: "mirroring dep a",
93 labels: { env: "test" },
94 });
95 return { vpc, subnet, health, backend, rule, collectors, deployment };
96 }),
97 );
98
99 expect(created.deployment.name).toContain("/mirroringDeployments/");
100 expect(created.deployment.location).toEqual("us-central1-a");
101 expect(created.deployment.description).toEqual("mirroring dep a");
102 expect(created.deployment.labels).toMatchObject({ env: "test" });
103 expect(created.deployment.mirroringDeploymentGroup).toEqual(created.collectors.name);
104
105 const fetched = yield* networksecurity.getProjectsLocationsMirroringDeployments({
106 name: created.deployment.name,
107 });
108 expect(fetched.name).toEqual(created.deployment.name);
109 expect(fetched.description).toEqual("mirroring dep a");
110 expect(fetched.labels?.env).toEqual("test");
111 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
112 true,
113 );
114
115 const updated = yield* stack.deploy(
116 Effect.gen(function* () {
117 const vpc = yield* GCP.Compute.Network("Vpc", {
118 networkName: created.vpc.networkName,
119 autoCreateSubnetworks: false,
120 description: "mirroring deployment vpc",
121 });
122 const subnet = yield* GCP.Compute.Subnetwork("Subnet", {
123 subnetworkName: created.subnet.subnetworkName,
124 region: "us-central1",
125 network: vpc.selfLink.as<string>(),
126 ipCidrRange: "10.10.0.0/24",
127 });
128 const health = yield* GCP.Compute.HealthCheck("Tcp", {
129 healthCheckName: created.health.healthCheckName,
130 type: "TCP",
131 tcpHealthCheck: { port: 80 },
132 });
133 const backend = yield* GCP.Compute.RegionBackendService("Collector", {
134 name: created.backend.name,
135 region: "us-central1",
136 protocol: "UDP",
137 loadBalancingScheme: "INTERNAL",
138 healthChecks: [health.selfLink.as<string>()],
139 });
140 const rule = yield* GCP.Compute.ForwardingRule("CollectorRule", {
141 forwardingRuleName: created.rule.forwardingRuleName,
142 region: "us-central1",
143 loadBalancingScheme: "INTERNAL",
144 ipProtocol: "UDP",
145 allPorts: true,
146 network: vpc.selfLink.as<string>(),
147 subnetwork: subnet.selfLink.as<string>(),
148 backendService: backend.selfLink.as<string>(),
149 isMirroringCollector: true,
150 });
151 const collectors = yield* GCP.NetworkSecurity.MirroringDeploymentGroup("Collectors", {
152 mirroringDeploymentGroupId: created.collectors.mirroringDeploymentGroupId,
153 network: vpc.selfLink.as<string>(),
154 });
155 const deployment = yield* GCP.NetworkSecurity.MirroringDeployment("ZoneA", {
156 mirroringDeploymentId: created.deployment.mirroringDeploymentId,
157 location: "us-central1-a",
158 mirroringDeploymentGroup: collectors.name,
159 forwardingRule: rule.selfLink.as<string>(),
160 description: "mirroring dep b",
161 labels: { env: "prod", role: "nsi" },
162 });
163 return { vpc, subnet, health, backend, rule, collectors, deployment };
164 }),
165 );
166
167 expect(updated.deployment.name).toEqual(created.deployment.name);
168 expect(updated.deployment.description).toEqual("mirroring dep b");
169 expect(updated.deployment.labels).toMatchObject({
170 env: "prod",
171 role: "nsi",
172 });
173
174 yield* stack.destroy();
175
176 const gone = yield* waitUntilGone(created.deployment.name);
177 expect(gone).toEqual("gone");
178 }).pipe(logLevel, withNetworkSlot),
179 {
180 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
181 timeout: 900_000,
182 },
183 );