@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 249 lines 9.26 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 EventIntegrationProps {
15 /**
16 * Name of the event integration. Unique per account/region. If omitted, a
17 * unique name is generated from the app, stage, and logical ID. Changing
18 * the name replaces the event integration.
19 */
20 name?: string;
21 /**
22 * Description of the event integration (1-1000 characters).
23 */
24 description?: string;
25 /**
26 * The partner event source that pushes events to the EventBridge bus,
27 * e.g. `aws.partner/examplepartner.com`. Changing the source replaces the
28 * event integration.
29 */
30 source: string;
31 /**
32 * The name of the Amazon EventBridge bus the partner events are delivered
33 * to, e.g. `default`. Only metadata is persisted — the bus itself is not
34 * created. Changing the bus replaces the event integration.
35 */
36 eventBridgeBus: string;
37 /**
38 * Tags to apply to the event integration. Merged with internal Alchemy tags.
39 */
40 tags?: Record<string, string>;
41 }
42
43 export interface EventIntegration extends Resource<
44 "AWS.AppIntegrations.EventIntegration",
45 EventIntegrationProps,
46 {
47 eventIntegrationName: string;
48 eventIntegrationArn: string;
49 eventBridgeBus: string;
50 source: string;
51 },
52 never,
53 Providers
54 > {}
55
56 /**
57 * An Amazon AppIntegrations event integration. An event integration
58 * associates a partner event source with an Amazon EventBridge bus in your
59 * account so external applications (e.g. Amazon Connect third-party apps)
60 * can publish events into it. Only metadata is persisted — no EventBridge
61 * objects are created.
62 *
63 * The event source and EventBridge bus are immutable; changing either
64 * replaces the event integration. Only the description can be updated in
65 * place.
66 * ### Creating an Event Integration
67 * **Example:** Basic Event Integration
68 * ```typescript
69 * import * as AppIntegrations from "@alchemy.run/aws/AppIntegrations";
70 *
71 * const events = yield* AppIntegrations.EventIntegration("PartnerEvents", {
72 * source: "aws.partner/examplepartner.com",
73 * eventBridgeBus: "default",
74 * });
75 * ```
76 *
77 * **Example:** Event Integration with Description and Tags
78 * ```typescript
79 * const events = yield* AppIntegrations.EventIntegration("PartnerEvents", {
80 * source: "aws.partner/examplepartner.com",
81 * eventBridgeBus: "default",
82 * description: "Events from Example Partner",
83 * tags: { team: "integrations" },
84 * });
85 * ```
86 *
87 * @resource
88 */
89 export const EventIntegration = Resource<EventIntegration>("AWS.AppIntegrations.EventIntegration");
90
91 /**
92 * Raised when the AppIntegrations API returns an event integration without
93 * the fields required to build the resource attributes.
94 */
95 export class EventIntegrationIncomplete extends Data.TaggedError("EventIntegrationIncomplete")<{
96 message: string;
97 }> {}
98
99 export const EventIntegrationProvider = () =>
100 Provider.effect(
101 EventIntegration,
102 Effect.gen(function* () {
103 const createName = Effect.fn(function* (id: string, props: { name?: string }) {
104 return props.name ?? (yield* createPhysicalName({ id, maxLength: 255 }));
105 });
106
107 /** Observe a single event integration by name; undefined if absent. */
108 const observe = (name: string) =>
109 appintegrations
110 .getEventIntegration({ Name: name })
111 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
112
113 const toAttrs = Effect.fn(function* (live: appintegrations.GetEventIntegrationResponse) {
114 if (
115 live.Name === undefined ||
116 live.EventIntegrationArn === undefined ||
117 live.EventBridgeBus === undefined ||
118 live.EventFilter?.Source === undefined
119 ) {
120 return yield* new EventIntegrationIncomplete({
121 message: `event integration '${live.Name}' is missing Name, Arn, EventBridgeBus, or EventFilter.Source`,
122 });
123 }
124 return {
125 eventIntegrationName: live.Name,
126 eventIntegrationArn: live.EventIntegrationArn,
127 eventBridgeBus: live.EventBridgeBus,
128 source: live.EventFilter.Source,
129 };
130 });
131
132 return EventIntegration.Provider.of({
133 stables: ["eventIntegrationName", "eventIntegrationArn", "eventBridgeBus", "source"],
134
135 list: () =>
136 appintegrations.listEventIntegrations.items({}).pipe(
137 Stream.runCollect,
138 Effect.map((chunk) =>
139 Array.from(chunk).flatMap((item) =>
140 item.Name !== undefined &&
141 item.EventIntegrationArn !== undefined &&
142 item.EventBridgeBus !== undefined &&
143 item.EventFilter?.Source !== undefined
144 ? [
145 {
146 eventIntegrationName: item.Name,
147 eventIntegrationArn: item.EventIntegrationArn,
148 eventBridgeBus: item.EventBridgeBus,
149 source: item.EventFilter.Source,
150 },
151 ]
152 : [],
153 ),
154 ),
155 ),
156
157 read: Effect.fn(function* ({ id, olds, output }) {
158 const name = output?.eventIntegrationName ?? (yield* createName(id, olds ?? {}));
159 const live = yield* observe(name);
160 if (live === undefined) return undefined;
161 const attrs = yield* toAttrs(live);
162 const tags = definedTags(live.Tags);
163 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
164 }),
165
166 diff: Effect.fn(function* ({ id, news, olds }) {
167 if (!isResolved(news)) return undefined;
168 if (olds === undefined) return undefined;
169 const oldName = yield* createName(id, olds);
170 const newName = yield* createName(id, news);
171 if (
172 oldName !== newName ||
173 olds.source !== news.source ||
174 olds.eventBridgeBus !== news.eventBridgeBus
175 ) {
176 return { action: "replace" } as const;
177 }
178 // fall through: default update path (description, tags)
179 }),
180
181 reconcile: Effect.fn(function* ({ id, news, output, session }) {
182 const name = output?.eventIntegrationName ?? (yield* createName(id, news));
183 const internalTags = yield* createInternalTags(id);
184 const desiredTags = { ...news.tags, ...internalTags };
185
186 // 1. Observe — cloud state is authoritative.
187 let live = yield* observe(name);
188
189 // 2. Ensure — create if missing; tolerate the concurrent-create race.
190 if (live === undefined) {
191 yield* appintegrations
192 .createEventIntegration({
193 Name: name,
194 Description: news.description,
195 EventFilter: { Source: news.source },
196 EventBridgeBus: news.eventBridgeBus,
197 Tags: desiredTags,
198 })
199 .pipe(Effect.catchTag("DuplicateResourceException", () => Effect.void));
200 live = yield* appintegrations.getEventIntegration({ Name: name });
201 }
202 const attrs = yield* toAttrs(live);
203
204 // 3. Sync description — the only mutable field. The API cannot
205 // clear a description (min length 1), so only push a defined
206 // value that differs from the observed one.
207 if (news.description !== undefined && news.description !== live.Description) {
208 yield* appintegrations.updateEventIntegration({
209 Name: name,
210 Description: news.description,
211 });
212 }
213
214 // 4. Sync tags — diff against OBSERVED cloud tags so adoption
215 // converges.
216 const observedTags = yield* appintegrations
217 .listTagsForResource({ resourceArn: attrs.eventIntegrationArn })
218 .pipe(
219 Effect.map((r) => definedTags(r.tags)),
220 Effect.catchTag("ResourceNotFoundException", () =>
221 Effect.succeed({} as Record<string, string>),
222 ),
223 );
224 const { upsert, removed } = diffTags(observedTags, desiredTags);
225 if (upsert.length > 0) {
226 yield* appintegrations.tagResource({
227 resourceArn: attrs.eventIntegrationArn,
228 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
229 });
230 }
231 if (removed.length > 0) {
232 yield* appintegrations.untagResource({
233 resourceArn: attrs.eventIntegrationArn,
234 tagKeys: removed,
235 });
236 }
237
238 yield* session.note(name);
239 return attrs;
240 }),
241
242 delete: Effect.fn(function* ({ output }) {
243 yield* appintegrations
244 .deleteEventIntegration({ Name: output.eventIntegrationName })
245 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
246 }),
247 });
248 }),
249 );