1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as dlp from "@distilled.cloud/gcp/dlp_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12// Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
13// administer the organization (the testing service account does not).
14const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(
15 /^organizations\//,
16 "",
17);
18const organization = `organizations/${organizationId}`;
19
20const logLevel = Effect.provideService(
21 MinimumLogLevel,
22 process.env.DEBUG ? "Debug" : "Info",
23);
24
25const location = "us-central1";
26
27const waitUntilGone = (name: string) =>
28 dlp.getOrganizationsLocationsConnections({ name }).pipe(
29 Effect.as("found" as const),
30 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("1 second"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38test.provider.skipIf(!organizationId)(
39 "getOrganizationsLocationsConnections on a missing connection fails with a typed tag",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 dlp.getOrganizationsLocationsConnections({
46 name: `${organization}/locations/${location}/connections/alchemy-missing`,
47 }),
48 );
49 expect(error._tag).toEqual("NotFound");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
54);
55
56test.provider.skipIf(!organizationId || !!process.env.FAST)(
57 "create, update, and delete an organization DLP connection",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61 yield* stack.destroy();
62
63 const cloudSql = {
64 connectionName: `${project}:${location}:alchemy-dlp-sql`,
65 databaseEngine: "DATABASE_ENGINE_POSTGRES" as const,
66 maxConnections: 2,
67 cloudSqlIam: {},
68 };
69
70 const created = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* GCP.DLP.OrganizationsLocationsConnection("Warehouse", {
73 organization,
74 location,
75 state: "MISSING_CREDENTIALS",
76 cloudSql,
77 });
78 }),
79 );
80
81 expect(created.location).toEqual(location);
82 expect(created.name).toContain("/connections/");
83 expect(created.cloudSql?.connectionName).toEqual(cloudSql.connectionName);
84
85 const fetched = yield* dlp.getOrganizationsLocationsConnections({
86 name: created.name,
87 });
88 expect(fetched.name).toEqual(created.name);
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.DLP.OrganizationsLocationsConnection("Warehouse", {
93 organization,
94 location,
95 state: "MISSING_CREDENTIALS",
96 cloudSql: { ...cloudSql, maxConnections: 3 },
97 });
98 }),
99 );
100
101 expect(updated.name).toEqual(created.name);
102 expect(updated.cloudSql?.maxConnections).toEqual(3);
103
104 yield* stack.destroy();
105
106 const gone = yield* waitUntilGone(created.name);
107 expect(gone).toEqual("gone");
108 }).pipe(logLevel),
109 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
110);