1import { GcpEnvironment } from "@/GCP/Environment";
2import { Action } from "@/Action";
3import * as GCP from "@/GCP";
4import * as Test from "@/Test/Alchemy";
5import * as alloydb from "@distilled.cloud/gcp/alloydb_v1";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// AlloyDB clusters and instances take well over 5 minutes to provision.
19const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
20
21const waitUntilGone = (name: string) =>
22 alloydb.getProjectsLocationsClustersUsers({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsLocationsClustersUsers on a missing user fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 alloydb.getProjectsLocationsClustersUsers({
42 name: `projects/${project}/locations/us-central1/clusters/alchemy-alloydb-missing/users/alchemy_alloydb_missing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 { tags: ["provider:gcp", "provider:gcp:alloydb", "live"], timeout: 90_000 },
50);
51
52test.provider.skipIf(!runLifecycle)(
53 "create, update, and delete an alloydb cluster user",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
61 location: "us-central1",
62 pscConfig: { pscEnabled: true },
63 displayName: "alchemy-test-user-cluster",
64 labels: { env: "test" },
65 initialUser: { user: "postgres", password: "AlchemyTest1" },
66 automatedBackupPolicy: { enabled: false },
67 continuousBackupConfig: { enabled: false },
68 });
69 const instance = yield* GCP.AlloyDB.Instance("Primary", {
70 cluster: cluster.name,
71 instanceType: "PRIMARY",
72 machineConfig: { cpuCount: 2 },
73 });
74 const user = yield* GCP.AlloyDB.ClustersUser("AppUser", {
75 cluster: instance.clusterName,
76 password: "AlchemyUser1",
77 databaseRoles: ["alloydbsuperuser"],
78 });
79 const Probe = Action(
80 "Probe",
81 Effect.gen(function* () {
82 yield* instance.name;
83 const getUser = yield* GCP.AlloyDB.GetUser(user);
84 return Effect.fn(function* () {
85 return yield* getUser();
86 });
87 }),
88 );
89 return { cluster, instance, user, probe: yield* Probe({}) };
90 }),
91 );
92
93 expect(created.user.name).toContain("/users/");
94 expect(created.user.clusterId).toEqual(created.cluster.clusterId);
95 expect(created.user.location).toEqual("us-central1");
96 expect(created.user.userType).toEqual("ALLOYDB_BUILT_IN");
97 expect(created.user.databaseRoles).toContain("alloydbsuperuser");
98 expect(created.probe.name).toEqual(created.user.name);
99
100 const fetched = yield* alloydb.getProjectsLocationsClustersUsers({
101 name: created.user.name,
102 });
103 expect(fetched.name).toEqual(created.user.name);
104 expect(fetched.userType).toEqual("ALLOYDB_BUILT_IN");
105 expect(fetched.databaseRoles ?? []).toContain("alloydbsuperuser");
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
110 clusterId: created.cluster.clusterId,
111 location: "us-central1",
112 pscConfig: { pscEnabled: true },
113 displayName: "alchemy-test-user-cluster",
114 labels: { env: "test" },
115 automatedBackupPolicy: { enabled: false },
116 continuousBackupConfig: { enabled: false },
117 });
118 const instance = yield* GCP.AlloyDB.Instance("Primary", {
119 cluster: cluster.name,
120 instanceId: created.instance.instanceId,
121 instanceType: "PRIMARY",
122 machineConfig: { cpuCount: 2 },
123 });
124 const user = yield* GCP.AlloyDB.ClustersUser("AppUser", {
125 cluster: instance.clusterName,
126 userId: created.user.userId,
127 password: "AlchemyUser2",
128 databaseRoles: ["alloydbsuperuser", "pg_monitor"],
129 });
130 return { cluster, instance, user };
131 }),
132 );
133
134 expect(updated.user.name).toEqual(created.user.name);
135 expect(updated.user.databaseRoles).toEqual(
136 expect.arrayContaining(["alloydbsuperuser", "pg_monitor"]),
137 );
138
139 const refetched = yield* alloydb.getProjectsLocationsClustersUsers({
140 name: created.user.name,
141 });
142 expect(refetched.databaseRoles ?? []).toEqual(
143 expect.arrayContaining(["alloydbsuperuser", "pg_monitor"]),
144 );
145
146 yield* stack.destroy();
147
148 const gone = yield* waitUntilGone(created.user.name);
149 expect(gone).toEqual("gone");
150 }).pipe(logLevel),
151 {
152 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
153 timeout: 3_600_000,
154 },
155);