@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 253 lines 8.93 KB
Raw
1 import * as athena from "@distilled.cloud/aws/athena";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import { createInternalTags, diffTags } from "alchemy/Tags";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import { AWSEnvironment, type AccountID } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import type { RegionID } from "../Region.ts";
11
12 export type DataCatalogArn = `arn:aws:athena:${RegionID}:${AccountID}:datacatalog/${string}`;
13
14 export interface DataCatalogProps {
15 /**
16 * Name of the data catalog. This is the catalog's identity — changing it
17 * replaces the resource. Up to 256 characters.
18 */
19 name: string;
20 /**
21 * Catalog type. Changing this replaces the resource.
22 * - `LAMBDA` — a federated connector backed by a Lambda metadata function.
23 * - `GLUE` — an AWS Glue Data Catalog (typically in another account).
24 * - `HIVE` — an external Apache Hive metastore via a Lambda function.
25 * - `FEDERATED` — a managed federated connector.
26 */
27 type: "LAMBDA" | "GLUE" | "HIVE" | "FEDERATED";
28 /**
29 * Optional description.
30 */
31 description?: string;
32 /**
33 * Type-specific parameters. For `LAMBDA`/`HIVE` this carries the
34 * `metadata-function` / `record-function` (or `function`) ARNs; for `GLUE`
35 * the `catalog-id`.
36 */
37 parameters?: Record<string, string>;
38 /**
39 * User-defined tags to apply to the data catalog.
40 */
41 tags?: Record<string, string>;
42 }
43
44 export interface DataCatalog extends Resource<
45 "AWS.Athena.DataCatalog",
46 DataCatalogProps,
47 {
48 /**
49 * Name of the data catalog.
50 */
51 name: string;
52 /**
53 * ARN of the data catalog.
54 */
55 dataCatalogArn: DataCatalogArn;
56 /**
57 * Catalog type (`LAMBDA`, `GLUE`, or `HIVE`).
58 */
59 type: string;
60 /**
61 * Type-specific connection parameters.
62 */
63 parameters: Record<string, string>;
64 /**
65 * Description of the data catalog.
66 */
67 description: string | undefined;
68 /**
69 * Tags on the data catalog.
70 */
71 tags: Record<string, string>;
72 },
73 never,
74 Providers
75 > {}
76
77 /**
78 * An Amazon Athena data catalog — registers an external metadata source (a
79 * federated Lambda connector, an external Hive metastore, or a cross-account
80 * Glue Data Catalog) that Athena queries can reference as a `catalog`.
81 *
82 * ### Registering Catalogs
83 * **Example:** A federated Lambda-backed catalog
84 * ```typescript
85 * const catalog = yield* AWS.Athena.DataCatalog("Cmdb", {
86 * name: "cmdb_connector",
87 * type: "LAMBDA",
88 * parameters: {
89 * "metadata-function": connector.functionArn,
90 * "record-function": connector.functionArn,
91 * },
92 * });
93 * ```
94 *
95 * @resource
96 */
97 export const DataCatalog = Resource<DataCatalog>("AWS.Athena.DataCatalog");
98
99 const observedTagsOf = (tags: readonly athena.Tag[] | undefined) =>
100 Object.fromEntries(
101 (tags ?? []).flatMap((t) =>
102 t.Key !== undefined && t.Value !== undefined ? [[t.Key, t.Value]] : [],
103 ),
104 );
105
106 const paramsOf = (params: { [key: string]: string | undefined } | undefined) =>
107 Object.fromEntries(
108 Object.entries(params ?? {}).flatMap((e) => (typeof e[1] === "string" ? [[e[0], e[1]]] : [])),
109 ) as Record<string, string>;
110
111 export const DataCatalogProvider = () =>
112 Provider.effect(
113 DataCatalog,
114 Effect.gen(function* () {
115 const getOne = (name: string) =>
116 // GetDataCatalog resolves catalogs through a workgroup — without one it
117 // reports even existing catalogs as not-found, so scope to `primary`.
118 athena.getDataCatalog({ Name: name, WorkGroup: "primary" }).pipe(
119 Effect.map((res) => res.DataCatalog),
120 Effect.catchTag("DataCatalogNotFound", () => Effect.undefined),
121 );
122
123 const fetchTags = (arn: string) =>
124 athena.listTagsForResource.items({ ResourceARN: arn }).pipe(
125 Stream.runCollect,
126 Effect.map((chunk) => observedTagsOf(Array.from(chunk))),
127 Effect.orElseSucceed(() => ({}) as Record<string, string>),
128 );
129
130 return {
131 stables: ["name", "dataCatalogArn"],
132 diff: Effect.fn(function* ({ olds, news }) {
133 if (!isResolved(news)) return;
134 if ((olds?.name ?? undefined) !== news.name) {
135 return { action: "replace" } as const;
136 }
137 if ((olds?.type ?? undefined) !== news.type) {
138 return { action: "replace" } as const;
139 }
140 }),
141 read: Effect.fn(function* ({ olds, output }) {
142 const { accountId, region } = yield* AWSEnvironment.current;
143 const name = output?.name ?? olds?.name;
144 if (!name) return undefined;
145 const dc = yield* getOne(name);
146 if (!dc) return undefined;
147 const arn = `arn:aws:athena:${region}:${accountId}:datacatalog/${name}` as DataCatalogArn;
148 return {
149 name,
150 dataCatalogArn: arn,
151 type: dc.Type,
152 parameters: paramsOf(dc.Parameters),
153 description: dc.Description,
154 tags: yield* fetchTags(arn),
155 };
156 }),
157 list: () =>
158 Effect.gen(function* () {
159 const { accountId, region } = yield* AWSEnvironment.current;
160 const pages = yield* athena.listDataCatalogs.pages({}).pipe(Stream.runCollect);
161 return Array.from(pages)
162 .flatMap((page) => page.DataCatalogsSummary ?? [])
163 .flatMap((dc) =>
164 dc.CatalogName
165 ? [
166 {
167 name: dc.CatalogName,
168 dataCatalogArn:
169 `arn:aws:athena:${region}:${accountId}:datacatalog/${dc.CatalogName}` as DataCatalogArn,
170 type: dc.Type ?? "LAMBDA",
171 parameters: {} as Record<string, string>,
172 description: undefined,
173 tags: {} as Record<string, string>,
174 },
175 ]
176 : [],
177 );
178 }),
179 reconcile: Effect.fn(function* ({ id, news, output, session }) {
180 const { accountId, region } = yield* AWSEnvironment.current;
181 const name = news.name ?? output?.name!;
182 const arn = `arn:aws:athena:${region}:${accountId}:datacatalog/${name}` as DataCatalogArn;
183 const internalTags = yield* createInternalTags(id);
184 const desiredTags = { ...internalTags, ...news.tags };
185 const desiredParams = news.parameters ?? {};
186
187 // Observe — cloud state is authoritative.
188 let dc = yield* getOne(name);
189
190 // Ensure — create if missing.
191 if (!dc) {
192 yield* athena.createDataCatalog({
193 Name: name,
194 Type: news.type,
195 Description: news.description,
196 Parameters: desiredParams,
197 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
198 Key,
199 Value,
200 })),
201 });
202 dc = yield* getOne(name);
203 } else {
204 // Sync — AWS derives extra parameter keys (e.g. `catalog`) beyond
205 // what we sent, so only update when a DESIRED key is missing or
206 // differs from the observed value (ignore AWS-added keys).
207 const observedParams = paramsOf(dc.Parameters);
208 const paramsDrift = Object.entries(desiredParams).some(
209 ([k, v]) => observedParams[k] !== v,
210 );
211 const descDrift = news.description !== undefined && dc.Description !== news.description;
212 if (paramsDrift || descDrift) {
213 yield* athena.updateDataCatalog({
214 Name: name,
215 Type: news.type,
216 Description: news.description,
217 Parameters: desiredParams,
218 });
219 }
220 }
221
222 // Sync tags — diff against OBSERVED cloud tags.
223 const observed = yield* fetchTags(arn);
224 const { upsert, removed } = diffTags(observed, desiredTags);
225 if (upsert.length > 0) {
226 yield* athena.tagResource({
227 ResourceARN: arn,
228 Tags: upsert.map((t) => ({ Key: t.Key, Value: t.Value })),
229 });
230 }
231 if (removed.length > 0) {
232 yield* athena.untagResource({ ResourceARN: arn, TagKeys: removed });
233 }
234
235 const final = (yield* getOne(name)) ?? dc;
236 yield* session.note(arn);
237 return {
238 name,
239 dataCatalogArn: arn,
240 type: final?.Type ?? news.type,
241 parameters: paramsOf(final?.Parameters),
242 description: final?.Description ?? news.description,
243 tags: desiredTags,
244 };
245 }),
246 delete: Effect.fn(function* ({ output }) {
247 yield* athena
248 .deleteDataCatalog({ Name: output.name })
249 .pipe(Effect.catchTag("DataCatalogNotFound", () => Effect.void));
250 }),
251 };
252 }),
253 );