1import * as GCP from "@/GCP";
2import * as Effect from "effect/Effect";
3import { serveProbes } from "../../bindingHost.ts";
4
5/** PSC-enabled cluster (no VPC network slot needed). */
6export const Db = GCP.AlloyDB.Cluster("Db", {
7 location: "us-central1",
8 pscConfig: { pscEnabled: true },
9 initialUser: { user: "postgres", password: "AlchemyTest1" },
10 automatedBackupPolicy: { enabled: false },
11 continuousBackupConfig: { enabled: false },
12});
13
14export const Primary = Effect.gen(function* () {
15 const cluster = yield* Db;
16 return yield* GCP.AlloyDB.Instance("Primary", {
17 cluster: cluster.name,
18 instanceType: "PRIMARY",
19 machineConfig: { cpuCount: 2 },
20 });
21});
22
23export const Snapshot = Effect.gen(function* () {
24 // Depend on the primary: a backup needs a running instance to snapshot.
25 const instance = yield* Primary;
26 return yield* GCP.AlloyDB.Backup("Snapshot", {
27 clusterName: instance.clusterName,
28 });
29});
30
31export const AppUser = Effect.gen(function* () {
32 const instance = yield* Primary;
33 return yield* GCP.AlloyDB.ClustersUser("AppUser", {
34 cluster: instance.clusterName,
35 password: "AlchemyUser1",
36 databaseRoles: ["alloydbsuperuser"],
37 });
38});
39
40/**
41 * Effect-native Cloud Run service exercising every AlloyDB binding as its
42 * own runtime service account. Deployed from {@link ../Bindings.test.ts}.
43 */
44export default class AlloyDbBindingsHost extends GCP.Function<AlloyDbBindingsHost>()(
45 "AlloyDbBindingsHost",
46 { main: import.meta.url, invokerIamDisabled: true },
47 Effect.gen(function* () {
48 const getCluster = yield* GCP.AlloyDB.GetCluster(Db);
49 const getInstance = yield* GCP.AlloyDB.GetInstance(Primary);
50 const getConnectionInfo = yield* GCP.AlloyDB.GetConnectionInfo(Primary);
51 const getBackup = yield* GCP.AlloyDB.GetBackup(Snapshot);
52 const getUser = yield* GCP.AlloyDB.GetUser(AppUser);
53
54 return {
55 fetch: serveProbes({
56 getCluster: getCluster(),
57 getInstance: getInstance(),
58 getConnectionInfo: getConnectionInfo(),
59 getBackup: getBackup(),
60 getUser: getUser(),
61 }),
62 };
63 }).pipe(
64 Effect.provide(GCP.AlloyDB.GetClusterHttp),
65 Effect.provide(GCP.AlloyDB.GetInstanceHttp),
66 Effect.provide(GCP.AlloyDB.GetConnectionInfoHttp),
67 Effect.provide(GCP.AlloyDB.GetBackupHttp),
68 Effect.provide(GCP.AlloyDB.GetUserHttp),
69 ),
70) {}