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.getProjectsLocationsAddressGroups({ 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 address group",
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.AddressGroup("Allowlist", {
36 type: "IPV4",
37 capacity: 100,
38 items: ["10.0.0.1"],
39 description: "address group a",
40 labels: { env: "test" },
41 });
42 }),
43 );
44
45 expect(created.name).toContain("/addressGroups/");
46 expect(created.name).toContain("/locations/global/");
47 expect(created.addressGroupId).toEqual(expect.any(String));
48 expect(created.location).toEqual("global");
49 expect(created.type).toEqual("IPV4");
50 expect(created.capacity).toEqual(100);
51 expect(created.items).toEqual(["10.0.0.1"]);
52 expect(created.description).toEqual("address group a");
53 expect(created.labels).toMatchObject({ env: "test" });
54 expect(created.createTime).toEqual(expect.any(String));
55
56 const fetched = yield* networksecurity.getProjectsLocationsAddressGroups({
57 name: created.name,
58 });
59 expect(fetched.name).toEqual(created.name);
60 expect(fetched.labels?.env).toEqual("test");
61 expect(fetched.items).toEqual(["10.0.0.1"]);
62 expect(
63 Object.keys(fetched.labels ?? {}).some((key) =>
64 key.startsWith("alchemy-"),
65 ),
66 ).toEqual(true);
67
68 const updated = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.NetworkSecurity.AddressGroup("Allowlist", {
71 addressGroupId: created.addressGroupId,
72 type: "IPV4",
73 capacity: 100,
74 items: ["10.0.0.1", "10.1.0.0/24"],
75 description: "address group b",
76 labels: { env: "prod", role: "allowlist" },
77 });
78 }),
79 );
80
81 expect(updated.name).toEqual(created.name);
82 expect(updated.description).toEqual("address group b");
83 expect(updated.items.sort()).toEqual(["10.0.0.1", "10.1.0.0/24"].sort());
84 expect(updated.labels).toMatchObject({ env: "prod", role: "allowlist" });
85
86 const refetched =
87 yield* networksecurity.getProjectsLocationsAddressGroups({
88 name: created.name,
89 });
90 expect(refetched.description).toEqual("address group b");
91 expect(refetched.labels?.env).toEqual("prod");
92 expect(refetched.labels?.role).toEqual("allowlist");
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);