@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 248 lines 9.23 KB
Raw
1 import * as rds from "@distilled.cloud/aws/rds";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { createInternalTags, diffTags } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11
12 export class RDSDBClusterParameterGroupUnexpectedResponse extends Data.TaggedError(
13 "RDSDBClusterParameterGroupUnexpectedResponse",
14 )<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 export interface DBClusterParameterGroupProps {
20 /**
21 * Name of the parameter group. If omitted, Alchemy generates one.
22 */
23 dbClusterParameterGroupName?: string;
24 /**
25 * Parameter group family, for example `aurora-postgresql16`.
26 */
27 family: string;
28 /**
29 * Human-readable description.
30 */
31 description?: string;
32 /**
33 * User-defined tags.
34 */
35 tags?: Record<string, string>;
36 }
37
38 export interface DBClusterParameterGroup extends Resource<
39 "AWS.RDS.DBClusterParameterGroup",
40 DBClusterParameterGroupProps,
41 {
42 /**
43 * Name of the cluster parameter group.
44 */
45 dbClusterParameterGroupName: string;
46 /**
47 * ARN of the cluster parameter group.
48 */
49 dbClusterParameterGroupArn: string | undefined;
50 /**
51 * Parameter group family (e.g. `aurora-postgresql16`).
52 */
53 family: string;
54 /**
55 * Description of the parameter group.
56 */
57 description: string | undefined;
58 /**
59 * Tags on the parameter group.
60 */
61 tags: Record<string, string>;
62 },
63 never,
64 Providers
65 > {}
66
67 /**
68 * An Aurora cluster parameter group — cluster-wide engine settings shared by
69 * every instance in a `DBCluster`.
70 *
71 * Name, family, and description changes force a replacement (RDS has no
72 * modify API for these); tags update in place.
73 * ### Creating a Cluster Parameter Group
74 * **Example:** Parameter Group for Aurora Postgres 16
75 * ```typescript
76 * const clusterParams = yield* DBClusterParameterGroup("ClusterParams", {
77 * family: "aurora-postgresql16",
78 * description: "Cluster-wide settings for the app database",
79 * });
80 * ```
81 *
82 * **Example:** Attach to a Cluster
83 * ```typescript
84 * const cluster = yield* DBCluster("Cluster", {
85 * engine: "aurora-postgresql",
86 * dbClusterParameterGroupName: clusterParams.dbClusterParameterGroupName,
87 * });
88 * ```
89 *
90 * @resource
91 */
92 export const DBClusterParameterGroup = Resource<DBClusterParameterGroup>(
93 "AWS.RDS.DBClusterParameterGroup",
94 );
95
96 export const DBClusterParameterGroupProvider = () =>
97 Provider.effect(
98 DBClusterParameterGroup,
99 Effect.gen(function* () {
100 const toName = (id: string, props: DBClusterParameterGroupProps) =>
101 props.dbClusterParameterGroupName
102 ? Effect.succeed(props.dbClusterParameterGroupName)
103 : createPhysicalName({ id, maxLength: 255 });
104
105 const readGroup = Effect.fn(function* (name: string) {
106 const response = yield* rds
107 .describeDBClusterParameterGroups({
108 DBClusterParameterGroupName: name,
109 })
110 .pipe(Effect.catchTag("DBParameterGroupNotFoundFault", () => Effect.undefined));
111 return response?.DBClusterParameterGroups?.[0];
112 });
113
114 return {
115 stables: ["dbClusterParameterGroupArn", "dbClusterParameterGroupName"],
116 list: () =>
117 // AWS account/region collection (pattern (a)): exhaustively paginate
118 // describeDBClusterParameterGroups and map each group to the exact
119 // `read` Attributes shape. `read` derives `tags` from the cached
120 // output (the describe response does not surface tags), so list
121 // returns `tags: {}` to match — a future read/delete can hydrate
122 // them via listTagsForResource. Per-item not-found
123 // (DBParameterGroupNotFoundFault) cannot occur here: enumeration
124 // passes no name, so the describe never targets a single group.
125 rds.describeDBClusterParameterGroups.pages({}).pipe(
126 Stream.runCollect,
127 Effect.map((chunk) =>
128 Array.from(chunk).flatMap((page) =>
129 (page.DBClusterParameterGroups ?? [])
130 .filter(
131 (
132 g,
133 ): g is rds.DBClusterParameterGroup & {
134 DBClusterParameterGroupName: string;
135 } =>
136 g.DBClusterParameterGroupName != null &&
137 // AWS-managed `default.*` groups cannot be deleted
138 // (InvalidDBParameterGroupStateFault) — don't enumerate.
139 !g.DBClusterParameterGroupName.startsWith("default."),
140 )
141 .map((g) => ({
142 dbClusterParameterGroupName: g.DBClusterParameterGroupName,
143 dbClusterParameterGroupArn: g.DBClusterParameterGroupArn,
144 family: g.DBParameterGroupFamily ?? "",
145 description: g.Description,
146 tags: {} as Record<string, string>,
147 })),
148 ),
149 ),
150 ),
151 diff: Effect.fn(function* ({ id, olds, news }) {
152 if (!isResolved(news)) return;
153 if (
154 (yield* toName(id, olds ?? ({} as DBClusterParameterGroupProps))) !==
155 (yield* toName(id, news))
156 ) {
157 return { action: "replace" } as const;
158 }
159 if (olds?.family !== news.family || olds?.description !== news.description) {
160 return { action: "replace" } as const;
161 }
162 }),
163 read: Effect.fn(function* ({ id, olds, output }) {
164 const name =
165 output?.dbClusterParameterGroupName ??
166 (yield* toName(id, olds ?? ({ family: "" } as DBClusterParameterGroupProps)));
167 const group = yield* readGroup(name);
168 if (!group?.DBClusterParameterGroupName) {
169 return undefined;
170 }
171 return {
172 dbClusterParameterGroupName: group.DBClusterParameterGroupName,
173 dbClusterParameterGroupArn: group.DBClusterParameterGroupArn,
174 family: group.DBParameterGroupFamily ?? olds?.family ?? "",
175 description: group.Description,
176 tags: output?.tags ?? {},
177 };
178 }),
179 reconcile: Effect.fn(function* ({ id, news, output, session }) {
180 const name = output?.dbClusterParameterGroupName ?? (yield* toName(id, news));
181 const internalTags = yield* createInternalTags(id);
182 const desiredTags = { ...internalTags, ...news.tags };
183
184 // Observe — fetch the parameter group's live state.
185 let observed = yield* readGroup(name);
186
187 // Ensure — create if missing. Tolerate
188 // `DBParameterGroupAlreadyExistsFault` as a race with a peer
189 // reconciler by re-reading.
190 if (!observed?.DBClusterParameterGroupName) {
191 yield* rds
192 .createDBClusterParameterGroup({
193 DBClusterParameterGroupName: name,
194 DBParameterGroupFamily: news.family,
195 Description: news.description ?? `Alchemy parameter group ${name}`,
196 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
197 Key,
198 Value,
199 })),
200 })
201 .pipe(Effect.catchTag("DBParameterGroupAlreadyExistsFault", () => Effect.void));
202 observed = yield* readGroup(name);
203 if (!observed?.DBClusterParameterGroupName) {
204 return yield* new RDSDBClusterParameterGroupUnexpectedResponse({
205 message: `Failed to create DB cluster parameter group '${name}'`,
206 });
207 }
208 }
209
210 const dbClusterParameterGroupArn = observed.DBClusterParameterGroupArn;
211
212 // Sync tags — diff observed (the describe response does not
213 // surface tags, so use prior `output.tags` as the baseline) ↔
214 // desired.
215 const observedTags = output?.tags ?? {};
216 const { removed, upsert } = diffTags(observedTags, desiredTags);
217 if (upsert.length > 0 && dbClusterParameterGroupArn) {
218 yield* rds.addTagsToResource({
219 ResourceName: dbClusterParameterGroupArn,
220 Tags: upsert,
221 });
222 }
223 if (removed.length > 0 && dbClusterParameterGroupArn) {
224 yield* rds.removeTagsFromResource({
225 ResourceName: dbClusterParameterGroupArn,
226 TagKeys: removed,
227 });
228 }
229
230 yield* session.note(dbClusterParameterGroupArn ?? name);
231 return {
232 dbClusterParameterGroupName: observed.DBClusterParameterGroupName,
233 dbClusterParameterGroupArn,
234 family: observed.DBParameterGroupFamily ?? news.family,
235 description: observed.Description,
236 tags: desiredTags,
237 };
238 }),
239 delete: Effect.fn(function* ({ output }) {
240 yield* rds
241 .deleteDBClusterParameterGroup({
242 DBClusterParameterGroupName: output.dbClusterParameterGroupName,
243 })
244 .pipe(Effect.catchTag("DBParameterGroupNotFoundFault", () => Effect.void));
245 }),
246 };
247 }),
248 );