The best way to control Claude Code, Codex and more from any computer or phone

main
ts 240 lines 8.16 KB
Raw
1 import * as appflow from "@distilled.cloud/aws/appflow";
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 Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11 import { readAppFlowTags, syncAppFlowTags } from "./internal.ts";
12
13 export interface FlowProps {
14 /**
15 * The name of the flow. Must be unique in the account/region, may contain
16 * only letters, digits, hyphens, underscores, `!`, `@`, `#`, `.`, `$`.
17 * If omitted, a deterministic physical name is generated. Changing the
18 * name replaces the flow.
19 */
20 flowName?: string;
21 /**
22 * A description of the flow.
23 */
24 description?: string;
25 /**
26 * The ARN of a KMS key AppFlow uses to encrypt data. If omitted, the
27 * AWS-managed AppFlow key is used.
28 */
29 kmsArn?: string;
30 /**
31 * How the flow is triggered — on demand, on a schedule, or by an event.
32 * @default { triggerType: "OnDemand" }
33 */
34 triggerConfig?: appflow.TriggerConfig;
35 /**
36 * Configuration of the source connector (e.g. S3, Salesforce) the flow
37 * pulls data from.
38 */
39 sourceFlowConfig: appflow.SourceFlowConfig;
40 /**
41 * Configuration of the destination connector(s) the flow pushes data to.
42 */
43 destinationFlowConfigList: appflow.DestinationFlowConfig[];
44 /**
45 * The field mapping tasks applied to records as they move from source to
46 * destination.
47 */
48 tasks: appflow.Task[];
49 /**
50 * Glue Data Catalog configuration for cataloging flow output.
51 */
52 metadataCatalogConfig?: appflow.MetadataCatalogConfig;
53 /**
54 * User-defined tags for the flow.
55 */
56 tags?: Record<string, string>;
57 }
58
59 export interface Flow extends Resource<
60 "AWS.AppFlow.Flow",
61 FlowProps,
62 {
63 flowName: string;
64 flowArn: string;
65 flowStatus: string | undefined;
66 },
67 never,
68 Providers
69 > {}
70
71 /**
72 * An Amazon AppFlow flow. A flow transfers data between a source connector
73 * and one or more destination connectors, applying field-mapping tasks.
74 * The credential-free S3-to-S3 path is directly testable; other connectors
75 * require a {@link ConnectorProfile} with vendor credentials.
76 *
77 * For an S3 source, the bucket policy must authorize the
78 * `appflow.amazonaws.com` service principal (`s3:GetObject` + `s3:ListBucket`
79 * on the source; `s3:PutObject` and the multipart/ACL actions on the
80 * destination), and the source prefix must contain at least one object when
81 * the flow is created — AppFlow validates connectivity by listing the source
82 * at `CreateFlow` time and rejects an empty prefix with
83 * `ConnectorServerException`.
84 * ### Creating a Flow
85 * **Example:** S3 to S3 On-Demand Flow
86 * ```typescript
87 * const flow = yield* AppFlow.Flow("Copy", {
88 * triggerConfig: { triggerType: "OnDemand" },
89 * sourceFlowConfig: {
90 * connectorType: "S3",
91 * sourceConnectorProperties: {
92 * S3: { bucketName: srcBucket, bucketPrefix: "input" },
93 * },
94 * },
95 * destinationFlowConfigList: [
96 * {
97 * connectorType: "S3",
98 * destinationConnectorProperties: {
99 * S3: { bucketName: dstBucket, bucketPrefix: "output" },
100 * },
101 * },
102 * ],
103 * tasks: [
104 * {
105 * taskType: "Map_all",
106 * sourceFields: [],
107 * connectorOperator: { S3: "NO_OP" },
108 * taskProperties: {},
109 * },
110 * ],
111 * });
112 * ```
113 *
114 * @resource
115 */
116 export const Flow = Resource<Flow>("AWS.AppFlow.Flow");
117
118 const ON_DEMAND: appflow.TriggerConfig = { triggerType: "OnDemand" };
119
120 export const FlowProvider = () =>
121 Provider.effect(
122 Flow,
123 Effect.gen(function* () {
124 const toName = (id: string, props: FlowProps) =>
125 props.flowName
126 ? Effect.succeed(props.flowName)
127 : createPhysicalName({ id, maxLength: 100 });
128
129 return {
130 stables: ["flowName", "flowArn"],
131
132 diff: Effect.fn(function* ({ id, olds, news }) {
133 if (!isResolved(news)) return undefined;
134 if ((yield* toName(id, olds)) !== (yield* toName(id, news))) {
135 return { action: "replace" } as const;
136 }
137 }),
138
139 read: Effect.fn(function* ({ id, olds, output }) {
140 const name = output?.flowName ?? (yield* toName(id, olds ?? {}));
141 const flow = yield* appflow
142 .describeFlow({ flowName: name })
143 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
144 if (flow === undefined || flow.flowArn === undefined) return undefined;
145 const attrs = {
146 flowName: name,
147 flowArn: flow.flowArn,
148 flowStatus: flow.flowStatus,
149 };
150 const tags = yield* readAppFlowTags(flow.flowArn);
151 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
152 }),
153
154 reconcile: Effect.fn(function* ({ id, news, output, session }) {
155 const name = output?.flowName ?? (yield* toName(id, news));
156 const internalTags = yield* createInternalTags(id);
157 const desiredTags = { ...internalTags, ...news.tags };
158 const trigger = news.triggerConfig ?? ON_DEMAND;
159
160 // 1. Observe.
161 let live = yield* appflow
162 .describeFlow({ flowName: name })
163 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
164
165 // 2. Ensure — create if missing. Tolerate an AlreadyExists race by
166 // falling back to describe.
167 if (live === undefined) {
168 yield* appflow
169 .createFlow({
170 flowName: name,
171 description: news.description,
172 kmsArn: news.kmsArn,
173 triggerConfig: trigger,
174 sourceFlowConfig: news.sourceFlowConfig,
175 destinationFlowConfigList: news.destinationFlowConfigList,
176 tasks: news.tasks,
177 metadataCatalogConfig: news.metadataCatalogConfig,
178 tags: desiredTags,
179 })
180 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
181 live = yield* appflow.describeFlow({ flowName: name });
182 } else {
183 // 3. Sync — updateFlow converges the mutable configuration
184 // (trigger, source/destination, tasks). It does not accept tags.
185 yield* appflow.updateFlow({
186 flowName: name,
187 description: news.description,
188 triggerConfig: trigger,
189 sourceFlowConfig: news.sourceFlowConfig,
190 destinationFlowConfigList: news.destinationFlowConfigList,
191 tasks: news.tasks,
192 metadataCatalogConfig: news.metadataCatalogConfig,
193 });
194 live = yield* appflow.describeFlow({ flowName: name });
195 }
196
197 // 3b. Sync tags against observed cloud tags.
198 if (live.flowArn) {
199 yield* syncAppFlowTags(live.flowArn, desiredTags);
200 }
201
202 yield* session.note(name);
203 return {
204 flowName: name,
205 flowArn: live.flowArn!,
206 flowStatus: live.flowStatus,
207 };
208 }),
209
210 delete: Effect.fn(function* ({ output }) {
211 yield* appflow
212 .deleteFlow({ flowName: output.flowName, forceDelete: true })
213 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
214 }),
215
216 list: () =>
217 appflow.listFlows.pages({}).pipe(
218 Stream.runCollect,
219 Effect.map((chunk) =>
220 Array.from(chunk).flatMap((page) =>
221 (page.flows ?? [])
222 .filter(
223 (
224 f,
225 ): f is appflow.FlowDefinition & {
226 flowName: string;
227 flowArn: string;
228 } => f.flowName !== undefined && f.flowArn !== undefined,
229 )
230 .map((f) => ({
231 flowName: f.flowName,
232 flowArn: f.flowArn,
233 flowStatus: f.flowStatus,
234 })),
235 ),
236 ),
237 ),
238 };
239 }),
240 );