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 vpclattice from "@distilled.cloud/aws/vpc-lattice"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, diffTags, hasAlchemyTags, tagRecord } 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 | import { retryOnConflict, waitUntilAbsent } from "./internal.ts"; |
| 12 | |
| 13 | export class ServiceNetworkVpcAssociationUnexpectedResponse extends Data.TaggedError( |
| 14 | "ServiceNetworkVpcAssociationUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface ServiceNetworkVpcAssociationProps { |
| 21 | /** |
| 22 | * ID or ARN of the service network to associate. Immutable — changing it |
| 23 | * replaces the association. |
| 24 | */ |
| 25 | serviceNetworkIdentifier: string; |
| 26 | /** |
| 27 | * ID of the VPC to associate. Immutable — changing it replaces the |
| 28 | * association. |
| 29 | */ |
| 30 | vpcIdentifier: string; |
| 31 | /** |
| 32 | * Security group IDs controlling access from the VPC to the service network. |
| 33 | */ |
| 34 | securityGroupIds?: string[]; |
| 35 | /** |
| 36 | * User-defined tags to apply to the association. |
| 37 | */ |
| 38 | tags?: Record<string, string>; |
| 39 | } |
| 40 | |
| 41 | export interface ServiceNetworkVpcAssociation extends Resource< |
| 42 | "AWS.VpcLattice.ServiceNetworkVpcAssociation", |
| 43 | ServiceNetworkVpcAssociationProps, |
| 44 | { |
| 45 | /** |
| 46 | * Service-assigned unique ID of the association. |
| 47 | */ |
| 48 | associationId: string; |
| 49 | /** |
| 50 | * ARN of the association. |
| 51 | */ |
| 52 | associationArn: string; |
| 53 | /** |
| 54 | * Current lifecycle status (e.g. `ACTIVE`, `CREATE_IN_PROGRESS`). |
| 55 | */ |
| 56 | status: string; |
| 57 | /** |
| 58 | * ID of the associated service network. |
| 59 | */ |
| 60 | serviceNetworkId?: string; |
| 61 | /** |
| 62 | * ID of the associated VPC. |
| 63 | */ |
| 64 | vpcId?: string; |
| 65 | /** |
| 66 | * Current tags reported for the association. |
| 67 | */ |
| 68 | tags: Record<string, string>; |
| 69 | }, |
| 70 | never, |
| 71 | Providers |
| 72 | > {} |
| 73 | |
| 74 | /** |
| 75 | * Associates a VPC with a VPC Lattice service network, letting workloads in the |
| 76 | * VPC reach every service in the network (subject to auth policies). The most |
| 77 | * common VPC Lattice wiring step. |
| 78 | * |
| 79 | * ### Associating a VPC |
| 80 | * **Example:** Basic Association |
| 81 | * ```typescript |
| 82 | * const assoc = yield* ServiceNetworkVpcAssociation("AppVpcLink", { |
| 83 | * serviceNetworkIdentifier: network.serviceNetworkId, |
| 84 | * vpcIdentifier: vpc.vpcId, |
| 85 | * securityGroupIds: [sg.groupId], |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * @resource |
| 90 | */ |
| 91 | export const ServiceNetworkVpcAssociation = Resource<ServiceNetworkVpcAssociation>( |
| 92 | "AWS.VpcLattice.ServiceNetworkVpcAssociation", |
| 93 | ); |
| 94 | |
| 95 | export const ServiceNetworkVpcAssociationProvider = () => |
| 96 | Provider.effect( |
| 97 | ServiceNetworkVpcAssociation, |
| 98 | Effect.gen(function* () { |
| 99 | const observe = (id: string) => |
| 100 | vpclattice |
| 101 | .getServiceNetworkVpcAssociation({ |
| 102 | serviceNetworkVpcAssociationIdentifier: id, |
| 103 | }) |
| 104 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 105 | |
| 106 | const syncTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) { |
| 107 | const listed = yield* vpclattice.listTagsForResource({ |
| 108 | resourceArn: arn, |
| 109 | }); |
| 110 | const { removed, upsert } = diffTags(tagRecord(listed.tags), desiredTags); |
| 111 | if (upsert.length > 0) { |
| 112 | yield* vpclattice.tagResource({ |
| 113 | resourceArn: arn, |
| 114 | tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 115 | }); |
| 116 | } |
| 117 | if (removed.length > 0) { |
| 118 | yield* vpclattice.untagResource({ |
| 119 | resourceArn: arn, |
| 120 | tagKeys: removed, |
| 121 | }); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | return { |
| 126 | stables: ["associationId", "associationArn"], |
| 127 | diff: Effect.fn(function* ({ olds, news }) { |
| 128 | if (!isResolved(news)) return; |
| 129 | if ( |
| 130 | olds?.serviceNetworkIdentifier !== news.serviceNetworkIdentifier || |
| 131 | olds?.vpcIdentifier !== news.vpcIdentifier |
| 132 | ) { |
| 133 | return { action: "replace" } as const; |
| 134 | } |
| 135 | }), |
| 136 | read: Effect.fn(function* ({ id, output }) { |
| 137 | if (!output?.associationId) return undefined; |
| 138 | const assoc = yield* observe(output.associationId); |
| 139 | if (!assoc?.arn || !assoc.id) return undefined; |
| 140 | const listed = yield* vpclattice.listTagsForResource({ |
| 141 | resourceArn: assoc.arn, |
| 142 | }); |
| 143 | const attrs = { |
| 144 | associationId: assoc.id, |
| 145 | associationArn: assoc.arn, |
| 146 | status: assoc.status ?? "UNKNOWN", |
| 147 | serviceNetworkId: assoc.serviceNetworkId, |
| 148 | vpcId: assoc.vpcId, |
| 149 | tags: tagRecord(listed.tags), |
| 150 | }; |
| 151 | return (yield* hasAlchemyTags(id, listed.tags)) ? attrs : Unowned(attrs); |
| 152 | }), |
| 153 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 154 | const internalTags = yield* createInternalTags(id); |
| 155 | const desiredTags = { ...internalTags, ...news.tags }; |
| 156 | |
| 157 | let assoc = output?.associationId ? yield* observe(output.associationId) : undefined; |
| 158 | |
| 159 | if (!assoc?.arn || !assoc.id) { |
| 160 | const created = yield* vpclattice.createServiceNetworkVpcAssociation({ |
| 161 | serviceNetworkIdentifier: news.serviceNetworkIdentifier, |
| 162 | vpcIdentifier: news.vpcIdentifier, |
| 163 | securityGroupIds: news.securityGroupIds, |
| 164 | tags: desiredTags, |
| 165 | }); |
| 166 | if (!created.arn || !created.id) { |
| 167 | return yield* new ServiceNetworkVpcAssociationUnexpectedResponse({ |
| 168 | message: "Failed to create service network VPC association", |
| 169 | }); |
| 170 | } |
| 171 | assoc = yield* observe(created.id); |
| 172 | if (!assoc?.arn || !assoc.id) { |
| 173 | assoc = { |
| 174 | id: created.id, |
| 175 | arn: created.arn, |
| 176 | status: created.status, |
| 177 | securityGroupIds: created.securityGroupIds, |
| 178 | }; |
| 179 | } |
| 180 | } else if (news.securityGroupIds && news.securityGroupIds.length > 0) { |
| 181 | // Sync security groups (association keeps at least one). |
| 182 | const current = (assoc.securityGroupIds ?? []).slice().sort(); |
| 183 | const desired = news.securityGroupIds.slice().sort(); |
| 184 | if (JSON.stringify(current) !== JSON.stringify(desired)) { |
| 185 | yield* vpclattice.updateServiceNetworkVpcAssociation({ |
| 186 | serviceNetworkVpcAssociationIdentifier: assoc.id, |
| 187 | securityGroupIds: news.securityGroupIds, |
| 188 | }); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Every branch above guarantees id/arn; this narrows the reassigned |
| 193 | // `let` for the type-checker and guards a malformed API response. |
| 194 | if (!assoc?.arn || !assoc.id) { |
| 195 | return yield* new ServiceNetworkVpcAssociationUnexpectedResponse({ |
| 196 | message: "service network VPC association is missing its id/arn", |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | yield* syncTags(assoc.arn, desiredTags); |
| 201 | |
| 202 | yield* session.note(assoc.arn); |
| 203 | return { |
| 204 | associationId: assoc.id, |
| 205 | associationArn: assoc.arn, |
| 206 | status: assoc.status ?? "ACTIVE", |
| 207 | serviceNetworkId: assoc.serviceNetworkId, |
| 208 | vpcId: assoc.vpcId, |
| 209 | tags: desiredTags, |
| 210 | }; |
| 211 | }), |
| 212 | list: () => |
| 213 | Effect.gen(function* () { |
| 214 | // AWS does not support an unfiltered account-wide association |
| 215 | // listing: at least one serviceNetworkIdentifier or vpcIdentifier |
| 216 | // is required. Enumerate service networks first, then list each |
| 217 | // network's associations so nuke can discover orphaned links. |
| 218 | const serviceNetworkIds = yield* vpclattice.listServiceNetworks.pages({}).pipe( |
| 219 | Stream.runCollect, |
| 220 | Effect.map((chunk) => |
| 221 | Array.from(chunk) |
| 222 | .flatMap((page) => page.items ?? []) |
| 223 | .flatMap((network) => (network.id === undefined ? [] : [network.id])), |
| 224 | ), |
| 225 | ); |
| 226 | const summaries = (yield* Effect.forEach( |
| 227 | serviceNetworkIds, |
| 228 | (serviceNetworkIdentifier) => |
| 229 | vpclattice.listServiceNetworkVpcAssociations |
| 230 | .pages({ serviceNetworkIdentifier }) |
| 231 | .pipe( |
| 232 | Stream.runCollect, |
| 233 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.items ?? [])), |
| 234 | ), |
| 235 | { concurrency: 10 }, |
| 236 | )).flat(); |
| 237 | return yield* Effect.forEach( |
| 238 | summaries.filter( |
| 239 | (s): s is typeof s & { id: string; arn: string } => s.id != null && s.arn != null, |
| 240 | ), |
| 241 | (summary) => |
| 242 | Effect.gen(function* () { |
| 243 | const listed = yield* vpclattice.listTagsForResource({ |
| 244 | resourceArn: summary.arn, |
| 245 | }); |
| 246 | return { |
| 247 | associationId: summary.id, |
| 248 | associationArn: summary.arn, |
| 249 | status: summary.status ?? "UNKNOWN", |
| 250 | serviceNetworkId: summary.serviceNetworkId, |
| 251 | vpcId: summary.vpcId, |
| 252 | tags: tagRecord(listed.tags), |
| 253 | }; |
| 254 | }), |
| 255 | { concurrency: 10 }, |
| 256 | ); |
| 257 | }), |
| 258 | delete: Effect.fn(function* ({ output }) { |
| 259 | yield* retryOnConflict( |
| 260 | vpclattice.deleteServiceNetworkVpcAssociation({ |
| 261 | serviceNetworkVpcAssociationIdentifier: output.associationId, |
| 262 | }), |
| 263 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 264 | // Deletion is asynchronous (the association sits in |
| 265 | // DELETE_IN_PROGRESS while hyperplane ENIs are torn down). Wait |
| 266 | // until it is actually gone so a dependent ServiceNetwork delete |
| 267 | // doesn't hit `ConflictException: has VPC(s) associated`. |
| 268 | yield* waitUntilAbsent(observe(output.associationId)); |
| 269 | }), |
| 270 | }; |
| 271 | }), |
| 272 | ); |