@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 294 lines 10.4 KB
Raw
1 import * as Neon from "@distilled.cloud/neon";
2 import { OwnedBySomeoneElse, unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { validateOrganizationVPCEndpoint } from "./OrganizationVPCEndpoint.ts";
9 import type { Providers } from "./Providers.ts";
10
11 export interface ProjectVPCEndpointProps {
12 /** Project to restrict. Changing the project replaces the association, cleaning up the old scope first. */
13 project: {
14 /** Neon project identity. */
15 projectId: string;
16 };
17 /** Already configured organization VPC endpoint in the project's organization and AWS region. */
18 endpoint: {
19 /** Existing Neon organization identity. */
20 orgId: string;
21 /** Neon AWS region identity. */
22 regionId: string;
23 /** Existing registered AWS VPC endpoint identity. */
24 vpcEndpointId: string;
25 };
26 /** Descriptive label for this project restriction. */
27 label: string;
28 }
29
30 export interface ProjectVPCEndpointAttributes {
31 /** Restricted project identity. */
32 projectId: string;
33 /** Organization containing the project and registered endpoint. */
34 orgId: string;
35 /** AWS Neon region shared by the project and registered endpoint. */
36 regionId: string;
37 /** Registered AWS VPC endpoint identity. */
38 vpcEndpointId: string;
39 /** Observed restriction label, or null when absent. */
40 label: string | null;
41 /** Original adopted restriction label; null means Alchemy created the restriction. */
42 initialLabel: string | null;
43 /** Last managed label, retained across refreshes for drift-safe cleanup. */
44 managedLabel: string;
45 }
46
47 export interface ProjectVPCEndpoint extends Resource<
48 "Neon.ProjectVPCEndpoint",
49 ProjectVPCEndpointProps,
50 ProjectVPCEndpointAttributes,
51 never,
52 Providers
53 > {}
54
55 /**
56 * Manage one project's restriction to an already registered organization VPC
57 * endpoint. Requires an organization admin entitled to AWS Private Networking.
58 * The project, endpoint, and client must share an AWS region and the endpoint
59 * must belong to the project's organization. Azure is unsupported.
60 * This does not change the AWS endpoint, its organization registration, or
61 * the project's separate block_public_connections setting. Other restrictions
62 * are left untouched. Removing the last restriction can broaden connectivity.
63 *
64 * Existing associations require explicit resource-scoped adoption. Their
65 * original label is durably captured before mutation and restored on destroy;
66 * an adopted restriction is never silently removed. New restrictions are
67 * removed on destroy only when their label still matches the managed value.
68 * Scope changes replace with old-scope cleanup first, avoiding stale managed
69 * grants. Preserve state and serialize writers; Neon has no conditional writes.
70 *
71 * ### Restrict a project
72 * **Example:** Reference an organization registration
73 * ```typescript
74 * const restriction = yield* Neon.ProjectVPCEndpoint("PrivateAccess", {
75 * project,
76 * endpoint: network,
77 * label: "Application access",
78 * });
79 * ```
80 *
81 * @resource
82 * @product Project
83 */
84 export const ProjectVPCEndpoint = Resource<ProjectVPCEndpoint>("Neon.ProjectVPCEndpoint");
85
86 export class InvalidProjectVPCEndpoint extends Data.TaggedError("InvalidProjectVPCEndpoint")<{
87 /** Invalid scope or unsafe mutation condition. */
88 message: string;
89 }> {}
90
91 /** @internal */
92 export const validateProjectVPCEndpoint = Effect.fn(function* (props: ProjectVPCEndpointProps) {
93 if (!props.project.projectId.trim()) {
94 return yield* new InvalidProjectVPCEndpoint({
95 message: "A project ID is required",
96 });
97 }
98 yield* validateOrganizationVPCEndpoint({
99 ...props.endpoint,
100 label: props.label,
101 });
102 });
103
104 const scopeOf = (props: ProjectVPCEndpointProps) => ({
105 projectId: props.project.projectId,
106 orgId: props.endpoint.orgId,
107 regionId: props.endpoint.regionId,
108 vpcEndpointId: props.endpoint.vpcEndpointId,
109 });
110
111 const request = (scope: { projectId: string; vpcEndpointId: string }) => ({
112 project_id: scope.projectId,
113 vpc_endpoint_id: scope.vpcEndpointId,
114 });
115
116 const observe = (scope: { projectId: string; vpcEndpointId: string }) =>
117 Neon.listProjectVPCEndpoints({ project_id: scope.projectId }).pipe(
118 Effect.map((result) =>
119 result.endpoints.find((endpoint) => endpoint.vpc_endpoint_id === scope.vpcEndpointId),
120 ),
121 Effect.catchTag("NotFound", () => Effect.undefined),
122 );
123
124 const validateCloudScope = Effect.fn(function* (scope: {
125 projectId: string;
126 orgId: string;
127 regionId: string;
128 vpcEndpointId: string;
129 }) {
130 const { project } = yield* Neon.getProject({ project_id: scope.projectId });
131 if (project.org_id !== scope.orgId || project.region_id !== scope.regionId) {
132 return yield* new InvalidProjectVPCEndpoint({
133 message: "Project and registered endpoint must share an organization and AWS region",
134 });
135 }
136 yield* Neon.getOrganizationVPCEndpointDetails({
137 org_id: scope.orgId,
138 region_id: scope.regionId,
139 vpc_endpoint_id: scope.vpcEndpointId,
140 });
141 });
142
143 export const ProjectVPCEndpointProvider = () =>
144 Provider.succeed(ProjectVPCEndpoint, {
145 stables: ["projectId", "orgId", "regionId", "vpcEndpointId", "initialLabel"],
146 diff: Effect.fn(function* ({ olds, news, output }) {
147 const previous = output ?? scopeOf(olds);
148 if (
149 !("project" in news) ||
150 !("endpoint" in news) ||
151 !isResolved(news.project) ||
152 !isResolved(news.endpoint) ||
153 news.project.projectId !== previous.projectId ||
154 news.endpoint.orgId !== previous.orgId ||
155 news.endpoint.regionId !== previous.regionId ||
156 news.endpoint.vpcEndpointId !== previous.vpcEndpointId
157 )
158 return { action: "replace", deleteFirst: true } as const;
159 if (!isResolved<ProjectVPCEndpointProps>(news)) return;
160 yield* validateProjectVPCEndpoint(news);
161 const scope = scopeOf(news);
162 yield* validateCloudScope(scope);
163 if ((yield* observe(scope))?.label !== news.label) {
164 return { action: "update" } as const;
165 }
166 }),
167 read: Effect.fn(function* ({ olds, output, previous }) {
168 if (
169 !output &&
170 (!olds?.project?.projectId ||
171 !olds.endpoint?.orgId ||
172 !olds.endpoint.regionId ||
173 !olds.endpoint.vpcEndpointId)
174 )
175 return;
176 const scope = output ?? scopeOf(olds);
177 const observed = yield* observe(scope);
178 if (output) return { ...output, label: observed?.label ?? null };
179 if (!observed) return;
180 yield* validateCloudScope(scope);
181 return unownedUnlessPrevious(
182 {
183 ...scope,
184 label: observed.label,
185 initialLabel: observed.label,
186 managedLabel: observed.label,
187 },
188 previous,
189 "vpcEndpointId",
190 );
191 }),
192 reconcile: Effect.fn(function* ({ news, output, olds }) {
193 yield* validateProjectVPCEndpoint(news);
194 const scope = scopeOf(news);
195 if (
196 output &&
197 (output.projectId !== scope.projectId ||
198 output.orgId !== scope.orgId ||
199 output.regionId !== scope.regionId ||
200 output.vpcEndpointId !== scope.vpcEndpointId)
201 )
202 return yield* new InvalidProjectVPCEndpoint({
203 message: "Restriction identity changed without replacement",
204 });
205 yield* validateCloudScope(scope);
206 const observed = yield* observe(scope);
207 if (observed && !output)
208 return yield* new OwnedBySomeoneElse({
209 message:
210 "Existing project VPC restriction requires scoped adoption; an equal endpoint ID is not ownership",
211 resourceType: "Neon.ProjectVPCEndpoint",
212 });
213 if (
214 output &&
215 !olds &&
216 observed?.label !== output.managedLabel &&
217 observed?.label !== news.label
218 ) {
219 return yield* new InvalidProjectVPCEndpoint({
220 message: "Project restriction changed since its adoption baseline was captured",
221 });
222 }
223 if (observed?.label !== news.label) {
224 yield* Neon.assignProjectVPCEndpoint({
225 ...request(scope),
226 label: news.label,
227 }).pipe(
228 Effect.catchTag(
229 "Conflict",
230 Effect.fn(function* (error) {
231 const raced = yield* observe(scope);
232 if (!raced || raced.label !== news.label) return yield* error;
233 if (!output)
234 return yield* new OwnedBySomeoneElse({
235 message:
236 "A project restriction appeared during creation; explicit adoption is required",
237 resourceType: "Neon.ProjectVPCEndpoint",
238 });
239 }),
240 ),
241 );
242 }
243 const current = yield* observe(scope);
244 if (!current || current.label !== news.label)
245 return yield* new InvalidProjectVPCEndpoint({
246 message: "Project restriction changed during reconciliation",
247 });
248 return {
249 ...scope,
250 label: current.label,
251 initialLabel: output?.initialLabel ?? null,
252 managedLabel: news.label,
253 };
254 }),
255 delete: Effect.fn(function* ({ output, olds }) {
256 const observed = yield* observe(output);
257 if (!observed) return;
258 if (output.initialLabel !== null && observed.label === output.initialLabel) return;
259 if (observed.label !== output.managedLabel && observed.label !== olds.label) {
260 return yield* new InvalidProjectVPCEndpoint({
261 message: "Refusing cleanup of an externally relabeled project restriction",
262 });
263 }
264 // A transferred project must not be mutated under its previous organization.
265 const exists = yield* validateCloudScope(output).pipe(
266 Effect.as(true),
267 Effect.catchTag(
268 "NotFound",
269 Effect.fn(function* (error) {
270 if (yield* observe(output)) return yield* error;
271 return false;
272 }),
273 ),
274 );
275 if (!exists) return;
276 if (output.initialLabel !== null) {
277 yield* Neon.assignProjectVPCEndpoint({
278 ...request(output),
279 label: output.initialLabel,
280 }).pipe(
281 Effect.catchTag(
282 "NotFound",
283 Effect.fn(function* (error) {
284 if (yield* observe(output)) return yield* error;
285 }),
286 ),
287 );
288 } else {
289 yield* Neon.deleteProjectVPCEndpoint(request(output)).pipe(
290 Effect.catchTag("NotFound", () => Effect.void),
291 );
292 }
293 }),
294 });