@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, 06:00 UTC

main
ts 179 lines 5.95 KB
Raw
1 import * as route53 from "@distilled.cloud/aws/route-53";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7
8 export interface VpcAssociationAuthorizationProps {
9 /**
10 * ID of the private hosted zone to authorize the association with. Must be
11 * a zone owned by the current account. Changing this forces replacement.
12 */
13 hostedZoneId: string;
14 /**
15 * ID of the VPC that is authorized to be associated with the hosted zone
16 * (typically a VPC owned by a different AWS account). Changing this forces
17 * replacement.
18 */
19 vpcId: string;
20 /**
21 * Region the VPC lives in. Changing this forces replacement.
22 */
23 vpcRegion: string;
24 }
25
26 export interface VpcAssociationAuthorization extends Resource<
27 "AWS.Route53.VpcAssociationAuthorization",
28 VpcAssociationAuthorizationProps,
29 {
30 /**
31 * ID of the private hosted zone.
32 */
33 hostedZoneId: string;
34 /**
35 * ID of the authorized VPC.
36 */
37 vpcId: string;
38 /**
39 * Region of the authorized VPC.
40 */
41 vpcRegion: string;
42 },
43 never,
44 Providers
45 > {}
46
47 /**
48 * Authorization for a VPC (usually in another AWS account) to be associated
49 * with a private hosted zone.
50 *
51 * Cross-account private-zone association is a two-step handshake: the
52 * zone-owning account creates a `VpcAssociationAuthorization` for the foreign
53 * VPC, then the VPC-owning account submits the association (see
54 * `ZoneVpcAssociation`). Same-account associations don't need an
55 * authorization.
56 * ### Authorizing Cross-Account Association
57 * **Example:** Authorize a VPC
58 * ```typescript
59 * const authorization = yield* VpcAssociationAuthorization("PeerVpcAuth", {
60 * hostedZoneId: zone.id,
61 * vpcId: "vpc-0123456789abcdef0", // VPC in the other account
62 * vpcRegion: "us-west-2",
63 * });
64 * ```
65 *
66 * @resource
67 */
68 export const VpcAssociationAuthorization = Resource<VpcAssociationAuthorization>(
69 "AWS.Route53.VpcAssociationAuthorization",
70 );
71
72 export const VpcAssociationAuthorizationProvider = () =>
73 Provider.effect(
74 VpcAssociationAuthorization,
75 Effect.gen(function* () {
76 // Enumerate the zone's pending authorizations and find ours. The list
77 // is per-zone and small; bound pagination defensively.
78 const observe = Effect.fn(function* (hostedZoneId: string, vpcId: string) {
79 let nextToken: string | undefined;
80 for (let page = 0; page < 10; page++) {
81 const response = yield* route53
82 .listVPCAssociationAuthorizations({
83 HostedZoneId: hostedZoneId,
84 NextToken: nextToken,
85 })
86 .pipe(
87 Effect.catchTag("NoSuchHostedZone", () =>
88 Effect.succeed({
89 HostedZoneId: hostedZoneId,
90 VPCs: [],
91 NextToken: undefined,
92 }),
93 ),
94 );
95 const match = (response.VPCs ?? []).find((vpc) => vpc.VPCId === vpcId);
96 if (match) {
97 return match;
98 }
99 if (!response.NextToken) {
100 return undefined;
101 }
102 nextToken = response.NextToken;
103 }
104 return undefined;
105 });
106
107 return {
108 stables: ["hostedZoneId", "vpcId", "vpcRegion"],
109 // Sub-resource: authorizations are keyed by their parent hosted zone
110 // and cannot be enumerated account-wide.
111 list: () => Effect.succeed([]),
112 // Existence-only resource — every property is part of its identity.
113 diff: Effect.fn(function* ({ olds, news }) {
114 if (!isResolved(news)) return undefined;
115 if (
116 olds.hostedZoneId !== news.hostedZoneId ||
117 olds.vpcId !== news.vpcId ||
118 olds.vpcRegion !== news.vpcRegion
119 ) {
120 return { action: "replace" } as const;
121 }
122 }),
123 read: Effect.fn(function* ({ olds, output }) {
124 const hostedZoneId = output?.hostedZoneId ?? olds?.hostedZoneId;
125 const vpcId = output?.vpcId ?? olds?.vpcId;
126 if (hostedZoneId === undefined || vpcId === undefined) {
127 return undefined;
128 }
129 const observed = yield* observe(hostedZoneId, vpcId);
130 if (!observed) {
131 return undefined;
132 }
133 return {
134 hostedZoneId,
135 vpcId,
136 vpcRegion: observed.VPCRegion ?? output?.vpcRegion ?? "",
137 };
138 }),
139 // Existence-only: observe → if missing, create. There is no sync step
140 // (an authorization has no mutable aspects).
141 reconcile: Effect.fn(function* ({ news, session }) {
142 const observed = yield* observe(news.hostedZoneId, news.vpcId);
143 if (!observed) {
144 // `createVPCAssociationAuthorization` is an idempotent upsert for
145 // the same (zone, VPC) pair, so races simply succeed.
146 yield* route53.createVPCAssociationAuthorization({
147 HostedZoneId: news.hostedZoneId,
148 VPC: {
149 VPCId: news.vpcId,
150 VPCRegion: news.vpcRegion as route53.VPCRegion,
151 },
152 });
153 }
154 yield* session.note(`${news.hostedZoneId}/${news.vpcId}`);
155 return {
156 hostedZoneId: news.hostedZoneId,
157 vpcId: news.vpcId,
158 vpcRegion: news.vpcRegion,
159 };
160 }),
161 delete: Effect.fn(function* ({ output }) {
162 yield* route53
163 .deleteVPCAssociationAuthorization({
164 HostedZoneId: output.hostedZoneId,
165 VPC: {
166 VPCId: output.vpcId,
167 VPCRegion: output.vpcRegion as route53.VPCRegion,
168 },
169 })
170 .pipe(
171 Effect.catchTags({
172 VPCAssociationAuthorizationNotFound: () => Effect.void,
173 NoSuchHostedZone: () => Effect.void,
174 }),
175 );
176 }),
177 };
178 }),
179 );