@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 372 lines 12.6 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 compute from "@distilled.cloud/gcp/compute_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const region = "us-central1";
15 const zone = "us-central1-a";
16 const names = {
17 healthCheck: "alchemy-pm-hc",
18 instanceGroup: "alchemy-pm-ig",
19 backend: "alchemy-pm-bs",
20 forwardingRule: "alchemy-pm-fr",
21 };
22
23 const lastSegment = (value: string | undefined) => {
24 if (value === undefined || value.length === 0) return "";
25 const parts = value.split("/").filter((part) => part.length > 0);
26 return parts[parts.length - 1] ?? "";
27 };
28
29 const waitUntilGone = (packetMirroringName: string) =>
30 GcpEnvironment.current.pipe(
31 Effect.flatMap(({ project }) =>
32 compute
33 .getPacketMirrorings({
34 project,
35 region,
36 packetMirroring: packetMirroringName,
37 })
38 .pipe(
39 Effect.as("found" as const),
40 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
41 Effect.repeat({
42 schedule: Schedule.spaced("2 seconds"),
43 until: (status) => status === "gone",
44 times: 10,
45 }),
46 ),
47 ),
48 );
49
50 const waitOp = (operation: compute.Operation, scope: "global" | "region" | "zone") => {
51 if (operation.status === "DONE") return Effect.succeed(operation);
52 const name = lastSegment(operation.name);
53 if (name.length === 0) return Effect.succeed(operation);
54 return GcpEnvironment.current.pipe(
55 Effect.flatMap(({ project }) => {
56 const poll =
57 scope === "global"
58 ? compute.getGlobalOperations({ project, operation: name })
59 : scope === "zone"
60 ? compute.getZoneOperations({ project, zone, operation: name })
61 : compute.getRegionOperations({ project, region, operation: name });
62 return poll.pipe(
63 Effect.catchTag("NotFound", () => Effect.succeed({ status: "DONE" } as compute.Operation)),
64 Effect.repeat({
65 schedule: Schedule.spaced("2 seconds"),
66 until: (op) => op.status === "DONE",
67 times: 8,
68 }),
69 );
70 }),
71 );
72 };
73
74 const ensureCollector = () =>
75 Effect.gen(function* () {
76 const { project } = yield* GcpEnvironment.current;
77 const network = yield* compute.getNetworks({
78 project,
79 network: "default",
80 });
81 const subnet = yield* compute.getSubnetworks({
82 project,
83 region,
84 subnetwork: "default",
85 });
86
87 const healthCheck = yield* compute
88 .getHealthChecks({ project, healthCheck: names.healthCheck })
89 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
90 if (healthCheck === undefined) {
91 yield* compute
92 .insertHealthChecks({
93 project,
94 body: {
95 name: names.healthCheck,
96 type: "TCP",
97 tcpHealthCheck: { port: 80 },
98 description: "alchemy packet-mirroring collector",
99 },
100 })
101 .pipe(
102 Effect.flatMap((operation) => waitOp(operation, "global")),
103 Effect.catchTag("Conflict", () => Effect.void),
104 );
105 }
106
107 const instanceGroup = yield* compute
108 .getInstanceGroups({
109 project,
110 zone,
111 instanceGroup: names.instanceGroup,
112 })
113 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
114 if (instanceGroup === undefined) {
115 yield* compute
116 .insertInstanceGroups({
117 project,
118 zone,
119 body: {
120 name: names.instanceGroup,
121 network: network.selfLink,
122 description: "alchemy packet-mirroring collector",
123 },
124 })
125 .pipe(
126 Effect.flatMap((operation) => waitOp(operation, "zone")),
127 Effect.catchTag("Conflict", () => Effect.void),
128 );
129 }
130
131 const backend = yield* compute
132 .getRegionBackendServices({
133 project,
134 region,
135 backendService: names.backend,
136 })
137 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
138 if (backend === undefined) {
139 const hc = yield* compute.getHealthChecks({
140 project,
141 healthCheck: names.healthCheck,
142 });
143 const group = yield* compute.getInstanceGroups({
144 project,
145 zone,
146 instanceGroup: names.instanceGroup,
147 });
148 yield* compute
149 .insertRegionBackendServices({
150 project,
151 region,
152 body: {
153 name: names.backend,
154 protocol: "TCP",
155 loadBalancingScheme: "INTERNAL",
156 healthChecks: hc.selfLink ? [hc.selfLink] : undefined,
157 backends: group.selfLink ? [{ group: group.selfLink }] : undefined,
158 network: network.selfLink,
159 description: "alchemy packet-mirroring collector",
160 },
161 })
162 .pipe(
163 Effect.flatMap((operation) => waitOp(operation, "region")),
164 Effect.catchTag("Conflict", () => Effect.void),
165 );
166 }
167
168 const rule = yield* compute
169 .getForwardingRules({
170 project,
171 region,
172 forwardingRule: names.forwardingRule,
173 })
174 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
175 if (rule === undefined) {
176 const backendService = yield* compute.getRegionBackendServices({
177 project,
178 region,
179 backendService: names.backend,
180 });
181 yield* compute
182 .insertForwardingRules({
183 project,
184 region,
185 body: {
186 name: names.forwardingRule,
187 loadBalancingScheme: "INTERNAL",
188 isMirroringCollector: true,
189 IPProtocol: "TCP",
190 allPorts: true,
191 backendService: backendService.selfLink,
192 network: network.selfLink,
193 subnetwork: subnet.selfLink,
194 description: "alchemy packet-mirroring collector",
195 },
196 })
197 .pipe(
198 Effect.flatMap((operation) => waitOp(operation, "region")),
199 Effect.catchTag("Conflict", () => Effect.void),
200 );
201 }
202
203 const forwardingRule = yield* compute.getForwardingRules({
204 project,
205 region,
206 forwardingRule: names.forwardingRule,
207 });
208 return {
209 network: network.selfLink ?? `projects/${project}/global/networks/default`,
210 forwardingRule:
211 forwardingRule.selfLink ??
212 `projects/${project}/regions/${region}/forwardingRules/${names.forwardingRule}`,
213 };
214 });
215
216 const deleteCollector = () =>
217 Effect.gen(function* () {
218 const { project } = yield* GcpEnvironment.current;
219 yield* compute
220 .deleteForwardingRules({
221 project,
222 region,
223 forwardingRule: names.forwardingRule,
224 })
225 .pipe(
226 Effect.flatMap((operation) => waitOp(operation, "region")),
227 Effect.catchTag("NotFound", () => Effect.void),
228 Effect.catchTag("Conflict", () => Effect.void),
229 );
230 yield* compute
231 .deleteRegionBackendServices({
232 project,
233 region,
234 backendService: names.backend,
235 })
236 .pipe(
237 Effect.flatMap((operation) => waitOp(operation, "region")),
238 Effect.catchTag("NotFound", () => Effect.void),
239 Effect.catchTag("Conflict", () => Effect.void),
240 );
241 yield* compute
242 .deleteInstanceGroups({
243 project,
244 zone,
245 instanceGroup: names.instanceGroup,
246 })
247 .pipe(
248 Effect.flatMap((operation) => waitOp(operation, "zone")),
249 Effect.catchTag("NotFound", () => Effect.void),
250 Effect.catchTag("Conflict", () => Effect.void),
251 );
252 yield* compute.deleteHealthChecks({ project, healthCheck: names.healthCheck }).pipe(
253 Effect.flatMap((operation) => waitOp(operation, "global")),
254 Effect.catchTag("NotFound", () => Effect.void),
255 Effect.catchTag("Conflict", () => Effect.void),
256 );
257 });
258
259 test.provider(
260 "create, update, replace, and delete a packet mirroring policy",
261 (stack) =>
262 Effect.gen(function* () {
263 yield* stack.destroy();
264 const collector = yield* ensureCollector();
265 expect(collector.forwardingRule).toEqual(expect.any(String));
266
267 const created = yield* stack.deploy(
268 Effect.gen(function* () {
269 return yield* GCP.Compute.PacketMirroring("Capture", {
270 region,
271 description: "test capture",
272 network: collector.network,
273 collectorIlb: collector.forwardingRule,
274 mirroredResources: { tags: ["alchemy-pm"] },
275 });
276 }),
277 );
278
279 expect(created.packetMirroringName).toEqual(expect.any(String));
280 expect(created.region).toEqual(region);
281 expect(created.description).toEqual("test capture");
282 expect(created.enable).toEqual(true);
283 expect(created.priority).toEqual(1000);
284 expect(created.network).toEqual(expect.stringContaining("networks/"));
285 expect(created.collectorIlb).toEqual(expect.stringContaining(names.forwardingRule));
286 expect(created.mirroredResources.tags).toEqual(["alchemy-pm"]);
287
288 const fetched = yield* compute.getPacketMirrorings({
289 project: created.project,
290 region: created.region,
291 packetMirroring: created.packetMirroringName,
292 });
293 expect(fetched.name).toEqual(created.packetMirroringName);
294 expect(fetched.description).toContain("[alchemy ");
295 expect(fetched.description).toContain("test capture");
296 expect(fetched.enable).toEqual("TRUE");
297 expect(fetched.mirroredResources?.tags).toEqual(["alchemy-pm"]);
298 expect(fetched.collectorIlb?.url).toEqual(expect.stringContaining(names.forwardingRule));
299
300 const updated = yield* stack.deploy(
301 Effect.gen(function* () {
302 return yield* GCP.Compute.PacketMirroring("Capture", {
303 packetMirroringName: created.packetMirroringName,
304 region,
305 description: "test capture",
306 network: collector.network,
307 collectorIlb: collector.forwardingRule,
308 mirroredResources: { tags: ["alchemy-pm", "alchemy-pm-web"] },
309 enable: false,
310 priority: 800,
311 filter: {
312 direction: "INGRESS",
313 ipProtocols: ["tcp"],
314 cidrRanges: ["10.0.0.0/8"],
315 },
316 });
317 }),
318 );
319
320 expect(updated.packetMirroringName).toEqual(created.packetMirroringName);
321 expect(updated.packetMirroringId).toEqual(created.packetMirroringId);
322 expect(updated.enable).toEqual(false);
323 expect(updated.priority).toEqual(800);
324 expect([...updated.mirroredResources.tags].sort()).toEqual(
325 ["alchemy-pm", "alchemy-pm-web"].sort(),
326 );
327 expect(updated.filter?.direction).toEqual("INGRESS");
328 expect(updated.filter?.ipProtocols).toEqual(["tcp"]);
329 expect(updated.filter?.cidrRanges).toEqual(["10.0.0.0/8"]);
330
331 const fetchedUpdate = yield* compute.getPacketMirrorings({
332 project: updated.project,
333 region: updated.region,
334 packetMirroring: updated.packetMirroringName,
335 });
336 expect(fetchedUpdate.enable).toEqual("FALSE");
337 expect(fetchedUpdate.priority).toEqual(800);
338 expect(fetchedUpdate.filter?.direction).toEqual("INGRESS");
339
340 const replaced = yield* stack.deploy(
341 Effect.gen(function* () {
342 return yield* GCP.Compute.PacketMirroring("Capture", {
343 packetMirroringName: created.packetMirroringName,
344 region,
345 description: "replaced capture",
346 network: collector.network,
347 collectorIlb: collector.forwardingRule,
348 mirroredResources: { tags: ["alchemy-pm"] },
349 });
350 }),
351 );
352
353 expect(replaced.packetMirroringName).toEqual(created.packetMirroringName);
354 expect(replaced.description).toEqual("replaced capture");
355 expect(replaced.enable).toEqual(true);
356 expect(replaced.packetMirroringId).not.toEqual(created.packetMirroringId);
357
358 const fetchedReplace = yield* compute.getPacketMirrorings({
359 project: replaced.project,
360 region: replaced.region,
361 packetMirroring: replaced.packetMirroringName,
362 });
363 expect(fetchedReplace.description).toContain("replaced capture");
364 expect(fetchedReplace.enable).toEqual("TRUE");
365
366 yield* stack.destroy();
367
368 const gone = yield* waitUntilGone(created.packetMirroringName);
369 expect(gone).toEqual("gone");
370 }).pipe(logLevel, Effect.ensuring(deleteCollector().pipe(Effect.ignore))),
371 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 },
372 );