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
| 1 | import * as docdb from "@distilled.cloud/aws/docdb"; |
| 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 { SubnetId } from "../EC2/Subnet.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | export class DBSubnetGroupOperationFailed extends Data.TaggedError("DBSubnetGroupOperationFailed")<{ |
| 14 | readonly message: string; |
| 15 | readonly cause?: unknown; |
| 16 | }> {} |
| 17 | |
| 18 | export class DBSubnetGroupUnexpectedResponse extends Data.TaggedError( |
| 19 | "DBSubnetGroupUnexpectedResponse", |
| 20 | )<{ |
| 21 | readonly message: string; |
| 22 | readonly cause?: unknown; |
| 23 | }> {} |
| 24 | |
| 25 | export interface DBSubnetGroupProps { |
| 26 | /** |
| 27 | * Name of the DB subnet group. If omitted, a deterministic name is generated. |
| 28 | */ |
| 29 | dbSubnetGroupName?: string; |
| 30 | /** |
| 31 | * Description for the subnet group. |
| 32 | * @default "Managed by Alchemy" |
| 33 | */ |
| 34 | description?: string; |
| 35 | /** |
| 36 | * Subnets the DocumentDB cluster may use. Must span at least two |
| 37 | * Availability Zones. |
| 38 | */ |
| 39 | subnetIds: SubnetId[]; |
| 40 | /** |
| 41 | * User-defined tags for the subnet group. |
| 42 | */ |
| 43 | tags?: Record<string, string>; |
| 44 | } |
| 45 | |
| 46 | export interface DBSubnetGroup extends Resource< |
| 47 | "AWS.DocDB.DBSubnetGroup", |
| 48 | DBSubnetGroupProps, |
| 49 | { |
| 50 | /** The subnet group name (unique per account/region). */ |
| 51 | dbSubnetGroupName: string; |
| 52 | /** The ARN of the subnet group. */ |
| 53 | dbSubnetGroupArn: string | undefined; |
| 54 | /** The VPC the subnets belong to. */ |
| 55 | vpcId: string | undefined; |
| 56 | /** The IDs of the subnets in the group. */ |
| 57 | subnetIds: string[]; |
| 58 | /** The current status of the subnet group, e.g. `Complete`. */ |
| 59 | status: string | undefined; |
| 60 | /** The network types the group supports, e.g. `IPV4`. */ |
| 61 | supportedNetworkTypes: string[] | undefined; |
| 62 | /** The tags attached to the subnet group. */ |
| 63 | tags: Record<string, string>; |
| 64 | }, |
| 65 | never, |
| 66 | Providers |
| 67 | > {} |
| 68 | |
| 69 | /** |
| 70 | * An Amazon DocumentDB subnet group — the set of VPC subnets a DocumentDB |
| 71 | * cluster and its instances are placed into. DocumentDB is VPC-only, so a |
| 72 | * subnet group spanning at least two Availability Zones is required before a |
| 73 | * cluster can be created. |
| 74 | * ### Creating a Subnet Group |
| 75 | * **Example:** Multi-AZ subnet group |
| 76 | * ```typescript |
| 77 | * const subnetGroup = yield* DBSubnetGroup("DocDbSubnets", { |
| 78 | * subnetIds: [subnetA.subnetId, subnetB.subnetId], |
| 79 | * }); |
| 80 | * ``` |
| 81 | * |
| 82 | * @resource |
| 83 | */ |
| 84 | export const DBSubnetGroup = Resource<DBSubnetGroup>("AWS.DocDB.DBSubnetGroup"); |
| 85 | |
| 86 | export const DBSubnetGroupProvider = () => |
| 87 | Provider.effect( |
| 88 | DBSubnetGroup, |
| 89 | Effect.gen(function* () { |
| 90 | const toName = (id: string, props: DBSubnetGroupProps) => |
| 91 | props.dbSubnetGroupName |
| 92 | ? Effect.succeed(props.dbSubnetGroupName) |
| 93 | : createPhysicalName({ id, maxLength: 255 }); |
| 94 | |
| 95 | const readGroup = Effect.fn(function* (groupName: string) { |
| 96 | const response = yield* docdb |
| 97 | .describeDBSubnetGroups({ |
| 98 | DBSubnetGroupName: groupName, |
| 99 | }) |
| 100 | .pipe(Effect.catchTag("DBSubnetGroupNotFoundFault", () => Effect.undefined)); |
| 101 | return response?.DBSubnetGroups?.[0]; |
| 102 | }); |
| 103 | |
| 104 | return { |
| 105 | stables: ["dbSubnetGroupArn", "dbSubnetGroupName", "vpcId"], |
| 106 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 107 | if (!isResolved(news)) return undefined; |
| 108 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 109 | return { action: "replace" } as const; |
| 110 | } |
| 111 | }), |
| 112 | read: Effect.fn(function* ({ id, olds, output }) { |
| 113 | const name = |
| 114 | output?.dbSubnetGroupName ?? |
| 115 | (yield* toName(id, olds ?? ({ subnetIds: [] } as DBSubnetGroupProps))); |
| 116 | const group = yield* readGroup(name); |
| 117 | if (!group?.DBSubnetGroupName) { |
| 118 | return undefined; |
| 119 | } |
| 120 | |
| 121 | return { |
| 122 | dbSubnetGroupName: group.DBSubnetGroupName, |
| 123 | dbSubnetGroupArn: group.DBSubnetGroupArn, |
| 124 | vpcId: group.VpcId, |
| 125 | subnetIds: (group.Subnets ?? []).flatMap((subnet) => |
| 126 | subnet.SubnetIdentifier ? [subnet.SubnetIdentifier] : [], |
| 127 | ), |
| 128 | status: group.SubnetGroupStatus, |
| 129 | supportedNetworkTypes: group.SupportedNetworkTypes, |
| 130 | tags: output?.tags ?? {}, |
| 131 | }; |
| 132 | }), |
| 133 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 134 | const dbSubnetGroupName = output?.dbSubnetGroupName ?? (yield* toName(id, news)); |
| 135 | const internalTags = yield* createInternalTags(id); |
| 136 | const desiredTags = { ...internalTags, ...news.tags }; |
| 137 | |
| 138 | // Observe — fetch live subnet-group state. |
| 139 | let observed = yield* readGroup(dbSubnetGroupName); |
| 140 | |
| 141 | // Ensure — create if missing. Tolerate |
| 142 | // `DBSubnetGroupAlreadyExistsFault` as a race with a peer |
| 143 | // reconciler by re-reading. |
| 144 | if (!observed?.DBSubnetGroupName) { |
| 145 | yield* docdb |
| 146 | .createDBSubnetGroup({ |
| 147 | DBSubnetGroupName: dbSubnetGroupName, |
| 148 | DBSubnetGroupDescription: news.description ?? "Managed by Alchemy", |
| 149 | SubnetIds: news.subnetIds, |
| 150 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 151 | Key, |
| 152 | Value, |
| 153 | })), |
| 154 | }) |
| 155 | .pipe(Effect.catchTag("DBSubnetGroupAlreadyExistsFault", () => Effect.void)); |
| 156 | observed = yield* readGroup(dbSubnetGroupName); |
| 157 | if (!observed?.DBSubnetGroupName) { |
| 158 | return yield* new DBSubnetGroupOperationFailed({ |
| 159 | message: `Failed to create DB subnet group '${dbSubnetGroupName}'`, |
| 160 | }); |
| 161 | } |
| 162 | } else { |
| 163 | // Sync description and subnet membership — `modifyDBSubnetGroup` |
| 164 | // accepts the full desired set and is idempotent for unchanged |
| 165 | // input. |
| 166 | yield* docdb.modifyDBSubnetGroup({ |
| 167 | DBSubnetGroupName: dbSubnetGroupName, |
| 168 | DBSubnetGroupDescription: news.description ?? "Managed by Alchemy", |
| 169 | SubnetIds: news.subnetIds, |
| 170 | }); |
| 171 | observed = yield* readGroup(dbSubnetGroupName); |
| 172 | if (!observed?.DBSubnetGroupName) { |
| 173 | return yield* new DBSubnetGroupUnexpectedResponse({ |
| 174 | message: `DB subnet group '${dbSubnetGroupName}' not found after update`, |
| 175 | }); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | const dbSubnetGroupArn = observed.DBSubnetGroupArn; |
| 180 | |
| 181 | // Sync tags — diff prior recorded tags against desired (describe |
| 182 | // does not surface tags inline). |
| 183 | const observedTags = output?.tags ?? {}; |
| 184 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 185 | if (upsert.length > 0 && dbSubnetGroupArn) { |
| 186 | yield* docdb.addTagsToResource({ |
| 187 | ResourceName: dbSubnetGroupArn, |
| 188 | Tags: upsert, |
| 189 | }); |
| 190 | } |
| 191 | if (removed.length > 0 && dbSubnetGroupArn) { |
| 192 | yield* docdb.removeTagsFromResource({ |
| 193 | ResourceName: dbSubnetGroupArn, |
| 194 | TagKeys: removed, |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | yield* session.note(dbSubnetGroupArn ?? dbSubnetGroupName); |
| 199 | return { |
| 200 | dbSubnetGroupName: observed.DBSubnetGroupName, |
| 201 | dbSubnetGroupArn, |
| 202 | vpcId: observed.VpcId, |
| 203 | subnetIds: (observed.Subnets ?? []).flatMap((subnet) => |
| 204 | subnet.SubnetIdentifier ? [subnet.SubnetIdentifier] : [], |
| 205 | ), |
| 206 | status: observed.SubnetGroupStatus, |
| 207 | supportedNetworkTypes: observed.SupportedNetworkTypes, |
| 208 | tags: desiredTags, |
| 209 | }; |
| 210 | }), |
| 211 | list: () => |
| 212 | // AWS account/region collection: `describeDBSubnetGroups` is |
| 213 | // paginated (items field `DBSubnetGroups`). Collect every page and |
| 214 | // map each group into the exact `read` Attributes shape. `read` does |
| 215 | // not hydrate tags from the cloud (it returns the cached |
| 216 | // `output.tags`), so we mirror that here with an empty tag map rather |
| 217 | // than issuing a per-item `listTagsForResource` call. |
| 218 | docdb.describeDBSubnetGroups.pages({}).pipe( |
| 219 | Stream.runCollect, |
| 220 | Effect.map((chunk) => |
| 221 | Array.from(chunk).flatMap((page) => |
| 222 | (page.DBSubnetGroups ?? []).flatMap((group) => |
| 223 | // `default` is the RDS-family subnet group AWS auto-creates |
| 224 | // for the default VPC — never ours, never census/nuke it. |
| 225 | group.DBSubnetGroupName && group.DBSubnetGroupName !== "default" |
| 226 | ? [ |
| 227 | { |
| 228 | dbSubnetGroupName: group.DBSubnetGroupName, |
| 229 | dbSubnetGroupArn: group.DBSubnetGroupArn, |
| 230 | vpcId: group.VpcId, |
| 231 | subnetIds: (group.Subnets ?? []).flatMap((subnet) => |
| 232 | subnet.SubnetIdentifier ? [subnet.SubnetIdentifier] : [], |
| 233 | ), |
| 234 | status: group.SubnetGroupStatus, |
| 235 | supportedNetworkTypes: group.SupportedNetworkTypes, |
| 236 | tags: {} as Record<string, string>, |
| 237 | }, |
| 238 | ] |
| 239 | : [], |
| 240 | ), |
| 241 | ), |
| 242 | ), |
| 243 | ), |
| 244 | delete: Effect.fn(function* ({ output }) { |
| 245 | yield* docdb |
| 246 | .deleteDBSubnetGroup({ |
| 247 | DBSubnetGroupName: output.dbSubnetGroupName, |
| 248 | }) |
| 249 | .pipe(Effect.catchTag("DBSubnetGroupNotFoundFault", () => Effect.void)); |
| 250 | }), |
| 251 | }; |
| 252 | }), |
| 253 | ); |