The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as dax from "@distilled.cloud/aws/dax"; |
| 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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { sameStringSet } from "./internal.ts"; |
| 11 | |
| 12 | export class DAXSubnetGroupUnexpectedResponse extends Data.TaggedError( |
| 13 | "DAXSubnetGroupUnexpectedResponse", |
| 14 | )<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export interface SubnetGroupProps { |
| 20 | /** |
| 21 | * Name of the subnet group. Must be 1-255 characters. If omitted, a |
| 22 | * deterministic physical name is generated. Changing the name replaces the |
| 23 | * subnet group. |
| 24 | */ |
| 25 | subnetGroupName?: string; |
| 26 | /** |
| 27 | * Human-readable description of the subnet group. |
| 28 | */ |
| 29 | description?: string; |
| 30 | /** |
| 31 | * VPC subnet IDs the subnet group spans. DAX places cluster nodes into |
| 32 | * these subnets; cover at least two Availability Zones for a multi-node |
| 33 | * cluster. |
| 34 | */ |
| 35 | subnetIds: string[]; |
| 36 | } |
| 37 | |
| 38 | export interface SubnetGroup extends Resource< |
| 39 | "AWS.DAX.SubnetGroup", |
| 40 | SubnetGroupProps, |
| 41 | { |
| 42 | /** Name of the subnet group. */ |
| 43 | subnetGroupName: string; |
| 44 | /** Description of the subnet group. */ |
| 45 | description: string | undefined; |
| 46 | /** ID of the VPC the subnets belong to. */ |
| 47 | vpcId: string | undefined; |
| 48 | /** IDs of the subnets in the group. */ |
| 49 | subnetIds: string[]; |
| 50 | }, |
| 51 | never, |
| 52 | Providers |
| 53 | > {} |
| 54 | |
| 55 | /** |
| 56 | * A DAX subnet group — the set of VPC subnets a DAX cluster's nodes are |
| 57 | * placed into. |
| 58 | * |
| 59 | * Subnet groups are free and provision instantly. A {@link Cluster} |
| 60 | * references one by name via `subnetGroupName`. DAX does not support tags on |
| 61 | * subnet groups. |
| 62 | * ### Creating a Subnet Group |
| 63 | * **Example:** Subnet Group Spanning Two Subnets |
| 64 | * ```typescript |
| 65 | * const subnetGroup = yield* SubnetGroup("DaxSubnets", { |
| 66 | * description: "DAX cluster subnets", |
| 67 | * subnetIds: [subnetA.subnetId, subnetB.subnetId], |
| 68 | * }); |
| 69 | * ``` |
| 70 | * |
| 71 | * ### Placing a Cluster |
| 72 | * **Example:** Cluster in the Subnet Group |
| 73 | * ```typescript |
| 74 | * const cluster = yield* Cluster("Cache", { |
| 75 | * nodeType: "dax.t3.small", |
| 76 | * replicationFactor: 1, |
| 77 | * iamRoleArn: role.roleArn, |
| 78 | * subnetGroupName: subnetGroup.subnetGroupName, |
| 79 | * }); |
| 80 | * ``` |
| 81 | * |
| 82 | * @resource |
| 83 | */ |
| 84 | export const SubnetGroup = Resource<SubnetGroup>("AWS.DAX.SubnetGroup"); |
| 85 | |
| 86 | export const SubnetGroupProvider = () => |
| 87 | Provider.effect( |
| 88 | SubnetGroup, |
| 89 | Effect.gen(function* () { |
| 90 | const toName = (id: string, props: SubnetGroupProps) => |
| 91 | props.subnetGroupName |
| 92 | ? Effect.succeed(props.subnetGroupName) |
| 93 | : createPhysicalName({ id, maxLength: 255, lowercase: true }); |
| 94 | |
| 95 | const readGroup = Effect.fn(function* (name: string) { |
| 96 | const response = yield* dax |
| 97 | .describeSubnetGroups({ SubnetGroupNames: [name] }) |
| 98 | .pipe(Effect.catchTag("SubnetGroupNotFoundFault", () => Effect.undefined)); |
| 99 | return response?.SubnetGroups?.[0]; |
| 100 | }); |
| 101 | |
| 102 | const toAttrs = (group: dax.SubnetGroup) => ({ |
| 103 | subnetGroupName: group.SubnetGroupName ?? "", |
| 104 | description: group.Description, |
| 105 | vpcId: group.VpcId, |
| 106 | subnetIds: (group.Subnets ?? []) |
| 107 | .map((s) => s.SubnetIdentifier) |
| 108 | .filter((id): id is string => id !== undefined), |
| 109 | }); |
| 110 | |
| 111 | return { |
| 112 | stables: ["subnetGroupName"], |
| 113 | |
| 114 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 115 | if (!isResolved(news)) return undefined; |
| 116 | if ( |
| 117 | (yield* toName(id, olds ?? { subnetIds: [] })) !== |
| 118 | (yield* toName(id, news ?? { subnetIds: [] })) |
| 119 | ) { |
| 120 | return { action: "replace" } as const; |
| 121 | } |
| 122 | }), |
| 123 | |
| 124 | read: Effect.fn(function* ({ id, olds, output }) { |
| 125 | const name = output?.subnetGroupName ?? (yield* toName(id, olds ?? { subnetIds: [] })); |
| 126 | const group = yield* readGroup(name); |
| 127 | if (group?.SubnetGroupName === undefined) return undefined; |
| 128 | // DAX subnet groups do not support tags, so ownership cannot be |
| 129 | // verified — return the attributes directly. |
| 130 | return toAttrs(group); |
| 131 | }), |
| 132 | |
| 133 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 134 | const props = news ?? { subnetIds: [] }; |
| 135 | const name = output?.subnetGroupName ?? (yield* toName(id, props)); |
| 136 | |
| 137 | // 1. Observe — cloud state is authoritative. |
| 138 | let observed = yield* readGroup(name); |
| 139 | |
| 140 | // 2. Ensure — create if missing; tolerate AlreadyExists as a race. |
| 141 | if (observed === undefined) { |
| 142 | yield* dax |
| 143 | .createSubnetGroup({ |
| 144 | SubnetGroupName: name, |
| 145 | Description: props.description, |
| 146 | SubnetIds: props.subnetIds, |
| 147 | }) |
| 148 | .pipe(Effect.catchTag("SubnetGroupAlreadyExistsFault", () => Effect.void)); |
| 149 | observed = yield* readGroup(name); |
| 150 | } |
| 151 | if (observed === undefined) { |
| 152 | return yield* new DAXSubnetGroupUnexpectedResponse({ |
| 153 | message: `DAX subnet group '${name}' not found after create`, |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | // 3. Sync — apply description / subnet delta from OBSERVED state. |
| 158 | const update: dax.UpdateSubnetGroupRequest = { |
| 159 | SubnetGroupName: name, |
| 160 | }; |
| 161 | let mutated = false; |
| 162 | if (props.description !== undefined && props.description !== observed.Description) { |
| 163 | update.Description = props.description; |
| 164 | mutated = true; |
| 165 | } |
| 166 | const observedSubnets = (observed.Subnets ?? []) |
| 167 | .map((s) => s.SubnetIdentifier) |
| 168 | .filter((s): s is string => s !== undefined); |
| 169 | if (!sameStringSet(props.subnetIds, observedSubnets)) { |
| 170 | update.SubnetIds = props.subnetIds; |
| 171 | mutated = true; |
| 172 | } |
| 173 | if (mutated) { |
| 174 | const response = yield* dax.updateSubnetGroup(update); |
| 175 | observed = response.SubnetGroup ?? observed; |
| 176 | } |
| 177 | |
| 178 | yield* session.note(name); |
| 179 | return toAttrs(observed); |
| 180 | }), |
| 181 | |
| 182 | delete: Effect.fn(function* ({ output }) { |
| 183 | // A subnet group still attached to a cluster rejects deletion with |
| 184 | // SubnetGroupInUseFault — retry (bounded) while the cluster |
| 185 | // releases it. NotFound is success (idempotent delete). |
| 186 | yield* dax.deleteSubnetGroup({ SubnetGroupName: output.subnetGroupName }).pipe( |
| 187 | Effect.catchTag("SubnetGroupNotFoundFault", () => Effect.void), |
| 188 | Effect.retry({ |
| 189 | while: (e) => e._tag === "SubnetGroupInUseFault", |
| 190 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 191 | }), |
| 192 | ); |
| 193 | }), |
| 194 | |
| 195 | list: () => |
| 196 | Effect.gen(function* () { |
| 197 | // DAX has no paginated distilled ops — hand-roll a bounded loop |
| 198 | // and treat an empty/absent NextToken as terminal. |
| 199 | const groups: dax.SubnetGroup[] = []; |
| 200 | let nextToken: string | undefined; |
| 201 | for (let page = 0; page < 20; page++) { |
| 202 | const response = yield* dax.describeSubnetGroups({ |
| 203 | NextToken: nextToken, |
| 204 | }); |
| 205 | groups.push(...(response.SubnetGroups ?? [])); |
| 206 | nextToken = response.NextToken; |
| 207 | if (!nextToken) break; |
| 208 | } |
| 209 | return groups.filter((group) => group.SubnetGroupName !== undefined).map(toAttrs); |
| 210 | }), |
| 211 | }; |
| 212 | }), |
| 213 | ); |