@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 199 lines 7.26 KB
Raw
1 import * as elasticache from "@distilled.cloud/aws/elasticache";
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 Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import type { Providers } from "../Providers.ts";
13 import { readElastiCacheTags, sameStringSet, tagsToWire } from "./internal.ts";
14
15 export class ElastiCacheSubnetGroupUnexpectedResponse extends Data.TaggedError(
16 "ElastiCacheSubnetGroupUnexpectedResponse",
17 )<{
18 readonly message: string;
19 readonly cause?: unknown;
20 }> {}
21
22 export interface SubnetGroupProps {
23 /** Name of the subnet group. Generated deterministically when omitted. */
24 subnetGroupName?: string;
25 /** Human-readable description. */
26 description: string;
27 /** VPC subnet IDs. Use at least two AZs for a highly-available cache. */
28 subnetIds: string[];
29 /** User-defined tags. */
30 tags?: Record<string, string>;
31 }
32
33 export interface SubnetGroup extends Resource<
34 "AWS.ElastiCache.SubnetGroup",
35 SubnetGroupProps,
36 {
37 subnetGroupName: string;
38 subnetGroupArn: string;
39 description: string | undefined;
40 vpcId: string | undefined;
41 subnetIds: string[];
42 tags: Record<string, string>;
43 },
44 never,
45 Providers
46 > {}
47
48 /**
49 * A VPC subnet group for provisioned ElastiCache resources.
50 *
51 * ### Creating a Subnet Group
52 * **Example:** Two Availability Zones
53 * ```typescript
54 * const subnets = yield* SubnetGroup("CacheSubnets", {
55 * description: "cache subnets",
56 * subnetIds: [subnetA.subnetId, subnetB.subnetId],
57 * });
58 * ```
59 *
60 * @resource
61 */
62 export const SubnetGroup = Resource<SubnetGroup>("AWS.ElastiCache.SubnetGroup");
63
64 export const SubnetGroupProvider = () =>
65 Provider.effect(
66 SubnetGroup,
67 Effect.gen(function* () {
68 const toName = (id: string, props: SubnetGroupProps) =>
69 props.subnetGroupName
70 ? Effect.succeed(props.subnetGroupName)
71 : createPhysicalName({ id, maxLength: 40, lowercase: true });
72 const readGroup = Effect.fn(function* (name: string) {
73 const response = yield* elasticache
74 .describeCacheSubnetGroups({ CacheSubnetGroupName: name })
75 .pipe(Effect.catchTag("CacheSubnetGroupNotFoundFault", () => Effect.undefined));
76 return response?.CacheSubnetGroups?.[0];
77 });
78 const attrs = Effect.fn(function* (group: elasticache.CacheSubnetGroup) {
79 if (!group.CacheSubnetGroupName || !group.ARN) {
80 return yield* new ElastiCacheSubnetGroupUnexpectedResponse({
81 message: "ElastiCache subnet group is missing its name or ARN",
82 });
83 }
84 return {
85 subnetGroupName: group.CacheSubnetGroupName,
86 subnetGroupArn: group.ARN,
87 description: group.CacheSubnetGroupDescription,
88 vpcId: group.VpcId,
89 subnetIds: (group.Subnets ?? [])
90 .map((subnet) => subnet.SubnetIdentifier)
91 .filter((id): id is string => id !== undefined),
92 tags: yield* readElastiCacheTags(group.ARN),
93 };
94 });
95
96 return {
97 stables: ["subnetGroupName", "subnetGroupArn"],
98 diff: Effect.fn(function* ({ id, olds, news }) {
99 if (!isResolved(news)) return undefined;
100 if (
101 (yield* toName(id, olds ?? { description: "", subnetIds: [] })) !==
102 (yield* toName(id, news ?? { description: "", subnetIds: [] }))
103 )
104 return { action: "replace" } as const;
105 }),
106 read: Effect.fn(function* ({ id, olds, output }) {
107 const group = yield* readGroup(
108 output?.subnetGroupName ??
109 (yield* toName(id, olds ?? { description: "", subnetIds: [] })),
110 );
111 if (!group?.ARN) return undefined;
112 const result = yield* attrs(group);
113 return (yield* hasAlchemyTags(id, result.tags)) ? result : Unowned(result);
114 }),
115 reconcile: Effect.fn(function* ({ id, news, output, session }) {
116 const props = news!;
117 const name = output?.subnetGroupName ?? (yield* toName(id, props));
118 const desiredTags = {
119 ...(yield* createInternalTags(id)),
120 ...props.tags,
121 };
122 let group = yield* readGroup(name);
123 if (!group) {
124 yield* elasticache
125 .createCacheSubnetGroup({
126 CacheSubnetGroupName: name,
127 CacheSubnetGroupDescription: props.description,
128 SubnetIds: props.subnetIds,
129 Tags: tagsToWire(desiredTags),
130 })
131 .pipe(Effect.catchTag("CacheSubnetGroupAlreadyExistsFault", () => Effect.void));
132 group = yield* readGroup(name);
133 }
134 if (!group)
135 return yield* new ElastiCacheSubnetGroupUnexpectedResponse({
136 message: `Subnet group '${name}' not found after create`,
137 });
138 const observedSubnets = (group.Subnets ?? [])
139 .map((subnet) => subnet.SubnetIdentifier)
140 .filter((id): id is string => id !== undefined);
141 if (
142 group.CacheSubnetGroupDescription !== props.description ||
143 !sameStringSet(observedSubnets, props.subnetIds)
144 ) {
145 group =
146 (yield* elasticache.modifyCacheSubnetGroup({
147 CacheSubnetGroupName: name,
148 CacheSubnetGroupDescription: props.description,
149 SubnetIds: props.subnetIds,
150 })).CacheSubnetGroup ?? group;
151 }
152 if (group.ARN) {
153 const { removed, upsert } = diffTags(
154 yield* readElastiCacheTags(group.ARN),
155 desiredTags,
156 );
157 if (upsert.length)
158 yield* elasticache.addTagsToResource({
159 ResourceName: group.ARN,
160 Tags: upsert,
161 });
162 if (removed.length)
163 yield* elasticache.removeTagsFromResource({
164 ResourceName: group.ARN,
165 TagKeys: removed,
166 });
167 }
168 yield* session.note(name);
169 return yield* attrs(group);
170 }),
171 delete: Effect.fn(function* ({ output }) {
172 yield* elasticache
173 .deleteCacheSubnetGroup({
174 CacheSubnetGroupName: output.subnetGroupName,
175 })
176 .pipe(
177 Effect.catchTag("CacheSubnetGroupNotFoundFault", () => Effect.void),
178 Effect.retry({
179 while: (error) => error._tag === "CacheSubnetGroupInUse",
180 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(8)]),
181 }),
182 );
183 }),
184 list: () =>
185 elasticache.describeCacheSubnetGroups.pages({}).pipe(
186 Stream.runCollect,
187 Effect.map((pages) =>
188 Array.from(pages).flatMap((page) => page.CacheSubnetGroups ?? []),
189 ),
190 Effect.flatMap((groups) =>
191 Effect.forEach(
192 groups.filter((group) => group.ARN),
193 attrs,
194 ),
195 ),
196 ),
197 };
198 }),
199 );