@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 262 lines 9.07 KB
Raw
1 import * as glue from "@distilled.cloud/aws/glue";
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, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Stream from "effect/Stream";
11 import { AWSEnvironment } from "../Environment.ts";
12 import type { Providers } from "../Providers.ts";
13 import { connectionArn, fetchObservedTags, syncTags } from "./internal.ts";
14
15 export type GlueConnectionType =
16 | "JDBC"
17 | "SFTP"
18 | "MONGODB"
19 | "KAFKA"
20 | "NETWORK"
21 | "MARKETPLACE"
22 | "CUSTOM"
23 | (string & {});
24
25 export interface ConnectionProps {
26 /**
27 * Name of the connection. If omitted, a unique name is generated. Changing
28 * the name replaces the connection.
29 * @default a generated physical name
30 */
31 connectionName?: string;
32 /**
33 * The type of connection. `JDBC` is the common relational case.
34 */
35 connectionType: GlueConnectionType;
36 /**
37 * A description of the connection.
38 */
39 description?: string;
40 /**
41 * Connection-type-specific properties, e.g. for `JDBC`:
42 * `{ JDBC_CONNECTION_URL, USERNAME, PASSWORD }`. Secret values (e.g.
43 * `PASSWORD`) should be wrapped with `Redacted.make(...)` so they are
44 * kept out of logs and state output.
45 */
46 connectionProperties: Record<string, string | Redacted.Redacted<string>>;
47 /**
48 * Criteria used to match connections (for MATCH_CRITERIA lookups).
49 */
50 matchCriteria?: string[];
51 /**
52 * VPC networking requirements — required for connections that reach into a
53 * VPC (e.g. JDBC to an RDS instance).
54 */
55 physicalConnectionRequirements?: {
56 /** The subnet the connection uses. */
57 subnetId?: string;
58 /** Security group IDs applied to the connection's ENI. */
59 securityGroupIdList?: string[];
60 /** The availability zone (must match the subnet). */
61 availabilityZone?: string;
62 };
63 /**
64 * The AWS account ID of the Data Catalog. Changing it replaces the
65 * connection.
66 * @default the caller's account (the default Data Catalog)
67 */
68 catalogId?: string;
69 /**
70 * Tags to apply to the connection. Merged with internal Alchemy tags.
71 */
72 tags?: Record<string, string>;
73 }
74
75 export interface Connection extends Resource<
76 "AWS.Glue.Connection",
77 ConnectionProps,
78 {
79 /** The name of the connection. */
80 connectionName: string;
81 /** The ARN of the connection. */
82 connectionArn: string;
83 /** The connection type, e.g. `JDBC`. */
84 connectionType: string;
85 /** The AWS account ID of the Data Catalog the connection lives in. */
86 catalogId: string;
87 },
88 {},
89 Providers
90 > {}
91
92 /**
93 * An AWS Glue connection — stores the connection details (JDBC URL, VPC
94 * networking, credentials) that crawlers and jobs use to reach a data store.
95 * ### Creating Connections
96 * **Example:** JDBC Connection
97 * ```typescript
98 * import * as AWS from "@alchemy.run/aws";
99 * import * as Redacted from "effect/Redacted";
100 *
101 * const connection = yield* AWS.Glue.Connection("Warehouse", {
102 * connectionType: "JDBC",
103 * connectionProperties: {
104 * JDBC_CONNECTION_URL: "jdbc:postgresql://db.example.com:5432/warehouse",
105 * USERNAME: "glue",
106 * PASSWORD: Redacted.make("secret"),
107 * },
108 * physicalConnectionRequirements: {
109 * subnetId: subnet.subnetId,
110 * securityGroupIdList: [securityGroup.groupId],
111 * availabilityZone: "us-west-2a",
112 * },
113 * });
114 * ```
115 *
116 * @resource
117 */
118 export const Connection = Resource<Connection>("AWS.Glue.Connection");
119
120 export const ConnectionProvider = () =>
121 Provider.effect(
122 Connection,
123 Effect.gen(function* () {
124 const createName = Effect.fn(function* (
125 id: string,
126 props: { connectionName?: string | undefined },
127 ) {
128 return props.connectionName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
129 });
130
131 const observe = Effect.fn(function* (name: string, catalogId: string | undefined) {
132 return yield* glue
133 .getConnection({
134 Name: name,
135 CatalogId: catalogId,
136 HidePassword: true,
137 })
138 .pipe(
139 Effect.map((r) => r.Connection),
140 Effect.catchTag("EntityNotFoundException", () => Effect.undefined),
141 );
142 });
143
144 const buildInput = (name: string, props: ConnectionProps) => ({
145 Name: name,
146 ConnectionType: props.connectionType,
147 Description: props.description,
148 ConnectionProperties: Object.fromEntries(
149 Object.entries(props.connectionProperties).map(([key, value]) => [
150 key,
151 Redacted.isRedacted(value) ? Redacted.value(value) : value,
152 ]),
153 ),
154 MatchCriteria: props.matchCriteria,
155 PhysicalConnectionRequirements: props.physicalConnectionRequirements
156 ? {
157 SubnetId: props.physicalConnectionRequirements.subnetId,
158 SecurityGroupIdList: props.physicalConnectionRequirements.securityGroupIdList,
159 AvailabilityZone: props.physicalConnectionRequirements.availabilityZone,
160 }
161 : undefined,
162 });
163
164 return Connection.Provider.of({
165 stables: ["connectionName", "connectionArn", "catalogId"],
166
167 list: () =>
168 Effect.gen(function* () {
169 const { accountId, region } = yield* AWSEnvironment.current;
170 const pages = yield* glue.getConnections.pages({}).pipe(Stream.runCollect);
171 return Array.from(pages)
172 .flatMap((page) => page.ConnectionList ?? [])
173 .filter((c) => c.Name !== undefined)
174 .map((c) => ({
175 connectionName: c.Name!,
176 connectionArn: connectionArn(region, accountId, c.Name!),
177 connectionType: c.ConnectionType ?? "",
178 catalogId: accountId,
179 }));
180 }),
181
182 read: Effect.fn(function* ({ id, olds, output }) {
183 const { accountId, region } = yield* AWSEnvironment.current;
184 const catalogId = output?.catalogId ?? olds?.catalogId ?? accountId;
185 const name = output?.connectionName ?? (yield* createName(id, olds ?? {}));
186 const connection = yield* observe(name, catalogId);
187 if (connection?.Name === undefined) return undefined;
188 const arn = connectionArn(region, accountId, connection.Name);
189 const attrs = {
190 connectionName: connection.Name,
191 connectionArn: arn,
192 connectionType: connection.ConnectionType ?? "",
193 catalogId,
194 };
195 const tags = yield* fetchObservedTags(arn);
196 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
197 }),
198
199 diff: Effect.fn(function* ({ id, news, olds }) {
200 if (!isResolved(news)) return undefined;
201 const oldName = yield* createName(id, olds);
202 const newName = yield* createName(id, news);
203 if (oldName !== newName) return { action: "replace" } as const;
204 if ((olds.catalogId ?? undefined) !== (news.catalogId ?? undefined)) {
205 return { action: "replace" } as const;
206 }
207 // type / properties / networking / description → update
208 }),
209
210 reconcile: Effect.fn(function* ({ id, news, output, session }) {
211 const { accountId, region } = yield* AWSEnvironment.current;
212 const catalogId = news.catalogId ?? output?.catalogId ?? accountId;
213 const name = output?.connectionName ?? (yield* createName(id, news));
214 const internalTags = yield* createInternalTags(id);
215 const desiredTags = { ...news.tags, ...internalTags };
216 const arn = connectionArn(region, accountId, name);
217 const input = buildInput(name, news);
218
219 // 1. OBSERVE
220 const existing = yield* observe(name, catalogId);
221
222 // 2. ENSURE / 3. SYNC
223 if (existing === undefined) {
224 yield* glue
225 .createConnection({
226 CatalogId: catalogId,
227 ConnectionInput: input,
228 Tags: desiredTags,
229 })
230 .pipe(Effect.catchTag("AlreadyExistsException", () => Effect.void));
231 } else {
232 yield* glue.updateConnection({
233 CatalogId: catalogId,
234 Name: name,
235 ConnectionInput: input,
236 });
237 }
238
239 // 3b. SYNC TAGS
240 const observedTags = yield* fetchObservedTags(arn);
241 yield* syncTags(arn, observedTags, desiredTags);
242
243 yield* session.note(name);
244 return {
245 connectionName: name,
246 connectionArn: arn,
247 connectionType: news.connectionType,
248 catalogId,
249 };
250 }),
251
252 delete: Effect.fn(function* ({ output }) {
253 yield* glue
254 .deleteConnection({
255 ConnectionName: output.connectionName,
256 CatalogId: output.catalogId,
257 })
258 .pipe(Effect.catchTag("EntityNotFoundException", () => Effect.void));
259 }),
260 });
261 }),
262 );