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
18 .getProjectsLocationsBackendAuthenticationConfigs({ name })
19 .pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29test.provider(
30 "create, update, and delete a backend authentication config",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.NetworkSecurity.BackendAuthenticationConfig(
38 "BackendTls",
39 {
40 wellKnownRoots: "PUBLIC_ROOTS",
41 description: "backend auth a",
42 labels: { env: "test" },
43 },
44 );
45 }),
46 );
47
48 expect(created.name).toContain("/backendAuthenticationConfigs/");
49 expect(created.name).toContain("/locations/global/");
50 expect(created.backendAuthenticationConfigId).toEqual(expect.any(String));
51 expect(created.location).toEqual("global");
52 expect(created.wellKnownRoots).toEqual("PUBLIC_ROOTS");
53 expect(created.description).toEqual("backend auth a");
54 expect(created.labels).toMatchObject({ env: "test" });
55 expect(created.createTime).toEqual(expect.any(String));
56
57 const fetched =
58 yield* networksecurity.getProjectsLocationsBackendAuthenticationConfigs(
59 { name: created.name },
60 );
61 expect(fetched.name).toEqual(created.name);
62 expect(fetched.labels?.env).toEqual("test");
63 expect(fetched.wellKnownRoots).toEqual("PUBLIC_ROOTS");
64 expect(
65 Object.keys(fetched.labels ?? {}).some((key) =>
66 key.startsWith("alchemy-"),
67 ),
68 ).toEqual(true);
69
70 const updated = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* GCP.NetworkSecurity.BackendAuthenticationConfig(
73 "BackendTls",
74 {
75 backendAuthenticationConfigId:
76 created.backendAuthenticationConfigId,
77 wellKnownRoots: "PUBLIC_ROOTS",
78 description: "backend auth b",
79 labels: { env: "prod", role: "backend-tls" },
80 },
81 );
82 }),
83 );
84
85 expect(updated.name).toEqual(created.name);
86 expect(updated.description).toEqual("backend auth b");
87 expect(updated.labels).toMatchObject({
88 env: "prod",
89 role: "backend-tls",
90 });
91
92 const refetched =
93 yield* networksecurity.getProjectsLocationsBackendAuthenticationConfigs(
94 { name: created.name },
95 );
96 expect(refetched.description).toEqual("backend auth b");
97 expect(refetched.labels?.env).toEqual("prod");
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);