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.getProjectsLocationsUrlLists({ 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 "getProjectsLocationsUrlLists on a missing list 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.getProjectsLocationsUrlLists({
37 name: `projects/${project}/locations/us-central1/urlLists/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 url list",
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.UrlList("Blocked", {
59 location: "us-central1",
60 description: "url list a",
61 values: ["malware.example.com"],
62 });
63 }),
64 );
65
66 expect(created.name).toContain("/urlLists/");
67 expect(created.name).toContain("/locations/us-central1/");
68 expect(created.urlListId).toEqual(expect.any(String));
69 expect(created.location).toEqual("us-central1");
70 expect(created.description).toEqual("url list a");
71 expect(created.values).toEqual(["malware.example.com"]);
72 expect(created.createTime).toEqual(expect.any(String));
73
74 const fetched = yield* networksecurity.getProjectsLocationsUrlLists({
75 name: created.name,
76 });
77 expect(fetched.name).toEqual(created.name);
78 expect(fetched.values).toEqual(["malware.example.com"]);
79 expect(fetched.description ?? "").toContain("[alchemy ");
80 expect(fetched.description ?? "").toContain("url list a");
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.NetworkSecurity.UrlList("Blocked", {
85 urlListId: created.urlListId,
86 location: "us-central1",
87 description: "url list b",
88 values: ["malware.example.com", "c2.example.net"],
89 });
90 }),
91 );
92
93 expect(updated.name).toEqual(created.name);
94 expect(updated.description).toEqual("url list b");
95 expect(updated.values).toEqual(["malware.example.com", "c2.example.net"]);
96
97 const refetched = yield* networksecurity.getProjectsLocationsUrlLists({
98 name: created.name,
99 });
100 expect(refetched.description ?? "").toContain("url list b");
101 expect(refetched.values).toEqual([
102 "malware.example.com",
103 "c2.example.net",
104 ]);
105
106 yield* stack.destroy();
107
108 const gone = yield* waitUntilGone(created.name);
109 expect(gone).toEqual("gone");
110 }).pipe(logLevel),
111 {
112 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
113 timeout: 90_000,
114 },
115);