@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 211 lines 7.39 KB
Raw
1 import * as appflow from "@distilled.cloud/aws/appflow";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 export class ConnectorProfileUnexpectedResponse extends Data.TaggedError(
11 "ConnectorProfileUnexpectedResponse",
12 )<{
13 readonly message: string;
14 readonly cause?: unknown;
15 }> {}
16
17 export interface ConnectorProfileProps {
18 /**
19 * The name of the connector profile. Unique per account/region and used as
20 * the profile's identity. Changing it replaces the profile.
21 */
22 connectorProfileName: string;
23 /**
24 * The type of connector (e.g. `Salesforce`, `Redshift`, `Snowflake`). S3
25 * and EventBridge do not require a connector profile.
26 */
27 connectorType: appflow.ConnectorType;
28 /**
29 * The label of the custom connector, required only for the
30 * `CustomConnector` type.
31 */
32 connectorLabel?: string;
33 /**
34 * Whether the connector connects over the public internet or a private
35 * PrivateLink connection.
36 * @default "Public"
37 */
38 connectionMode?: "Public" | "Private";
39 /**
40 * The connector-specific properties and credentials. Credentials
41 * (OAuth tokens, API keys, secrets) are supplied here — these typically
42 * require a human-in-the-loop authorization step with the vendor.
43 */
44 connectorProfileConfig: appflow.ConnectorProfileConfig;
45 /**
46 * The ARN of a KMS key AppFlow uses to encrypt the stored credentials.
47 */
48 kmsArn?: string;
49 }
50
51 export interface ConnectorProfile extends Resource<
52 "AWS.AppFlow.ConnectorProfile",
53 ConnectorProfileProps,
54 {
55 connectorProfileName: string;
56 connectorProfileArn: string;
57 connectorType: string | undefined;
58 credentialsArn: string | undefined;
59 },
60 never,
61 Providers
62 > {}
63
64 /**
65 * An Amazon AppFlow connector profile. A connector profile stores the
66 * connection settings and credentials for a SaaS/data-warehouse connector
67 * (Salesforce, Snowflake, Redshift, etc.) so flows can reference it.
68 *
69 * Most connectors require vendor credentials obtained through a
70 * human-in-the-loop OAuth or API-key step, so a connector profile's live
71 * lifecycle generally cannot be created purely programmatically. S3 and
72 * EventBridge flows do not need a connector profile at all.
73 * ### Creating a Connector Profile
74 * **Example:** Redshift Connector Profile
75 * ```typescript
76 * const profile = yield* AppFlow.ConnectorProfile("Warehouse", {
77 * connectorProfileName: "warehouse",
78 * connectorType: "Redshift",
79 * connectionMode: "Public",
80 * connectorProfileConfig: {
81 * connectorProfileProperties: {
82 * Redshift: {
83 * databaseUrl: "jdbc:redshift://cluster:5439/db",
84 * bucketName: "appflow-staging",
85 * roleArn: role.roleArn,
86 * },
87 * },
88 * connectorProfileCredentials: {
89 * Redshift: { username: "admin", password: "..." },
90 * },
91 * },
92 * });
93 * ```
94 *
95 * @resource
96 */
97 export const ConnectorProfile = Resource<ConnectorProfile>("AWS.AppFlow.ConnectorProfile");
98
99 export const ConnectorProfileProvider = () =>
100 Provider.effect(
101 ConnectorProfile,
102 Effect.gen(function* () {
103 /** Describe a single connector profile by name; undefined if absent. */
104 const observe = (name: string) =>
105 appflow
106 .describeConnectorProfiles({ connectorProfileNames: [name] })
107 .pipe(
108 Effect.map((r) =>
109 (r.connectorProfileDetails ?? []).find((p) => p.connectorProfileName === name),
110 ),
111 );
112
113 const toAttrs = (name: string, profile: appflow.ConnectorProfile) => ({
114 connectorProfileName: name,
115 connectorProfileArn: profile.connectorProfileArn!,
116 connectorType: profile.connectorType,
117 credentialsArn: profile.credentialsArn,
118 });
119
120 return {
121 stables: ["connectorProfileName", "connectorProfileArn"],
122
123 diff: Effect.fn(function* ({ olds = {}, news }) {
124 if (!isResolved(news)) return undefined;
125 if (
126 (olds.connectorProfileName !== undefined &&
127 olds.connectorProfileName !== news.connectorProfileName) ||
128 (olds.connectorType !== undefined && olds.connectorType !== news.connectorType)
129 ) {
130 return { action: "replace" } as const;
131 }
132 }),
133
134 read: Effect.fn(function* ({ olds, output }) {
135 const name = output?.connectorProfileName ?? olds?.connectorProfileName;
136 if (name === undefined) return undefined;
137 const profile = yield* observe(name);
138 if (profile === undefined || profile.connectorProfileArn === undefined) return undefined;
139 return toAttrs(name, profile);
140 }),
141
142 reconcile: Effect.fn(function* ({ news, session }) {
143 const name = news.connectorProfileName;
144 const connectionMode = news.connectionMode ?? "Public";
145
146 // 1. Observe.
147 let live = yield* observe(name);
148
149 // 2/3. Ensure + sync — create if missing, else push the new config
150 // (credentials cannot be read back, so an update is always sent).
151 if (live === undefined) {
152 yield* appflow.createConnectorProfile({
153 connectorProfileName: name,
154 connectorType: news.connectorType,
155 connectorLabel: news.connectorLabel,
156 connectionMode,
157 kmsArn: news.kmsArn,
158 connectorProfileConfig: news.connectorProfileConfig,
159 });
160 } else {
161 yield* appflow.updateConnectorProfile({
162 connectorProfileName: name,
163 connectionMode,
164 connectorProfileConfig: news.connectorProfileConfig,
165 });
166 }
167 live = yield* observe(name);
168 if (live === undefined || live.connectorProfileArn === undefined) {
169 return yield* new ConnectorProfileUnexpectedResponse({
170 message: `Connector profile '${name}' was not found after reconcile`,
171 });
172 }
173
174 yield* session.note(name);
175 return toAttrs(name, live);
176 }),
177
178 delete: Effect.fn(function* ({ output }) {
179 yield* appflow
180 .deleteConnectorProfile({
181 connectorProfileName: output.connectorProfileName,
182 forceDelete: true,
183 })
184 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
185 }),
186
187 // maxResults is load-bearing: a fully-empty request body serializes
188 // to null and AppFlow rejects it ("The request object must be
189 // non-null", verified live).
190 list: () =>
191 appflow.describeConnectorProfiles.pages({ maxResults: 100 }).pipe(
192 Stream.runCollect,
193 Effect.map((chunk) =>
194 Array.from(chunk).flatMap((page) =>
195 (page.connectorProfileDetails ?? [])
196 .filter(
197 (
198 p,
199 ): p is appflow.ConnectorProfile & {
200 connectorProfileName: string;
201 connectorProfileArn: string;
202 } =>
203 p.connectorProfileName !== undefined && p.connectorProfileArn !== undefined,
204 )
205 .map((p) => toAttrs(p.connectorProfileName, p)),
206 ),
207 ),
208 ),
209 };
210 }),
211 );