@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 635 lines 22.3 KB
Raw
1 import * as dm from "@distilled.cloud/gcp/datamigration_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 * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import {
14 fieldMask,
15 fingerprint,
16 hasAlchemyLabelMap,
17 locationParent,
18 normalizeLocation,
19 parseName,
20 replaceOnIdentity,
21 ResourceNotResolved,
22 toPhysicalId,
23 userLabels,
24 waitForOperation,
25 waitUntilExists,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type ConnectionProfileProvider = dm.ConnectionProfileProviderEnum | (string & {});
30 export type ConnectionProfileState = dm.ConnectionProfileStateEnum | (string & {});
31 export type ConnectionProfileRole = dm.ConnectionProfileRoleEnum | (string & {});
32 export type MySqlConnectionProfile = dm.MySqlConnectionProfile;
33 export type PostgreSqlConnectionProfile = dm.PostgreSqlConnectionProfile;
34 export type OracleConnectionProfile = dm.OracleConnectionProfile;
35 export type SqlServerConnectionProfile = dm.SqlServerConnectionProfile;
36 export type CloudSqlConnectionProfile = dm.CloudSqlConnectionProfile;
37 export type CloudSqlSettings = dm.CloudSqlSettings;
38 export type AlloyDbConnectionProfile = dm.AlloyDbConnectionProfile;
39 export type AlloyDbSettings = dm.AlloyDbSettings;
40 export type SslConfig = dm.SslConfig;
41 export type Status = dm.Status;
42
43 export type ConnectionProfileProps = {
44 /**
45 * Connection profile id (the `{connectionProfile}` segment of
46 * `projects/{project}/locations/{location}/connectionProfiles/{connectionProfile}`).
47 * If omitted, a unique RFC1035 name is generated. Immutable — changing it
48 * replaces the profile.
49 */
50 connectionProfileId?: string;
51 /**
52 * Region (`us-central1`, …). Immutable — changing it replaces the
53 * profile. `US-CENTRAL1` is accepted and normalized to `us-central1`.
54 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
55 */
56 location?: string;
57 /**
58 * User-friendly display name.
59 */
60 displayName?: string;
61 /**
62 * User labels. Alchemy ownership labels are merged in automatically.
63 */
64 labels?: Record<string, string>;
65 /**
66 * Database provider (`CLOUDSQL`, `RDS`, `AURORA`, `ALLOYDB`,
67 * `AZURE_DATABASE`).
68 */
69 provider?: ConnectionProfileProvider;
70 /**
71 * Connection profile role (`SOURCE` or `DESTINATION`).
72 */
73 role?: ConnectionProfileRole;
74 /**
75 * MySQL source/destination connection. Mutually exclusive with the other
76 * engine blocks. Switching engines replaces the profile.
77 */
78 mysql?: MySqlConnectionProfile;
79 /**
80 * PostgreSQL source/destination connection. Mutually exclusive with the
81 * other engine blocks. Switching engines replaces the profile.
82 */
83 postgresql?: PostgreSqlConnectionProfile;
84 /**
85 * Oracle source connection. Mutually exclusive with the other engine
86 * blocks. Switching engines replaces the profile.
87 */
88 oracle?: OracleConnectionProfile;
89 /**
90 * SQL Server source connection. Mutually exclusive with the other engine
91 * blocks. Switching engines replaces the profile.
92 */
93 sqlserver?: SqlServerConnectionProfile;
94 /**
95 * Cloud SQL destination. `settings` are immutable after create —
96 * changing them replaces the profile. Creating this engine provisions a
97 * Cloud SQL replica. Delete always force-deletes that replica.
98 */
99 cloudsql?: CloudSqlConnectionProfile;
100 /**
101 * AlloyDB destination. `clusterId` and `settings` are immutable —
102 * changing them replaces the profile.
103 */
104 alloydb?: AlloyDbConnectionProfile;
105 /**
106 * Oracle only: create without validating connectivity.
107 */
108 skipValidation?: boolean;
109 /**
110 * Oracle only: validate without creating resources.
111 */
112 validateOnly?: boolean;
113 };
114
115 export type ConnectionProfile = Resource<
116 "GCP.DataMigration.ConnectionProfile",
117 ConnectionProfileProps,
118 {
119 /** Full resource name. */
120 name: string;
121 /** Connection profile id (last path segment). */
122 connectionProfileId: string;
123 /** Project id. */
124 project: string;
125 /** Location id. */
126 location: string;
127 /** User-friendly display name. */
128 displayName: string | undefined;
129 /** User labels (Alchemy ownership labels stripped). */
130 labels: Record<string, string>;
131 /** Database provider. */
132 provider: string | undefined;
133 /** Source or destination role. */
134 role: string | undefined;
135 /** Server-reported state. */
136 state: string | undefined;
137 /** MySQL connection (password omitted). */
138 mysql: MySqlConnectionProfile | undefined;
139 /** PostgreSQL connection (password omitted). */
140 postgresql: PostgreSqlConnectionProfile | undefined;
141 /** Oracle connection (password omitted). */
142 oracle: OracleConnectionProfile | undefined;
143 /** SQL Server connection (password omitted). */
144 sqlserver: SqlServerConnectionProfile | undefined;
145 /** Cloud SQL destination (root password omitted). */
146 cloudsql: CloudSqlConnectionProfile | undefined;
147 /** AlloyDB destination (initial user password omitted). */
148 alloydb: AlloyDbConnectionProfile | undefined;
149 /** Failure details when `state` is `FAILED`. */
150 error: Status | undefined;
151 /** RFC3339 creation timestamp. */
152 createTime: string | undefined;
153 /** RFC3339 last-update timestamp. */
154 updateTime: string | undefined;
155 },
156 never,
157 Providers
158 >;
159
160 /**
161 * A Database Migration Service connection profile: credentials and
162 * connectivity for a source database, or settings used to provision a
163 * Cloud SQL / AlloyDB destination.
164 *
165 * `connectionProfileId`, `location`, and the engine kind
166 * (`mysql` / `postgresql` / `oracle` / `sqlserver` / `cloudsql` /
167 * `alloydb`) plus immutable Cloud SQL / AlloyDB settings are replacement
168 * triggers. Display name, labels, role, provider, and credential fields
169 * update in place. Passwords are input-only and never returned.
170 *
171 * ### Creating a Connection Profile
172 * **Example:** MySQL source
173 * ```typescript
174 * const source = yield* GCP.DataMigration.ConnectionProfile("MysqlSrc", {
175 * mysql: {
176 * host: "10.0.0.8",
177 * port: 3306,
178 * username: "dms",
179 * password: process.env.MYSQL_PASSWORD,
180 * },
181 * labels: { env: "test" },
182 * });
183 * ```
184 *
185 * **Example:** Cloud SQL destination
186 * ```typescript
187 * const dest = yield* GCP.DataMigration.ConnectionProfile("MysqlDest", {
188 * cloudsql: {
189 * settings: {
190 * sourceId: source.name,
191 * databaseVersion: "MYSQL_8_0",
192 * tier: "db-n1-standard-1",
193 * rootPassword: process.env.MYSQL_ROOT_PASSWORD,
194 * },
195 * },
196 * });
197 * ```
198 *
199 * @resource
200 * @category DataMigration
201 */
202 export const ConnectionProfile = Resource<ConnectionProfile>("GCP.DataMigration.ConnectionProfile");
203
204 const resourceName = (project: string, location: string, connectionProfileId: string) =>
205 `${locationParent(project, location)}/connectionProfiles/${connectionProfileId}`;
206
207 const kindOf = (value: {
208 mysql?: unknown;
209 postgresql?: unknown;
210 oracle?: unknown;
211 sqlserver?: unknown;
212 cloudsql?: unknown;
213 alloydb?: unknown;
214 }) =>
215 value.mysql
216 ? "mysql"
217 : value.postgresql
218 ? "postgresql"
219 : value.oracle
220 ? "oracle"
221 : value.sqlserver
222 ? "sqlserver"
223 : value.cloudsql
224 ? "cloudsql"
225 : value.alloydb
226 ? "alloydb"
227 : "";
228
229 const publicSsl = (ssl: SslConfig | undefined) =>
230 ssl === undefined
231 ? undefined
232 : {
233 type: ssl.type,
234 sslFlags: ssl.sslFlags,
235 };
236
237 const publicMysql = (mysql: MySqlConnectionProfile | undefined) =>
238 mysql === undefined
239 ? undefined
240 : {
241 host: mysql.host,
242 port: mysql.port,
243 username: mysql.username,
244 cloudSqlId: mysql.cloudSqlId,
245 passwordSet: mysql.passwordSet,
246 ssl: publicSsl(mysql.ssl),
247 };
248
249 const publicPostgresql = (postgresql: PostgreSqlConnectionProfile | undefined) =>
250 postgresql === undefined
251 ? undefined
252 : {
253 host: postgresql.host,
254 port: postgresql.port,
255 username: postgresql.username,
256 database: postgresql.database,
257 cloudSqlId: postgresql.cloudSqlId,
258 alloydbClusterId: postgresql.alloydbClusterId,
259 enableIamAuthentication: postgresql.enableIamAuthentication,
260 privateConnectivity: postgresql.privateConnectivity,
261 privateServiceConnectConnectivity: postgresql.privateServiceConnectConnectivity,
262 forwardSshConnectivity: postgresql.forwardSshConnectivity
263 ? {
264 hostname: postgresql.forwardSshConnectivity.hostname,
265 port: postgresql.forwardSshConnectivity.port,
266 username: postgresql.forwardSshConnectivity.username,
267 }
268 : undefined,
269 passwordSet: postgresql.passwordSet,
270 ssl: publicSsl(postgresql.ssl),
271 };
272
273 const publicOracle = (oracle: OracleConnectionProfile | undefined) =>
274 oracle === undefined
275 ? undefined
276 : {
277 host: oracle.host,
278 port: oracle.port,
279 username: oracle.username,
280 databaseService: oracle.databaseService,
281 privateConnectivity: oracle.privateConnectivity,
282 forwardSshConnectivity: oracle.forwardSshConnectivity
283 ? {
284 hostname: oracle.forwardSshConnectivity.hostname,
285 port: oracle.forwardSshConnectivity.port,
286 username: oracle.forwardSshConnectivity.username,
287 }
288 : undefined,
289 oracleAsmConfig: oracle.oracleAsmConfig
290 ? {
291 hostname: oracle.oracleAsmConfig.hostname,
292 port: oracle.oracleAsmConfig.port,
293 asmService: oracle.oracleAsmConfig.asmService,
294 username: oracle.oracleAsmConfig.username,
295 ssl: publicSsl(oracle.oracleAsmConfig.ssl),
296 }
297 : undefined,
298 passwordSet: oracle.passwordSet,
299 ssl: publicSsl(oracle.ssl),
300 };
301
302 const publicSqlserver = (sqlserver: SqlServerConnectionProfile | undefined) =>
303 sqlserver === undefined
304 ? undefined
305 : {
306 host: sqlserver.host,
307 port: sqlserver.port,
308 username: sqlserver.username,
309 database: sqlserver.database,
310 cloudSqlId: sqlserver.cloudSqlId,
311 cloudSqlProjectId: sqlserver.cloudSqlProjectId,
312 dbmPort: sqlserver.dbmPort,
313 backups: sqlserver.backups,
314 privateConnectivity: sqlserver.privateConnectivity,
315 privateServiceConnectConnectivity: sqlserver.privateServiceConnectConnectivity,
316 passwordSet: sqlserver.passwordSet,
317 ssl: publicSsl(sqlserver.ssl),
318 };
319
320 const publicCloudsql = (cloudsql: CloudSqlConnectionProfile | undefined) =>
321 cloudsql === undefined
322 ? undefined
323 : {
324 cloudSqlId: cloudsql.cloudSqlId,
325 publicIp: cloudsql.publicIp,
326 privateIp: cloudsql.privateIp,
327 additionalPublicIp: cloudsql.additionalPublicIp,
328 settings: cloudsql.settings
329 ? {
330 ...cloudsql.settings,
331 rootPassword: undefined,
332 }
333 : undefined,
334 };
335
336 const publicAlloydb = (alloydb: AlloyDbConnectionProfile | undefined) =>
337 alloydb === undefined
338 ? undefined
339 : {
340 clusterId: alloydb.clusterId,
341 settings: alloydb.settings
342 ? {
343 ...alloydb.settings,
344 initialUser: alloydb.settings.initialUser
345 ? {
346 user: alloydb.settings.initialUser.user,
347 passwordSet: alloydb.settings.initialUser.passwordSet,
348 }
349 : undefined,
350 }
351 : undefined,
352 };
353
354 const toAttrs = (profile: dm.ConnectionProfile, project: string) => {
355 const name = profile.name ?? "";
356 const parsed = parseName(name, "connectionProfiles");
357 return {
358 name,
359 connectionProfileId: parsed.id,
360 project: parsed.project || project,
361 location: parsed.location,
362 displayName: profile.displayName,
363 labels: userLabels(profile.labels),
364 provider: profile.provider,
365 role: profile.role,
366 state: profile.state,
367 mysql: publicMysql(profile.mysql),
368 postgresql: publicPostgresql(profile.postgresql),
369 oracle: publicOracle(profile.oracle),
370 sqlserver: publicSqlserver(profile.sqlserver),
371 cloudsql: publicCloudsql(profile.cloudsql),
372 alloydb: publicAlloydb(profile.alloydb),
373 error: profile.error,
374 createTime: profile.createTime,
375 updateTime: profile.updateTime,
376 };
377 };
378
379 const getByName = (name: string) =>
380 name.length === 0
381 ? Effect.undefined
382 : dm
383 .getProjectsLocationsConnectionProfiles({ name })
384 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
385
386 const listOwned = (project: string, region: string) =>
387 dm.listProjectsLocationsConnectionProfiles
388 .pages({
389 parent: `projects/${project}/locations/-`,
390 pageSize: 1000,
391 })
392 .pipe(
393 Stream.flatMap((page) => Stream.fromIterable(page.connectionProfiles ?? [])),
394 Stream.filter((item) => hasAlchemyLabelMap(item.labels)),
395 Stream.runCollect,
396 Effect.map((chunk) => Array.from(chunk)),
397 Effect.catchTag("NotFound", () =>
398 dm.listProjectsLocationsConnectionProfiles
399 .pages({
400 parent: locationParent(project, region),
401 pageSize: 1000,
402 })
403 .pipe(
404 Stream.flatMap((page) => Stream.fromIterable(page.connectionProfiles ?? [])),
405 Stream.filter((item) => hasAlchemyLabelMap(item.labels)),
406 Stream.runCollect,
407 Effect.map((chunk) => Array.from(chunk)),
408 Effect.catchTag("NotFound", () => Effect.succeed([] as dm.ConnectionProfile[])),
409 ),
410 ),
411 );
412
413 const secretChanged = (newsHasSecret: boolean, publicPrevious: unknown, publicNext: unknown) =>
414 newsHasSecret || fingerprint(publicPrevious) !== fingerprint(publicNext);
415
416 export const ConnectionProfileProvider = () =>
417 Provider.succeed(ConnectionProfile, {
418 stables: ["name", "connectionProfileId", "project", "location", "createTime"],
419
420 diff: Effect.fn(function* ({ news, olds, output }) {
421 if (!isResolved(news)) return undefined;
422 const env = yield* GcpEnvironment.current;
423 const previousKind = kindOf({
424 mysql: olds?.mysql ?? output?.mysql,
425 postgresql: olds?.postgresql ?? output?.postgresql,
426 oracle: olds?.oracle ?? output?.oracle,
427 sqlserver: olds?.sqlserver ?? output?.sqlserver,
428 cloudsql: olds?.cloudsql ?? output?.cloudsql,
429 alloydb: olds?.alloydb ?? output?.alloydb,
430 });
431 const nextKind = kindOf(news) || previousKind;
432 const previousCloudsql = fingerprint(
433 publicCloudsql(olds?.cloudsql ?? output?.cloudsql)?.settings,
434 );
435 const nextCloudsql = fingerprint(publicCloudsql(news.cloudsql)?.settings);
436 const previousAlloy = fingerprint({
437 clusterId: olds?.alloydb?.clusterId ?? output?.alloydb?.clusterId,
438 settings: publicAlloydb(olds?.alloydb ?? output?.alloydb)?.settings,
439 });
440 const nextAlloy = fingerprint({
441 clusterId: news.alloydb?.clusterId,
442 settings: publicAlloydb(news.alloydb)?.settings,
443 });
444 return replaceOnIdentity({
445 previousId: olds?.connectionProfileId ?? output?.connectionProfileId,
446 nextId:
447 news.connectionProfileId ?? olds?.connectionProfileId ?? output?.connectionProfileId,
448 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
449 nextLocation: normalizeLocation(
450 news.location ?? olds?.location ?? output?.location,
451 env.region,
452 ),
453 extra:
454 previousKind !== nextKind ||
455 (nextKind === "cloudsql" &&
456 news.cloudsql !== undefined &&
457 previousCloudsql !== nextCloudsql) ||
458 (nextKind === "alloydb" && news.alloydb !== undefined && previousAlloy !== nextAlloy),
459 });
460 }),
461
462 read: Effect.fn(function* ({ id, olds, output }) {
463 const env = yield* GcpEnvironment.current;
464 const connectionProfileId = yield* toPhysicalId(
465 id,
466 olds?.connectionProfileId,
467 output?.connectionProfileId,
468 "profile",
469 );
470 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
471 const name = output?.name ?? resourceName(env.project, location, connectionProfileId);
472 const existing = yield* getByName(name);
473 if (existing === undefined) return undefined;
474 const attrs = toAttrs(existing, env.project);
475 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
476 }),
477
478 list: () =>
479 Effect.gen(function* () {
480 const env = yield* GcpEnvironment.current;
481 const items = yield* listOwned(env.project, env.region);
482 return items.map((item) => toAttrs(item, env.project));
483 }),
484
485 reconcile: Effect.fn(function* ({ id, news, output }) {
486 const env = yield* GcpEnvironment.current;
487 const connectionProfileId = yield* toPhysicalId(
488 id,
489 news.connectionProfileId,
490 output?.connectionProfileId,
491 "profile",
492 );
493 const location = normalizeLocation(news.location ?? output?.location, env.region);
494 const name = resourceName(env.project, location, connectionProfileId);
495 const desiredLabels = {
496 ...toLabels(news.labels),
497 ...(yield* createInternalLabels(id)),
498 };
499 const displayName = news.displayName ?? connectionProfileId;
500 const body: dm.ConnectionProfile = {
501 displayName,
502 labels: desiredLabels,
503 provider: news.provider,
504 role: news.role,
505 mysql: news.mysql,
506 postgresql: news.postgresql,
507 oracle: news.oracle,
508 sqlserver: news.sqlserver,
509 cloudsql: news.cloudsql,
510 alloydb: news.alloydb,
511 };
512
513 let current = yield* getByName(output?.name ?? name);
514
515 if (current === undefined) {
516 const created = yield* dm
517 .createProjectsLocationsConnectionProfiles({
518 parent: locationParent(env.project, location),
519 connectionProfileId,
520 skipValidation: news.skipValidation,
521 validateOnly: news.validateOnly,
522 body,
523 })
524 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
525 if (created !== undefined) {
526 yield* waitForOperation(created);
527 }
528 current = yield* waitUntilExists(getByName(name), name);
529 }
530
531 if (current === undefined) {
532 return yield* new ResourceNotResolved({ resourceName: name });
533 }
534
535 const observedLabels = tagRecord(current.labels);
536 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
537 const labelsChanged = upsert.length > 0 || removed.length > 0;
538 const displayNameChanged = (current.displayName ?? "") !== displayName;
539 const providerChanged =
540 news.provider !== undefined && (current.provider ?? "") !== news.provider;
541 const roleChanged = news.role !== undefined && (current.role ?? "") !== news.role;
542 const mysqlChanged =
543 news.mysql !== undefined &&
544 secretChanged(
545 news.mysql.password !== undefined ||
546 news.mysql.ssl?.caCertificate !== undefined ||
547 news.mysql.ssl?.clientCertificate !== undefined ||
548 news.mysql.ssl?.clientKey !== undefined,
549 publicMysql(current.mysql),
550 publicMysql(news.mysql),
551 );
552 const postgresqlChanged =
553 news.postgresql !== undefined &&
554 secretChanged(
555 news.postgresql.password !== undefined ||
556 news.postgresql.ssl?.caCertificate !== undefined ||
557 news.postgresql.ssl?.clientCertificate !== undefined ||
558 news.postgresql.ssl?.clientKey !== undefined ||
559 news.postgresql.forwardSshConnectivity?.password !== undefined ||
560 news.postgresql.forwardSshConnectivity?.privateKey !== undefined,
561 publicPostgresql(current.postgresql),
562 publicPostgresql(news.postgresql),
563 );
564 const oracleChanged =
565 news.oracle !== undefined &&
566 secretChanged(
567 news.oracle.password !== undefined ||
568 news.oracle.ssl?.caCertificate !== undefined ||
569 news.oracle.oracleAsmConfig?.password !== undefined,
570 publicOracle(current.oracle),
571 publicOracle(news.oracle),
572 );
573 const sqlserverChanged =
574 news.sqlserver !== undefined &&
575 secretChanged(
576 news.sqlserver.password !== undefined || news.sqlserver.ssl?.caCertificate !== undefined,
577 publicSqlserver(current.sqlserver),
578 publicSqlserver(news.sqlserver),
579 );
580 const mask = fieldMask([
581 labelsChanged && "labels",
582 displayNameChanged && "displayName",
583 providerChanged && "provider",
584 roleChanged && "role",
585 mysqlChanged && "mysql",
586 postgresqlChanged && "postgresql",
587 oracleChanged && "oracle",
588 sqlserverChanged && "sqlserver",
589 ]);
590
591 if (mask.length > 0) {
592 const operation = yield* dm.patchProjectsLocationsConnectionProfiles({
593 name: current.name ?? name,
594 updateMask: mask,
595 skipValidation: news.skipValidation,
596 validateOnly: news.validateOnly,
597 body: {
598 name: current.name ?? name,
599 displayName,
600 labels: desiredLabels,
601 provider: news.provider,
602 role: news.role,
603 mysql: news.mysql,
604 postgresql: news.postgresql,
605 oracle: news.oracle,
606 sqlserver: news.sqlserver,
607 },
608 });
609 yield* waitForOperation(operation);
610 current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name);
611 }
612
613 return toAttrs(current, env.project);
614 }),
615
616 delete: Effect.fn(function* ({ output }) {
617 const operation = yield* dm
618 .deleteProjectsLocationsConnectionProfiles({
619 name: output.name,
620 force: true,
621 })
622 .pipe(
623 Effect.retry({
624 while: (error) => error._tag === "Conflict",
625 times: 8,
626 schedule: Schedule.spaced("2 seconds"),
627 }),
628 Effect.catchTag("NotFound", () => Effect.undefined),
629 );
630 if (operation !== undefined) {
631 yield* waitForOperation(operation, { notFoundOk: true });
632 }
633 yield* waitUntilGone(getByName(output.name), output.name);
634 }),
635 });