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";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (name: string) =>
17 networksecurity.getProjectsLocationsAuthorizationPolicies({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider(
28 "create, update, and delete an authorization policy",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.NetworkSecurity.AuthorizationPolicy("Allow", {
36 action: "ALLOW",
37 description: "authz policy a",
38 labels: { env: "test" },
39 });
40 }),
41 );
42
43 expect(created.name).toContain("/authorizationPolicies/");
44 expect(created.name).toContain("/locations/global/");
45 expect(created.authorizationPolicyId).toEqual(expect.any(String));
46 expect(created.location).toEqual("global");
47 expect(created.action).toEqual("ALLOW");
48 expect(created.description).toEqual("authz policy a");
49 expect(created.labels).toMatchObject({ env: "test" });
50 expect(created.createTime).toEqual(expect.any(String));
51
52 const fetched =
53 yield* networksecurity.getProjectsLocationsAuthorizationPolicies({
54 name: created.name,
55 });
56 expect(fetched.name).toEqual(created.name);
57 expect(fetched.labels?.env).toEqual("test");
58 expect(fetched.action).toEqual("ALLOW");
59 expect(
60 Object.keys(fetched.labels ?? {}).some((key) =>
61 key.startsWith("alchemy-"),
62 ),
63 ).toEqual(true);
64
65 const updated = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.NetworkSecurity.AuthorizationPolicy("Allow", {
68 authorizationPolicyId: created.authorizationPolicyId,
69 action: "ALLOW",
70 rules: [
71 {
72 destinations: [{ hosts: ["api.example.com"], ports: [443] }],
73 },
74 ],
75 description: "authz policy b",
76 labels: { env: "prod", role: "authz" },
77 });
78 }),
79 );
80
81 expect(updated.name).toEqual(created.name);
82 expect(updated.description).toEqual("authz policy b");
83 expect(updated.rules.length).toBeGreaterThan(0);
84 expect(updated.labels).toMatchObject({ env: "prod", role: "authz" });
85
86 const refetched =
87 yield* networksecurity.getProjectsLocationsAuthorizationPolicies({
88 name: created.name,
89 });
90 expect(refetched.description).toEqual("authz policy b");
91 expect(refetched.labels?.env).toEqual("prod");
92 expect(refetched.rules?.length).toBeGreaterThan(0);
93
94 yield* stack.destroy();
95
96 const gone = yield* waitUntilGone(created.name);
97 expect(gone).toEqual("gone");
98 }).pipe(logLevel),
99 {
100 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
101 timeout: 90_000,
102 },
103);