@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 254 lines 9.3 KB
Raw
1 import * as appconfig from "@distilled.cloud/aws/appconfig";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import { AWSEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 extensionAssociationArn,
13 readAppConfigTags,
14 syncAppConfigTags,
15 toTagRecord,
16 } from "./internal.ts";
17
18 export interface ExtensionAssociationProps {
19 /**
20 * The extension to associate: its ID, name, or ARN. Changing it replaces
21 * the association.
22 */
23 extensionIdentifier: string;
24 /**
25 * The version of the extension to pin. If omitted, AppConfig uses the
26 * latest version. Changing it replaces the association.
27 */
28 extensionVersionNumber?: number;
29 /**
30 * ARN of the AppConfig resource the extension attaches to: an application,
31 * environment, or configuration profile. Changing it replaces the
32 * association.
33 */
34 resourceIdentifier: string;
35 /**
36 * Values for the parameters declared by the extension, keyed by parameter
37 * name.
38 */
39 parameters?: Record<string, string>;
40 /**
41 * User-defined tags for the extension association.
42 */
43 tags?: Record<string, string>;
44 }
45
46 export interface ExtensionAssociation extends Resource<
47 "AWS.AppConfig.ExtensionAssociation",
48 ExtensionAssociationProps,
49 {
50 extensionAssociationId: string;
51 extensionAssociationArn: string;
52 extensionArn: string;
53 resourceArn: string;
54 },
55 never,
56 Providers
57 > {}
58
59 /**
60 * An AWS AppConfig extension association — attaches an {@link Extension} to an
61 * application, environment, or configuration profile so the extension's
62 * actions fire for that resource's workflow events.
63 *
64 * ### Associating an Extension
65 * **Example:** Attach an Extension to an Application
66 * ```typescript
67 * const association = yield* AppConfig.ExtensionAssociation("Hook", {
68 * extensionIdentifier: extension.extensionId,
69 * resourceIdentifier: app.applicationArn,
70 * });
71 * ```
72 *
73 * **Example:** Attach with Parameter Values
74 * ```typescript
75 * const association = yield* AppConfig.ExtensionAssociation("Hook", {
76 * extensionIdentifier: extension.extensionId,
77 * resourceIdentifier: env.environmentArn,
78 * parameters: { topicArn: topic.topicArn },
79 * });
80 * ```
81 *
82 * @resource
83 */
84 export const ExtensionAssociation = Resource<ExtensionAssociation>(
85 "AWS.AppConfig.ExtensionAssociation",
86 );
87
88 export const ExtensionAssociationProvider = () =>
89 Provider.effect(
90 ExtensionAssociation,
91 Effect.gen(function* () {
92 const readAssociation = Effect.fn(function* (extensionAssociationId: string) {
93 return yield* appconfig
94 .getExtensionAssociation({
95 ExtensionAssociationId: extensionAssociationId,
96 })
97 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
98 });
99
100 /**
101 * Find an existing association between the extension and the resource
102 * (associations have no name; the pair is the natural identity).
103 *
104 * `ListExtensionAssociations` rejects requests carrying BOTH
105 * `ExtensionIdentifier` and `ResourceIdentifier` ("Request cannot
106 * contain both..."), so list by resource and match the extension
107 * client-side via its resolved id (association `ExtensionArn`s are
108 * versioned, `.../extension/{id}/{version}`).
109 */
110 const findByIdentifiers = Effect.fn(function* (props: Partial<ExtensionAssociationProps>) {
111 if (props.extensionIdentifier === undefined || props.resourceIdentifier === undefined) {
112 return undefined;
113 }
114 const extension = yield* appconfig
115 .getExtension({ ExtensionIdentifier: props.extensionIdentifier })
116 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
117 if (extension?.Id === undefined) return undefined;
118 const summaries = yield* appconfig.listExtensionAssociations
119 .pages({ ResourceIdentifier: props.resourceIdentifier })
120 .pipe(
121 Stream.runCollect,
122 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Items ?? [])),
123 );
124 const summary = summaries.find(
125 (item) =>
126 item.Id !== undefined &&
127 item.ExtensionArn !== undefined &&
128 item.ExtensionArn.includes(extension.Id!),
129 );
130 return summary?.Id === undefined ? undefined : yield* readAssociation(summary.Id);
131 });
132
133 const toAttrs = Effect.fn(function* (association: appconfig.ExtensionAssociation) {
134 const { accountId, region } = yield* AWSEnvironment.current;
135 return {
136 extensionAssociationId: association.Id!,
137 extensionAssociationArn:
138 association.Arn ?? extensionAssociationArn(region, accountId, association.Id!),
139 extensionArn: association.ExtensionArn!,
140 resourceArn: association.ResourceArn!,
141 };
142 });
143
144 return {
145 stables: [
146 "extensionAssociationId",
147 "extensionAssociationArn",
148 "extensionArn",
149 "resourceArn",
150 ],
151
152 diff: Effect.fn(function* ({ olds, news }) {
153 if (!isResolved(news)) return undefined;
154 if (
155 olds !== undefined &&
156 (olds.extensionIdentifier !== news.extensionIdentifier ||
157 olds.resourceIdentifier !== news.resourceIdentifier ||
158 (olds.extensionVersionNumber ?? undefined) !==
159 (news.extensionVersionNumber ?? undefined))
160 ) {
161 return { action: "replace" } as const;
162 }
163 }),
164
165 read: Effect.fn(function* ({ id, olds, output }) {
166 const association = output?.extensionAssociationId
167 ? yield* readAssociation(output.extensionAssociationId)
168 : yield* findByIdentifiers(olds ?? {});
169 if (association?.Id === undefined) return undefined;
170 const attrs = yield* toAttrs(association);
171 const tags = yield* readAppConfigTags(attrs.extensionAssociationArn);
172 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
173 }),
174
175 reconcile: Effect.fn(function* ({ id, news, output, session }) {
176 const internalTags = yield* createInternalTags(id);
177 const desiredTags = { ...internalTags, ...news.tags };
178
179 // 1. Observe.
180 let observed = output?.extensionAssociationId
181 ? yield* readAssociation(output.extensionAssociationId)
182 : undefined;
183 if (observed === undefined) {
184 observed = yield* findByIdentifiers(news);
185 }
186
187 // 2. Ensure.
188 if (observed?.Id === undefined) {
189 observed = yield* appconfig.createExtensionAssociation({
190 ExtensionIdentifier: news.extensionIdentifier,
191 ExtensionVersionNumber: news.extensionVersionNumber,
192 ResourceIdentifier: news.resourceIdentifier,
193 Parameters: news.parameters,
194 Tags: desiredTags,
195 });
196 } else {
197 // 3. Sync — parameter values are mutable in place.
198 const observedParameters = toTagRecord(observed.Parameters);
199 const desiredParameters = news.parameters ?? {};
200 const { removed, upsert } = diffTags(observedParameters, desiredParameters);
201 if (removed.length > 0 || upsert.length > 0) {
202 observed = yield* appconfig.updateExtensionAssociation({
203 ExtensionAssociationId: observed.Id,
204 Parameters: desiredParameters,
205 });
206 }
207 }
208
209 const attrs = yield* toAttrs(observed);
210
211 // 3b. Sync tags against observed cloud tags.
212 yield* syncAppConfigTags(attrs.extensionAssociationArn, desiredTags);
213
214 yield* session.note(attrs.extensionAssociationId);
215 return attrs;
216 }),
217
218 delete: Effect.fn(function* ({ output }) {
219 yield* appconfig
220 .deleteExtensionAssociation({
221 ExtensionAssociationId: output.extensionAssociationId,
222 })
223 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
224 }),
225
226 list: () =>
227 Effect.gen(function* () {
228 const { accountId, region } = yield* AWSEnvironment.current;
229 const summaries = yield* appconfig.listExtensionAssociations.pages({}).pipe(
230 Stream.runCollect,
231 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Items ?? [])),
232 );
233 return summaries.flatMap((summary) =>
234 summary.Id !== undefined &&
235 summary.ExtensionArn !== undefined &&
236 summary.ResourceArn !== undefined
237 ? [
238 {
239 extensionAssociationId: summary.Id,
240 extensionAssociationArn: extensionAssociationArn(
241 region,
242 accountId,
243 summary.Id,
244 ),
245 extensionArn: summary.ExtensionArn,
246 resourceArn: summary.ResourceArn,
247 },
248 ]
249 : [],
250 );
251 }),
252 };
253 }),
254 );