1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as parametermanager from "@distilled.cloud/gcp/parametermanager_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 parametermanager.getProjectsLocationsParameters({ 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 parameter",
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.ParameterManager.Parameter("AppConfig", {
36 labels: { env: "test" },
37 });
38 }),
39 );
40
41 expect(created.name).toContain("/parameters/");
42 expect(created.parameterId).toEqual(expect.any(String));
43 expect(created.location).toEqual("global");
44 expect(created.format).toEqual("UNFORMATTED");
45 expect(created.labels).toMatchObject({ env: "test" });
46
47 const fetched = yield* parametermanager.getProjectsLocationsParameters({
48 name: created.name,
49 });
50 expect(fetched.name).toEqual(created.name);
51 expect(fetched.labels?.env).toEqual("test");
52 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
53 expect(
54 fetched.format === undefined || fetched.format === "UNFORMATTED",
55 ).toBe(true);
56
57 const updated = yield* stack.deploy(
58 Effect.gen(function* () {
59 return yield* GCP.ParameterManager.Parameter("AppConfig", {
60 parameterId: created.parameterId,
61 location: created.location,
62 labels: { env: "prod", role: "config" },
63 });
64 }),
65 );
66
67 expect(updated.name).toEqual(created.name);
68 expect(updated.labels).toMatchObject({ env: "prod", role: "config" });
69
70 const fetchedUpdate =
71 yield* parametermanager.getProjectsLocationsParameters({
72 name: created.name,
73 });
74 expect(fetchedUpdate.labels?.env).toEqual("prod");
75 expect(fetchedUpdate.labels?.role).toEqual("config");
76
77 yield* stack.destroy();
78
79 const gone = yield* waitUntilGone(created.name);
80 expect(gone).toEqual("gone");
81 }).pipe(logLevel),
82 {
83 tags: ["provider:gcp", "provider:gcp:parametermanager", "live"],
84 timeout: 90_000,
85 },
86);
87
88test.provider(
89 "replace a parameter when format changes",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const created = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.ParameterManager.Parameter("TypedConfig", {
97 format: "UNFORMATTED",
98 });
99 }),
100 );
101
102 expect(created.format).toEqual("UNFORMATTED");
103
104 const replaced = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* GCP.ParameterManager.Parameter("TypedConfig", {
107 parameterId: created.parameterId,
108 location: created.location,
109 format: "JSON",
110 });
111 }),
112 );
113
114 expect(replaced.name).toEqual(created.name);
115 expect(replaced.parameterId).toEqual(created.parameterId);
116 expect(replaced.format).toEqual("JSON");
117
118 const fetched = yield* parametermanager.getProjectsLocationsParameters({
119 name: replaced.name,
120 });
121 expect(fetched.format).toEqual("JSON");
122
123 yield* stack.destroy();
124
125 const gone = yield* waitUntilGone(replaced.name);
126 expect(gone).toEqual("gone");
127 }).pipe(logLevel),
128 {
129 tags: ["provider:gcp", "provider:gcp:parametermanager", "live"],
130 timeout: 90_000,
131 },
132);