@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 319 lines 10.4 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 DataDomainContact = dataplex.GoogleCloudDataplexV1ContactIdentity;
31 export type DataDomainContacts = dataplex.GoogleCloudDataplexV1Contacts;
32
33 export type DataDomainProps = {
34 /**
35 * DataDomain id. If omitted, a unique name is generated. Must contain
36 * only lowercase letters, numbers and hyphens, start with a letter,
37 * end with a letter or number, and be 1-63 characters. Immutable —
38 * changing it replaces the domain.
39 */
40 dataDomainId?: string;
41 /**
42 * Region (`us-central1`, …). Immutable — changing it replaces the
43 * domain.
44 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
45 */
46 location?: string;
47 /**
48 * User-friendly display name. Required.
49 */
50 displayName: string;
51 /**
52 * User-provided description.
53 */
54 description?: string;
55 /**
56 * Immutable parent DataDomain resource name. Empty for a top-level
57 * domain. Changing it replaces the domain.
58 */
59 parentDataDomain?: string;
60 /**
61 * User labels. Alchemy ownership labels are merged in automatically.
62 */
63 labels?: Record<string, string>;
64 /**
65 * Business contacts for the domain. Required.
66 */
67 contacts: DataDomainContacts;
68 };
69
70 export type DataDomain = Resource<
71 "GCP.Dataplex.DataDomain",
72 DataDomainProps,
73 {
74 /** Full resource name. */
75 name: string;
76 /** DataDomain id (last path segment). */
77 dataDomainId: string;
78 /** Project id. */
79 project: string;
80 /** Location id. */
81 location: string;
82 /** Display name. */
83 displayName: string | undefined;
84 /** Description. */
85 description: string | undefined;
86 /** Parent DataDomain resource name, if nested. */
87 parentDataDomain: string | undefined;
88 /** User labels (Alchemy ownership labels stripped). */
89 labels: Record<string, string>;
90 /** Business contacts. */
91 contacts: DataDomainContacts | undefined;
92 /** System uid. */
93 uid: string | undefined;
94 /** RFC3339 creation timestamp. */
95 createTime: string | undefined;
96 /** RFC3339 last-update timestamp. */
97 updateTime: string | undefined;
98 },
99 never,
100 Providers
101 >;
102
103 /**
104 * A Dataplex DataDomain — a logical grouping of data resources for
105 * governance, discovery, and management.
106 *
107 * Location, domain id, and parent domain are immutable. Display name,
108 * description, contacts, and labels update in place.
109 *
110 * ### Creating a DataDomain
111 * **Example:** Top-level domain
112 * ```typescript
113 * const domain = yield* GCP.Dataplex.DataDomain("Finance", {
114 * displayName: "Finance",
115 * labels: { env: "test" },
116 * contacts: {
117 * identities: [
118 * {
119 * contactName: "steward",
120 * contactRole: "steward",
121 * contactId: "steward@example.com",
122 * },
123 * ],
124 * },
125 * });
126 * ```
127 *
128 * @resource
129 * @category Dataplex
130 */
131 export const DataDomain = Resource<DataDomain>("GCP.Dataplex.DataDomain");
132
133 const resourceName = (project: string, location: string, dataDomainId: string) =>
134 `projects/${project}/locations/${location}/dataDomains/${dataDomainId}`;
135
136 const toAttrs = (domain: dataplex.GoogleCloudDataplexV1DataDomain, project: string) => {
137 const name = domain.name ?? "";
138 const parsed = parseName(name, "dataDomains");
139 return {
140 name,
141 dataDomainId: parsed.id,
142 project: parsed.project || project,
143 location: parsed.location,
144 displayName: domain.displayName,
145 description: domain.description,
146 parentDataDomain: domain.parentDataDomain,
147 labels: userLabels(domain.labels),
148 contacts: domain.contacts,
149 uid: domain.uid,
150 createTime: domain.createTime,
151 updateTime: domain.updateTime,
152 };
153 };
154
155 const getByName = (name: string) =>
156 retryQuota(dataplex.getProjectsLocationsDataDomains({ name })).pipe(
157 Effect.catchTag("NotFound", () => Effect.undefined),
158 );
159
160 const listDomains = (project: string, region: string) => {
161 const collect = (parent: string) =>
162 collectPages(
163 dataplex.listProjectsLocationsDataDomains.pages({
164 parent,
165 pageSize: 100,
166 }),
167 (page) => page.dataDomains,
168 ).pipe(
169 Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels))),
170 Effect.catchTag("NotFound", () => Effect.succeed([])),
171 );
172 return listAtLocation(project, region, collect);
173 };
174
175 export const listAlchemyDataDomains = (project: string, region: string) =>
176 listDomains(project, region);
177
178 export const DataDomainProvider = () =>
179 Provider.succeed(DataDomain, {
180 stables: [
181 "name",
182 "dataDomainId",
183 "project",
184 "location",
185 "parentDataDomain",
186 "uid",
187 "createTime",
188 ],
189
190 diff: Effect.fn(function* ({ news, olds, output }) {
191 if (!isResolved(news)) return undefined;
192 const env = yield* GcpEnvironment.current;
193 return replaceOnIdentity({
194 previousId: olds?.dataDomainId ?? output?.dataDomainId,
195 nextId: news.dataDomainId ?? olds?.dataDomainId ?? output?.dataDomainId,
196 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
197 nextLocation: normalizeLocation(
198 news.location ?? olds?.location ?? output?.location,
199 env.region,
200 ),
201 previousParent: olds?.parentDataDomain ?? output?.parentDataDomain,
202 nextParent: news.parentDataDomain ?? olds?.parentDataDomain,
203 });
204 }),
205
206 read: Effect.fn(function* ({ id, olds, output }) {
207 const env = yield* GcpEnvironment.current;
208 const dataDomainId = yield* toPhysicalId(
209 id,
210 olds?.dataDomainId,
211 output?.dataDomainId,
212 "datadomain",
213 );
214 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
215 const name = output?.name ?? resourceName(env.project, location, dataDomainId);
216 const existing = yield* getByName(name);
217 if (existing === undefined) return undefined;
218 const attrs = toAttrs(existing, env.project);
219 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
220 }),
221
222 list: () =>
223 Effect.gen(function* () {
224 const env = yield* GcpEnvironment.current;
225 const items = yield* listDomains(env.project, env.region);
226 return items.map((item) => toAttrs(item, env.project));
227 }),
228
229 reconcile: Effect.fn(function* ({ id, news, output }) {
230 const env = yield* GcpEnvironment.current;
231 const dataDomainId = yield* toPhysicalId(
232 id,
233 news.dataDomainId,
234 output?.dataDomainId,
235 "datadomain",
236 );
237 const location = normalizeLocation(news.location ?? output?.location, env.region);
238 const name = resourceName(env.project, location, dataDomainId);
239 const desiredLabels = {
240 ...toLabels(news.labels),
241 ...(yield* createInternalLabels(id)),
242 };
243
244 let current = yield* getByName(output?.name ?? name);
245
246 if (current === undefined) {
247 const created = yield* retryQuota(
248 dataplex.createProjectsLocationsDataDomains({
249 parent: parentOf(env.project, location),
250 dataDomainId,
251 body: {
252 displayName: news.displayName,
253 description: news.description,
254 parentDataDomain: news.parentDataDomain,
255 labels: desiredLabels,
256 contacts: news.contacts,
257 },
258 }),
259 ).pipe(Effect.catchTag("Conflict", () => Effect.undefined));
260 if (created !== undefined) {
261 yield* waitForOperation(created);
262 }
263 current = yield* waitUntilExists(getByName(name), name);
264 }
265
266 if (current === undefined) {
267 return yield* new DataplexNotResolved({ resourceName: name });
268 }
269
270 const observedLabels = tagRecord(current.labels);
271 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
272 const labelsChanged = upsert.length > 0 || removed.length > 0;
273 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
274 const displayNameChanged = (current.displayName ?? "") !== news.displayName;
275 const contactsChanged = fingerprint(current.contacts) !== fingerprint(news.contacts);
276
277 if (labelsChanged || descriptionChanged || displayNameChanged || contactsChanged) {
278 const operation = yield* dataplex.patchProjectsLocationsDataDomains({
279 name: current.name ?? name,
280 updateMask: [
281 labelsChanged ? "labels" : undefined,
282 descriptionChanged ? "description" : undefined,
283 displayNameChanged ? "displayName" : undefined,
284 contactsChanged ? "contacts" : undefined,
285 ]
286 .filter((field): field is string => field !== undefined)
287 .join(","),
288 body: {
289 name: current.name ?? name,
290 displayName: news.displayName,
291 description: news.description,
292 labels: desiredLabels,
293 contacts: news.contacts,
294 },
295 });
296 yield* waitForOperation(operation);
297 current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name);
298 }
299
300 return toAttrs(current, env.project);
301 }),
302
303 delete: Effect.fn(function* ({ output }) {
304 const operation = yield* dataplex
305 .deleteProjectsLocationsDataDomains({ name: output.name })
306 .pipe(
307 Effect.retry({
308 while: (error) => error._tag === "Conflict",
309 times: 8,
310 schedule: Schedule.spaced("2 seconds"),
311 }),
312 Effect.catchTag("NotFound", () => Effect.undefined),
313 );
314 if (operation !== undefined) {
315 yield* waitForOperation(operation, { notFoundOk: true });
316 }
317 yield* waitUntilGone(getByName(output.name), output.name);
318 }),
319 });