1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as certificatemanager from "@distilled.cloud/gcp/certificatemanager_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 certificatemanager.getProjectsLocationsCertificateMaps({ 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 a certificate map",
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.CertificateManager.CertificateMap("FrontendMap", {
36 location: "global",
37 description: "frontend map a",
38 labels: { env: "test" },
39 });
40 }),
41 );
42
43 expect(created.name).toContain("/certificateMaps/");
44 expect(created.certificateMapId).toEqual(expect.any(String));
45 expect(created.location).toEqual("global");
46 expect(created.description).toEqual("frontend map a");
47 expect(created.labels).toMatchObject({ env: "test" });
48 expect(created.gclbTargets).toEqual([]);
49 expect(created.createTime).toEqual(expect.any(String));
50
51 const fetched =
52 yield* certificatemanager.getProjectsLocationsCertificateMaps({
53 name: created.name,
54 });
55 expect(fetched.name).toEqual(created.name);
56 expect(fetched.labels?.env).toEqual("test");
57 expect(fetched.description).toEqual("frontend map a");
58 expect(
59 Object.keys(fetched.labels ?? {}).some((key) =>
60 key.startsWith("alchemy-"),
61 ),
62 ).toEqual(true);
63
64 const updated = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.CertificateManager.CertificateMap("FrontendMap", {
67 certificateMapId: created.certificateMapId,
68 location: "global",
69 description: "frontend map b",
70 labels: { env: "prod", role: "tls" },
71 });
72 }),
73 );
74
75 expect(updated.name).toEqual(created.name);
76 expect(updated.description).toEqual("frontend map b");
77 expect(updated.labels).toMatchObject({ env: "prod", role: "tls" });
78
79 const refetched =
80 yield* certificatemanager.getProjectsLocationsCertificateMaps({
81 name: created.name,
82 });
83 expect(refetched.description).toEqual("frontend map b");
84 expect(refetched.labels?.env).toEqual("prod");
85 expect(refetched.labels?.role).toEqual("tls");
86
87 yield* stack.destroy();
88
89 const gone = yield* waitUntilGone(created.name);
90 expect(gone).toEqual("gone");
91 }).pipe(logLevel),
92 {
93 tags: ["provider:gcp", "provider:gcp:certificatemanager", "live"],
94 timeout: 90_000,
95 },
96);