The best way to control Claude Code, Codex and more from any computer or phone

main
ts 261 lines 8.22 KB
Raw
1 import * as sasportal from "@distilled.cloud/gcp/sasportal_v1alpha1";
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 Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 encodeOwnershipLine,
12 expandPath,
13 findOwnedNodeNodeDeployment,
14 getNodeDeployment,
15 hasOwnershipMarker,
16 lastSegment,
17 listNodeNodeDeployments,
18 MAX_DISPLAY_NAME_LENGTH,
19 ownedByAlchemy,
20 ownershipLabels,
21 parentOf,
22 parseOwnership,
23 replaceOnIdentity,
24 retryDelete,
25 sameStringList,
26 sameText,
27 scanOwnedNodeNodeDeployment,
28 toDisplayName,
29 updateMaskOf,
30 waitUntilGone,
31 walkNodeChildren,
32 } from "./internal.ts";
33
34 export type NodesNodesDeploymentProps = {
35 /**
36 * Parent nested SAS node, as `nodes/{node}/nodes/{node}`.
37 * Immutable — changing it replaces the deployment.
38 */
39 parent: string;
40 /**
41 * Server-assigned deployment resource name
42 * (`nodes/{node}/nodes/{node}/deployments/{deployment}`). Immutable —
43 * changing it replaces the deployment.
44 */
45 name?: string;
46 /**
47 * Display name. SAS Portal deployments have no labels field, so
48 * Alchemy stamps ownership into a `[alchemy …]` prefix and strips it
49 * from attributes.
50 */
51 displayName?: string;
52 /**
53 * SAS user ids used by devices that belong to this deployment. Each
54 * deployment should have one unique user id.
55 */
56 sasUserIds?: string[];
57 };
58
59 export type NodesNodesDeployment = Resource<
60 "GCP.SasPortal.NodesNodesDeployment",
61 NodesNodesDeploymentProps,
62 {
63 /** Resource name `nodes/{node}/nodes/{node}/deployments/{deployment}`. */
64 name: string;
65 /** Deployment id (last path segment). */
66 deploymentId: string;
67 /** Parent resource name. */
68 parent: string;
69 /** Project id used when the deployment was reconciled. */
70 project: string;
71 /** User-facing display name with the Alchemy prefix stripped. */
72 displayName: string | undefined;
73 /** SAS user ids on the deployment. */
74 sasUserIds: string[] | undefined;
75 /** FCC Registration Numbers inherited from the parent. */
76 frns: string[] | undefined;
77 },
78 never,
79 Providers
80 >;
81
82 /**
83 * A Spectrum Access System (SAS) Portal deployment under a nested node.
84 *
85 * SAS Portal deployments have no labels field, so Alchemy stamps
86 * ownership into `displayName` for `list` / nuke. Parent is identity.
87 * Display name and SAS user ids update in place. `frns` is output-only.
88 *
89 * ### Creating a Deployment
90 * **Example:** Explicit name and SAS user id
91 * ```typescript
92 * const deployment = yield* GCP.SasPortal.NodesNodesDeployment("Site", {
93 * parent: child.name,
94 * displayName: "downtown",
95 * sasUserIds: ["operator-1"],
96 * });
97 * ```
98 *
99 * ### Updating a Deployment
100 * Change props on the same logical id; the engine keeps the physical id.
101 *
102 * **Example:** Rename
103 * ```typescript
104 * const deployment = yield* GCP.SasPortal.NodesNodesDeployment("Site", {
105 * parent: child.name,
106 * displayName: "downtown-west",
107 * sasUserIds: ["operator-1"],
108 * });
109 * ```
110 *
111 * @resource
112 * @category SasPortal
113 */
114 export const NodesNodesDeployment = Resource<NodesNodesDeployment>(
115 "GCP.SasPortal.NodesNodesDeployment",
116 );
117
118 export class NodesNodesDeploymentNotResolved extends Data.TaggedError(
119 "GCP.SasPortal.NodesNodesDeploymentNotResolved",
120 )<{
121 parent: string;
122 resourceName: string;
123 }> {}
124
125 const toAttrs = (deployment: sasportal.SasPortalDeployment, parent: string, project: string) => {
126 const name = deployment.name ?? "";
127 return {
128 name,
129 deploymentId: lastSegment(name),
130 parent: parentOf(name) || parent,
131 project,
132 displayName: parseOwnership(deployment.displayName).text,
133 sasUserIds: deployment.sasUserIds,
134 frns: deployment.frns,
135 };
136 };
137
138 export const NodesNodesDeploymentProvider = () =>
139 Provider.succeed(NodesNodesDeployment, {
140 stables: ["name", "deploymentId", "parent", "project"],
141
142 diff: Effect.fn(function* ({ news, olds, output }) {
143 if (!isResolved(news)) return undefined;
144 return replaceOnIdentity({
145 previousParent: olds?.parent ?? output?.parent,
146 nextParent: expandPath(news.parent),
147 previousId: olds?.name ?? output?.name,
148 nextId: news.name,
149 });
150 }),
151
152 read: Effect.fn(function* ({ id, olds, output }) {
153 const env = yield* GcpEnvironment.current;
154 const parent = expandPath(olds?.parent ?? output?.parent ?? "");
155 const name = olds?.name ?? output?.name ?? "";
156 let existing = yield* getNodeDeployment(name);
157 let locatedParent = existing ? parentOf(existing.name ?? "") || parent : parent;
158 if (existing === undefined) {
159 const found =
160 (yield* findOwnedNodeNodeDeployment(id, parent)) ??
161 (yield* scanOwnedNodeNodeDeployment(id, parent));
162 existing = found?.row;
163 locatedParent = found?.parent ?? locatedParent;
164 }
165 if (existing === undefined) return undefined;
166 const attrs = toAttrs(existing, locatedParent, env.project);
167 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
168 }),
169
170 list: () =>
171 Effect.gen(function* () {
172 const env = yield* GcpEnvironment.current;
173 const parents = yield* walkNodeChildren();
174 const pages = yield* Effect.forEach(
175 parents,
176 (entry) => {
177 const parent = entry.node.name ?? "";
178 return parent.length === 0
179 ? Effect.succeed([])
180 : listNodeNodeDeployments(parent).pipe(
181 Effect.map((rows) =>
182 rows
183 .filter((row) => hasOwnershipMarker(row.displayName))
184 .map((row) => toAttrs(row, parent, env.project)),
185 ),
186 );
187 },
188 { concurrency: 4 },
189 );
190 return pages.flat();
191 }),
192
193 reconcile: Effect.fn(function* ({ id, news, output }) {
194 const env = yield* GcpEnvironment.current;
195 const parent = expandPath(news.parent);
196 const ownership = yield* ownershipLabels(id);
197 const displayName = encodeOwnershipLine(
198 ownership,
199 yield* toDisplayName(id, news.displayName, output?.displayName),
200 MAX_DISPLAY_NAME_LENGTH,
201 );
202 const desired: sasportal.SasPortalDeployment = {
203 displayName,
204 sasUserIds: news.sasUserIds,
205 };
206
207 let current = yield* getNodeDeployment(news.name ?? output?.name ?? "");
208 if (current === undefined) {
209 const found =
210 (yield* findOwnedNodeNodeDeployment(id, parent)) ??
211 (yield* scanOwnedNodeNodeDeployment(id, parent));
212 current = found?.row;
213 }
214
215 if (current === undefined) {
216 const created = yield* sasportal
217 .createNodesNodesDeployments({
218 parent,
219 body: desired,
220 })
221 .pipe(
222 Effect.catchTag("Conflict", () =>
223 findOwnedNodeNodeDeployment(id, parent).pipe(Effect.map((found) => found?.row)),
224 ),
225 );
226 current = created ?? undefined;
227 }
228
229 if (current === undefined) {
230 return yield* new NodesNodesDeploymentNotResolved({
231 parent,
232 resourceName: news.name ?? output?.name ?? displayName,
233 });
234 }
235
236 const name = current.name ?? news.name ?? output?.name ?? "";
237 const nameChanged = !sameText(current.displayName, displayName);
238 const usersChanged =
239 news.sasUserIds !== undefined && !sameStringList(current.sasUserIds, news.sasUserIds);
240 if (nameChanged || usersChanged) {
241 current = yield* sasportal.patchNodesDeployments({
242 name,
243 updateMask: updateMaskOf(
244 "displayName",
245 news.sasUserIds !== undefined ? "sasUserIds" : undefined,
246 ),
247 body: desired,
248 });
249 }
250
251 return toAttrs(current, parent, env.project);
252 }),
253
254 delete: Effect.fn(function* ({ output }) {
255 if (!output.name) return;
256 yield* retryDelete(sasportal.deleteNodesDeployments({ name: output.name })).pipe(
257 Effect.catchTag("NotFound", () => Effect.void),
258 );
259 yield* waitUntilGone(getNodeDeployment(output.name));
260 }),
261 });