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.getOrganizationsLocationsDiscoveryConfigs({ 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 "getOrganizationsLocationsDiscoveryConfigs on a missing config fails with a typed tag",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 dlp.getOrganizationsLocationsDiscoveryConfigs({
46 name: `${organization}/locations/${location}/discoveryConfigs/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 discovery config",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61 yield* stack.destroy();
62
63 const orgId = organization.replace(/^organizations\//, "") || "0";
64 const targets = [
65 {
66 bigQueryTarget: {
67 filter: { otherTables: {} },
68 disabled: {},
69 },
70 },
71 ];
72
73 const parent = `${organization}/locations/${location}`;
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* GCP.DLP.OrganizationsLocationsDiscoveryConfig(
78 "OrgProfiles",
79 {
80 organization,
81 location,
82 displayName: "org-profiles",
83 status: "PAUSED",
84 orgConfig: {
85 projectId: project,
86 location: { organizationId: orgId },
87 },
88 targets,
89 },
90 );
91 }),
92 );
93
94 expect(created.location).toEqual(location);
95 expect(created.status).toEqual("PAUSED");
96 expect(created.name).toEqual(
97 `${parent}/discoveryConfigs/${created.configId}`,
98 );
99
100 const fetched = yield* dlp.getOrganizationsLocationsDiscoveryConfigs({
101 name: created.name,
102 });
103 expect(fetched.displayName).toContain("alchemy-id=");
104
105 const updated = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.DLP.OrganizationsLocationsDiscoveryConfig(
108 "OrgProfiles",
109 {
110 organization,
111 location,
112 configId: created.configId,
113 displayName: "org-profiles-v2",
114 status: "PAUSED",
115 orgConfig: {
116 projectId: project,
117 location: { organizationId: orgId },
118 },
119 targets,
120 },
121 );
122 }),
123 );
124
125 expect(updated.name).toEqual(created.name);
126 expect(updated.displayName).toEqual("org-profiles-v2");
127
128 yield* stack.destroy();
129
130 const gone = yield* waitUntilGone(created.name);
131 expect(gone).toEqual("gone");
132 }).pipe(logLevel),
133 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
134);