@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 244 lines 8.43 KB
Raw
1 import * as apihub from "@distilled.cloud/gcp/apihub_v1";
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 * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 ApihubNotResolved,
12 locationParent,
13 normalizeLocation,
14 parseName,
15 projectIdOf,
16 projectNameOf,
17 replaceOnIdentity,
18 } from "./internal.ts";
19
20 export type RuntimeProjectAttachmentProps = {
21 /**
22 * Runtime project to attach, as `projects/{project}` or a bare project
23 * id. Immutable — changing it replaces the attachment. The attachment
24 * id must equal this project's id. Defaults to the stack project.
25 */
26 runtimeProject?: string;
27 /**
28 * Attachment id. Must equal the project id of `runtimeProject`. If
29 * omitted, that project id is used. Immutable — changing it replaces
30 * the attachment.
31 */
32 runtimeProjectAttachmentId?: string;
33 /**
34 * Location of the API Hub host (`us-central1`, …). Immutable —
35 * changing it replaces the attachment.
36 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
37 */
38 location?: string;
39 };
40
41 export type RuntimeProjectAttachment = Resource<
42 "GCP.ApiHub.RuntimeProjectAttachment",
43 RuntimeProjectAttachmentProps,
44 {
45 /** Full resource name. */
46 name: string;
47 /** Attachment id (last path segment, equal to the runtime project id). */
48 runtimeProjectAttachmentId: string;
49 /** Host project id. */
50 project: string;
51 /** Location id. */
52 location: string;
53 /** Runtime project resource name (`projects/{number}` on output). */
54 runtimeProject: string | undefined;
55 /** RFC3339 creation timestamp. */
56 createTime: string | undefined;
57 },
58 never,
59 Providers
60 >;
61
62 /**
63 * An API Hub runtime project attachment. Attaching a runtime project
64 * lets API Hub discover deployments in that project.
65 *
66 * Attachments have no labels, description, or display name, and the id
67 * is forced to the runtime project id, so `list` cannot stamp ownership.
68 * The resource is existence-only: runtime project and location are
69 * identity, and there is nothing mutable to sync.
70 *
71 * ### Creating a Runtime Project Attachment
72 * **Example:** Attach the stack project
73 * ```typescript
74 * const attachment = yield* GCP.ApiHub.RuntimeProjectAttachment(
75 * "Runtime",
76 * {},
77 * );
78 * ```
79 *
80 * **Example:** Attach a specific runtime project
81 * ```typescript
82 * const attachment = yield* GCP.ApiHub.RuntimeProjectAttachment(
83 * "Runtime",
84 * {
85 * runtimeProject: "projects/my-runtime",
86 * location: "us-central1",
87 * },
88 * );
89 * ```
90 *
91 * @resource
92 * @category ApiHub
93 */
94 export const RuntimeProjectAttachment = Resource<RuntimeProjectAttachment>(
95 "GCP.ApiHub.RuntimeProjectAttachment",
96 );
97
98 const resourceName = (project: string, location: string, attachmentId: string) =>
99 `${locationParent(project, location)}/runtimeProjectAttachments/${attachmentId}`;
100
101 const toAttrs = (
102 attachment: apihub.GoogleCloudApihubV1RuntimeProjectAttachment,
103 project: string,
104 region: string,
105 ) => {
106 const name = attachment.name ?? "";
107 const parsed = parseName(name, "runtimeProjectAttachments", region);
108 return {
109 name,
110 runtimeProjectAttachmentId: parsed.id,
111 project: parsed.project || project,
112 location: parsed.location,
113 runtimeProject: attachment.runtimeProject,
114 createTime: attachment.createTime,
115 };
116 };
117
118 const getByName = (name: string) =>
119 name.length === 0
120 ? Effect.undefined
121 : apihub
122 .getProjectsLocationsRuntimeProjectAttachments({ name })
123 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
124
125 export const RuntimeProjectAttachmentProvider = () =>
126 Provider.succeed(RuntimeProjectAttachment, {
127 stables: [
128 "name",
129 "runtimeProjectAttachmentId",
130 "project",
131 "location",
132 "runtimeProject",
133 "createTime",
134 ],
135
136 diff: Effect.fn(function* ({ news, olds, output }) {
137 if (!isResolved(news)) return undefined;
138 const env = yield* GcpEnvironment.current;
139 const previousProject = projectIdOf(olds?.runtimeProject ?? output?.runtimeProject, "");
140 const nextProject = projectIdOf(
141 news.runtimeProject ?? olds?.runtimeProject ?? output?.runtimeProject,
142 previousProject,
143 );
144 return replaceOnIdentity({
145 previousId: olds?.runtimeProjectAttachmentId ?? output?.runtimeProjectAttachmentId,
146 nextId:
147 news.runtimeProjectAttachmentId ??
148 olds?.runtimeProjectAttachmentId ??
149 output?.runtimeProjectAttachmentId,
150 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
151 nextLocation: normalizeLocation(
152 news.location ?? olds?.location ?? output?.location,
153 env.region,
154 ),
155 extra:
156 previousProject.length > 0 && nextProject.length > 0 && previousProject !== nextProject,
157 });
158 }),
159
160 read: Effect.fn(function* ({ olds, output }) {
161 const env = yield* GcpEnvironment.current;
162 const runtimeProjectId = projectIdOf(
163 olds?.runtimeProject ?? output?.runtimeProject,
164 env.project,
165 );
166 const attachmentId =
167 olds?.runtimeProjectAttachmentId ?? output?.runtimeProjectAttachmentId ?? runtimeProjectId;
168 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
169 const name = output?.name ?? resourceName(env.project, location, attachmentId);
170 const existing = yield* getByName(name);
171 if (existing === undefined) return undefined;
172 const attrs = toAttrs(existing, env.project, env.region);
173 // Attachments cannot carry ownership metadata. Adopt only when the
174 // observed id matches the deterministic runtime project id we would
175 // have created; anything else is foreign.
176 return attrs.runtimeProjectAttachmentId === attachmentId ? attrs : Unowned(attrs);
177 }),
178
179 list: () =>
180 Effect.gen(function* () {
181 const env = yield* GcpEnvironment.current;
182 // No labels or description — return nothing so nuke does not
183 // delete foreign attachments.
184 return yield* apihub.listProjectsLocationsRuntimeProjectAttachments
185 .pages({
186 parent: locationParent(env.project, env.region),
187 pageSize: 1000,
188 })
189 .pipe(
190 Stream.flatMap((page) => Stream.fromIterable(page.runtimeProjectAttachments ?? [])),
191 Stream.filter(
192 (item) =>
193 projectIdOf(item.runtimeProject, "") === env.project ||
194 parseName(item.name ?? "", "runtimeProjectAttachments", env.region).id ===
195 env.project,
196 ),
197 Stream.map((item) => toAttrs(item, env.project, env.region)),
198 Stream.runCollect,
199 Effect.map((chunk) => Array.from(chunk)),
200 Effect.catchTag("NotFound", () => Effect.succeed([])),
201 );
202 }),
203
204 reconcile: Effect.fn(function* ({ news, output }) {
205 const env = yield* GcpEnvironment.current;
206 const location = normalizeLocation(news.location ?? output?.location, env.region);
207 const runtimeProject = projectNameOf(news.runtimeProject, env.project);
208 const attachmentId =
209 news.runtimeProjectAttachmentId ??
210 output?.runtimeProjectAttachmentId ??
211 projectIdOf(runtimeProject, env.project);
212 const name = resourceName(env.project, location, attachmentId);
213 const parent = locationParent(env.project, location);
214
215 let current = yield* getByName(output?.name ?? name);
216
217 if (current === undefined) {
218 const created = yield* apihub
219 .createProjectsLocationsRuntimeProjectAttachments({
220 parent,
221 runtimeProjectAttachmentId: attachmentId,
222 body: {
223 runtimeProject,
224 },
225 })
226 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
227 current = created ?? (yield* getByName(name));
228 }
229
230 if (current === undefined) {
231 return yield* new ApihubNotResolved({ resourceName: name });
232 }
233
234 return toAttrs(current, env.project, env.region);
235 }),
236
237 delete: Effect.fn(function* ({ output }) {
238 yield* apihub
239 .deleteProjectsLocationsRuntimeProjectAttachments({
240 name: output.name,
241 })
242 .pipe(Effect.catchTag("NotFound", () => Effect.void));
243 }),
244 });