@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, 06:00 UTC

main
ts 377 lines 13.3 KB
Raw
1 import * as dms from "@distilled.cloud/aws/database-migration-service";
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 { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Redacted from "effect/Redacted";
11 import * as Stream from "effect/Stream";
12 import type { Providers } from "../Providers.ts";
13
14 export class EndpointUnexpectedResponse extends Data.TaggedError("EndpointUnexpectedResponse")<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 /**
20 * Direction of a DMS endpoint relative to the replication:
21 * `source` reads from the database, `target` writes to it.
22 */
23 export type EndpointType = "source" | "target";
24
25 export interface EndpointProps {
26 /**
27 * Database-migration endpoint identifier. Must be lowercase, 1-255
28 * characters, begin with a letter, and contain only letters, digits, and
29 * hyphens (no two consecutive hyphens, no trailing hyphen). If omitted, a
30 * deterministic physical name is generated. Changing it replaces the
31 * endpoint.
32 */
33 endpointIdentifier?: string;
34 /**
35 * Whether this endpoint is the migration `source` or `target`.
36 */
37 endpointType: EndpointType;
38 /**
39 * Database engine, e.g. `"mysql"`, `"postgres"`, `"aurora"`, `"s3"`,
40 * `"kinesis"`, `"docdb"`. Determines which `*Settings` block applies.
41 */
42 engineName: string;
43 /**
44 * User name used to connect to the endpoint database.
45 */
46 username?: string;
47 /**
48 * Password used to connect to the endpoint database. Marked sensitive.
49 */
50 password?: Redacted.Redacted<string>;
51 /**
52 * Host name of the endpoint database server.
53 */
54 serverName?: string;
55 /**
56 * Port the endpoint database listens on.
57 */
58 port?: number;
59 /**
60 * Name of the endpoint database.
61 */
62 databaseName?: string;
63 /**
64 * Additional attributes accepted by DMS as a semicolon-separated string.
65 */
66 extraConnectionAttributes?: string;
67 /**
68 * Customer-managed KMS key for connection-secret encryption. Changing the
69 * key replaces the endpoint.
70 * @default AWS-owned DMS key
71 */
72 kmsKeyId?: string;
73 /**
74 * ARN of a certificate DMS uses for SSL connections to the endpoint.
75 */
76 certificateArn?: string;
77 /**
78 * SSL mode for the connection.
79 * @default "none"
80 */
81 sslMode?: dms.DmsSslModeValue;
82 /**
83 * IAM role ARN DMS assumes to access the endpoint (required for S3,
84 * DynamoDB, Kinesis, and other AWS-service targets).
85 */
86 serviceAccessRoleArn?: string;
87 /**
88 * Externally-defined table definition JSON (S3/DynamoDB targets).
89 */
90 externalTableDefinition?: string;
91 /** Settings for an Amazon S3 endpoint. */
92 s3Settings?: dms.S3Settings;
93 /** Settings for an Amazon DynamoDB target endpoint. */
94 dynamoDbSettings?: dms.DynamoDbSettings;
95 /** Settings for an Amazon Kinesis Data Streams target endpoint. */
96 kinesisSettings?: dms.KinesisSettings;
97 /** Settings for an Apache Kafka target endpoint. */
98 kafkaSettings?: dms.KafkaSettings;
99 /** Settings for an OpenSearch/Elasticsearch target endpoint. */
100 elasticsearchSettings?: dms.ElasticsearchSettings;
101 /** Settings for an Amazon Neptune target endpoint. */
102 neptuneSettings?: dms.NeptuneSettings;
103 /** Settings for an Amazon Redshift target endpoint. */
104 redshiftSettings?: dms.RedshiftSettings;
105 /** Settings for a PostgreSQL endpoint. */
106 postgreSQLSettings?: dms.PostgreSQLSettings;
107 /** Settings for a MySQL endpoint. */
108 mySQLSettings?: dms.MySQLSettings;
109 /** Settings for an Oracle endpoint. */
110 oracleSettings?: dms.OracleSettings;
111 /** Settings for a Microsoft SQL Server endpoint. */
112 microsoftSQLServerSettings?: dms.MicrosoftSQLServerSettings;
113 /** Settings for a MongoDB endpoint. */
114 mongoDbSettings?: dms.MongoDbSettings;
115 /** Settings for an Amazon DocumentDB endpoint. */
116 docDbSettings?: dms.DocDbSettings;
117 /** Settings for a Redis target endpoint. */
118 redisSettings?: dms.RedisSettings;
119 /**
120 * User-defined tags for the endpoint.
121 */
122 tags?: Record<string, string>;
123 }
124
125 export interface Endpoint extends Resource<
126 "AWS.DMS.Endpoint",
127 EndpointProps,
128 {
129 /** The endpoint identifier (unique per account/region). */
130 endpointIdentifier: string;
131 /** The ARN of the endpoint. */
132 endpointArn: string;
133 /** Whether the endpoint is a `source` or `target`. */
134 endpointType: string;
135 /** The database engine of the endpoint, e.g. `postgres`, `mysql`. */
136 engineName: string;
137 /** The current status of the endpoint, e.g. `active`. */
138 status: string | undefined;
139 /** The tags attached to the endpoint. */
140 tags: Record<string, string>;
141 },
142 never,
143 Providers
144 > {}
145
146 /**
147 * An AWS Database Migration Service (DMS) endpoint — the source or target
148 * database of a replication. Endpoints are metadata-only (they store
149 * connection information, not data), so they are free and fast to create.
150 * ### Creating Endpoints
151 * **Example:** MySQL Source Endpoint
152 * ```typescript
153 * const source = yield* Endpoint("Source", {
154 * endpointType: "source",
155 * engineName: "mysql",
156 * serverName: "source-db.example.com",
157 * port: 3306,
158 * username: "admin",
159 * password: Redacted.make("super-secret"),
160 * databaseName: "app",
161 * });
162 * ```
163 *
164 * **Example:** S3 Target Endpoint
165 * ```typescript
166 * const target = yield* Endpoint("Target", {
167 * endpointType: "target",
168 * engineName: "s3",
169 * serviceAccessRoleArn: role.roleArn,
170 * s3Settings: {
171 * BucketName: bucket.bucketName,
172 * ServiceAccessRoleArn: role.roleArn,
173 * },
174 * });
175 * ```
176 *
177 * @resource
178 */
179 export const Endpoint = Resource<Endpoint>("AWS.DMS.Endpoint");
180
181 const toTagRecord = (
182 tags: Array<{ Key?: string; Value?: string }> | undefined,
183 ): Record<string, string> =>
184 Object.fromEntries(
185 (tags ?? [])
186 .filter(
187 (tag): tag is { Key: string; Value: string } =>
188 typeof tag.Key === "string" && typeof tag.Value === "string",
189 )
190 .map((tag) => [tag.Key, tag.Value]),
191 );
192
193 export const EndpointProvider = () =>
194 Provider.effect(
195 Endpoint,
196 Effect.gen(function* () {
197 const toName = (id: string, props: EndpointProps) =>
198 props.endpointIdentifier
199 ? Effect.succeed(props.endpointIdentifier)
200 : createPhysicalName({ id, maxLength: 200, lowercase: true });
201
202 // DMS has no get-by-identifier; describeEndpoints with an `endpoint-id`
203 // filter returns the single matching endpoint (or ResourceNotFoundFault).
204 const findEndpoint = Effect.fn(function* (identifier: string) {
205 const response = yield* dms
206 .describeEndpoints({
207 Filters: [{ Name: "endpoint-id", Values: [identifier] }],
208 })
209 .pipe(Effect.catchTag("ResourceNotFoundFault", () => Effect.undefined));
210 return response?.Endpoints?.[0];
211 });
212
213 const readTags = Effect.fn(function* (arn: string) {
214 const response = yield* dms
215 .listTagsForResource({ ResourceArn: arn })
216 .pipe(Effect.orElseSucceed(() => undefined));
217 return toTagRecord(response?.TagList);
218 });
219
220 const toAttrs = Effect.fn(function* (endpoint: dms.Endpoint) {
221 if (!endpoint.EndpointIdentifier || !endpoint.EndpointArn) {
222 return yield* new EndpointUnexpectedResponse({
223 message: `DMS endpoint '${endpoint.EndpointIdentifier}' is missing its identifier or ARN`,
224 });
225 }
226 return {
227 endpointIdentifier: endpoint.EndpointIdentifier,
228 endpointArn: endpoint.EndpointArn,
229 endpointType: endpoint.EndpointType ?? "source",
230 engineName: endpoint.EngineName ?? "",
231 status: endpoint.Status,
232 tags: yield* readTags(endpoint.EndpointArn),
233 };
234 });
235
236 // Settings blocks and secret material shared by create + modify.
237 const buildSettings = (props: EndpointProps) => ({
238 Username: props.username,
239 Password: props.password,
240 ServerName: props.serverName,
241 Port: props.port,
242 DatabaseName: props.databaseName,
243 ExtraConnectionAttributes: props.extraConnectionAttributes,
244 CertificateArn: props.certificateArn,
245 SslMode: props.sslMode,
246 ServiceAccessRoleArn: props.serviceAccessRoleArn,
247 ExternalTableDefinition: props.externalTableDefinition,
248 S3Settings: props.s3Settings,
249 DynamoDbSettings: props.dynamoDbSettings,
250 KinesisSettings: props.kinesisSettings,
251 KafkaSettings: props.kafkaSettings,
252 ElasticsearchSettings: props.elasticsearchSettings,
253 NeptuneSettings: props.neptuneSettings,
254 RedshiftSettings: props.redshiftSettings,
255 PostgreSQLSettings: props.postgreSQLSettings,
256 MySQLSettings: props.mySQLSettings,
257 OracleSettings: props.oracleSettings,
258 MicrosoftSQLServerSettings: props.microsoftSQLServerSettings,
259 MongoDbSettings: props.mongoDbSettings,
260 DocDbSettings: props.docDbSettings,
261 RedisSettings: props.redisSettings,
262 });
263
264 return {
265 stables: ["endpointIdentifier", "endpointArn"],
266
267 diff: Effect.fn(function* ({ id, olds, news }) {
268 if (!isResolved(news)) return undefined;
269 if ((yield* toName(id, olds ?? ({} as EndpointProps))) !== (yield* toName(id, news))) {
270 return { action: "replace" } as const;
271 }
272 // KMS key is create-only.
273 if ((news.kmsKeyId ?? undefined) !== (olds?.kmsKeyId ?? undefined)) {
274 return { action: "replace" } as const;
275 }
276 }),
277
278 read: Effect.fn(function* ({ id, olds, output }) {
279 const name =
280 output?.endpointIdentifier ?? (yield* toName(id, olds ?? ({} as EndpointProps)));
281 const endpoint = yield* findEndpoint(name);
282 if (!endpoint?.EndpointArn) return undefined;
283 const attrs = yield* toAttrs(endpoint);
284 return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs);
285 }),
286
287 reconcile: Effect.fn(function* ({ id, news, output, session }) {
288 const name = output?.endpointIdentifier ?? (yield* toName(id, news));
289 const internalTags = yield* createInternalTags(id);
290 const desiredTags = { ...internalTags, ...news.tags };
291
292 // 1. Observe — cloud state is authoritative.
293 let observed = yield* findEndpoint(name);
294
295 // 2. Ensure — create if missing; tolerate AlreadyExists as a race.
296 if (observed === undefined) {
297 observed = yield* dms
298 .createEndpoint({
299 EndpointIdentifier: name,
300 EndpointType: news.endpointType,
301 EngineName: news.engineName,
302 KmsKeyId: news.kmsKeyId,
303 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
304 Key,
305 Value,
306 })),
307 ...buildSettings(news),
308 })
309 .pipe(
310 Effect.map((r) => r.Endpoint),
311 Effect.catchTag("ResourceAlreadyExistsFault", () => findEndpoint(name)),
312 );
313 } else {
314 // 3. Sync — the endpoint exists; push desired configuration. DMS
315 // modifyEndpoint is a full upsert of the connection settings,
316 // so send them all (cheap, no delta computation needed).
317 observed = yield* dms
318 .modifyEndpoint({
319 EndpointArn: observed.EndpointArn!,
320 EndpointIdentifier: name,
321 EndpointType: news.endpointType,
322 EngineName: news.engineName,
323 ...buildSettings(news),
324 })
325 .pipe(Effect.map((r) => r.Endpoint));
326 }
327
328 if (!observed?.EndpointArn) {
329 return yield* new EndpointUnexpectedResponse({
330 message: `DMS endpoint '${name}' has no ARN after reconcile`,
331 });
332 }
333
334 // 3b. Sync tags — diff against OBSERVED cloud tags.
335 const arn = observed.EndpointArn;
336 const observedTags = yield* readTags(arn);
337 const { upsert, removed } = diffTags(observedTags, desiredTags);
338 if (upsert.length > 0) {
339 yield* dms.addTagsToResource({ ResourceArn: arn, Tags: upsert });
340 }
341 if (removed.length > 0) {
342 yield* dms.removeTagsFromResource({
343 ResourceArn: arn,
344 TagKeys: removed,
345 });
346 }
347
348 yield* session.note(name);
349 return yield* toAttrs(observed);
350 }),
351
352 delete: Effect.fn(function* ({ output }) {
353 yield* dms
354 .deleteEndpoint({ EndpointArn: output.endpointArn })
355 .pipe(Effect.catchTag("ResourceNotFoundFault", () => Effect.void));
356 }),
357
358 list: () =>
359 dms.describeEndpoints.pages({}).pipe(
360 Stream.runCollect,
361 Effect.map((chunk) =>
362 Array.from(chunk).flatMap((page) =>
363 (page.Endpoints ?? []).filter(
364 (endpoint) =>
365 endpoint.EndpointIdentifier !== undefined && endpoint.EndpointArn !== undefined,
366 ),
367 ),
368 ),
369 Effect.flatMap(
370 Effect.forEach((endpoint) => toAttrs(endpoint), {
371 concurrency: 4,
372 }),
373 ),
374 ),
375 };
376 }),
377 );