@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, 06:00 UTC

main
ts 195 lines 7.04 KB
Raw
1 import * as appregistry from "@distilled.cloud/aws/service-catalog-appregistry";
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, tagRecord } 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 { clientToken, stripAwsSystemTags } from "./internal.ts";
12
13 export interface ApplicationProps {
14 /**
15 * Name of the application. Must be unique in the account and region and
16 * may only contain letters, numbers, dots, dashes, and underscores.
17 * If omitted, a unique name is generated. Changing it replaces the
18 * application.
19 */
20 applicationName?: string;
21 /**
22 * Description of the application. Updatable in place.
23 */
24 description?: string;
25 /**
26 * Tags to apply to the application. Merged with internal Alchemy tags.
27 */
28 tags?: Record<string, string>;
29 }
30
31 export interface Application extends Resource<
32 "AWS.AppRegistry.Application",
33 ApplicationProps,
34 {
35 /** The auto-generated application ID. */
36 applicationId: string;
37 /** The ARN of the application. */
38 applicationArn: string;
39 /** The name of the application. */
40 applicationName: string;
41 },
42 never,
43 Providers
44 > {}
45
46 /**
47 * An AWS Service Catalog AppRegistry application — the top-level node that
48 * groups related cloud resources and metadata under a single logical
49 * application (surfaced in myApplications and the `awsApplication` tag).
50 *
51 * ### Creating an Application
52 * **Example:** Basic Application
53 * ```typescript
54 * import * as AppRegistry from "@alchemy.run/aws/AppRegistry";
55 *
56 * const app = yield* AppRegistry.Application("Storefront", {});
57 * ```
58 *
59 * **Example:** Application with Description and Tags
60 * ```typescript
61 * const app = yield* AppRegistry.Application("Storefront", {
62 * applicationName: "storefront",
63 * description: "Customer-facing storefront services",
64 * tags: { team: "commerce" },
65 * });
66 * ```
67 *
68 * @resource
69 */
70 export const Application = Resource<Application>("AWS.AppRegistry.Application");
71
72 export const ApplicationProvider = () =>
73 Provider.effect(
74 Application,
75 Effect.gen(function* () {
76 const createName = Effect.fn(function* (
77 id: string,
78 props: Pick<ApplicationProps, "applicationName">,
79 ) {
80 return props.applicationName ?? (yield* createPhysicalName({ id, maxLength: 256 }));
81 });
82
83 // getApplication accepts a name, ID, or ARN specifier.
84 const observe = Effect.fn(function* (specifier: string) {
85 return yield* appregistry
86 .getApplication({ application: specifier })
87 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
88 });
89
90 return Application.Provider.of({
91 stables: ["applicationId", "applicationArn", "applicationName"],
92 list: () =>
93 appregistry.listApplications.pages({}).pipe(
94 Stream.runCollect,
95 Effect.map((chunk) =>
96 Array.from(chunk)
97 .flatMap((page) => page.applications ?? [])
98 .filter((a) => a.id != null && a.arn != null && a.name != null)
99 .map((a) => ({
100 applicationId: a.id!,
101 applicationArn: a.arn!,
102 applicationName: a.name!,
103 })),
104 ),
105 ),
106 read: Effect.fn(function* ({ id, olds, output }) {
107 const specifier = output?.applicationId ?? (yield* createName(id, olds ?? {}));
108 const found = yield* observe(specifier);
109 if (!found?.id) return undefined;
110 const attrs = {
111 applicationId: found.id,
112 applicationArn: found.arn!,
113 applicationName: found.name!,
114 };
115 return (yield* hasAlchemyTags(id, tagRecord(found.tags))) ? attrs : Unowned(attrs);
116 }),
117 // The application name is its user-facing identity — changing it
118 // replaces the application.
119 diff: Effect.fn(function* ({ id, olds, news }) {
120 if (!isResolved(news)) return undefined;
121 const oldName = yield* createName(id, olds);
122 const newName = yield* createName(id, news);
123 if (oldName !== newName) return { action: "replace" } as const;
124 }),
125 reconcile: Effect.fn(function* ({ id, news, output, session, instanceId }) {
126 const applicationName = yield* createName(id, news);
127 const internalTags = yield* createInternalTags(id);
128 const desiredTags: Record<string, string> = {
129 ...news.tags,
130 ...internalTags,
131 };
132
133 // 1. OBSERVE — cloud is authoritative; output caches the ID only.
134 let found = output?.applicationId ? yield* observe(output.applicationId) : undefined;
135 if (!found?.id) {
136 found = yield* observe(applicationName);
137 }
138
139 // 2. ENSURE — create when missing; tolerate a concurrent-create
140 // race (ConflictException) by re-reading.
141 if (!found?.id) {
142 yield* appregistry
143 .createApplication({
144 name: applicationName,
145 description: news.description,
146 tags: desiredTags,
147 clientToken: clientToken(instanceId),
148 })
149 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
150 found = yield* observe(applicationName);
151 }
152
153 const applicationId = found!.id!;
154 const applicationArn = found!.arn!;
155
156 // 3a. SYNC description — apply only when it actually changed.
157 if (news.description !== undefined && found!.description !== news.description) {
158 yield* appregistry.updateApplication({
159 application: applicationId,
160 description: news.description,
161 });
162 }
163
164 // 3b. SYNC tags — diff against OBSERVED cloud tags so adoption
165 // converges.
166 const observedTags = stripAwsSystemTags(tagRecord(found!.tags));
167 const { upsert, removed } = diffTags(observedTags, desiredTags);
168 if (upsert.length > 0) {
169 yield* appregistry.tagResource({
170 resourceArn: applicationArn,
171 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
172 });
173 }
174 if (removed.length > 0) {
175 yield* appregistry.untagResource({
176 resourceArn: applicationArn,
177 tagKeys: removed,
178 });
179 }
180
181 yield* session.note(applicationId);
182 return {
183 applicationId,
184 applicationArn,
185 applicationName,
186 };
187 }),
188 delete: Effect.fn(function* ({ output }) {
189 yield* appregistry
190 .deleteApplication({ application: output.applicationId })
191 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
192 }),
193 });
194 }),
195 );