@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 975 lines 32.3 KB
Raw
1 import * as bigqueryconnection from "@distilled.cloud/gcp/bigqueryconnection_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13
14 const MAX_NAME_LENGTH = 60;
15 const MULTI_REGION = new Set(["us", "eu"]);
16
17 export type SparkHistoryServerConfig = {
18 /**
19 * Existing Dataproc cluster used as a Spark History Server, as
20 * `projects/{project}/regions/{region}/clusters/{cluster}`.
21 */
22 dataprocCluster?: string;
23 };
24
25 export type MetastoreServiceConfig = {
26 /**
27 * Existing Dataproc Metastore service, as
28 * `projects/{project}/locations/{location}/services/{service}`.
29 */
30 metastoreService?: string;
31 };
32
33 export type SparkProperties = {
34 /** Spark History Server configuration. */
35 sparkHistoryServerConfig?: SparkHistoryServerConfig;
36 /** Dataproc Metastore service configuration. */
37 metastoreServiceConfig?: MetastoreServiceConfig;
38 };
39
40 export type CloudSqlCredential = {
41 /** Database username. */
42 username?: string;
43 /** Database password. Input-only — omitted from attributes. */
44 password?: string;
45 };
46
47 export type CloudSqlProperties = {
48 /**
49 * Cloud SQL instance id in the form `project:location:instance`.
50 */
51 instanceId?: string;
52 /** Database engine. */
53 type?: "DATABASE_TYPE_UNSPECIFIED" | "POSTGRES" | "MYSQL" | (string & {});
54 /** Database name. */
55 database?: string;
56 /** Cloud SQL credential. Input-only password. */
57 credential?: CloudSqlCredential;
58 };
59
60 export type CloudResourceProperties = {
61 /**
62 * Google-managed service account for this connection. Output-only —
63 * ignored on create.
64 */
65 serviceAccountId?: string;
66 };
67
68 export type AwsAccessRole = {
69 /** Customer AWS IAM role that trusts the Google-owned AWS identity. */
70 iamRoleId?: string;
71 };
72
73 export type AwsProperties = {
74 /** AWS IAM role assumption. */
75 accessRole?: AwsAccessRole;
76 };
77
78 export type CloudSpannerProperties = {
79 /**
80 * Cloud Spanner database as `project/instance/database`.
81 */
82 database?: string;
83 /** Fine-grained access control database role. */
84 databaseRole?: string;
85 /** Whether to read from Spanner in parallel. */
86 useParallelism?: boolean;
87 /**
88 * Deprecated. Prefer `useDataBoost`. Requires `useParallelism`.
89 */
90 useServerlessAnalytics?: boolean;
91 /** Execute via Spanner independent compute. Requires `useParallelism`. */
92 useDataBoost?: boolean;
93 /** Max parallelism per query. Requires `useParallelism` and `useDataBoost`. */
94 maxParallelism?: number;
95 };
96
97 export type SalesforceDataCloudProperties = {
98 /** Salesforce DataCloud instance URL. */
99 instanceUri?: string;
100 /** Salesforce tenant id. */
101 tenantId?: string;
102 };
103
104 export type AzureProperties = {
105 /** Customer Azure Active Directory tenant that hosts the data. */
106 customerTenantId?: string;
107 /** Redirect URL after consent during connection setup. */
108 redirectUri?: string;
109 /** Customer Azure AD application client id for federated auth. */
110 federatedApplicationClientId?: string;
111 };
112
113 export type ConnectorSecret = {
114 /** Input-only plaintext secret. */
115 plaintext?: string;
116 };
117
118 export type ConnectorUsernamePassword = {
119 /** Username. */
120 username?: string;
121 /** Password secret. */
122 password?: ConnectorSecret;
123 };
124
125 export type ConnectorParameterValue = {
126 int32Value?: number;
127 stringValue?: string;
128 boolValue?: boolean;
129 doubleValue?: number;
130 secretValue?: ConnectorSecret;
131 };
132
133 export type ConnectorAuthentication = {
134 usernamePassword?: ConnectorUsernamePassword;
135 parameters?: Record<string, ConnectorParameterValue | undefined>;
136 };
137
138 export type ConnectorNetwork = {
139 /** Private Service Connect network attachment. */
140 privateServiceConnect?: {
141 networkAttachment?: string;
142 };
143 };
144
145 export type ConnectorAsset = {
146 database?: string;
147 googleCloudResource?: string;
148 };
149
150 export type ConnectorEndpoint = {
151 /** Host and port as `hostname:port`. */
152 hostPort?: string;
153 };
154
155 export type ConnectorConfiguration = {
156 /**
157 * Connector id (for example `google-alloydb`). Immutable — changing it
158 * replaces the connection.
159 */
160 connectorId?: string;
161 authentication?: ConnectorAuthentication;
162 network?: ConnectorNetwork;
163 parameters?: Record<string, ConnectorParameterValue | undefined>;
164 asset?: ConnectorAsset;
165 endpoint?: ConnectorEndpoint;
166 };
167
168 export type ConnectionKind =
169 | "cloudResource"
170 | "cloudSql"
171 | "cloudSpanner"
172 | "spark"
173 | "aws"
174 | "azure"
175 | "salesforceDataCloud"
176 | "configuration";
177
178 export type ConnectionProps = {
179 /**
180 * Connection id (the `{connection}` segment of
181 * `projects/{project}/locations/{location}/connections/{connection}`).
182 * If omitted, a unique name is generated from the stack, stage, and
183 * logical id. Must contain only letters, numbers, and underscores, start
184 * with a letter or underscore, and be at most 64 characters. Immutable —
185 * changing it replaces the connection.
186 */
187 connectionId?: string;
188 /**
189 * BigQuery connection location (`us-central1`, `US`, `EU`,
190 * `aws-us-east-1`, …). Immutable — changing it replaces the connection.
191 * Regional ids are lowercased; multi-regions `US` / `EU` stay uppercase.
192 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
193 */
194 location?: string;
195 /**
196 * User-friendly display name.
197 */
198 friendlyName?: string;
199 /**
200 * Human-readable description. Connections have no labels, so Alchemy
201 * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is stored
202 * in a `[alchemy …]` prefix for `list` / nuke.
203 */
204 description?: string;
205 /**
206 * Cloud KMS key used to encrypt credentials, as
207 * `projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`.
208 * Immutable — changing it replaces the connection.
209 */
210 kmsKeyName?: string;
211 /**
212 * Cloud resource connection (GCS, Bigtable, and other GCP resources).
213 * Default when no other connection type is set. Cheap to create — no
214 * extra resources required.
215 */
216 cloudResource?: CloudResourceProperties;
217 /**
218 * Cloud SQL connection. Requires an existing Cloud SQL instance,
219 * database, and credential.
220 */
221 cloudSql?: CloudSqlProperties;
222 /**
223 * Cloud Spanner connection. Requires an existing Spanner database.
224 */
225 cloudSpanner?: CloudSpannerProperties;
226 /**
227 * Apache Spark stored-procedure connection.
228 */
229 spark?: SparkProperties;
230 /**
231 * BigQuery Omni AWS connection. Location must be an AWS region such as
232 * `aws-us-east-1`.
233 */
234 aws?: AwsProperties;
235 /**
236 * BigQuery Omni Azure connection. Location must be an Azure region such
237 * as `azure-eastus2`.
238 */
239 azure?: AzureProperties;
240 /**
241 * Salesforce DataCloud connection. Partner projects only.
242 */
243 salesforceDataCloud?: SalesforceDataCloudProperties;
244 /**
245 * Connector-framework configuration (AlloyDB, MySQL, PostgreSQL, …).
246 * Mutually exclusive with the typed property blocks. `connectorId` is
247 * immutable.
248 */
249 configuration?: ConnectorConfiguration;
250 };
251
252 export type Connection = Resource<
253 "GCP.BigQueryConnection.Connection",
254 ConnectionProps,
255 {
256 /** Full resource name `projects/{project}/locations/{location}/connections/{connection}`. */
257 name: string;
258 /** Connection id (last path segment). */
259 connectionId: string;
260 /** Project id. */
261 project: string;
262 /** Location id (`us-central1`, `US`, …). */
263 location: string;
264 /** User-friendly display name. */
265 friendlyName: string | undefined;
266 /** User description with the Alchemy ownership prefix stripped. */
267 description: string | undefined;
268 /** Cloud KMS key used for credential encryption, if any. */
269 kmsKeyName: string | undefined;
270 /** True when a credential is configured. */
271 hasCredential: boolean;
272 /** RFC3339 creation timestamp (millis since epoch as a string). */
273 creationTime: string | undefined;
274 /** RFC3339 last-update timestamp (millis since epoch as a string). */
275 lastModifiedTime: string | undefined;
276 /** Connection type currently configured. */
277 kind: ConnectionKind;
278 /**
279 * Google-managed service account used by jobs that use this
280 * connection, if the type exposes one.
281 */
282 serviceAccountId: string | undefined;
283 /** Cloud resource properties, if this is a cloud-resource connection. */
284 cloudResource: CloudResourceProperties | undefined;
285 /** Cloud SQL properties (password omitted). */
286 cloudSql: CloudSqlProperties | undefined;
287 /** Cloud Spanner properties. */
288 cloudSpanner: CloudSpannerProperties | undefined;
289 /** Spark properties. */
290 spark: SparkProperties | undefined;
291 /** AWS properties. */
292 aws: AwsProperties | undefined;
293 /** Azure properties. */
294 azure: AzureProperties | undefined;
295 /** Salesforce DataCloud properties. */
296 salesforceDataCloud: SalesforceDataCloudProperties | undefined;
297 /** Connector-framework configuration (secrets omitted). */
298 configuration: ConnectorConfiguration | undefined;
299 },
300 never,
301 Providers
302 >;
303
304 /**
305 * A BigQuery Connection to an external data source.
306 *
307 * Connections have no resource labels. Alchemy stamps ownership into the
308 * description (`[alchemy alchemy-stack=… alchemy-stage=… alchemy-id=…]`)
309 * so `list` / `pnpm nuke:gcp` can find them.
310 *
311 * When no connection type is set, Alchemy creates a Cloud resource
312 * connection (`cloudResource: {}`) — the cheapest type, with no extra
313 * Cloud SQL / Spanner / AWS resources. `connectionId`, `location`,
314 * `kmsKeyName`, and the connection type are immutable.
315 *
316 * ### Creating a Connection
317 * **Example:** Generated Cloud resource connection
318 * ```typescript
319 * const gcs = yield* GCP.BigQueryConnection.Connection("Gcs", {});
320 * ```
321 *
322 * **Example:** Named Cloud resource connection
323 * ```typescript
324 * const gcs = yield* GCP.BigQueryConnection.Connection("Gcs", {
325 * connectionId: "app-gcs",
326 * location: "us-central1",
327 * friendlyName: "GCS access",
328 * description: "delegates to Cloud Storage",
329 * cloudResource: {},
330 * });
331 * ```
332 *
333 * ### Spark
334 * **Example:** Spark stored-procedure connection
335 * ```typescript
336 * const spark = yield* GCP.BigQueryConnection.Connection("Spark", {
337 * spark: {},
338 * });
339 * ```
340 *
341 * ### Cloud SQL
342 * Cloud SQL connections need a live instance, database, and password —
343 * skip them in cheap tests.
344 *
345 * **Example:** Postgres Cloud SQL connection
346 * ```typescript
347 * const sql = yield* GCP.BigQueryConnection.Connection("Sql", {
348 * location: "US",
349 * cloudSql: {
350 * instanceId: "my-project:us-central1:app-sql",
351 * database: "app",
352 * type: "POSTGRES",
353 * credential: { username: "app", password: "secret" },
354 * },
355 * });
356 * ```
357 *
358 * @resource
359 * @category BigQueryConnection
360 */
361 export const Connection = Resource<Connection>("GCP.BigQueryConnection.Connection");
362
363 export class ConnectionNotResolved extends Data.TaggedError(
364 "GCP.BigQueryConnection.ConnectionNotResolved",
365 )<{
366 resourceName: string;
367 }> {}
368
369 const lastSegment = (value: string) => {
370 const trimmed = value.replace(/\/+$/, "");
371 const parts = trimmed.split("/");
372 return parts[parts.length - 1] || trimmed;
373 };
374
375 const normalizeLocation = (location: string | undefined, fallback: string) => {
376 const raw = lastSegment(location ?? fallback);
377 const lower = raw.toLowerCase();
378 return MULTI_REGION.has(lower) ? lower.toUpperCase() : lower;
379 };
380
381 const resourceName = (project: string, location: string, connectionId: string) =>
382 `projects/${project}/locations/${location}/connections/${connectionId}`;
383
384 const parentOf = (project: string, location: string) => `projects/${project}/locations/${location}`;
385
386 const parseName = (name: string) => {
387 const parts = name.split("/").filter((part) => part.length > 0);
388 const connectionsAt = parts.lastIndexOf("connections");
389 const locationsAt = parts.lastIndexOf("locations");
390 const projectsAt = parts.lastIndexOf("projects");
391 return {
392 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
393 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
394 connectionId:
395 connectionsAt >= 0 && parts[connectionsAt + 1]
396 ? parts[connectionsAt + 1]!
397 : lastSegment(name),
398 };
399 };
400
401 const toId = (id: string, connectionId: string | undefined, existing?: string) =>
402 Effect.gen(function* () {
403 if (connectionId !== undefined) return connectionId;
404 if (existing !== undefined) return existing;
405 const generated = yield* createPhysicalName({
406 id,
407 maxLength: MAX_NAME_LENGTH,
408 lowercase: true,
409 delimiter: "_",
410 });
411 const sanitized = generated.replaceAll("-", "_");
412 return /^[a-z_]/.test(sanitized) ? sanitized : `c${sanitized}`.slice(0, MAX_NAME_LENGTH);
413 });
414
415 const encodeDescription = (
416 labels: Record<string, string>,
417 description: string | undefined,
418 ): string => {
419 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
420 return description ? `${marker}\n${description}` : marker;
421 };
422
423 const parseDescription = (
424 description: string | undefined,
425 ): {
426 labels: Record<string, string>;
427 description: string | undefined;
428 } => {
429 if (!description?.startsWith("[alchemy ")) {
430 return { labels: {}, description };
431 }
432 const end = description.indexOf("]");
433 if (end < 0) return { labels: {}, description };
434 const labels: Record<string, string> = {};
435 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
436 const eq = part.indexOf("=");
437 if (eq > 0) {
438 labels[part.slice(0, eq)] = part.slice(eq + 1);
439 }
440 }
441 const rest = description.slice(end + 1).replace(/^\n/, "");
442 return { labels, description: rest.length > 0 ? rest : undefined };
443 };
444
445 const hasOwnershipMarker = (description: string | undefined) =>
446 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
447
448 const jsonEqual = (left: unknown, right: unknown) =>
449 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
450
451 const specifiedKind = (value: {
452 cloudSql?: unknown;
453 cloudSpanner?: unknown;
454 spark?: unknown;
455 aws?: unknown;
456 azure?: unknown;
457 salesforceDataCloud?: unknown;
458 configuration?: unknown;
459 cloudResource?: unknown;
460 }): ConnectionKind | undefined => {
461 if (value.cloudSql !== undefined) return "cloudSql";
462 if (value.cloudSpanner !== undefined) return "cloudSpanner";
463 if (value.spark !== undefined) return "spark";
464 if (value.aws !== undefined) return "aws";
465 if (value.azure !== undefined) return "azure";
466 if (value.salesforceDataCloud !== undefined) return "salesforceDataCloud";
467 if (value.configuration !== undefined) return "configuration";
468 if (value.cloudResource !== undefined) return "cloudResource";
469 return undefined;
470 };
471
472 const kindOf = (value: Parameters<typeof specifiedKind>[0]): ConnectionKind =>
473 specifiedKind(value) ?? "cloudResource";
474
475 const toCloudSql = (
476 properties: bigqueryconnection.CloudSqlProperties | undefined,
477 ): CloudSqlProperties | undefined => {
478 if (properties === undefined) return undefined;
479 return {
480 instanceId: properties.instanceId,
481 type: properties.type,
482 database: properties.database,
483 credential: properties.credential ? { username: properties.credential.username } : undefined,
484 };
485 };
486
487 const toSpark = (
488 properties: bigqueryconnection.SparkProperties | undefined,
489 ): SparkProperties | undefined => {
490 if (properties === undefined) return undefined;
491 return {
492 sparkHistoryServerConfig: properties.sparkHistoryServerConfig,
493 metastoreServiceConfig: properties.metastoreServiceConfig,
494 };
495 };
496
497 const toAws = (
498 properties: bigqueryconnection.AwsProperties | undefined,
499 ): AwsProperties | undefined => {
500 if (properties === undefined) return undefined;
501 return {
502 accessRole: properties.accessRole ? { iamRoleId: properties.accessRole.iamRoleId } : undefined,
503 };
504 };
505
506 const toAzure = (
507 properties: bigqueryconnection.AzureProperties | undefined,
508 ): AzureProperties | undefined => {
509 if (properties === undefined) return undefined;
510 return {
511 customerTenantId: properties.customerTenantId,
512 redirectUri: properties.redirectUri,
513 federatedApplicationClientId: properties.federatedApplicationClientId,
514 };
515 };
516
517 const toSpanner = (
518 properties: bigqueryconnection.CloudSpannerProperties | undefined,
519 ): CloudSpannerProperties | undefined => {
520 if (properties === undefined) return undefined;
521 return {
522 database: properties.database,
523 databaseRole: properties.databaseRole,
524 useParallelism: properties.useParallelism,
525 useServerlessAnalytics: properties.useServerlessAnalytics,
526 useDataBoost: properties.useDataBoost,
527 maxParallelism: properties.maxParallelism,
528 };
529 };
530
531 const toSalesforce = (
532 properties: bigqueryconnection.SalesforceDataCloudProperties | undefined,
533 ): SalesforceDataCloudProperties | undefined => {
534 if (properties === undefined) return undefined;
535 return {
536 instanceUri: properties.instanceUri,
537 tenantId: properties.tenantId,
538 };
539 };
540
541 const toSecret = (
542 secret: bigqueryconnection.ConnectorConfigurationSecret | undefined,
543 ): ConnectorSecret | undefined => {
544 if (secret === undefined) return undefined;
545 return secret.plaintext !== undefined ? { plaintext: secret.plaintext } : undefined;
546 };
547
548 const toParameterMap = (
549 parameters: bigqueryconnection.ConnectorConfigurationParameterValueMap | undefined,
550 ): Record<string, ConnectorParameterValue | undefined> | undefined => {
551 if (parameters === undefined) return undefined;
552 return Object.fromEntries(
553 Object.entries(parameters).map(([key, value]) => [
554 key,
555 value
556 ? {
557 int32Value: value.int32Value,
558 stringValue: value.stringValue,
559 boolValue: value.boolValue,
560 doubleValue: value.doubleValue,
561 secretValue: toSecret(value.secretValue),
562 }
563 : undefined,
564 ]),
565 );
566 };
567
568 const toConfiguration = (
569 configuration: bigqueryconnection.ConnectorConfiguration | undefined,
570 ): ConnectorConfiguration | undefined => {
571 if (configuration === undefined) return undefined;
572 return {
573 connectorId: configuration.connectorId,
574 authentication: configuration.authentication
575 ? {
576 usernamePassword: configuration.authentication.usernamePassword
577 ? {
578 username: configuration.authentication.usernamePassword.username,
579 password: toSecret(configuration.authentication.usernamePassword.password),
580 }
581 : undefined,
582 parameters: toParameterMap(configuration.authentication.parameters),
583 }
584 : undefined,
585 network: configuration.network,
586 parameters: toParameterMap(configuration.parameters),
587 asset: configuration.asset,
588 endpoint: configuration.endpoint,
589 };
590 };
591
592 const toCloudResource = (
593 properties: bigqueryconnection.CloudResourceProperties | undefined,
594 kind: ConnectionKind,
595 ): CloudResourceProperties | undefined => {
596 if (properties === undefined) {
597 return kind === "cloudResource" ? {} : undefined;
598 }
599 return { serviceAccountId: properties.serviceAccountId };
600 };
601
602 const serviceAccountOf = (connection: bigqueryconnection.Connection): string | undefined =>
603 connection.cloudResource?.serviceAccountId ??
604 connection.cloudSql?.serviceAccountId ??
605 connection.spark?.serviceAccountId ??
606 connection.configuration?.authentication?.serviceAccount ??
607 connection.aws?.accessRole?.identity ??
608 connection.azure?.identity ??
609 connection.salesforceDataCloud?.identity;
610
611 const toAttrs = (connection: bigqueryconnection.Connection, project: string) => {
612 const name = connection.name ?? "";
613 const parsed = parseName(name);
614 const kind = kindOf(connection);
615 const parsedDescription = parseDescription(connection.description);
616 return {
617 name,
618 connectionId: parsed.connectionId,
619 project: parsed.project || project,
620 location: parsed.location,
621 friendlyName: connection.friendlyName,
622 description: parsedDescription.description,
623 kmsKeyName: connection.kmsKeyName,
624 hasCredential: connection.hasCredential === true,
625 creationTime: connection.creationTime,
626 lastModifiedTime: connection.lastModifiedTime,
627 kind,
628 serviceAccountId: serviceAccountOf(connection),
629 cloudResource: toCloudResource(connection.cloudResource, kind),
630 cloudSql: toCloudSql(connection.cloudSql),
631 cloudSpanner: toSpanner(connection.cloudSpanner),
632 spark: toSpark(connection.spark),
633 aws: toAws(connection.aws),
634 azure: toAzure(connection.azure),
635 salesforceDataCloud: toSalesforce(connection.salesforceDataCloud),
636 configuration: toConfiguration(connection.configuration),
637 };
638 };
639
640 const writableSpark = (properties: SparkProperties | undefined) => {
641 if (properties === undefined) return undefined;
642 return {
643 sparkHistoryServerConfig: properties.sparkHistoryServerConfig,
644 metastoreServiceConfig: properties.metastoreServiceConfig,
645 };
646 };
647
648 const writableAws = (properties: AwsProperties | undefined) => {
649 if (properties === undefined) return undefined;
650 return {
651 accessRole: properties.accessRole ? { iamRoleId: properties.accessRole.iamRoleId } : undefined,
652 };
653 };
654
655 const writableAzure = (properties: AzureProperties | undefined) => {
656 if (properties === undefined) return undefined;
657 return {
658 customerTenantId: properties.customerTenantId,
659 redirectUri: properties.redirectUri,
660 federatedApplicationClientId: properties.federatedApplicationClientId,
661 };
662 };
663
664 const writableSpanner = (properties: CloudSpannerProperties | undefined) => {
665 if (properties === undefined) return undefined;
666 return {
667 database: properties.database,
668 databaseRole: properties.databaseRole,
669 useParallelism: properties.useParallelism,
670 useServerlessAnalytics: properties.useServerlessAnalytics,
671 useDataBoost: properties.useDataBoost,
672 maxParallelism: properties.maxParallelism,
673 };
674 };
675
676 const writableSalesforce = (properties: SalesforceDataCloudProperties | undefined) => {
677 if (properties === undefined) return undefined;
678 return {
679 instanceUri: properties.instanceUri,
680 tenantId: properties.tenantId,
681 };
682 };
683
684 const writableConfiguration = (configuration: ConnectorConfiguration | undefined) => {
685 if (configuration === undefined) return undefined;
686 return {
687 connectorId: configuration.connectorId,
688 authentication: configuration.authentication,
689 network: configuration.network,
690 parameters: configuration.parameters,
691 asset: configuration.asset,
692 endpoint: configuration.endpoint,
693 };
694 };
695
696 const toCreateBody = (
697 news: ConnectionProps,
698 ownership: Record<string, string>,
699 ): bigqueryconnection.Connection => {
700 const kind = kindOf(news);
701 const body: bigqueryconnection.Connection = {
702 friendlyName: news.friendlyName,
703 description: encodeDescription(ownership, news.description),
704 kmsKeyName: news.kmsKeyName,
705 };
706 switch (kind) {
707 case "cloudSql":
708 body.cloudSql = news.cloudSql;
709 break;
710 case "cloudSpanner":
711 body.cloudSpanner = news.cloudSpanner;
712 break;
713 case "spark":
714 body.spark = news.spark ?? {};
715 break;
716 case "aws":
717 body.aws = news.aws;
718 break;
719 case "azure":
720 body.azure = news.azure;
721 break;
722 case "salesforceDataCloud":
723 body.salesforceDataCloud = news.salesforceDataCloud;
724 break;
725 case "configuration":
726 body.configuration = news.configuration;
727 break;
728 default:
729 body.cloudResource = {};
730 break;
731 }
732 return body;
733 };
734
735 const getByName = (name: string) =>
736 bigqueryconnection
737 .getProjectsLocationsConnections({ name })
738 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
739
740 const listOwnedAt = (parent: string, project: string) =>
741 bigqueryconnection.listProjectsLocationsConnections
742 .pages({
743 parent,
744 pageSize: 1000,
745 })
746 .pipe(
747 Stream.flatMap((page) => Stream.fromIterable(page.connections ?? [])),
748 Stream.filter((connection) => hasOwnershipMarker(connection.description)),
749 Stream.map((connection) => toAttrs(connection, project)),
750 Stream.runCollect,
751 Effect.map((chunk) => Array.from(chunk)),
752 Effect.catchTag("NotFound", () => Effect.succeed([])),
753 );
754
755 export const ConnectionProvider = () =>
756 Provider.succeed(Connection, {
757 stables: ["name", "connectionId", "project", "location", "kind", "kmsKeyName", "creationTime"],
758
759 diff: Effect.fn(function* ({ news, olds, output }) {
760 const env = yield* GcpEnvironment.current;
761 if (!isResolved(news)) return undefined;
762
763 const previousId = olds?.connectionId ?? output?.connectionId;
764 const nextId = news.connectionId ?? previousId;
765 const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId;
766
767 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
768 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
769 const locationChanged = previousLocation !== nextLocation;
770
771 const previousKms = olds?.kmsKeyName ?? output?.kmsKeyName ?? "";
772 const nextKms = news.kmsKeyName ?? previousKms;
773 const kmsChanged = nextKms !== previousKms;
774
775 const previousKind =
776 specifiedKind(olds ?? {}) ?? specifiedKind(output ?? {}) ?? "cloudResource";
777 const nextKind = specifiedKind(news) ?? previousKind;
778 const kindChanged = previousKind !== nextKind;
779
780 const previousConnector =
781 olds?.configuration?.connectorId ?? output?.configuration?.connectorId;
782 const nextConnector = news.configuration?.connectorId ?? previousConnector;
783 const connectorChanged =
784 previousConnector !== undefined &&
785 nextConnector !== undefined &&
786 nextConnector !== previousConnector;
787
788 if (!idChanged && !locationChanged && !kmsChanged && !kindChanged && !connectorChanged) {
789 return undefined;
790 }
791 return {
792 action: "replace" as const,
793 deleteFirst: !idChanged && !locationChanged,
794 };
795 }),
796
797 read: Effect.fn(function* ({ id, olds, output }) {
798 const env = yield* GcpEnvironment.current;
799 const connectionId = yield* toId(id, olds?.connectionId, output?.connectionId);
800 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
801 const name = output?.name ?? resourceName(env.project, location, connectionId);
802 const existing = yield* getByName(name);
803 if (existing === undefined) return undefined;
804 const attrs = toAttrs(existing, env.project);
805 return (yield* hasAlchemyLabels(id, parseDescription(existing.description).labels))
806 ? attrs
807 : Unowned(attrs);
808 }),
809
810 list: () =>
811 Effect.gen(function* () {
812 const env = yield* GcpEnvironment.current;
813 const wildcard = yield* listOwnedAt(parentOf(env.project, "-"), env.project);
814 if (wildcard.length > 0) return wildcard;
815 const fallback = yield* Effect.forEach(
816 [...new Set(["us-central1", env.region, "US", "EU", "us-east1"])],
817 (location) => listOwnedAt(parentOf(env.project, location), env.project),
818 { concurrency: 4 },
819 );
820 const seen = new Set<string>();
821 return fallback.flat().filter((connection) => {
822 if (seen.has(connection.name)) return false;
823 seen.add(connection.name);
824 return true;
825 });
826 }),
827
828 reconcile: Effect.fn(function* ({ id, news, output }) {
829 const env = yield* GcpEnvironment.current;
830 const connectionId = yield* toId(id, news.connectionId, output?.connectionId);
831 const location = normalizeLocation(news.location ?? output?.location, env.region);
832 const name = resourceName(env.project, location, connectionId);
833 const parent = parentOf(env.project, location);
834 const ownership = yield* createInternalLabels(id);
835 const desiredDescription = encodeDescription(ownership, news.description);
836
837 let current = yield* getByName(output?.name ?? name);
838
839 if (current === undefined) {
840 const created = yield* bigqueryconnection
841 .createProjectsLocationsConnections({
842 parent,
843 connectionId,
844 body: toCreateBody(news, ownership),
845 })
846 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
847 current = created ?? undefined;
848 }
849
850 if (current === undefined) {
851 return yield* new ConnectionNotResolved({ resourceName: name });
852 }
853
854 const updateMask: string[] = [];
855 const body: bigqueryconnection.Connection = {};
856
857 if ((current.friendlyName ?? "") !== (news.friendlyName ?? "")) {
858 updateMask.push("friendlyName");
859 body.friendlyName = news.friendlyName;
860 }
861 if ((current.description ?? "") !== desiredDescription) {
862 updateMask.push("description");
863 body.description = desiredDescription;
864 }
865
866 const kind = kindOf(news);
867 if (kind === "cloudSql" && news.cloudSql !== undefined) {
868 const observedSql = {
869 instanceId: current.cloudSql?.instanceId,
870 type: current.cloudSql?.type,
871 database: current.cloudSql?.database,
872 };
873 const desiredSql = {
874 instanceId: news.cloudSql.instanceId,
875 type: news.cloudSql.type,
876 database: news.cloudSql.database,
877 };
878 if (!jsonEqual(observedSql, desiredSql)) {
879 updateMask.push("cloudSql");
880 body.cloudSql = news.cloudSql;
881 }
882 }
883 if (
884 kind === "cloudSpanner" &&
885 news.cloudSpanner !== undefined &&
886 !jsonEqual(
887 writableSpanner(toSpanner(current.cloudSpanner)),
888 writableSpanner(news.cloudSpanner),
889 )
890 ) {
891 updateMask.push("cloudSpanner");
892 body.cloudSpanner = news.cloudSpanner;
893 }
894 if (
895 kind === "spark" &&
896 !jsonEqual(writableSpark(toSpark(current.spark)), writableSpark(news.spark ?? {}))
897 ) {
898 updateMask.push("spark");
899 body.spark = news.spark ?? {};
900 }
901 if (
902 kind === "aws" &&
903 news.aws !== undefined &&
904 !jsonEqual(writableAws(toAws(current.aws)), writableAws(news.aws))
905 ) {
906 updateMask.push("aws");
907 body.aws = news.aws;
908 }
909 if (
910 kind === "azure" &&
911 news.azure !== undefined &&
912 !jsonEqual(writableAzure(toAzure(current.azure)), writableAzure(news.azure))
913 ) {
914 updateMask.push("azure");
915 body.azure = news.azure;
916 }
917 if (
918 kind === "salesforceDataCloud" &&
919 news.salesforceDataCloud !== undefined &&
920 !jsonEqual(
921 writableSalesforce(toSalesforce(current.salesforceDataCloud)),
922 writableSalesforce(news.salesforceDataCloud),
923 )
924 ) {
925 updateMask.push("salesforceDataCloud");
926 body.salesforceDataCloud = news.salesforceDataCloud;
927 }
928 if (
929 kind === "configuration" &&
930 news.configuration !== undefined &&
931 !jsonEqual(
932 writableConfiguration(toConfiguration(current.configuration)),
933 writableConfiguration(news.configuration),
934 )
935 ) {
936 updateMask.push("configuration");
937 body.configuration = news.configuration;
938 }
939
940 if (updateMask.length > 0) {
941 const patched = yield* bigqueryconnection
942 .patchProjectsLocationsConnections({
943 name,
944 updateMask: updateMask.join(","),
945 body,
946 })
947 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
948 if (patched === undefined) {
949 const created = yield* bigqueryconnection
950 .createProjectsLocationsConnections({
951 parent,
952 connectionId,
953 body: toCreateBody(news, ownership),
954 })
955 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
956 current = created ?? undefined;
957 } else {
958 current = patched;
959 }
960 }
961
962 if (current === undefined) {
963 return yield* new ConnectionNotResolved({ resourceName: name });
964 }
965
966 const latest = (yield* getByName(current.name ?? name)) ?? current;
967 return toAttrs(latest, env.project);
968 }),
969
970 delete: Effect.fn(function* ({ output }) {
971 yield* bigqueryconnection
972 .deleteProjectsLocationsConnections({ name: output.name })
973 .pipe(Effect.catchTag("NotFound", () => Effect.void));
974 }),
975 });