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
17const waitUntilGone = (name: string) =>
18 networksecurity.getProjectsLocationsGatewaySecurityPolicies({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsGatewaySecurityPolicies on a missing policy fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const { project } = yield* GcpEnvironment.current;
35 const error = yield* Effect.flip(
36 networksecurity.getProjectsLocationsGatewaySecurityPolicies({
37 name: `projects/${project}/locations/us-central1/gatewaySecurityPolicies/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, and delete a gateway security policy",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 return yield* GCP.NetworkSecurity.GatewaySecurityPolicy("Swp", {
59 location: "us-central1",
60 description: "gateway policy a",
61 });
62 }),
63 );
64
65 expect(created.name).toContain("/gatewaySecurityPolicies/");
66 expect(created.gatewaySecurityPolicyId).toEqual(expect.any(String));
67 expect(created.location).toEqual("us-central1");
68 expect(created.description).toEqual("gateway policy a");
69 expect(created.createTime).toEqual(expect.any(String));
70
71 const fetched =
72 yield* networksecurity.getProjectsLocationsGatewaySecurityPolicies({
73 name: created.name,
74 });
75 expect(fetched.name).toEqual(created.name);
76 expect(fetched.description).toContain("alchemy-id=");
77 expect(fetched.description).toContain("gateway policy a");
78
79 const updated = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* GCP.NetworkSecurity.GatewaySecurityPolicy("Swp", {
82 gatewaySecurityPolicyId: created.gatewaySecurityPolicyId,
83 location: "us-central1",
84 description: "gateway policy b",
85 });
86 }),
87 );
88
89 expect(updated.name).toEqual(created.name);
90 expect(updated.description).toEqual("gateway policy b");
91
92 const refetched =
93 yield* networksecurity.getProjectsLocationsGatewaySecurityPolicies({
94 name: created.name,
95 });
96 expect(refetched.description).toContain("gateway policy b");
97 expect(refetched.description).toContain("alchemy-id=");
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(created.name);
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 {
105 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
106 timeout: 90_000,
107 },
108);