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

main
ts 283 lines 8.57 KB
Raw
1 import * as licensing from "@distilled.cloud/gcp/licensing_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 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 deleteAssignment,
12 findAssignment,
13 getAssignment,
14 listAssignments,
15 listCustomerId,
16 listProductId,
17 listSkuId,
18 listUserId,
19 normalizeCustomerId,
20 replaceOnIdentity,
21 sameText,
22 sameUser,
23 } from "./internal.ts";
24
25 export type LicenseAssignmentProps = {
26 /**
27 * Product id (`Google-Apps`, `Google-Vault`, …). See Products and
28 * SKUs in the Enterprise License Manager docs. Immutable — changing
29 * it replaces the assignment.
30 */
31 productId: string;
32 /**
33 * Product SKU id (`Google-Apps-For-Business`, `1010020027`, …).
34 * Reassigning to another SKU of the same product updates in place.
35 */
36 skuId: string;
37 /**
38 * User's primary email. Immutable — changing it replaces the
39 * assignment. If the email changes in Admin Console, pass the new
40 * address; the previous id is not a stable key.
41 */
42 userId: string;
43 /**
44 * Workspace customer id (`C0123abc` or `my_customer`). Used to look
45 * up an existing assignment after a SKU change and by `list` when
46 * matching env vars are set. Not sent on insert.
47 */
48 customerId?: string;
49 };
50
51 export type LicenseAssignment = Resource<
52 "GCP.Licensing.LicenseAssignment",
53 LicenseAssignmentProps,
54 {
55 /** Product id. */
56 productId: string;
57 /** Product SKU id. */
58 skuId: string;
59 /** User's primary email. */
60 userId: string;
61 /** Workspace customer id used for list lookups, if any. */
62 customerId: string | undefined;
63 /** Project id used when the assignment was reconciled. */
64 project: string;
65 /** Display name of the product. */
66 productName: string | undefined;
67 /** Display name of the SKU. */
68 skuName: string | undefined;
69 /** API self link. */
70 selfLink: string | undefined;
71 /** Resource kind (`licensing#licenseAssignment`). */
72 kind: string | undefined;
73 /** ETag of the assignment. */
74 etags: string | undefined;
75 },
76 never,
77 Providers
78 >;
79
80 /**
81 * A Google Workspace Enterprise License Manager assignment.
82 *
83 * Assignments have no labels or description, so Alchemy cannot stamp
84 * ownership. `list` / nuke only returns rows when
85 * `GOOGLE_LICENSE_CUSTOMER_ID` (or `GOOGLE_WORKSPACE_CUSTOMER_ID`),
86 * `GOOGLE_LICENSE_PRODUCT_ID`, and `GOOGLE_LICENSE_USER_ID` are set, so
87 * a Workspace domain is never bulk-revoked. Product and user are
88 * identity — changing either replaces the assignment. SKU reassignment
89 * within the same product updates in place via `licenseAssignments.patch`.
90 *
91 * Creating an assignment requires the
92 * `https://www.googleapis.com/auth/apps.licensing` scope and a
93 * Workspace admin on the customer.
94 *
95 * ### Creating an Assignment
96 * **Example:** Assign a Workspace SKU
97 * ```typescript
98 * const seat = yield* GCP.Licensing.LicenseAssignment("Ada", {
99 * productId: "Google-Apps",
100 * skuId: "1010020027",
101 * userId: "ada@example.com",
102 * customerId: "my_customer",
103 * });
104 * ```
105 *
106 * ### Reassigning a SKU
107 * **Example:** Move the user to another SKU of the same product
108 * ```typescript
109 * const seat = yield* GCP.Licensing.LicenseAssignment("Ada", {
110 * productId: "Google-Apps",
111 * skuId: "1010020028",
112 * userId: "ada@example.com",
113 * customerId: "my_customer",
114 * });
115 * ```
116 *
117 * @resource
118 * @category Licensing
119 */
120 export const LicenseAssignment = Resource<LicenseAssignment>("GCP.Licensing.LicenseAssignment");
121
122 export class LicenseAssignmentNotResolved extends Data.TaggedError(
123 "GCP.Licensing.LicenseAssignmentNotResolved",
124 )<{
125 productId: string;
126 skuId: string;
127 userId: string;
128 }> {}
129
130 const toAttrs = (
131 assignment: licensing.LicenseAssignment,
132 project: string,
133 customerId: string | undefined,
134 ) => ({
135 productId: assignment.productId ?? "",
136 skuId: assignment.skuId ?? "",
137 userId: assignment.userId ?? "",
138 customerId,
139 project,
140 productName: assignment.productName,
141 skuName: assignment.skuName,
142 selfLink: assignment.selfLink,
143 kind: assignment.kind,
144 etags: assignment.etags,
145 });
146
147 const observeAssignment = (input: {
148 productId: string;
149 skuId: string;
150 userId: string;
151 customerId?: string;
152 previousSkuId?: string;
153 }) =>
154 Effect.gen(function* () {
155 const bySku = yield* getAssignment(input.productId, input.skuId, input.userId);
156 if (bySku !== undefined) return bySku;
157 if (
158 input.previousSkuId !== undefined &&
159 input.previousSkuId.length > 0 &&
160 !sameText(input.previousSkuId, input.skuId)
161 ) {
162 const previous = yield* getAssignment(input.productId, input.previousSkuId, input.userId);
163 if (previous !== undefined) return previous;
164 }
165 return yield* findAssignment(input.productId, input.userId, input.customerId);
166 });
167
168 export const LicenseAssignmentProvider = () =>
169 Provider.succeed(LicenseAssignment, {
170 stables: ["productId", "userId", "project"],
171
172 diff: Effect.fn(function* ({ news, olds, output }) {
173 if (!isResolved(news)) return undefined;
174 return replaceOnIdentity({
175 previousProductId: olds?.productId ?? output?.productId,
176 nextProductId: news.productId,
177 previousUserId: olds?.userId ?? output?.userId,
178 nextUserId: news.userId,
179 });
180 }),
181
182 read: Effect.fn(function* ({ olds, output }) {
183 const env = yield* GcpEnvironment.current;
184 const productId = olds?.productId ?? output?.productId ?? "";
185 const skuId = olds?.skuId ?? output?.skuId ?? "";
186 const userId = olds?.userId ?? output?.userId ?? "";
187 const customerId = normalizeCustomerId(
188 olds?.customerId ?? output?.customerId ?? (yield* listCustomerId()),
189 );
190 const existing = yield* observeAssignment({
191 productId,
192 skuId,
193 userId,
194 customerId,
195 previousSkuId: output?.skuId,
196 });
197 if (existing === undefined) return undefined;
198 const attrs = toAttrs(existing, env.project, customerId);
199 return output !== undefined ? attrs : Unowned(attrs);
200 }),
201
202 list: () =>
203 Effect.gen(function* () {
204 const customerId = yield* listCustomerId();
205 const productId = yield* listProductId();
206 const userId = yield* listUserId();
207 if (customerId === undefined || productId === undefined || userId === undefined) {
208 return [];
209 }
210 const env = yield* GcpEnvironment.current;
211 const items = yield* listAssignments(productId, customerId, yield* listSkuId());
212 return items
213 .filter((item) => sameUser(item.userId, userId))
214 .map((item) => toAttrs(item, env.project, customerId));
215 }),
216
217 reconcile: Effect.fn(function* ({ news, output }) {
218 const env = yield* GcpEnvironment.current;
219 const productId = news.productId;
220 const skuId = news.skuId;
221 const userId = news.userId;
222 const customerId = normalizeCustomerId(
223 news.customerId ?? output?.customerId ?? (yield* listCustomerId()),
224 );
225
226 let current = yield* observeAssignment({
227 productId,
228 skuId,
229 userId,
230 customerId,
231 previousSkuId: output?.skuId,
232 });
233
234 if (current !== undefined && !sameUser(current.userId, userId)) {
235 current = undefined;
236 }
237
238 if (current === undefined) {
239 const created = yield* licensing
240 .insertLicenseAssignments({
241 productId,
242 skuId,
243 body: { userId },
244 })
245 .pipe(
246 Effect.catchTag("Conflict", () =>
247 observeAssignment({
248 productId,
249 skuId,
250 userId,
251 customerId,
252 previousSkuId: output?.skuId,
253 }),
254 ),
255 );
256 current = created ?? undefined;
257 }
258
259 if (current === undefined) {
260 return yield* new LicenseAssignmentNotResolved({
261 productId,
262 skuId,
263 userId,
264 });
265 }
266
267 const observedSku = current.skuId ?? skuId;
268 if (!sameText(observedSku, skuId)) {
269 current = yield* licensing.patchLicenseAssignments({
270 productId,
271 skuId: observedSku,
272 userId: current.userId ?? userId,
273 body: { skuId },
274 });
275 }
276
277 return toAttrs(current, env.project, customerId);
278 }),
279
280 delete: Effect.fn(function* ({ output }) {
281 yield* deleteAssignment(output.productId, output.skuId, output.userId);
282 }),
283 });