@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 539 lines 22.3 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 ReplicationGroupNotReady extends Data.TaggedError("ReplicationGroupNotReady")<{
16 readonly message: string;
17 readonly cause?: unknown;
18 }> {}
19
20 export class ReplicationGroupUnexpectedResponse extends Data.TaggedError(
21 "ReplicationGroupUnexpectedResponse",
22 )<{
23 readonly message: string;
24 readonly cause?: unknown;
25 }> {}
26
27 export interface ReplicationGroupProps {
28 /** Replication group identifier. Generated deterministically when omitted. */
29 replicationGroupId?: string;
30 /** Required human-readable description. */
31 description: string;
32 /** Cache engine. */
33 engine: "valkey" | "redis";
34 engineVersion?: string;
35 /** ElastiCache node type. Must start with `cache.`, for example `cache.t4g.micro`. @default cache.t4g.micro */
36 nodeType?: `cache.${string}`;
37 subnetGroupName?: string;
38 securityGroupIds?: string[];
39 /** Shard count. Changes use online resharding. */
40 numNodeGroups?: number;
41 /** Read replicas per shard, from 0 through 5. */
42 replicasPerNodeGroup?: number;
43 automaticFailoverEnabled?: boolean;
44 multiAzEnabled?: boolean;
45 parameterGroupName?: string;
46 port?: number;
47 maintenanceWindow?: string;
48 snapshotRetentionLimit?: number;
49 snapshotWindow?: string;
50 autoMinorVersionUpgrade?: boolean;
51 /** Enables TLS for client connections. @default true */
52 transitEncryptionEnabled?: boolean;
53 transitEncryptionMode?: elasticache.TransitEncryptionMode;
54 atRestEncryptionEnabled?: boolean;
55 kmsKeyId?: string;
56 networkType?: elasticache.NetworkType;
57 ipDiscovery?: elasticache.IpDiscovery;
58 /** User group IDs for Valkey/Redis RBAC. */
59 userGroupIds?: string[];
60 /** Snapshot used only while creating a new group. */
61 snapshotName?: string;
62 snapshotArns?: string[];
63 /** Optional final snapshot created before destroy. */
64 finalSnapshotName?: string;
65 tags?: Record<string, string>;
66 }
67
68 export interface ReplicationGroup extends Resource<
69 "AWS.ElastiCache.ReplicationGroup",
70 ReplicationGroupProps,
71 {
72 replicationGroupId: string;
73 replicationGroupArn: string;
74 status: string;
75 engine: string;
76 engineVersion: string | undefined;
77 nodeType: string | undefined;
78 configurationEndpointAddress: string | undefined;
79 configurationEndpointPort: number | undefined;
80 primaryEndpointAddress: string | undefined;
81 primaryEndpointPort: number | undefined;
82 readerEndpointAddress: string | undefined;
83 readerEndpointPort: number | undefined;
84 transitEncryptionEnabled: boolean;
85 nodeGroupIds: string[];
86 tags: Record<string, string>;
87 },
88 never,
89 Providers
90 > {}
91
92 /** Invalid desired topology rejected before AWS can start a partial create. */
93 export class InvalidReplicationGroupConfiguration extends Data.TaggedError(
94 "InvalidReplicationGroupConfiguration",
95 )<{ message: string }> {}
96
97 export const validateReplicationGroupProps = (
98 props: ReplicationGroupProps,
99 ): InvalidReplicationGroupConfiguration | undefined => {
100 const invalid = (message: string) => new InvalidReplicationGroupConfiguration({ message });
101 if (
102 props.port !== undefined &&
103 (!Number.isInteger(props.port) || props.port < 1 || props.port > 65_535)
104 ) {
105 return invalid("port must be an integer from 1 through 65535");
106 }
107 if (
108 props.numNodeGroups !== undefined &&
109 (!Number.isInteger(props.numNodeGroups) || props.numNodeGroups < 1)
110 ) {
111 return invalid("numNodeGroups must be an integer of at least 1");
112 }
113 if (
114 props.replicasPerNodeGroup !== undefined &&
115 (!Number.isInteger(props.replicasPerNodeGroup) ||
116 props.replicasPerNodeGroup < 0 ||
117 props.replicasPerNodeGroup > 5)
118 ) {
119 return invalid("replicasPerNodeGroup must be an integer from 0 through 5");
120 }
121 if (props.automaticFailoverEnabled === true && (props.replicasPerNodeGroup ?? 0) < 1) {
122 return invalid("automaticFailoverEnabled requires replicasPerNodeGroup to be at least 1");
123 }
124 if (props.multiAzEnabled === true && (props.replicasPerNodeGroup ?? 0) < 1) {
125 return invalid("multiAzEnabled requires replicasPerNodeGroup to be at least 1");
126 }
127 if (props.multiAzEnabled === true && props.automaticFailoverEnabled !== true) {
128 return invalid("multiAzEnabled requires automaticFailoverEnabled to be true");
129 }
130 if (props.transitEncryptionMode !== undefined && props.transitEncryptionEnabled === false) {
131 return invalid("transitEncryptionMode requires transitEncryptionEnabled");
132 }
133 return undefined;
134 };
135
136 /**
137 * A provisioned Valkey or Redis OSS replication group.
138 *
139 * ### Creating a Highly Available Cache
140 * **Example:** Valkey with a replica in a VPC
141 * ```typescript
142 * const cache = yield* ReplicationGroup("Cache", {
143 * description: "application cache",
144 * engine: "valkey",
145 * subnetGroupName: subnets.subnetGroupName,
146 * securityGroupIds: [cacheSecurityGroup.groupId],
147 * replicasPerNodeGroup: 1,
148 * automaticFailoverEnabled: true,
149 * multiAzEnabled: true,
150 * transitEncryptionEnabled: true,
151 * });
152 * ```
153 *
154 * @resource
155 */
156 export const ReplicationGroup = Resource<ReplicationGroup>("AWS.ElastiCache.ReplicationGroup");
157
158 const DEFAULT_NODE_TYPE = "cache.t4g.micro";
159
160 export const ReplicationGroupProvider = () =>
161 Provider.effect(
162 ReplicationGroup,
163 Effect.gen(function* () {
164 const toName = (id: string, props: Partial<ReplicationGroupProps>) =>
165 props.replicationGroupId
166 ? Effect.succeed(props.replicationGroupId)
167 : createPhysicalName({ id, maxLength: 40, lowercase: true });
168 const readGroup = Effect.fn(function* (name: string) {
169 const response = yield* elasticache
170 .describeReplicationGroups({ ReplicationGroupId: name })
171 .pipe(Effect.catchTag("ReplicationGroupNotFoundFault", () => Effect.undefined));
172 return response?.ReplicationGroups?.[0];
173 });
174 const waitForAvailable = Effect.fn(function* (name: string) {
175 return yield* readGroup(name).pipe(
176 Effect.flatMap((group) =>
177 group?.Status === "available"
178 ? Effect.succeed(group)
179 : Effect.fail(
180 new ReplicationGroupNotReady({
181 message: `Replication group '${name}' is not available`,
182 }),
183 ),
184 ),
185 Effect.retry({
186 schedule: Schedule.max([
187 Schedule.fixed("15 seconds"),
188 // Engine upgrades can remain in "modifying" for more than
189 // fifteen minutes before ElastiCache reports "available".
190 Schedule.recurs(100),
191 ]),
192 }),
193 );
194 });
195 const waitForDeleted = Effect.fn(function* (name: string) {
196 return yield* readGroup(name).pipe(
197 Effect.flatMap((group) =>
198 group === undefined
199 ? Effect.void
200 : Effect.fail(
201 new ReplicationGroupNotReady({
202 message: `Replication group '${name}' is still deleting`,
203 }),
204 ),
205 ),
206 Effect.retry({
207 schedule: Schedule.max([
208 Schedule.fixed("15 seconds"),
209 // Multi-AZ failover deletes can remain visible longer than the
210 // normal fifteen-minute lifecycle window.
211 Schedule.recurs(100),
212 ]),
213 }),
214 );
215 });
216 const retryWhileCacheTransitioning = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
217 effect.pipe(
218 Effect.retry({
219 while: (error) =>
220 (error as { _tag?: string })._tag === "InvalidReplicationGroupStateFault" ||
221 (error as { _tag?: string })._tag === "InvalidCacheClusterStateFault",
222 schedule: Schedule.max([
223 Schedule.fixed("15 seconds"),
224 // AWS can report the group as available before its member
225 // clusters accept tag changes after an in-place update.
226 Schedule.recurs(100),
227 ]),
228 }),
229 );
230 const attrs = Effect.fn(function* (group: elasticache.ReplicationGroup) {
231 if (!group.ReplicationGroupId || !group.ARN || !group.Status || !group.Engine) {
232 return yield* new ReplicationGroupUnexpectedResponse({
233 message: "ElastiCache replication group is missing its id, ARN, status, or engine",
234 });
235 }
236 const engineVersion = group.MemberClusters?.[0]
237 ? (yield* elasticache.describeCacheClusters({
238 CacheClusterId: group.MemberClusters[0],
239 })).CacheClusters?.[0]?.EngineVersion
240 : undefined;
241 const primary = group.NodeGroups?.[0]?.PrimaryEndpoint;
242 const reader = group.NodeGroups?.[0]?.ReaderEndpoint;
243 return {
244 replicationGroupId: group.ReplicationGroupId,
245 replicationGroupArn: group.ARN,
246 status: group.Status,
247 engine: group.Engine,
248 engineVersion,
249 nodeType: group.CacheNodeType,
250 configurationEndpointAddress: group.ConfigurationEndpoint?.Address,
251 configurationEndpointPort: group.ConfigurationEndpoint?.Port,
252 primaryEndpointAddress: primary?.Address,
253 primaryEndpointPort: primary?.Port,
254 readerEndpointAddress: reader?.Address,
255 readerEndpointPort: reader?.Port,
256 transitEncryptionEnabled: group.TransitEncryptionEnabled ?? false,
257 nodeGroupIds: (group.NodeGroups ?? [])
258 .map((nodeGroup) => nodeGroup.NodeGroupId)
259 .filter((id): id is string => id !== undefined),
260 tags: yield* readElastiCacheTags(group.ARN),
261 };
262 });
263 const replacement = (
264 olds: Partial<ReplicationGroupProps>,
265 news: Partial<ReplicationGroupProps>,
266 ) =>
267 olds.subnetGroupName !== news.subnetGroupName ||
268 olds.port !== news.port ||
269 olds.kmsKeyId !== news.kmsKeyId ||
270 olds.atRestEncryptionEnabled !== news.atRestEncryptionEnabled ||
271 olds.networkType !== news.networkType ||
272 olds.snapshotName !== news.snapshotName ||
273 // AWS cannot enable cluster mode on an existing non-cluster-mode
274 // replication group, so scaling from one shard must replace it.
275 ((olds.numNodeGroups ?? 1) <= 1 && (news.numNodeGroups ?? 1) > 1) ||
276 !sameStringSet(olds.snapshotArns, news.snapshotArns);
277
278 return {
279 stables: ["replicationGroupId", "replicationGroupArn"],
280 diff: Effect.fn(function* ({ id, olds, news }) {
281 if (!isResolved(news)) return undefined;
282 if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) {
283 return { action: "replace" } as const;
284 }
285 if (replacement(olds ?? {}, news ?? {})) return { action: "replace" } as const;
286 }),
287 read: Effect.fn(function* ({ id, olds, output }) {
288 const group = yield* readGroup(
289 output?.replicationGroupId ?? (yield* toName(id, olds ?? {})),
290 );
291 if (!group?.ARN) return undefined;
292 const result = yield* attrs(group);
293 return (yield* hasAlchemyTags(id, result.tags)) ? result : Unowned(result);
294 }),
295 reconcile: Effect.fn(function* ({ id, news, output, session }) {
296 const props = news!;
297 const invalid = validateReplicationGroupProps(props);
298 if (invalid) return yield* invalid;
299 const name = output?.replicationGroupId ?? (yield* toName(id, props));
300 const desiredTags = {
301 ...(yield* createInternalTags(id)),
302 ...props.tags,
303 };
304 let group = yield* readGroup(name);
305 if (!group) {
306 yield* elasticache
307 .createReplicationGroup({
308 ReplicationGroupId: name,
309 ReplicationGroupDescription: props.description,
310 Engine: props.engine,
311 EngineVersion: props.engineVersion,
312 CacheNodeType: props.nodeType ?? DEFAULT_NODE_TYPE,
313 CacheSubnetGroupName: props.subnetGroupName,
314 SecurityGroupIds: props.securityGroupIds,
315 NumNodeGroups: props.numNodeGroups,
316 ReplicasPerNodeGroup: props.replicasPerNodeGroup,
317 AutomaticFailoverEnabled: props.automaticFailoverEnabled,
318 MultiAZEnabled: props.multiAzEnabled,
319 CacheParameterGroupName: props.parameterGroupName,
320 Port: props.port,
321 PreferredMaintenanceWindow: props.maintenanceWindow,
322 SnapshotRetentionLimit: props.snapshotRetentionLimit,
323 SnapshotWindow: props.snapshotWindow,
324 AutoMinorVersionUpgrade: props.autoMinorVersionUpgrade,
325 TransitEncryptionEnabled: props.transitEncryptionEnabled ?? true,
326 TransitEncryptionMode: props.transitEncryptionMode,
327 AtRestEncryptionEnabled: props.atRestEncryptionEnabled,
328 KmsKeyId: props.kmsKeyId,
329 NetworkType: props.networkType,
330 IpDiscovery: props.ipDiscovery,
331 UserGroupIds: props.userGroupIds,
332 SnapshotName: props.snapshotName,
333 SnapshotArns: props.snapshotArns,
334 Tags: tagsToWire(desiredTags),
335 })
336 .pipe(Effect.catchTag("ReplicationGroupAlreadyExistsFault", () => Effect.void));
337 }
338 group = yield* waitForAvailable(name);
339 const update: elasticache.ModifyReplicationGroupMessage = {
340 ReplicationGroupId: name,
341 ApplyImmediately: true,
342 };
343 let changed = false;
344 const set = <K extends keyof elasticache.ModifyReplicationGroupMessage>(
345 key: K,
346 desired: elasticache.ModifyReplicationGroupMessage[K],
347 observed: unknown,
348 ) => {
349 if (desired !== undefined && desired !== observed) {
350 update[key] = desired;
351 changed = true;
352 }
353 };
354 set("ReplicationGroupDescription", props.description, group.Description);
355 set("CacheNodeType", props.nodeType, group.CacheNodeType);
356 set("Engine", props.engine, group.Engine);
357 set(
358 "AutoMinorVersionUpgrade",
359 props.autoMinorVersionUpgrade,
360 group.AutoMinorVersionUpgrade,
361 );
362 set("PreferredMaintenanceWindow", props.maintenanceWindow, undefined);
363 set("SnapshotRetentionLimit", props.snapshotRetentionLimit, group.SnapshotRetentionLimit);
364 set("SnapshotWindow", props.snapshotWindow, group.SnapshotWindow);
365 set("CacheParameterGroupName", props.parameterGroupName, undefined);
366 set("IpDiscovery", props.ipDiscovery, group.IpDiscovery);
367 set("TransitEncryptionMode", props.transitEncryptionMode, group.TransitEncryptionMode);
368 // ElastiCache reports VPC security groups and engine version on a
369 // member cluster, not on the replication-group response.
370 const member = group.MemberClusters?.[0]
371 ? (yield* elasticache.describeCacheClusters({
372 CacheClusterId: group.MemberClusters[0],
373 })).CacheClusters?.[0]
374 : undefined;
375 const existingSecurityGroups = (member?.SecurityGroups ?? [])
376 .map((membership) => membership.SecurityGroupId)
377 .filter((id): id is string => id !== undefined);
378 set("EngineVersion", props.engineVersion, member?.EngineVersion);
379 if (
380 props.securityGroupIds &&
381 !sameStringSet(props.securityGroupIds, existingSecurityGroups)
382 ) {
383 update.SecurityGroupIds = props.securityGroupIds;
384 changed = true;
385 }
386 const existingUsers = group.UserGroupIds ?? [];
387 if (props.userGroupIds && !sameStringSet(props.userGroupIds, existingUsers)) {
388 update.UserGroupIdsToAdd = props.userGroupIds.filter(
389 (id) => !existingUsers.includes(id),
390 );
391 update.UserGroupIdsToRemove = existingUsers.filter(
392 (id) => !props.userGroupIds!.includes(id),
393 );
394 changed = true;
395 }
396 if (changed) {
397 yield* elasticache.modifyReplicationGroup(update);
398 group = yield* waitForAvailable(name);
399 }
400 const currentNodeGroups = group.NodeGroups?.length ?? 1;
401 if (props.numNodeGroups !== undefined && props.numNodeGroups !== currentNodeGroups) {
402 yield* elasticache.modifyReplicationGroupShardConfiguration({
403 ReplicationGroupId: name,
404 NodeGroupCount: props.numNodeGroups,
405 ApplyImmediately: true,
406 ...(props.numNodeGroups < currentNodeGroups
407 ? {
408 NodeGroupsToRemove: (group.NodeGroups ?? [])
409 .slice(props.numNodeGroups)
410 .map((node) => node.NodeGroupId)
411 .filter((id): id is string => id !== undefined),
412 }
413 : {}),
414 });
415 group = yield* waitForAvailable(name);
416 }
417 const setAutomaticFailover = Effect.fn(function* (enabled: boolean) {
418 const current = group;
419 if (!current) {
420 return yield* Effect.die(
421 `Replication group '${name}' disappeared while updating automatic failover`,
422 );
423 }
424 if ((current.AutomaticFailover === "enabled") === enabled) return;
425 yield* retryWhileCacheTransitioning(
426 elasticache.modifyReplicationGroup({
427 ReplicationGroupId: name,
428 AutomaticFailoverEnabled: enabled,
429 ApplyImmediately: true,
430 }),
431 );
432 group = yield* waitForAvailable(name);
433 });
434 const setMultiAz = Effect.fn(function* (enabled: boolean) {
435 const current = group;
436 if (!current) {
437 return yield* Effect.die(
438 `Replication group '${name}' disappeared while updating Multi-AZ`,
439 );
440 }
441 if ((current.MultiAZ === "enabled") === enabled) return;
442 yield* retryWhileCacheTransitioning(
443 elasticache.modifyReplicationGroup({
444 ReplicationGroupId: name,
445 MultiAZEnabled: enabled,
446 ApplyImmediately: true,
447 }),
448 );
449 group = yield* waitForAvailable(name);
450 });
451 if (props.replicasPerNodeGroup !== undefined) {
452 const replicaCount = Math.max(
453 ...(group.NodeGroups ?? []).map((node) =>
454 Math.max(0, (node.NodeGroupMembers?.length ?? 1) - 1),
455 ),
456 0,
457 );
458 // AWS rejects HA before the first replica exists, and rejects
459 // removing the final replica while either HA setting is on.
460 if (props.replicasPerNodeGroup === 0 && replicaCount > 0) {
461 yield* setMultiAz(false);
462 yield* setAutomaticFailover(false);
463 }
464 if (props.replicasPerNodeGroup > replicaCount) {
465 yield* elasticache.increaseReplicaCount({
466 ReplicationGroupId: name,
467 NewReplicaCount: props.replicasPerNodeGroup,
468 ApplyImmediately: true,
469 });
470 group = yield* waitForAvailable(name);
471 } else if (props.replicasPerNodeGroup < replicaCount) {
472 yield* elasticache.decreaseReplicaCount({
473 ReplicationGroupId: name,
474 NewReplicaCount: props.replicasPerNodeGroup,
475 ApplyImmediately: true,
476 });
477 group = yield* waitForAvailable(name);
478 }
479 }
480 if (props.automaticFailoverEnabled !== undefined) {
481 yield* setAutomaticFailover(props.automaticFailoverEnabled);
482 }
483 if (props.multiAzEnabled !== undefined) {
484 yield* setMultiAz(props.multiAzEnabled);
485 }
486 if (group.ARN) {
487 const { removed, upsert } = diffTags(
488 yield* retryWhileCacheTransitioning(readElastiCacheTags(group.ARN)),
489 desiredTags,
490 );
491 if (upsert.length)
492 yield* retryWhileCacheTransitioning(
493 elasticache.addTagsToResource({
494 ResourceName: group.ARN,
495 Tags: upsert,
496 }),
497 );
498 if (removed.length)
499 yield* retryWhileCacheTransitioning(
500 elasticache.removeTagsFromResource({
501 ResourceName: group.ARN,
502 TagKeys: removed,
503 }),
504 );
505 // AWS can accept a tag update before ListTagsForResource is
506 // available again. Do not report the resource as reconciled
507 // until callers can observe the finished tag state.
508 yield* retryWhileCacheTransitioning(readElastiCacheTags(group.ARN));
509 }
510 yield* session.note(name);
511 return yield* attrs(group);
512 }),
513 delete: Effect.fn(function* ({ olds, output }) {
514 yield* retryWhileCacheTransitioning(
515 elasticache
516 .deleteReplicationGroup({
517 ReplicationGroupId: output.replicationGroupId,
518 FinalSnapshotIdentifier: olds.finalSnapshotName,
519 })
520 .pipe(Effect.catchTag("ReplicationGroupNotFoundFault", () => Effect.void)),
521 );
522 yield* waitForDeleted(output.replicationGroupId);
523 }),
524 list: () =>
525 elasticache.describeReplicationGroups.pages({}).pipe(
526 Stream.runCollect,
527 Effect.map((pages) =>
528 Array.from(pages).flatMap((page) => page.ReplicationGroups ?? []),
529 ),
530 Effect.flatMap((groups) =>
531 Effect.forEach(
532 groups.filter((group) => group.ARN),
533 attrs,
534 ),
535 ),
536 ),
537 };
538 }),
539 );