@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, 04:45 UTC

main
ts 237 lines 8.54 KB
Raw
1 import * as aoss from "@distilled.cloud/aws/opensearchserverless";
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 Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9 import { awaitVpcEndpointActive, retryWhileConflict } from "./internal.ts";
10
11 export interface VpcEndpointProps {
12 /**
13 * Name of the VPC endpoint (3-32 characters, lowercase). Changing the name
14 * replaces the endpoint.
15 * @default a generated physical name
16 */
17 endpointName?: string;
18 /**
19 * The ID of the VPC from which the endpoint accesses OpenSearch Serverless.
20 * Changing the VPC replaces the endpoint.
21 */
22 vpcId: string;
23 /**
24 * The IDs of the subnets in which to create the interface endpoint's network
25 * interfaces.
26 */
27 subnetIds: string[];
28 /**
29 * The IDs of the security groups to attach to the endpoint's network
30 * interfaces.
31 */
32 securityGroupIds?: string[];
33 }
34
35 export interface VpcEndpoint extends Resource<
36 "AWS.OpenSearchServerless.VpcEndpoint",
37 VpcEndpointProps,
38 {
39 /**
40 * Unique identifier of the VPC endpoint.
41 */
42 vpcEndpointId: string;
43 /**
44 * Name of the VPC endpoint.
45 */
46 endpointName: string;
47 /**
48 * Endpoint status (e.g. `ACTIVE`, `PENDING`, `DELETING`).
49 */
50 status?: string;
51 },
52 {},
53 Providers
54 > {}
55
56 /**
57 * An Amazon OpenSearch Serverless-managed interface VPC endpoint. Creating one
58 * lets resources in a VPC reach a collection privately (without traversing the
59 * public internet), and lets a network {@link SecurityPolicy} restrict a
60 * collection's access to only that endpoint. Creation is asynchronous — the
61 * provider polls (bounded) until the endpoint reaches `ACTIVE`.
62 *
63 * ### Creating VPC Endpoints
64 * **Example:** Interface Endpoint in a VPC
65 * ```typescript
66 * import * as AWS from "@alchemy.run/aws";
67 *
68 * const endpoint = yield* AWS.OpenSearchServerless.VpcEndpoint("Endpoint", {
69 * endpointName: "my-endpoint",
70 * vpcId: vpc.vpcId,
71 * subnetIds: [subnet.subnetId],
72 * securityGroupIds: [securityGroup.groupId],
73 * });
74 * ```
75 *
76 * @resource
77 */
78 export const VpcEndpoint = Resource<VpcEndpoint>("AWS.OpenSearchServerless.VpcEndpoint");
79
80 export const VpcEndpointProvider = () =>
81 Provider.effect(
82 VpcEndpoint,
83 Effect.gen(function* () {
84 const createName = Effect.fn(function* (
85 id: string,
86 props: { endpointName?: string | undefined },
87 ) {
88 return (
89 props.endpointName ?? (yield* createPhysicalName({ id, maxLength: 32, lowercase: true }))
90 );
91 });
92
93 const findByName = Effect.fn(function* (name: string) {
94 const pages = yield* aoss.listVpcEndpoints.pages({}).pipe(Stream.runCollect);
95 return Array.from(pages)
96 .flatMap((page) => page.vpcEndpointSummaries ?? [])
97 .find((s) => s.name === name);
98 });
99
100 const observe = Effect.fn(function* (name: string, vpcEndpointId: string | undefined) {
101 if (vpcEndpointId !== undefined) {
102 const byId = yield* aoss
103 .batchGetVpcEndpoint({ ids: [vpcEndpointId] })
104 .pipe(Effect.map((r) => r.vpcEndpointDetails?.[0]));
105 if (byId?.id !== undefined && byId.status !== "DELETING") {
106 return byId;
107 }
108 }
109 const summary = yield* findByName(name);
110 if (summary?.id === undefined || summary.status === "DELETING") {
111 return undefined;
112 }
113 return yield* aoss
114 .batchGetVpcEndpoint({ ids: [summary.id] })
115 .pipe(Effect.map((r) => r.vpcEndpointDetails?.[0]));
116 });
117
118 return VpcEndpoint.Provider.of({
119 stables: ["vpcEndpointId", "endpointName"],
120
121 list: () =>
122 Effect.gen(function* () {
123 const pages = yield* aoss.listVpcEndpoints.pages({}).pipe(Stream.runCollect);
124 return Array.from(pages)
125 .flatMap((page) => page.vpcEndpointSummaries ?? [])
126 .filter((s) => s.id !== undefined && s.name !== undefined)
127 .map((s) => ({
128 vpcEndpointId: s.id!,
129 endpointName: s.name!,
130 status: s.status,
131 }));
132 }),
133
134 read: Effect.fn(function* ({ id, olds, output }) {
135 const name = output?.endpointName ?? (yield* createName(id, olds ?? {}));
136 const detail = yield* observe(name, output?.vpcEndpointId);
137 if (detail?.id === undefined) {
138 return undefined;
139 }
140 // VPC endpoints carry no tags — an existing same-name endpoint is
141 // adopted.
142 return {
143 vpcEndpointId: detail.id,
144 endpointName: detail.name!,
145 status: detail.status,
146 };
147 }),
148
149 diff: Effect.fn(function* ({ id, news, olds }) {
150 if (!isResolved(news)) return undefined;
151 const oldName = yield* createName(id, olds);
152 const newName = yield* createName(id, news);
153 if (oldName !== newName) {
154 return { action: "replace" } as const;
155 }
156 if (olds.vpcId !== news.vpcId) {
157 return { action: "replace" } as const;
158 }
159 // subnetIds/securityGroupIds fall through to the default update path
160 }),
161
162 reconcile: Effect.fn(function* ({ id, news, output, session }) {
163 const name = output?.endpointName ?? (yield* createName(id, news));
164
165 // 1. OBSERVE
166 let detail = yield* observe(name, output?.vpcEndpointId);
167
168 // 2. ENSURE — create if missing; wait for ACTIVE (async provisioning)
169 if (detail === undefined) {
170 const created = yield* aoss
171 .createVpcEndpoint({
172 name,
173 vpcId: news.vpcId,
174 subnetIds: news.subnetIds,
175 securityGroupIds: news.securityGroupIds,
176 })
177 .pipe(
178 Effect.map((r) => r.createVpcEndpointDetail),
179 Effect.catchTag("ConflictException", () => Effect.undefined),
180 );
181 const endpointId = created?.id ?? (yield* findByName(name))?.id;
182 if (endpointId === undefined) {
183 return yield* aoss.ResourceNotFoundException.make({
184 message: `VPC endpoint ${name} not visible after create`,
185 });
186 }
187 yield* session.note(`creating VPC endpoint ${name} (async)...`);
188 detail = yield* awaitVpcEndpointActive(endpointId);
189 } else if (detail.status !== "ACTIVE") {
190 detail = yield* awaitVpcEndpointActive(detail.id!);
191 }
192
193 // 3. SYNC — subnet/security-group membership deltas
194 const observedSubnets = new Set(detail.subnetIds ?? []);
195 const desiredSubnets = new Set(news.subnetIds);
196 const addSubnetIds = news.subnetIds.filter((s) => !observedSubnets.has(s));
197 const removeSubnetIds = (detail.subnetIds ?? []).filter((s) => !desiredSubnets.has(s));
198 const observedSgs = new Set(detail.securityGroupIds ?? []);
199 const desiredSgs = new Set(news.securityGroupIds ?? []);
200 const addSecurityGroupIds = (news.securityGroupIds ?? []).filter(
201 (s) => !observedSgs.has(s),
202 );
203 const removeSecurityGroupIds = (detail.securityGroupIds ?? []).filter(
204 (s) => !desiredSgs.has(s),
205 );
206 if (
207 addSubnetIds.length > 0 ||
208 removeSubnetIds.length > 0 ||
209 addSecurityGroupIds.length > 0 ||
210 removeSecurityGroupIds.length > 0
211 ) {
212 yield* aoss.updateVpcEndpoint({
213 id: detail.id!,
214 addSubnetIds: addSubnetIds.length > 0 ? addSubnetIds : undefined,
215 removeSubnetIds: removeSubnetIds.length > 0 ? removeSubnetIds : undefined,
216 addSecurityGroupIds: addSecurityGroupIds.length > 0 ? addSecurityGroupIds : undefined,
217 removeSecurityGroupIds:
218 removeSecurityGroupIds.length > 0 ? removeSecurityGroupIds : undefined,
219 });
220 }
221
222 yield* session.note(detail.id!);
223 return {
224 vpcEndpointId: detail.id!,
225 endpointName: detail.name!,
226 status: detail.status,
227 };
228 }),
229
230 delete: Effect.fn(function* ({ output }) {
231 yield* retryWhileConflict(aoss.deleteVpcEndpoint({ id: output.vpcEndpointId })).pipe(
232 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
233 );
234 }),
235 });
236 }),
237 );