@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 349 lines 12.8 KB
Raw
1 import * as appintegrations from "@distilled.cloud/aws/appintegrations";
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 Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import { definedTags } from "./internal.ts";
13
14 export interface DataIntegrationScheduleConfig {
15 /**
16 * The start date for objects to import, in ISO 8601 format,
17 * e.g. `2024-01-01T00:00:00Z`.
18 */
19 firstExecutionFrom?: string;
20 /**
21 * The name of the object to pull from the data source.
22 */
23 object?: string;
24 /**
25 * How often the data should be pulled from the data source, e.g.
26 * `rate(1 hours)`.
27 */
28 scheduleExpression: string;
29 }
30
31 export interface DataIntegrationProps {
32 /**
33 * Name of the data integration. If omitted, a unique name is generated
34 * from the app, stage, and logical ID. The name can be updated in place.
35 */
36 name?: string;
37 /**
38 * Description of the data integration (1-1000 characters).
39 */
40 description?: string;
41 /**
42 * The ARN of the KMS key used to encrypt the data integration. Changing
43 * the key replaces the data integration.
44 */
45 kmsKey: string;
46 /**
47 * The URI of the data source, e.g. `s3://my-bucket` or
48 * `Salesforce://AppFlow/my-connector-profile`. Changing the source URI
49 * replaces the data integration.
50 */
51 sourceURI: string;
52 /**
53 * The scheduling configuration for pulling data from the source. Required
54 * for SaaS (AppFlow) sources; must be omitted for S3 sources. Changing it
55 * replaces the data integration.
56 */
57 scheduleConfig?: DataIntegrationScheduleConfig;
58 /**
59 * The configuration for which files to pull from the source (folders and
60 * filters). Changing it replaces the data integration.
61 */
62 fileConfiguration?: {
63 /**
64 * Identifiers for the source folders to pull all files from recursively.
65 */
66 folders: string[];
67 /**
68 * Restrictions for what files should be pulled from the source.
69 */
70 filters?: Record<string, string[]>;
71 };
72 /**
73 * The configuration for which objects and fields to pull from the source.
74 * Changing it replaces the data integration.
75 */
76 objectConfiguration?: Record<string, Record<string, string[]>>;
77 /**
78 * Tags to apply to the data integration. Merged with internal Alchemy tags.
79 */
80 tags?: Record<string, string>;
81 }
82
83 export interface DataIntegration extends Resource<
84 "AWS.AppIntegrations.DataIntegration",
85 DataIntegrationProps,
86 {
87 dataIntegrationId: string;
88 dataIntegrationArn: string;
89 dataIntegrationName: string;
90 kmsKey: string;
91 sourceURI: string;
92 },
93 never,
94 Providers
95 > {}
96
97 /**
98 * An Amazon AppIntegrations data integration. Data integrations reference an
99 * external data source (an S3 bucket, or a SaaS application through Amazon
100 * AppFlow) so services like Amazon Q in Connect can ingest its content.
101 *
102 * The KMS key, source URI, schedule, file configuration, and object
103 * configuration are immutable; changing any of them replaces the data
104 * integration. Only the name and description can be updated in place.
105 * ### Creating a Data Integration
106 * **Example:** S3 Data Integration
107 * ```typescript
108 * import * as AppIntegrations from "@alchemy.run/aws/AppIntegrations";
109 * import * as KMS from "@alchemy.run/aws/KMS";
110 * import * as S3 from "@alchemy.run/aws/S3";
111 *
112 * const bucket = yield* S3.Bucket("Content");
113 * const key = yield* KMS.Key("ContentKey");
114 *
115 * const integration = yield* AppIntegrations.DataIntegration("Content", {
116 * kmsKey: key.keyArn,
117 * sourceURI: Output.interpolate`s3://${bucket.bucketName}`,
118 * });
119 * ```
120 *
121 * **Example:** Scheduled SaaS Data Integration
122 * ```typescript
123 * const integration = yield* AppIntegrations.DataIntegration("Salesforce", {
124 * kmsKey: key.keyArn,
125 * sourceURI: "Salesforce://AppFlow/my-connector-profile",
126 * scheduleConfig: {
127 * firstExecutionFrom: "2024-01-01T00:00:00Z",
128 * object: "Account",
129 * scheduleExpression: "rate(1 hours)",
130 * },
131 * });
132 * ```
133 *
134 * @resource
135 */
136 export const DataIntegration = Resource<DataIntegration>("AWS.AppIntegrations.DataIntegration");
137
138 /**
139 * Raised when the AppIntegrations API returns a data integration without
140 * the fields required to build the resource attributes.
141 */
142 export class DataIntegrationIncomplete extends Data.TaggedError("DataIntegrationIncomplete")<{
143 message: string;
144 }> {}
145
146 export const DataIntegrationProvider = () =>
147 Provider.effect(
148 DataIntegration,
149 Effect.gen(function* () {
150 const createName = Effect.fn(function* (id: string, props: { name?: string }) {
151 return props.name ?? (yield* createPhysicalName({ id, maxLength: 255 }));
152 });
153
154 /** Get a single data integration by ARN or ID; undefined if absent. */
155 const observe = (arnOrId: string) =>
156 appintegrations
157 .getDataIntegration({ Identifier: arnOrId })
158 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
159
160 /** Find a data integration ARN by name via list enumeration. */
161 const findByName = (name: string) =>
162 appintegrations.listDataIntegrations.items({}).pipe(
163 Stream.filter((item) => item.Name === name),
164 Stream.take(1),
165 Stream.runCollect,
166 Effect.map((chunk) => Array.from(chunk)[0]?.Arn),
167 );
168
169 const toAttrs = Effect.fn(function* (live: appintegrations.GetDataIntegrationResponse) {
170 if (
171 live.Arn === undefined ||
172 live.Id === undefined ||
173 live.Name === undefined ||
174 live.KmsKey === undefined ||
175 live.SourceURI === undefined
176 ) {
177 return yield* new DataIntegrationIncomplete({
178 message: `data integration '${live.Name}' is missing Arn, Id, Name, KmsKey, or SourceURI`,
179 });
180 }
181 return {
182 dataIntegrationId: live.Id,
183 dataIntegrationArn: live.Arn,
184 dataIntegrationName: live.Name,
185 kmsKey: live.KmsKey,
186 sourceURI: live.SourceURI,
187 };
188 });
189
190 const toScheduleConfiguration = (
191 config: DataIntegrationScheduleConfig,
192 ): appintegrations.ScheduleConfiguration => ({
193 ScheduleExpression: config.scheduleExpression,
194 ...(config.firstExecutionFrom !== undefined
195 ? { FirstExecutionFrom: config.firstExecutionFrom }
196 : {}),
197 ...(config.object !== undefined ? { Object: config.object } : {}),
198 });
199
200 return DataIntegration.Provider.of({
201 stables: ["dataIntegrationId", "dataIntegrationArn", "kmsKey", "sourceURI"],
202
203 // The list API only returns Arn/Name/SourceURI summaries — hydrate
204 // each into the full attributes shape, tolerating per-item NotFound
205 // races.
206 list: () =>
207 Effect.gen(function* () {
208 const arns = yield* appintegrations.listDataIntegrations.items({}).pipe(
209 Stream.runCollect,
210 Effect.map((chunk) =>
211 Array.from(chunk).flatMap((item) => (item.Arn !== undefined ? [item.Arn] : [])),
212 ),
213 );
214 const items = yield* Effect.forEach(
215 arns,
216 (arn) =>
217 observe(arn).pipe(
218 Effect.flatMap((live) => (live === undefined ? Effect.undefined : toAttrs(live))),
219 ),
220 { concurrency: 10 },
221 );
222 return items.filter(
223 (item): item is DataIntegration["Attributes"] => item !== undefined,
224 );
225 }),
226
227 read: Effect.fn(function* ({ id, olds, output }) {
228 let identifier: string | undefined = output?.dataIntegrationId;
229 if (identifier === undefined) {
230 const name = yield* createName(id, olds ?? {});
231 identifier = yield* findByName(name);
232 }
233 if (identifier === undefined) return undefined;
234 const live = yield* observe(identifier);
235 if (live === undefined) return undefined;
236 const attrs = yield* toAttrs(live);
237 const tags = definedTags(live.Tags);
238 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
239 }),
240
241 diff: Effect.fn(function* ({ news, olds }) {
242 if (!isResolved(news)) return undefined;
243 if (olds === undefined) return undefined;
244 if (
245 olds.kmsKey !== news.kmsKey ||
246 olds.sourceURI !== news.sourceURI ||
247 JSON.stringify(olds.scheduleConfig) !== JSON.stringify(news.scheduleConfig) ||
248 JSON.stringify(olds.fileConfiguration) !== JSON.stringify(news.fileConfiguration) ||
249 JSON.stringify(olds.objectConfiguration) !== JSON.stringify(news.objectConfiguration)
250 ) {
251 return { action: "replace" } as const;
252 }
253 // fall through: default update path (name, description, tags)
254 }),
255
256 reconcile: Effect.fn(function* ({ id, news, output, session }) {
257 const name = yield* createName(id, news);
258 const internalTags = yield* createInternalTags(id);
259 const desiredTags = { ...news.tags, ...internalTags };
260
261 // 1. Observe — prefer the cached ID; fall back to enumerating by
262 // name so a lost-state re-run converges.
263 let identifier: string | undefined = output?.dataIntegrationId;
264 let live = identifier === undefined ? undefined : yield* observe(identifier);
265 if (live === undefined) {
266 identifier = yield* findByName(name);
267 live = identifier === undefined ? undefined : yield* observe(identifier);
268 }
269
270 // 2. Ensure — create if missing.
271 if (live === undefined) {
272 const created = yield* appintegrations.createDataIntegration({
273 Name: name,
274 Description: news.description,
275 KmsKey: news.kmsKey,
276 SourceURI: news.sourceURI,
277 ScheduleConfig: news.scheduleConfig
278 ? toScheduleConfiguration(news.scheduleConfig)
279 : undefined,
280 FileConfiguration: news.fileConfiguration
281 ? {
282 Folders: news.fileConfiguration.folders,
283 ...(news.fileConfiguration.filters
284 ? { Filters: news.fileConfiguration.filters }
285 : {}),
286 }
287 : undefined,
288 ObjectConfiguration: news.objectConfiguration,
289 Tags: desiredTags,
290 });
291 if (created.Arn === undefined) {
292 return yield* new DataIntegrationIncomplete({
293 message: `createDataIntegration for '${name}' returned no Arn`,
294 });
295 }
296 live = yield* appintegrations.getDataIntegration({
297 Identifier: created.Arn,
298 });
299 }
300 const attrs = yield* toAttrs(live);
301
302 // 3. Sync mutable aspects — only the name and description can be
303 // updated in place. The API cannot clear a description (min
304 // length 1), so only push a defined value that differs.
305 const update: Omit<appintegrations.UpdateDataIntegrationRequest, "Identifier"> = {};
306 if (live.Name !== name) {
307 update.Name = name;
308 }
309 if (news.description !== undefined && news.description !== live.Description) {
310 update.Description = news.description;
311 }
312 if (Object.keys(update).length > 0) {
313 yield* appintegrations.updateDataIntegration({
314 Identifier: attrs.dataIntegrationId,
315 ...update,
316 });
317 }
318
319 // 4. Sync tags — diff against OBSERVED cloud tags so adoption
320 // converges.
321 const observedTags = definedTags(live.Tags);
322 const { upsert, removed } = diffTags(observedTags, desiredTags);
323 if (upsert.length > 0) {
324 yield* appintegrations.tagResource({
325 resourceArn: attrs.dataIntegrationArn,
326 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
327 });
328 }
329 if (removed.length > 0) {
330 yield* appintegrations.untagResource({
331 resourceArn: attrs.dataIntegrationArn,
332 tagKeys: removed,
333 });
334 }
335
336 yield* session.note(name);
337 return { ...attrs, dataIntegrationName: name };
338 }),
339
340 delete: Effect.fn(function* ({ output }) {
341 yield* appintegrations
342 .deleteDataIntegration({
343 DataIntegrationIdentifier: output.dataIntegrationId,
344 })
345 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
346 }),
347 });
348 }),
349 );