@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 188 lines 6.14 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 * as Schedule from "effect/Schedule";
7 import type { Providers } from "../Providers.ts";
8
9 export interface ZoneVpcAssociationProps {
10 /**
11 * ID of the private hosted zone to associate the VPC with. Changing this
12 * forces replacement.
13 */
14 hostedZoneId: string;
15 /**
16 * ID of the VPC to associate. The VPC must have DNS support and DNS
17 * hostnames enabled. Changing this forces replacement.
18 */
19 vpcId: string;
20 /**
21 * Region the VPC lives in. Changing this forces replacement.
22 */
23 vpcRegion: string;
24 /**
25 * Optional comment recorded on the association request.
26 */
27 comment?: string;
28 }
29
30 export interface ZoneVpcAssociation extends Resource<
31 "AWS.Route53.ZoneVpcAssociation",
32 ZoneVpcAssociationProps,
33 {
34 /**
35 * ID of the private hosted zone.
36 */
37 hostedZoneId: string;
38 /**
39 * ID of the associated VPC.
40 */
41 vpcId: string;
42 /**
43 * Region of the associated VPC.
44 */
45 vpcRegion: string;
46 },
47 never,
48 Providers
49 > {}
50
51 /**
52 * An association between an additional VPC and a private hosted zone.
53 *
54 * A private hosted zone is created with one initial VPC (see
55 * `HostedZone.vpc`); `ZoneVpcAssociation` attaches further VPCs so their DNS
56 * resolvers can answer from the zone. For a VPC in a different account, the
57 * zone owner must first create a `VpcAssociationAuthorization` for it.
58 *
59 * The initial VPC of a private zone cannot be modeled with this resource —
60 * Route 53 refuses to disassociate the last VPC from a private zone.
61 * ### Associating VPCs
62 * **Example:** Attach a Second VPC
63 * ```typescript
64 * const zone = yield* HostedZone("InternalZone", {
65 * name: "internal.example.com",
66 * privateZone: true,
67 * vpc: { vpcId: primary.vpcId, vpcRegion: "us-west-2" },
68 * });
69 *
70 * const association = yield* ZoneVpcAssociation("SecondaryVpc", {
71 * hostedZoneId: zone.id,
72 * vpcId: secondary.vpcId,
73 * vpcRegion: "us-west-2",
74 * });
75 * ```
76 *
77 * @resource
78 */
79 export const ZoneVpcAssociation = Resource<ZoneVpcAssociation>("AWS.Route53.ZoneVpcAssociation");
80
81 /**
82 * Route 53 zone-change submissions can collide with other in-flight changes
83 * on the same zone (`PriorRequestNotComplete`) — retry on a bounded schedule
84 * (~40s).
85 *
86 * Explicitly typed: inlining `Effect.retry` with options in provider
87 * lifecycle code can widen the provider layer to `unknown` in declaration
88 * emit.
89 *
90 * @internal
91 */
92 const retryPriorRequest = <A, E extends { _tag: string }, R>(
93 self: Effect.Effect<A, E, R>,
94 ): Effect.Effect<A, E, R> =>
95 Effect.retry(self, {
96 while: (e) => e._tag === "PriorRequestNotComplete",
97 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
98 });
99
100 export const ZoneVpcAssociationProvider = () =>
101 Provider.effect(
102 ZoneVpcAssociation,
103 Effect.gen(function* () {
104 // Observe via the zone detail — `getHostedZone` returns every
105 // associated VPC for a private zone.
106 const observe = Effect.fn(function* (hostedZoneId: string, vpcId: string) {
107 const detail = yield* route53
108 .getHostedZone({ Id: hostedZoneId })
109 .pipe(Effect.catchTag("NoSuchHostedZone", () => Effect.undefined));
110 return (detail?.VPCs ?? []).find((vpc) => vpc.VPCId === vpcId);
111 });
112
113 return {
114 stables: ["hostedZoneId", "vpcId", "vpcRegion"],
115 // Sub-resource: associations are keyed by their parent hosted zone;
116 // there is no account-wide enumeration API.
117 list: () => Effect.succeed([]),
118 // Existence-only resource — every identifying property change
119 // replaces the association.
120 diff: Effect.fn(function* ({ olds, news }) {
121 if (!isResolved(news)) return undefined;
122 if (
123 olds.hostedZoneId !== news.hostedZoneId ||
124 olds.vpcId !== news.vpcId ||
125 olds.vpcRegion !== news.vpcRegion
126 ) {
127 return { action: "replace" } as const;
128 }
129 }),
130 read: Effect.fn(function* ({ olds, output }) {
131 const hostedZoneId = output?.hostedZoneId ?? olds?.hostedZoneId;
132 const vpcId = output?.vpcId ?? olds?.vpcId;
133 if (hostedZoneId === undefined || vpcId === undefined) {
134 return undefined;
135 }
136 const observed = yield* observe(hostedZoneId, vpcId);
137 if (!observed) {
138 return undefined;
139 }
140 return {
141 hostedZoneId,
142 vpcId,
143 vpcRegion: observed.VPCRegion ?? output?.vpcRegion ?? olds?.vpcRegion ?? "",
144 };
145 }),
146 // Existence-only: observe → if missing, associate. There is no sync
147 // step (an association has no mutable aspects).
148 reconcile: Effect.fn(function* ({ news, session }) {
149 const observed = yield* observe(news.hostedZoneId, news.vpcId);
150 if (!observed) {
151 yield* retryPriorRequest(
152 route53.associateVPCWithHostedZone({
153 HostedZoneId: news.hostedZoneId,
154 VPC: {
155 VPCId: news.vpcId,
156 VPCRegion: news.vpcRegion as route53.VPCRegion,
157 },
158 Comment: news.comment,
159 }),
160 );
161 }
162 yield* session.note(`${news.hostedZoneId}/${news.vpcId}`);
163 return {
164 hostedZoneId: news.hostedZoneId,
165 vpcId: news.vpcId,
166 vpcRegion: news.vpcRegion,
167 };
168 }),
169 delete: Effect.fn(function* ({ output }) {
170 yield* route53
171 .disassociateVPCFromHostedZone({
172 HostedZoneId: output.hostedZoneId,
173 VPC: {
174 VPCId: output.vpcId,
175 VPCRegion: output.vpcRegion as route53.VPCRegion,
176 },
177 })
178 .pipe(
179 Effect.asVoid,
180 Effect.catchTags({
181 VPCAssociationNotFound: () => Effect.void,
182 NoSuchHostedZone: () => Effect.void,
183 }),
184 );
185 }),
186 };
187 }),
188 );