The best way to control Claude Code, Codex and more from any computer or phone

main
ts 348 lines 11.6 KB
Raw
1 import * as dataplex from "@distilled.cloud/gcp/dataplex_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 DataplexNotResolved,
14 collectPages,
15 fingerprint,
16 hasAlchemyLabelMap,
17 listAtLocation,
18 normalizeLocation,
19 parentOf,
20 parseName,
21 replaceOnIdentity,
22 retryQuota,
23 toPhysicalId,
24 userLabels,
25 waitForOperation,
26 waitUntilExists,
27 waitUntilGone,
28 } from "./internal.ts";
29
30 export type DataProductAccessGroup = dataplex.GoogleCloudDataplexV1DataProductAccessGroup;
31
32 export type DataProductAccessApprovalConfig =
33 dataplex.GoogleCloudDataplexV1DataProductAccessApprovalConfig;
34
35 export type DataProductProps = {
36 /**
37 * Data product id. If omitted, a unique name is generated. Must match
38 * `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`. Immutable — changing it
39 * replaces the product.
40 */
41 dataProductId?: string;
42 /**
43 * Region (`us-central1`, …). Immutable — changing it replaces the
44 * product.
45 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
46 */
47 location?: string;
48 /**
49 * User-friendly display name. Required.
50 */
51 displayName: string;
52 /**
53 * Description of the data product.
54 */
55 description?: string;
56 /**
57 * User labels. Alchemy ownership labels are merged in automatically.
58 */
59 labels?: Record<string, string>;
60 /**
61 * Emails of the data product owners. Required.
62 */
63 ownerEmails: string[];
64 /**
65 * Base64-encoded icon (max 3 MiB).
66 */
67 icon?: string;
68 /**
69 * Access groups keyed by group id.
70 */
71 accessGroups?: Record<string, DataProductAccessGroup>;
72 /**
73 * Access-approval configuration.
74 */
75 accessApprovalConfig?: DataProductAccessApprovalConfig;
76 };
77
78 export type DataProduct = Resource<
79 "GCP.Dataplex.DataProduct",
80 DataProductProps,
81 {
82 /** Full resource name. */
83 name: string;
84 /** Data product id (last path segment). */
85 dataProductId: string;
86 /** Project id. */
87 project: string;
88 /** Location id. */
89 location: string;
90 /** Display name. */
91 displayName: string | undefined;
92 /** Description. */
93 description: string | undefined;
94 /** User labels (Alchemy ownership labels stripped). */
95 labels: Record<string, string>;
96 /** Owner emails. */
97 ownerEmails: string[];
98 /** Icon payload. */
99 icon: string | undefined;
100 /** Access groups. */
101 accessGroups: Record<string, DataProductAccessGroup | undefined> | undefined;
102 /** Access-approval configuration. */
103 accessApprovalConfig: DataProductAccessApprovalConfig | undefined;
104 /** Number of attached data assets. */
105 assetCount: number | undefined;
106 /** Server etag. */
107 etag: string | undefined;
108 /** System uid. */
109 uid: string | undefined;
110 /** RFC3339 creation timestamp. */
111 createTime: string | undefined;
112 /** RFC3339 last-update timestamp. */
113 updateTime: string | undefined;
114 },
115 never,
116 Providers
117 >;
118
119 /**
120 * A Dataplex Data Product — a curated collection of data assets packaged
121 * for a use case.
122 *
123 * Location and product id are immutable. Display name, description,
124 * labels, owners, icon, access groups, and approval config update in
125 * place.
126 *
127 * ### Creating a Data Product
128 * **Example:** Product with an owner
129 * ```typescript
130 * const product = yield* GCP.Dataplex.DataProduct("Sales", {
131 * displayName: "Sales mart",
132 * ownerEmails: ["owner@example.com"],
133 * labels: { env: "test" },
134 * });
135 * ```
136 *
137 * @resource
138 * @category Dataplex
139 */
140 export const DataProduct = Resource<DataProduct>("GCP.Dataplex.DataProduct");
141
142 const resourceName = (project: string, location: string, dataProductId: string) =>
143 `projects/${project}/locations/${location}/dataProducts/${dataProductId}`;
144
145 const toAttrs = (product: dataplex.GoogleCloudDataplexV1DataProduct, project: string) => {
146 const name = product.name ?? "";
147 const parsed = parseName(name, "dataProducts");
148 return {
149 name,
150 dataProductId: parsed.id,
151 project: parsed.project || project,
152 location: parsed.location,
153 displayName: product.displayName,
154 description: product.description,
155 labels: userLabels(product.labels),
156 ownerEmails: [...(product.ownerEmails ?? [])],
157 icon: product.icon,
158 accessGroups: product.accessGroups,
159 accessApprovalConfig: product.accessApprovalConfig,
160 assetCount: product.assetCount,
161 etag: product.etag,
162 uid: product.uid,
163 createTime: product.createTime,
164 updateTime: product.updateTime,
165 };
166 };
167
168 const getByName = (name: string) =>
169 retryQuota(dataplex.getProjectsLocationsDataProducts({ name })).pipe(
170 Effect.catchTag("NotFound", () => Effect.undefined),
171 );
172
173 const listProducts = (project: string, region: string) => {
174 const collect = (parent: string) =>
175 collectPages(
176 dataplex.listProjectsLocationsDataProducts.pages({
177 parent,
178 pageSize: 1000,
179 }),
180 (page) => page.dataProducts,
181 ).pipe(
182 Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels))),
183 Effect.catchTag("NotFound", () => Effect.succeed([])),
184 );
185 return listAtLocation(project, region, collect);
186 };
187
188 export const listAlchemyDataProducts = (project: string, region: string) =>
189 listProducts(project, region);
190
191 export const DataProductProvider = () =>
192 Provider.succeed(DataProduct, {
193 stables: ["name", "dataProductId", "project", "location", "uid", "createTime"],
194
195 diff: Effect.fn(function* ({ news, olds, output }) {
196 if (!isResolved(news)) return undefined;
197 const env = yield* GcpEnvironment.current;
198 return replaceOnIdentity({
199 previousId: olds?.dataProductId ?? output?.dataProductId,
200 nextId: news.dataProductId ?? olds?.dataProductId ?? output?.dataProductId,
201 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
202 nextLocation: normalizeLocation(
203 news.location ?? olds?.location ?? output?.location,
204 env.region,
205 ),
206 });
207 }),
208
209 read: Effect.fn(function* ({ id, olds, output }) {
210 const env = yield* GcpEnvironment.current;
211 const dataProductId = yield* toPhysicalId(
212 id,
213 olds?.dataProductId,
214 output?.dataProductId,
215 "dataproduct",
216 );
217 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
218 const name = output?.name ?? resourceName(env.project, location, dataProductId);
219 const existing = yield* getByName(name);
220 if (existing === undefined) return undefined;
221 const attrs = toAttrs(existing, env.project);
222 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
223 }),
224
225 list: () =>
226 Effect.gen(function* () {
227 const env = yield* GcpEnvironment.current;
228 const items = yield* listProducts(env.project, env.region);
229 return items.map((item) => toAttrs(item, env.project));
230 }),
231
232 reconcile: Effect.fn(function* ({ id, news, output }) {
233 const env = yield* GcpEnvironment.current;
234 const dataProductId = yield* toPhysicalId(
235 id,
236 news.dataProductId,
237 output?.dataProductId,
238 "dataproduct",
239 );
240 const location = normalizeLocation(news.location ?? output?.location, env.region);
241 const name = resourceName(env.project, location, dataProductId);
242 const desiredLabels = {
243 ...toLabels(news.labels),
244 ...(yield* createInternalLabels(id)),
245 };
246
247 let current = yield* getByName(output?.name ?? name);
248
249 if (current === undefined) {
250 const created = yield* retryQuota(
251 dataplex.createProjectsLocationsDataProducts({
252 parent: parentOf(env.project, location),
253 dataProductId,
254 body: {
255 displayName: news.displayName,
256 description: news.description,
257 labels: desiredLabels,
258 ownerEmails: news.ownerEmails,
259 icon: news.icon,
260 accessGroups: news.accessGroups,
261 accessApprovalConfig: news.accessApprovalConfig,
262 },
263 }),
264 ).pipe(Effect.catchTag("Conflict", () => Effect.undefined));
265 if (created !== undefined) {
266 yield* waitForOperation(created);
267 }
268 current = yield* waitUntilExists(getByName(name), name);
269 }
270
271 if (current === undefined) {
272 return yield* new DataplexNotResolved({ resourceName: name });
273 }
274
275 const observedLabels = tagRecord(current.labels);
276 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
277 const labelsChanged = upsert.length > 0 || removed.length > 0;
278 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
279 const displayNameChanged = (current.displayName ?? "") !== news.displayName;
280 const ownersChanged =
281 fingerprint([...(current.ownerEmails ?? [])].sort()) !==
282 fingerprint([...(news.ownerEmails ?? [])].sort());
283 const iconChanged = (current.icon ?? "") !== (news.icon ?? "");
284 const groupsChanged = fingerprint(current.accessGroups) !== fingerprint(news.accessGroups);
285 const approvalChanged =
286 fingerprint(current.accessApprovalConfig) !== fingerprint(news.accessApprovalConfig);
287
288 if (
289 labelsChanged ||
290 descriptionChanged ||
291 displayNameChanged ||
292 ownersChanged ||
293 iconChanged ||
294 groupsChanged ||
295 approvalChanged
296 ) {
297 const operation = yield* dataplex.patchProjectsLocationsDataProducts({
298 name: current.name ?? name,
299 updateMask: [
300 labelsChanged ? "labels" : undefined,
301 descriptionChanged ? "description" : undefined,
302 displayNameChanged ? "displayName" : undefined,
303 ownersChanged ? "ownerEmails" : undefined,
304 iconChanged ? "icon" : undefined,
305 groupsChanged ? "accessGroups" : undefined,
306 approvalChanged ? "accessApprovalConfig" : undefined,
307 ]
308 .filter((field): field is string => field !== undefined)
309 .join(","),
310 body: {
311 name: current.name ?? name,
312 etag: current.etag,
313 displayName: news.displayName,
314 description: news.description,
315 labels: desiredLabels,
316 ownerEmails: news.ownerEmails,
317 icon: news.icon,
318 accessGroups: news.accessGroups,
319 accessApprovalConfig: news.accessApprovalConfig,
320 },
321 });
322 yield* waitForOperation(operation);
323 current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name);
324 }
325
326 return toAttrs(current, env.project);
327 }),
328
329 delete: Effect.fn(function* ({ output }) {
330 const operation = yield* dataplex
331 .deleteProjectsLocationsDataProducts({
332 name: output.name,
333 etag: output.etag,
334 })
335 .pipe(
336 Effect.retry({
337 while: (error) => error._tag === "Conflict",
338 times: 8,
339 schedule: Schedule.spaced("2 seconds"),
340 }),
341 Effect.catchTag("NotFound", () => Effect.undefined),
342 );
343 if (operation !== undefined) {
344 yield* waitForOperation(operation, { notFoundOk: true });
345 }
346 yield* waitUntilGone(getByName(output.name), output.name);
347 }),
348 });