1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// Firewall endpoints (Cloud NGFW Enterprise) bill hourly and take well over 5
18// minutes to provision; set GCP_TEST_NGFW=1 together with GCP_TEST_SLOW to run.
19const runLifecycle =
20 !!process.env.GCP_TEST_NGFW &&
21 !!process.env.GCP_TEST_SLOW &&
22 !process.env.FAST;
23
24const waitUntilGone = (name: string) =>
25 networksecurity.getProjectsLocationsFirewallEndpoints({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsFirewallEndpoints on a missing endpoint fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const { project } = yield* GcpEnvironment.current;
42 const error = yield* Effect.flip(
43 networksecurity.getProjectsLocationsFirewallEndpoints({
44 name: `projects/${project}/locations/us-central1-a/firewallEndpoints/alchemy-missing`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider.skipIf(!runLifecycle)(
58 "create, update, and delete a firewall endpoint",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 return yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
66 location: "us-central1-a",
67 description: "firewall endpoint a",
68 labels: { env: "test" },
69 });
70 }),
71 );
72
73 expect(created.name).toContain("/firewallEndpoints/");
74 expect(created.firewallEndpointId).toEqual(expect.any(String));
75 expect(created.location).toEqual("us-central1-a");
76 expect(created.description).toEqual("firewall endpoint a");
77 expect(created.labels).toMatchObject({ env: "test" });
78 expect(created.createTime).toEqual(expect.any(String));
79
80 const fetched =
81 yield* networksecurity.getProjectsLocationsFirewallEndpoints({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.description).toEqual("firewall endpoint a");
86 expect(fetched.labels?.env).toEqual("test");
87 expect(
88 Object.keys(fetched.labels ?? {}).some((key) =>
89 key.startsWith("alchemy-"),
90 ),
91 ).toEqual(true);
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
96 firewallEndpointId: created.firewallEndpointId,
97 location: "us-central1-a",
98 description: "firewall endpoint b",
99 labels: { env: "prod", role: "ngfw" },
100 });
101 }),
102 );
103
104 expect(updated.name).toEqual(created.name);
105 expect(updated.description).toEqual("firewall endpoint b");
106 expect(updated.labels).toMatchObject({ env: "prod", role: "ngfw" });
107
108 const refetched =
109 yield* networksecurity.getProjectsLocationsFirewallEndpoints({
110 name: created.name,
111 });
112 expect(refetched.description).toEqual("firewall endpoint b");
113 expect(refetched.labels?.env).toEqual("prod");
114 expect(refetched.labels?.role).toEqual("ngfw");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 {
122 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
123 timeout: 90_000,
124 },
125);