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 runLifecycle = !process.env.FAST;
18
19const waitUntilGone = (name: string) =>
20 networksecurity.getProjectsLocationsSacRealms({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30test.provider(
31 "getProjectsLocationsSacRealms on a missing realm fails with a typed tag",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const { project } = yield* GcpEnvironment.current;
37 const error = yield* Effect.flip(
38 networksecurity.getProjectsLocationsSacRealms({
39 name: `projects/${project}/locations/global/sacRealms/alchemy-missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
48 timeout: 90_000,
49 },
50);
51
52test.provider.skipIf(!runLifecycle)(
53 "create and delete a sac realm",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* GCP.NetworkSecurity.SacRealm("Prisma", {
61 securityService: "PALO_ALTO_PRISMA_ACCESS",
62 labels: { env: "test" },
63 });
64 }),
65 );
66
67 expect(created.name).toContain("/sacRealms/");
68 expect(created.name).toContain("/locations/global/");
69 expect(created.sacRealmId).toEqual(expect.any(String));
70 expect(created.location).toEqual("global");
71 expect(created.securityService).toEqual("PALO_ALTO_PRISMA_ACCESS");
72 expect(created.labels).toMatchObject({ env: "test" });
73
74 const fetched = yield* networksecurity.getProjectsLocationsSacRealms({
75 name: created.name,
76 });
77 expect(fetched.name).toEqual(created.name);
78 expect(fetched.securityService).toEqual("PALO_ALTO_PRISMA_ACCESS");
79 expect(fetched.labels?.env).toEqual("test");
80 expect(
81 Object.keys(fetched.labels ?? {}).some((key) =>
82 key.startsWith("alchemy-"),
83 ),
84 ).toEqual(true);
85
86 yield* stack.destroy();
87
88 const gone = yield* waitUntilGone(created.name);
89 expect(gone).toEqual("gone");
90 }).pipe(logLevel),
91 {
92 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
93 timeout: 90_000,
94 },
95);