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 waitUntilGone = (name: string) =>
24 dataplex.getProjectsLocationsDataDomainsBindings({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider(
35 "getProjectsLocationsDataDomainsBindings on a missing binding fails with a typed tag",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 dataplex.getProjectsLocationsDataDomainsBindings({
43 name: `projects/${project}/locations/us-central1/dataDomains/alchemy-missing/bindings/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual(runLifecycle ? "NotFound" : "Forbidden");
47
48 yield* stack.destroy();
49 }).pipe(logLevel, withDataplexSlot),
50 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
51);
52
53test.provider.skipIf(!runLifecycle)(
54 "create and delete a data domain binding",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const domain = yield* GCP.Dataplex.DataDomain("Finance", {
63 location: "us-central1",
64 displayName: "Finance",
65 labels: { env: "test" },
66 contacts: {
67 identities: [
68 {
69 contactName: "steward",
70 contactRole: "steward",
71 contactId: "steward@example.com",
72 },
73 ],
74 },
75 });
76 return yield* GCP.Dataplex.DataDomainsBinding("Project", {
77 parent: domain.name,
78 resource: `//cloudresourcemanager.googleapis.com/projects/${project}`,
79 });
80 }),
81 );
82
83 expect(created.name).toContain("/bindings/");
84 expect(created.dataDomainBindingId).toEqual(expect.any(String));
85 expect(created.resource).toContain(`projects/${project}`);
86
87 const fetched = yield* dataplex.getProjectsLocationsDataDomainsBindings({
88 name: created.name,
89 });
90 expect(fetched.name).toEqual(created.name);
91 expect(fetched.resource).toEqual(created.resource);
92
93 yield* stack.destroy();
94 const gone = yield* waitUntilGone(created.name);
95 expect(gone).toEqual("gone");
96 }).pipe(logLevel, withDataplexSlot),
97 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
98);