1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as dataplex from "@distilled.cloud/gcp/dataplex_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { withDataplexSlot } from "./quota.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Data domains are allowlisted: the testing project gets Forbidden
19// "Permission 'dataplex.dataDomains.get' denied". Set
20// GCP_TEST_DATAPLEX_DATA_DOMAIN=1 on an allowlisted project.
21const runLifecycle = process.env.GCP_TEST_DATAPLEX_DATA_DOMAIN === "1";
22
23const contactsA: dataplex.GoogleCloudDataplexV1Contacts = {
24 identities: [
25 {
26 contactName: "steward",
27 contactRole: "steward",
28 contactId: "steward@example.com",
29 },
30 ],
31};
32
33const contactsB: dataplex.GoogleCloudDataplexV1Contacts = {
34 identities: [
35 {
36 contactName: "owner",
37 contactRole: "owner",
38 contactId: "owner@example.com",
39 },
40 ],
41};
42
43const waitUntilGone = (name: string) =>
44 dataplex.getProjectsLocationsDataDomains({ name }).pipe(
45 Effect.as("found" as const),
46 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
47 Effect.repeat({
48 schedule: Schedule.spaced("1 second"),
49 until: (status) => status === "gone",
50 times: 10,
51 }),
52 );
53
54test.provider(
55 "getProjectsLocationsDataDomains on a missing domain fails with a typed tag",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const error = yield* Effect.flip(
62 dataplex.getProjectsLocationsDataDomains({
63 name: `projects/${project}/locations/us-central1/dataDomains/alchemy-missing-domain`,
64 }),
65 );
66 expect(error._tag).toEqual(runLifecycle ? "NotFound" : "Forbidden");
67
68 yield* stack.destroy();
69 }).pipe(logLevel, withDataplexSlot),
70 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
71);
72
73test.provider.skipIf(!runLifecycle)(
74 "create, update, and delete a data domain",
75 (stack) =>
76 Effect.gen(function* () {
77 yield* stack.destroy();
78
79 const created = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* GCP.Dataplex.DataDomain("Finance", {
82 location: "us-central1",
83 displayName: "Finance",
84 description: "domain a",
85 labels: { env: "test" },
86 contacts: contactsA,
87 });
88 }),
89 );
90
91 expect(created.name).toContain("/dataDomains/");
92 expect(created.dataDomainId).toEqual(expect.any(String));
93 expect(created.location).toEqual("us-central1");
94 expect(created.displayName).toEqual("Finance");
95 expect(created.description).toEqual("domain a");
96 expect(created.labels).toMatchObject({ env: "test" });
97
98 const fetched = yield* dataplex.getProjectsLocationsDataDomains({
99 name: created.name,
100 });
101 expect(fetched.name).toEqual(created.name);
102 expect(fetched.labels?.env).toEqual("test");
103 expect(
104 Object.keys(fetched.labels ?? {}).some((key) =>
105 key.startsWith("alchemy-"),
106 ),
107 ).toEqual(true);
108
109 const updated = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* GCP.Dataplex.DataDomain("Finance", {
112 dataDomainId: created.dataDomainId,
113 location: "us-central1",
114 displayName: "Finance prod",
115 description: "domain b",
116 labels: { env: "prod", team: "data" },
117 contacts: contactsB,
118 });
119 }),
120 );
121
122 expect(updated.name).toEqual(created.name);
123 expect(updated.displayName).toEqual("Finance prod");
124 expect(updated.description).toEqual("domain b");
125 expect(updated.labels).toMatchObject({ env: "prod", team: "data" });
126
127 yield* stack.destroy();
128 const gone = yield* waitUntilGone(created.name);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel, withDataplexSlot),
131 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
132);