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

main
ts 151 lines 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 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 { withNetworkSlots } 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.getProjectsLocationsMirroringEndpointGroupAssociations({ 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 "getProjectsLocationsMirroringEndpointGroupAssociations on a missing association 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.getProjectsLocationsMirroringEndpointGroupAssociations({
35 name: `projects/${project}/locations/global/mirroringEndpointGroupAssociations/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 endpoint group association",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 const producer = yield* GCP.Compute.Network("Producer", {
57 autoCreateSubnetworks: false,
58 description: "mirroring producer vpc",
59 });
60 const consumer = yield* GCP.Compute.Network("Consumer", {
61 autoCreateSubnetworks: false,
62 description: "mirroring consumer vpc",
63 });
64 const collectors = yield* GCP.NetworkSecurity.MirroringDeploymentGroup("Collectors", {
65 network: producer.selfLink.as<string>(),
66 description: "collectors",
67 });
68 const endpoints = yield* GCP.NetworkSecurity.MirroringEndpointGroup("Front", {
69 mirroringDeploymentGroup: collectors.name,
70 });
71 const association = yield* GCP.NetworkSecurity.MirroringEndpointGroupAssociation("Link", {
72 mirroringEndpointGroup: endpoints.name,
73 network: consumer.selfLink.as<string>(),
74 labels: { env: "test" },
75 });
76 return { producer, consumer, collectors, endpoints, association };
77 }),
78 );
79
80 expect(created.association.name).toContain("/mirroringEndpointGroupAssociations/");
81 expect(created.association.location).toEqual("global");
82 expect(created.association.labels).toMatchObject({ env: "test" });
83 expect(created.association.mirroringEndpointGroup).toEqual(created.endpoints.name);
84 expect(created.association.networkName).toEqual(created.consumer.networkName);
85
86 const fetched = yield* networksecurity.getProjectsLocationsMirroringEndpointGroupAssociations(
87 {
88 name: created.association.name,
89 },
90 );
91 expect(fetched.name).toEqual(created.association.name);
92 expect(fetched.labels?.env).toEqual("test");
93 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
94 true,
95 );
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 const producer = yield* GCP.Compute.Network("Producer", {
100 networkName: created.producer.networkName,
101 autoCreateSubnetworks: false,
102 description: "mirroring producer vpc",
103 });
104 const consumer = yield* GCP.Compute.Network("Consumer", {
105 networkName: created.consumer.networkName,
106 autoCreateSubnetworks: false,
107 description: "mirroring consumer vpc",
108 });
109 const collectors = yield* GCP.NetworkSecurity.MirroringDeploymentGroup("Collectors", {
110 mirroringDeploymentGroupId: created.collectors.mirroringDeploymentGroupId,
111 network: producer.selfLink.as<string>(),
112 description: "collectors",
113 });
114 const endpoints = yield* GCP.NetworkSecurity.MirroringEndpointGroup("Front", {
115 mirroringEndpointGroupId: created.endpoints.mirroringEndpointGroupId,
116 mirroringDeploymentGroup: collectors.name,
117 });
118 const association = yield* GCP.NetworkSecurity.MirroringEndpointGroupAssociation("Link", {
119 mirroringEndpointGroupAssociationId:
120 created.association.mirroringEndpointGroupAssociationId,
121 mirroringEndpointGroup: endpoints.name,
122 network: consumer.selfLink.as<string>(),
123 labels: { env: "prod", role: "nsi" },
124 });
125 return { producer, consumer, collectors, endpoints, association };
126 }),
127 );
128
129 expect(updated.association.name).toEqual(created.association.name);
130 expect(updated.association.labels).toMatchObject({
131 env: "prod",
132 role: "nsi",
133 });
134
135 const refetched =
136 yield* networksecurity.getProjectsLocationsMirroringEndpointGroupAssociations({
137 name: created.association.name,
138 });
139 expect(refetched.labels?.env).toEqual("prod");
140 expect(refetched.labels?.role).toEqual("nsi");
141
142 yield* stack.destroy();
143
144 const gone = yield* waitUntilGone(created.association.name);
145 expect(gone).toEqual("gone");
146 }).pipe(logLevel, withNetworkSlots(2)),
147 {
148 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
149 timeout: 600_000,
150 },
151 );