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

main
ts 576 lines 21 KB
Raw
1 import * as analyticshub from "@distilled.cloud/gcp/analyticshub_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 * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import {
10 AnalyticshubNotResolved,
11 displayNameOf,
12 encodeDescription,
13 expandParent,
14 deleteRetry,
15 hasOwnershipMarker,
16 listChildResources,
17 listExchangesInProject,
18 listListings,
19 namedOf,
20 normalizeLocation,
21 ownedById,
22 ownershipLabels,
23 parseDescription,
24 parseResourceName,
25 replaceOnIdentity,
26 retryTransient,
27 sameBool,
28 sameJson,
29 sameText,
30 toPhysicalId,
31 updateMaskOf,
32 } from "./internal.ts";
33
34 export type ListingDataProvider = analyticshub.DataProvider;
35 export type ListingPublisher = analyticshub.Publisher;
36 export type ListingRestrictedExportConfig = analyticshub.RestrictedExportConfig;
37 export type ListingStoredProcedureConfig = analyticshub.StoredProcedureConfig;
38 export type ListingBigQueryDatasetSource = analyticshub.BigQueryDatasetSource;
39 export type ListingPubSubTopicSource = analyticshub.PubSubTopicSource;
40 export type ListingDiscoveryType = analyticshub.ListingDiscoveryTypeEnum | (string & {});
41 export type ListingCategory = analyticshub.ListingCategoriesItemEnum | (string & {});
42
43 export type DataExchangesListingProps = {
44 /**
45 * Parent data exchange. Full name
46 * `projects/{project}/locations/{location}/dataExchanges/{dataExchange}`
47 * or the exchange id (combined with `location`). Immutable — changing
48 * it replaces the listing.
49 */
50 dataExchange: string;
51 /**
52 * Listing id (the `{listing}` segment of
53 * `.../dataExchanges/{dataExchange}/listings/{listing}`). If omitted, a
54 * unique id is generated. Letters, numbers, and underscores; max 100
55 * bytes. Immutable — changing it replaces the listing.
56 */
57 listingId?: string;
58 /**
59 * Location used when `dataExchange` is a bare id.
60 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
61 */
62 location?: string;
63 /**
64 * Human-readable display name. Max 63 bytes. Defaults to the listing
65 * id.
66 */
67 displayName?: string;
68 /**
69 * Short description (max 2000 bytes). Listings have no labels field,
70 * so Alchemy ownership is stored in a `[alchemy …]` prefix and
71 * stripped from attributes.
72 */
73 description?: string;
74 /**
75 * Documentation describing the listing.
76 */
77 documentation?: string;
78 /**
79 * Email or URL of the primary point of contact. Max 1000 bytes.
80 */
81 primaryContact?: string;
82 /**
83 * Email or URL subscribers can use to request access. Max 1000 bytes.
84 */
85 requestAccess?: string;
86 /**
87 * Base64-encoded icon. Max 3.0 MiB encoded.
88 */
89 icon?: string;
90 /**
91 * Shared BigQuery dataset. Provide `dataset` as
92 * `projects/{project}/datasets/{dataset}` or a dataset id. The dataset
93 * location must match the exchange. Immutable — changing the dataset
94 * replaces the listing.
95 */
96 bigqueryDataset?: ListingBigQueryDatasetSource;
97 /**
98 * Shared Pub/Sub topic. Provide `topic` as
99 * `projects/{project}/topics/{topic}`. Immutable — changing the topic
100 * replaces the listing.
101 */
102 pubsubTopic?: ListingPubSubTopicSource;
103 /**
104 * Details of the data provider who owns the source data.
105 */
106 dataProvider?: ListingDataProvider;
107 /**
108 * Details of the publisher who owns the listing.
109 */
110 publisher?: ListingPublisher;
111 /**
112 * Categories (up to five).
113 */
114 categories?: ListingCategory[];
115 /**
116 * Discovery visibility (`DISCOVERY_TYPE_PRIVATE`,
117 * `DISCOVERY_TYPE_PUBLIC`).
118 */
119 discoveryType?: ListingDiscoveryType;
120 /**
121 * Restricted export configuration propagated to linked datasets.
122 */
123 restrictedExportConfig?: ListingRestrictedExportConfig;
124 /**
125 * Stored-procedure sharing configuration for linked datasets.
126 */
127 storedProcedureConfig?: ListingStoredProcedureConfig;
128 /**
129 * When true, query jobs against linked datasets record the subscriber
130 * email in the logs.
131 * @default false
132 */
133 logLinkedDatasetQueryUserEmail?: boolean;
134 /**
135 * When true, the listing exposes metadata only and cannot be
136 * subscribed to. Immutable on some exchanges.
137 * @default false
138 */
139 allowOnlyMetadataSharing?: boolean;
140 /**
141 * When true, deleting a commercial listing is allowed. Commercial
142 * listings refuse delete unless this is set.
143 * @default false
144 */
145 deleteCommercial?: boolean;
146 };
147
148 export type DataExchangesListing = Resource<
149 "GCP.AnalyticsHub.DataExchangesListing",
150 DataExchangesListingProps,
151 {
152 /** Full resource name. */
153 name: string;
154 /** Listing id (last path segment). */
155 listingId: string;
156 /** Parent data exchange resource name. */
157 dataExchange: string;
158 /** Project id. */
159 project: string;
160 /** Location id. */
161 location: string;
162 /** Display name. */
163 displayName: string | undefined;
164 /** User description (Alchemy ownership marker stripped). */
165 description: string | undefined;
166 /** Documentation. */
167 documentation: string | undefined;
168 /** Primary contact. */
169 primaryContact: string | undefined;
170 /** Request-access URL or email. */
171 requestAccess: string | undefined;
172 /** Base64-encoded icon. */
173 icon: string | undefined;
174 /** Shared BigQuery dataset source. */
175 bigqueryDataset: ListingBigQueryDatasetSource | undefined;
176 /** Shared Pub/Sub topic source. */
177 pubsubTopic: ListingPubSubTopicSource | undefined;
178 /** Data provider. */
179 dataProvider: ListingDataProvider | undefined;
180 /** Publisher. */
181 publisher: ListingPublisher | undefined;
182 /** Categories. */
183 categories: ListingCategory[] | undefined;
184 /** Discovery type. */
185 discoveryType: string | undefined;
186 /** Restricted export configuration. */
187 restrictedExportConfig: ListingRestrictedExportConfig | undefined;
188 /** Stored-procedure configuration. */
189 storedProcedureConfig: ListingStoredProcedureConfig | undefined;
190 /** Whether linked-dataset query emails are logged. */
191 logLinkedDatasetQueryUserEmail: boolean;
192 /** Whether the listing is metadata-only. */
193 allowOnlyMetadataSharing: boolean;
194 /** Shared asset type (`BIGQUERY_DATASET`, `PUBSUB_TOPIC`). */
195 resourceType: string | undefined;
196 /** Listing lifecycle state. */
197 state: string | undefined;
198 },
199 never,
200 Providers
201 >;
202
203 /**
204 * An Analytics Hub listing published into a data exchange. Subscribers
205 * get a linked BigQuery dataset or a Pub/Sub subscription pointing at
206 * the shared source.
207 *
208 * Parent exchange, location, listing id, and the source dataset/topic
209 * are immutable. Display name, description, documentation, contacts,
210 * categories, and export settings update in place.
211 *
212 * Listings have no labels. Alchemy stamps ownership into the
213 * description so `list` / `pnpm nuke:gcp` can find them.
214 *
215 * ### Creating a Listing
216 * **Example:** Share a BigQuery dataset
217 * ```typescript
218 * const dataset = yield* GCP.BigQuery.Dataset("Source", {
219 * location: "us-central1",
220 * forceDestroy: true,
221 * });
222 * const exchange = yield* GCP.AnalyticsHub.DataExchange("Marketplace", {
223 * displayName: "Marketplace",
224 * });
225 * const listing = yield* GCP.AnalyticsHub.DataExchangesListing("Orders", {
226 * dataExchange: exchange.name,
227 * displayName: "Orders",
228 * bigqueryDataset: { dataset: dataset.name },
229 * });
230 * ```
231 *
232 * **Example:** Share a Pub/Sub topic
233 * ```typescript
234 * const listing = yield* GCP.AnalyticsHub.DataExchangesListing("Events", {
235 * dataExchange: exchange.name,
236 * displayName: "Events",
237 * pubsubTopic: { topic: topic.name },
238 * });
239 * ```
240 *
241 * @resource
242 * @category AnalyticsHub
243 */
244 export const DataExchangesListing = Resource<DataExchangesListing>(
245 "GCP.AnalyticsHub.DataExchangesListing",
246 );
247
248 const parentExchange = (dataExchange: string, project: string, location: string) =>
249 expandParent(dataExchange, project, location, "dataExchanges");
250
251 const resourceName = (dataExchange: string, listingId: string) =>
252 `${dataExchange}/listings/${listingId}`;
253
254 const datasetName = (value: string | undefined, project: string) => {
255 if (value === undefined || value.length === 0) return undefined;
256 if (value.includes("/")) return value.replace(/\/+$/, "");
257 return `projects/${project}/datasets/${value}`;
258 };
259
260 const topicName = (value: string | undefined, project: string) => {
261 if (value === undefined || value.length === 0) return undefined;
262 if (value.includes("/")) return value.replace(/\/+$/, "");
263 return `projects/${project}/topics/${value}`;
264 };
265
266 const desiredBigQuery = (
267 source: ListingBigQueryDatasetSource | undefined,
268 project: string,
269 ): ListingBigQueryDatasetSource | undefined => {
270 if (source === undefined) return undefined;
271 return {
272 ...source,
273 dataset: datasetName(source.dataset, project),
274 };
275 };
276
277 const desiredPubSub = (
278 source: ListingPubSubTopicSource | undefined,
279 project: string,
280 ): ListingPubSubTopicSource | undefined => {
281 if (source === undefined) return undefined;
282 return {
283 ...source,
284 topic: topicName(source.topic, project),
285 };
286 };
287
288 const sourceIdentity = (
289 bigqueryDataset: ListingBigQueryDatasetSource | undefined,
290 pubsubTopic: ListingPubSubTopicSource | undefined,
291 ) => `${bigqueryDataset?.dataset ?? ""}|${pubsubTopic?.topic ?? ""}`;
292
293 const toAttrs = (listing: analyticshub.Listing, project: string) => {
294 const name = listing.name ?? "";
295 const parsed = parseResourceName(name, "listings");
296 const { description } = parseDescription(listing.description);
297 return {
298 name,
299 listingId: parsed.id,
300 dataExchange: parsed.parent,
301 project: parsed.project || project,
302 location: parsed.location,
303 displayName: listing.displayName,
304 description,
305 documentation: listing.documentation,
306 primaryContact: listing.primaryContact,
307 requestAccess: listing.requestAccess,
308 icon: listing.icon,
309 bigqueryDataset: listing.bigqueryDataset,
310 pubsubTopic: listing.pubsubTopic,
311 dataProvider: listing.dataProvider,
312 publisher: listing.publisher,
313 categories: listing.categories,
314 discoveryType: listing.discoveryType,
315 restrictedExportConfig: listing.restrictedExportConfig,
316 storedProcedureConfig: listing.storedProcedureConfig,
317 logLinkedDatasetQueryUserEmail: listing.logLinkedDatasetQueryUserEmail === true,
318 allowOnlyMetadataSharing: listing.allowOnlyMetadataSharing === true,
319 resourceType: listing.resourceType,
320 state: listing.state,
321 };
322 };
323
324 const getByName = (name: string) =>
325 Effect.gen(function* () {
326 if (name.length === 0) return undefined;
327 const parsed = parseResourceName(name, "listings");
328 const items = yield* listListings(parsed.parent);
329 return items.find((item) => (item.name ?? "") === name);
330 });
331
332 export const DataExchangesListingProvider = () =>
333 Provider.succeed(DataExchangesListing, {
334 nuke: {
335 dependsOn: ["GCP.AnalyticsHub.DataExchange", "GCP.BigQuery.Dataset"],
336 },
337 stables: ["name", "listingId", "dataExchange", "project", "location"],
338
339 diff: Effect.fn(function* ({ news, olds, output }) {
340 if (!isResolved(news)) return undefined;
341 const env = yield* GcpEnvironment.current;
342 const location = normalizeLocation(
343 news.location ?? olds?.location ?? output?.location,
344 env.region,
345 );
346 const nextSource = sourceIdentity(
347 desiredBigQuery(news.bigqueryDataset, env.project),
348 desiredPubSub(news.pubsubTopic, env.project),
349 );
350 const previousSource = sourceIdentity(
351 olds?.bigqueryDataset ?? output?.bigqueryDataset,
352 olds?.pubsubTopic ?? output?.pubsubTopic,
353 );
354 return replaceOnIdentity({
355 previousId: olds?.listingId ?? output?.listingId,
356 nextId: news.listingId,
357 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
358 nextLocation: location,
359 previousParent: olds?.dataExchange ?? output?.dataExchange,
360 nextParent: parentExchange(news.dataExchange, env.project, location),
361 extra: previousSource.length > 1 && nextSource.length > 1 && previousSource !== nextSource,
362 });
363 }),
364
365 read: Effect.fn(function* ({ id, olds, output }) {
366 const env = yield* GcpEnvironment.current;
367 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
368 const listingId = yield* toPhysicalId(id, olds?.listingId, output?.listingId);
369 const dataExchange =
370 olds?.dataExchange !== undefined
371 ? parentExchange(olds.dataExchange, env.project, location)
372 : (output?.dataExchange ?? "");
373 const name = output?.name ?? (dataExchange ? resourceName(dataExchange, listingId) : "");
374 const existing = yield* getByName(name);
375 if (existing === undefined) return undefined;
376 const attrs = toAttrs(existing, env.project);
377 return (yield* ownedById(id, existing.description)) ? attrs : Unowned(attrs);
378 }),
379
380 list: () =>
381 Effect.gen(function* () {
382 const env = yield* GcpEnvironment.current;
383 const exchanges = yield* listExchangesInProject(env.project, env.region);
384 const listings = yield* listChildResources(namedOf(exchanges), listListings);
385 return listings
386 .filter((item) => hasOwnershipMarker(item.description))
387 .map((item) => toAttrs(item, env.project));
388 }),
389
390 reconcile: Effect.fn(function* ({ id, news, output }) {
391 const env = yield* GcpEnvironment.current;
392 const location = normalizeLocation(news.location ?? output?.location, env.region);
393 const dataExchange = parentExchange(news.dataExchange, env.project, location);
394 const listingId = yield* toPhysicalId(id, news.listingId, output?.listingId);
395 const name = output?.name ?? resourceName(dataExchange, listingId);
396 const ownership = yield* ownershipLabels(id);
397 const desiredDescription = encodeDescription(ownership, news.description);
398 const displayName = displayNameOf(news.displayName, listingId);
399 const bigqueryDataset = desiredBigQuery(news.bigqueryDataset, env.project);
400 const pubsubTopic = desiredPubSub(news.pubsubTopic, env.project);
401 const body: analyticshub.Listing = {
402 displayName,
403 description: desiredDescription,
404 };
405 if (news.documentation !== undefined) {
406 body.documentation = news.documentation;
407 }
408 if (news.primaryContact !== undefined) {
409 body.primaryContact = news.primaryContact;
410 }
411 if (news.requestAccess !== undefined) {
412 body.requestAccess = news.requestAccess;
413 }
414 if (news.icon !== undefined) {
415 body.icon = news.icon;
416 }
417 if (bigqueryDataset !== undefined) {
418 body.bigqueryDataset = bigqueryDataset;
419 }
420 if (pubsubTopic !== undefined) {
421 body.pubsubTopic = pubsubTopic;
422 }
423 if (news.dataProvider !== undefined) {
424 body.dataProvider = news.dataProvider;
425 }
426 if (news.publisher !== undefined) {
427 body.publisher = news.publisher;
428 }
429 if (news.categories !== undefined) {
430 body.categories = news.categories;
431 }
432 if (news.discoveryType !== undefined) {
433 body.discoveryType = news.discoveryType;
434 }
435 if (news.restrictedExportConfig !== undefined) {
436 body.restrictedExportConfig = news.restrictedExportConfig;
437 }
438 if (news.storedProcedureConfig !== undefined) {
439 body.storedProcedureConfig = news.storedProcedureConfig;
440 }
441 if (news.logLinkedDatasetQueryUserEmail === true) {
442 body.logLinkedDatasetQueryUserEmail = true;
443 }
444 if (news.allowOnlyMetadataSharing === true) {
445 body.allowOnlyMetadataSharing = true;
446 }
447
448 let current = yield* getByName(name);
449
450 if (current === undefined) {
451 const created = yield* retryTransient(
452 analyticshub.createProjectsLocationsDataExchangesListings({
453 parent: dataExchange,
454 listingId,
455 body,
456 }),
457 ).pipe(Effect.catchTag("Conflict", () => getByName(name)));
458 current = created ?? undefined;
459 }
460
461 if (current === undefined) {
462 return yield* new AnalyticshubNotResolved({ resourceName: name });
463 }
464
465 const currentName = current.name ?? name;
466 const displayChanged = !sameText(current.displayName, displayName);
467 const descriptionChanged = !sameText(current.description, desiredDescription);
468 const documentationChanged = !sameText(current.documentation, news.documentation);
469 const contactChanged = !sameText(current.primaryContact, news.primaryContact);
470 const requestChanged = !sameText(current.requestAccess, news.requestAccess);
471 const iconChanged = !sameText(current.icon, news.icon);
472 const dataProviderChanged =
473 news.dataProvider !== undefined && !sameJson(current.dataProvider, news.dataProvider);
474 const publisherChanged =
475 news.publisher !== undefined && !sameJson(current.publisher, news.publisher);
476 const categoriesChanged =
477 news.categories !== undefined && !sameJson(current.categories, news.categories);
478 const discoveryChanged =
479 news.discoveryType !== undefined && !sameText(current.discoveryType, news.discoveryType);
480 const restrictedChanged =
481 news.restrictedExportConfig !== undefined &&
482 !sameJson(
483 {
484 enabled: current.restrictedExportConfig?.enabled,
485 restrictQueryResult: current.restrictedExportConfig?.restrictQueryResult,
486 },
487 {
488 enabled: news.restrictedExportConfig.enabled,
489 restrictQueryResult: news.restrictedExportConfig.restrictQueryResult,
490 },
491 );
492 const storedChanged =
493 news.storedProcedureConfig !== undefined &&
494 !sameBool(current.storedProcedureConfig?.enabled, news.storedProcedureConfig.enabled);
495 const emailChanged =
496 news.logLinkedDatasetQueryUserEmail !== undefined &&
497 !sameBool(current.logLinkedDatasetQueryUserEmail, news.logLinkedDatasetQueryUserEmail);
498 const metadataChanged =
499 news.allowOnlyMetadataSharing !== undefined &&
500 !sameBool(current.allowOnlyMetadataSharing, news.allowOnlyMetadataSharing);
501 const selectedChanged =
502 bigqueryDataset?.selectedResources !== undefined &&
503 !sameJson(current.bigqueryDataset?.selectedResources, bigqueryDataset.selectedResources);
504 const replicaChanged =
505 bigqueryDataset?.replicaLocations !== undefined &&
506 !sameJson(current.bigqueryDataset?.replicaLocations, bigqueryDataset.replicaLocations);
507 const restrictedPolicyChanged =
508 bigqueryDataset?.restrictedExportPolicy !== undefined &&
509 !sameJson(
510 current.bigqueryDataset?.restrictedExportPolicy,
511 bigqueryDataset.restrictedExportPolicy,
512 );
513 const affinityChanged =
514 pubsubTopic?.dataAffinityRegions !== undefined &&
515 !sameJson(current.pubsubTopic?.dataAffinityRegions, pubsubTopic.dataAffinityRegions);
516
517 if (
518 displayChanged ||
519 descriptionChanged ||
520 documentationChanged ||
521 contactChanged ||
522 requestChanged ||
523 iconChanged ||
524 dataProviderChanged ||
525 publisherChanged ||
526 categoriesChanged ||
527 discoveryChanged ||
528 restrictedChanged ||
529 storedChanged ||
530 emailChanged ||
531 metadataChanged ||
532 selectedChanged ||
533 replicaChanged ||
534 restrictedPolicyChanged ||
535 affinityChanged
536 ) {
537 current = yield* retryTransient(
538 analyticshub.patchProjectsLocationsDataExchangesListings({
539 name: currentName,
540 updateMask: updateMaskOf(
541 displayChanged ? "displayName" : undefined,
542 descriptionChanged ? "description" : undefined,
543 documentationChanged ? "documentation" : undefined,
544 contactChanged ? "primaryContact" : undefined,
545 requestChanged ? "requestAccess" : undefined,
546 iconChanged ? "icon" : undefined,
547 dataProviderChanged ? "dataProvider" : undefined,
548 publisherChanged ? "publisher" : undefined,
549 categoriesChanged ? "categories" : undefined,
550 discoveryChanged ? "discoveryType" : undefined,
551 restrictedChanged ? "restrictedExportConfig" : undefined,
552 storedChanged ? "storedProcedureConfig" : undefined,
553 emailChanged ? "logLinkedDatasetQueryUserEmail" : undefined,
554 metadataChanged ? "allowOnlyMetadataSharing" : undefined,
555 selectedChanged ? "bigqueryDataset.selectedResources" : undefined,
556 replicaChanged ? "bigqueryDataset.replicaLocations" : undefined,
557 restrictedPolicyChanged ? "bigqueryDataset.restrictedExportPolicy" : undefined,
558 affinityChanged ? "pubsubTopic.dataAffinityRegions" : undefined,
559 ),
560 body: { name: currentName, ...body },
561 }),
562 );
563 }
564
565 return toAttrs(current, env.project);
566 }),
567
568 delete: Effect.fn(function* ({ output }) {
569 yield* deleteRetry(
570 analyticshub.deleteProjectsLocationsDataExchangesListings({
571 name: output.name,
572 deleteCommercial: true,
573 }),
574 );
575 }),
576 });