@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 267 lines 8.38 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 expandCustomer,
13 findOwnedCustomerDeployment,
14 getCustomerDeployment,
15 hasOwnershipMarker,
16 lastSegment,
17 listCustomerDeployments,
18 listCustomers,
19 MAX_DISPLAY_NAME_LENGTH,
20 ownedByAlchemy,
21 ownershipLabels,
22 parentOf,
23 parseOwnership,
24 replaceOnIdentity,
25 retryDelete,
26 sameStringList,
27 sameText,
28 scanOwnedCustomerDeployment,
29 toDisplayName,
30 updateMaskOf,
31 waitUntilGone,
32 } from "./internal.ts";
33
34 export type CustomersDeploymentProps = {
35 /**
36 * Parent SAS customer, as `customers/{customer}` or the customer id.
37 * Immutable — changing it replaces the deployment.
38 */
39 parent: string;
40 /**
41 * Server-assigned deployment resource name
42 * (`customers/{customer}/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 CustomersDeployment = Resource<
60 "GCP.SasPortal.CustomersDeployment",
61 CustomersDeploymentProps,
62 {
63 /** Resource name `customers/{customer}/deployments/{deployment}`. */
64 name: string;
65 /** Deployment id (last path segment). */
66 deploymentId: string;
67 /** Parent customer 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 customer.
84 *
85 * SAS Portal deployments have no labels field, so Alchemy stamps
86 * ownership into `displayName` for `list` / nuke. Parent customer is
87 * identity. Display name and SAS user ids update in place. `frns` is
88 * output-only.
89 *
90 * ### Creating a Deployment
91 * **Example:** Generated display name
92 * ```typescript
93 * const deployment = yield* GCP.SasPortal.CustomersDeployment("Site", {
94 * parent: "customers/123",
95 * });
96 * ```
97 *
98 * **Example:** Explicit name and SAS user id
99 * ```typescript
100 * const deployment = yield* GCP.SasPortal.CustomersDeployment("Site", {
101 * parent: "customers/123",
102 * displayName: "downtown",
103 * sasUserIds: ["operator-1"],
104 * });
105 * ```
106 *
107 * ### Updating a Deployment
108 * Change props on the same logical id; the engine keeps the physical id.
109 *
110 * **Example:** Rename
111 * ```typescript
112 * const deployment = yield* GCP.SasPortal.CustomersDeployment("Site", {
113 * parent: "customers/123",
114 * displayName: "downtown-west",
115 * sasUserIds: ["operator-1"],
116 * });
117 * ```
118 *
119 * @resource
120 * @category SasPortal
121 */
122 export const CustomersDeployment = Resource<CustomersDeployment>(
123 "GCP.SasPortal.CustomersDeployment",
124 );
125
126 export class CustomersDeploymentNotResolved extends Data.TaggedError(
127 "GCP.SasPortal.CustomersDeploymentNotResolved",
128 )<{
129 parent: string;
130 resourceName: string;
131 }> {}
132
133 const toAttrs = (deployment: sasportal.SasPortalDeployment, parent: string, project: string) => {
134 const name = deployment.name ?? "";
135 return {
136 name,
137 deploymentId: lastSegment(name),
138 parent: parentOf(name) || parent,
139 project,
140 displayName: parseOwnership(deployment.displayName).text,
141 sasUserIds: deployment.sasUserIds,
142 frns: deployment.frns,
143 };
144 };
145
146 export const CustomersDeploymentProvider = () =>
147 Provider.succeed(CustomersDeployment, {
148 stables: ["name", "deploymentId", "parent", "project"],
149
150 diff: Effect.fn(function* ({ news, olds, output }) {
151 if (!isResolved(news)) return undefined;
152 return replaceOnIdentity({
153 previousParent: olds?.parent ?? output?.parent,
154 nextParent: expandCustomer(news.parent),
155 previousId: olds?.name ?? output?.name,
156 nextId: news.name,
157 });
158 }),
159
160 read: Effect.fn(function* ({ id, olds, output }) {
161 const env = yield* GcpEnvironment.current;
162 const parent = expandCustomer(olds?.parent ?? output?.parent ?? "");
163 const name = olds?.name ?? output?.name ?? "";
164 let existing = yield* getCustomerDeployment(name);
165 let locatedParent = existing ? parentOf(existing.name ?? "") || parent : parent;
166 if (existing === undefined) {
167 const found =
168 (yield* findOwnedCustomerDeployment(id, parent)) ??
169 (yield* scanOwnedCustomerDeployment(id));
170 existing = found?.row;
171 locatedParent = found?.parent ?? locatedParent;
172 }
173 if (existing === undefined) return undefined;
174 const attrs = toAttrs(existing, locatedParent, env.project);
175 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
176 }),
177
178 list: () =>
179 Effect.gen(function* () {
180 const env = yield* GcpEnvironment.current;
181 const customers = yield* listCustomers();
182 const pages = yield* Effect.forEach(
183 customers,
184 (customer) =>
185 customer.name
186 ? listCustomerDeployments(customer.name).pipe(
187 Effect.map((rows) =>
188 rows
189 .filter((row) => hasOwnershipMarker(row.displayName))
190 .map((row) => toAttrs(row, customer.name ?? "", env.project)),
191 ),
192 )
193 : Effect.succeed([]),
194 { concurrency: 4 },
195 );
196 return pages.flat();
197 }),
198
199 reconcile: Effect.fn(function* ({ id, news, output }) {
200 const env = yield* GcpEnvironment.current;
201 const parent = expandCustomer(news.parent);
202 const ownership = yield* ownershipLabels(id);
203 const displayName = encodeOwnershipLine(
204 ownership,
205 yield* toDisplayName(id, news.displayName, output?.displayName),
206 MAX_DISPLAY_NAME_LENGTH,
207 );
208 const desired: sasportal.SasPortalDeployment = {
209 displayName,
210 sasUserIds: news.sasUserIds,
211 };
212
213 let current = yield* getCustomerDeployment(news.name ?? output?.name ?? "");
214 if (current === undefined) {
215 const found =
216 (yield* findOwnedCustomerDeployment(id, parent)) ??
217 (yield* scanOwnedCustomerDeployment(id));
218 current = found?.row;
219 }
220
221 if (current === undefined) {
222 const created = yield* sasportal
223 .createCustomersDeployments({
224 parent,
225 body: desired,
226 })
227 .pipe(
228 Effect.catchTag("Conflict", () =>
229 findOwnedCustomerDeployment(id, parent).pipe(Effect.map((found) => found?.row)),
230 ),
231 );
232 current = created ?? undefined;
233 }
234
235 if (current === undefined) {
236 return yield* new CustomersDeploymentNotResolved({
237 parent,
238 resourceName: news.name ?? output?.name ?? displayName,
239 });
240 }
241
242 const name = current.name ?? news.name ?? output?.name ?? "";
243 const nameChanged = !sameText(current.displayName, displayName);
244 const usersChanged =
245 news.sasUserIds !== undefined && !sameStringList(current.sasUserIds, news.sasUserIds);
246 if (nameChanged || usersChanged) {
247 current = yield* sasportal.patchCustomersDeployments({
248 name,
249 updateMask: updateMaskOf(
250 "displayName",
251 news.sasUserIds !== undefined ? "sasUserIds" : undefined,
252 ),
253 body: desired,
254 });
255 }
256
257 return toAttrs(current, parent, env.project);
258 }),
259
260 delete: Effect.fn(function* ({ output }) {
261 if (!output.name) return;
262 yield* retryDelete(sasportal.deleteCustomersDeployments({ name: output.name })).pipe(
263 Effect.catchTag("NotFound", () => Effect.void),
264 );
265 yield* waitUntilGone(getCustomerDeployment(output.name));
266 }),
267 });