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
18const waitUntilGone = (name: string) =>
19 dataplex.getProjectsLocationsDataProducts({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29test.provider(
30 "getProjectsLocationsDataProducts on a missing product fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 dataplex.getProjectsLocationsDataProducts({
38 name: `projects/${project}/locations/us-central1/dataProducts/alchemy-missing-product`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel, withDataplexSlot),
45 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
46);
47
48test.provider.skipIf(!!process.env.FAST)(
49 "create, update, and delete a data product",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 return yield* GCP.Dataplex.DataProduct("Sales", {
57 location: "us-central1",
58 displayName: "Sales mart",
59 description: "product a",
60 ownerEmails: ["owner@example.com"],
61 labels: { env: "test" },
62 });
63 }),
64 );
65
66 expect(created.name).toContain("/dataProducts/");
67 expect(created.dataProductId).toEqual(expect.any(String));
68 expect(created.location).toEqual("us-central1");
69 expect(created.displayName).toEqual("Sales mart");
70 expect(created.description).toEqual("product a");
71 expect(created.ownerEmails).toContain("owner@example.com");
72 expect(created.labels).toMatchObject({ env: "test" });
73
74 const fetched = yield* dataplex.getProjectsLocationsDataProducts({
75 name: created.name,
76 });
77 expect(fetched.name).toEqual(created.name);
78 expect(fetched.labels?.env).toEqual("test");
79 expect(
80 Object.keys(fetched.labels ?? {}).some((key) =>
81 key.startsWith("alchemy-"),
82 ),
83 ).toEqual(true);
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Dataplex.DataProduct("Sales", {
88 dataProductId: created.dataProductId,
89 location: "us-central1",
90 displayName: "Sales prod",
91 description: "product b",
92 ownerEmails: ["owner@example.com", "steward@example.com"],
93 labels: { env: "prod", team: "data" },
94 });
95 }),
96 );
97
98 expect(updated.name).toEqual(created.name);
99 expect(updated.displayName).toEqual("Sales prod");
100 expect(updated.description).toEqual("product b");
101 expect(updated.labels).toMatchObject({ env: "prod", team: "data" });
102
103 yield* stack.destroy();
104 const gone = yield* waitUntilGone(created.name);
105 expect(gone).toEqual("gone");
106 }).pipe(logLevel, withDataplexSlot),
107 { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 },
108);