@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 337 lines 11.9 KB
Raw
1 import * as androidmanagement from "@distilled.cloud/gcp/androidmanagement_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 findEnterpriseByDisplayName,
12 getEnterprise,
13 jsonEqual,
14 lastSegment,
15 MAX_DISPLAY_NAME_LENGTH,
16 sameStringList,
17 sameText,
18 toDisplayName,
19 toEnterpriseName,
20 updateMaskOf,
21 } from "./internal.ts";
22
23 export type EnterpriseProps = {
24 /**
25 * Cloud project that owns the enterprise. Defaults to the stack
26 * project. Immutable — changing it replaces the enterprise.
27 */
28 projectId?: string;
29 /**
30 * Signup URL name from `signupUrls.create`. Set with
31 * `enterpriseToken` for a customer-managed enterprise.
32 */
33 signupUrlName?: string;
34 /**
35 * Enterprise token appended to the signup callback URL. Set with
36 * `signupUrlName` for a customer-managed enterprise.
37 */
38 enterpriseToken?: string;
39 /**
40 * Whether the admin accepted the managed Google Play Agreement.
41 * Required for EMM-managed enterprises; must be omitted for
42 * customer-managed ones.
43 * @default true when `signupUrlName` is omitted
44 */
45 agreementAccepted?: boolean;
46 /**
47 * Display name shown to users (max 100 characters). Enterprises have
48 * no labels field, so without state Alchemy finds the enterprise by
49 * this name.
50 * @default a unique name generated from the stack, stage, and logical id
51 */
52 enterpriseDisplayName?: string;
53 /**
54 * Predominant UI color as `(red << 16) | (green << 8) | blue`.
55 */
56 primaryColor?: number;
57 /**
58 * Pub/Sub topic for enterprise notifications
59 * (`projects/{project}/topics/{topic}`).
60 */
61 pubsubTopic?: string;
62 /**
63 * Enabled Pub/Sub notification types (`ENROLLMENT`, `COMMAND`, …).
64 */
65 enabledNotificationTypes?: Array<
66 androidmanagement.EnterpriseEnabledNotificationTypesItemEnum | (string & {})
67 >;
68 /**
69 * Logo shown during device provisioning.
70 */
71 logo?: androidmanagement.ExternalData;
72 /**
73 * Terms and conditions pages shown during provisioning.
74 */
75 termsAndConditions?: androidmanagement.TermsAndConditionsList;
76 /**
77 * Sign-in details used for custom enrollment.
78 */
79 signinDetails?: androidmanagement.SigninDetailList;
80 /**
81 * Contact info for an EMM-managed enterprise.
82 */
83 contactInfo?: androidmanagement.ContactInfo;
84 };
85
86 export type Enterprise = Resource<
87 "GCP.AndroidManagement.Enterprise",
88 EnterpriseProps,
89 {
90 /** Resource name `enterprises/{enterprise}`. */
91 name: string;
92 /** Enterprise id (last path segment). */
93 enterpriseId: string;
94 /** Project id used when the enterprise was reconciled. */
95 project: string;
96 /** Display name. */
97 enterpriseDisplayName: string | undefined;
98 /** Predominant UI color. */
99 primaryColor: number | undefined;
100 /** Pub/Sub topic for notifications. */
101 pubsubTopic: string | undefined;
102 /** Enabled notification types. */
103 enabledNotificationTypes: string[] | undefined;
104 /** Provisioning logo. */
105 logo: androidmanagement.ExternalData | undefined;
106 /** Terms and conditions. */
107 termsAndConditions: androidmanagement.TermsAndConditionsList | undefined;
108 /** Sign-in details. */
109 signinDetails: androidmanagement.SigninDetailList | undefined;
110 /** Contact info. */
111 contactInfo: androidmanagement.ContactInfo | undefined;
112 /** Enterprise type. */
113 enterpriseType: string | undefined;
114 /** Managed Google Play Accounts enterprise type. */
115 managedGooglePlayAccountsEnterpriseType: string | undefined;
116 /** Managed Google domain type. */
117 managedGoogleDomainType: string | undefined;
118 /** Google authentication settings. */
119 googleAuthenticationSettings: androidmanagement.GoogleAuthenticationSettings | undefined;
120 },
121 never,
122 Providers
123 >;
124
125 /**
126 * An Android Management API enterprise.
127 *
128 * Enterprises have no labels field, so Alchemy tracks the enterprise by
129 * its resource name; without state it is found by display name, and one
130 * found under an explicit (non-generated) display name is reported as
131 * unowned. Customer-managed
132 * enterprises need `signupUrlName` plus `enterpriseToken`; otherwise an
133 * EMM-managed enterprise is created with `agreementAccepted`. Display
134 * name, color, notifications, logo, terms, sign-in details, and contact
135 * info update in place. Changing `projectId` replaces the enterprise.
136 * `enterprises.delete` only works for EMM-managed enterprises.
137 *
138 * ### Creating an Enterprise
139 * **Example:** EMM-managed enterprise
140 * ```typescript
141 * const enterprise = yield* GCP.AndroidManagement.Enterprise("Work", {
142 * enterpriseDisplayName: "Alchemy Work",
143 * });
144 * ```
145 *
146 * **Example:** Customer-managed after signup
147 * ```typescript
148 * const enterprise = yield* GCP.AndroidManagement.Enterprise("Work", {
149 * signupUrlName: "signupUrls/abc",
150 * enterpriseToken: token,
151 * enterpriseDisplayName: "Acme",
152 * });
153 * ```
154 *
155 * ### Updating an Enterprise
156 * **Example:** Rename
157 * ```typescript
158 * const enterprise = yield* GCP.AndroidManagement.Enterprise("Work", {
159 * enterpriseDisplayName: "Alchemy Work 2026",
160 * });
161 * ```
162 *
163 * @resource
164 * @category AndroidManagement
165 */
166 export const Enterprise = Resource<Enterprise>("GCP.AndroidManagement.Enterprise");
167
168 export class EnterpriseNotResolved extends Data.TaggedError(
169 "GCP.AndroidManagement.EnterpriseNotResolved",
170 )<{
171 resourceName: string;
172 }> {}
173
174 const toAttrs = (enterprise: androidmanagement.Enterprise, project: string) => {
175 const name = enterprise.name ?? "";
176 return {
177 name,
178 enterpriseId: lastSegment(name),
179 project,
180 enterpriseDisplayName: enterprise.enterpriseDisplayName,
181 primaryColor: enterprise.primaryColor,
182 pubsubTopic: enterprise.pubsubTopic,
183 enabledNotificationTypes: enterprise.enabledNotificationTypes,
184 logo: enterprise.logo,
185 termsAndConditions: enterprise.termsAndConditions,
186 signinDetails: enterprise.signinDetails,
187 contactInfo: enterprise.contactInfo,
188 enterpriseType: enterprise.enterpriseType,
189 managedGooglePlayAccountsEnterpriseType: enterprise.managedGooglePlayAccountsEnterpriseType,
190 managedGoogleDomainType: enterprise.managedGoogleDomainType,
191 googleAuthenticationSettings: enterprise.googleAuthenticationSettings,
192 };
193 };
194
195 const desiredBody = (input: {
196 displayName: string;
197 news: EnterpriseProps;
198 }): androidmanagement.Enterprise => ({
199 enterpriseDisplayName: input.displayName,
200 primaryColor: input.news.primaryColor,
201 pubsubTopic: input.news.pubsubTopic,
202 enabledNotificationTypes: input.news.enabledNotificationTypes,
203 logo: input.news.logo,
204 termsAndConditions: input.news.termsAndConditions,
205 signinDetails: input.news.signinDetails,
206 contactInfo: input.news.contactInfo,
207 });
208
209 export const EnterpriseProvider = () =>
210 Provider.succeed(Enterprise, {
211 stables: ["name", "enterpriseId", "project", "enterpriseType"],
212
213 diff: Effect.fn(function* ({ news, olds, output }) {
214 if (!isResolved(news)) return undefined;
215 const previousProject = olds?.projectId ?? output?.project;
216 if (
217 news.projectId !== undefined &&
218 previousProject !== undefined &&
219 news.projectId !== previousProject
220 ) {
221 return { action: "replace" as const, deleteFirst: false };
222 }
223 return undefined;
224 }),
225
226 read: Effect.fn(function* ({ id, olds, output }) {
227 const env = yield* GcpEnvironment.current;
228 const projectId = olds?.projectId ?? output?.project ?? env.project;
229 const byName = yield* getEnterprise(
230 output?.name ?? toEnterpriseName(output?.enterpriseId ?? ""),
231 );
232 if (byName !== undefined) return toAttrs(byName, projectId);
233 const generated = yield* toDisplayName(id, undefined, undefined);
234 const displayName = olds?.enterpriseDisplayName ?? generated;
235 const found = yield* findEnterpriseByDisplayName(projectId, displayName);
236 if (found === undefined) return undefined;
237 const attrs = toAttrs(found, projectId);
238 // A generated display name is unique to this stack, stage and id.
239 return displayName === generated ? attrs : Unowned(attrs);
240 }),
241
242 reconcile: Effect.fn(function* ({ id, news, output }) {
243 const env = yield* GcpEnvironment.current;
244 const projectId = news.projectId ?? output?.project ?? env.project;
245 const displayName = yield* toDisplayName(
246 id,
247 news.enterpriseDisplayName,
248 output?.enterpriseDisplayName,
249 MAX_DISPLAY_NAME_LENGTH,
250 );
251 const desired = desiredBody({ displayName, news });
252 const customerManaged =
253 (news.signupUrlName !== undefined && news.signupUrlName.length > 0) ||
254 (news.enterpriseToken !== undefined && news.enterpriseToken.length > 0);
255
256 let current = yield* getEnterprise(
257 output?.name ?? toEnterpriseName(output?.enterpriseId ?? ""),
258 );
259 if (current === undefined) {
260 current = yield* findEnterpriseByDisplayName(projectId, displayName);
261 }
262
263 if (current === undefined) {
264 const created = yield* androidmanagement
265 .createEnterprises({
266 projectId,
267 signupUrlName: news.signupUrlName,
268 enterpriseToken: news.enterpriseToken,
269 agreementAccepted: customerManaged
270 ? news.agreementAccepted
271 : (news.agreementAccepted ?? true),
272 body: desired,
273 })
274 .pipe(
275 Effect.catchTag("Conflict", () => findEnterpriseByDisplayName(projectId, displayName)),
276 );
277 current = created ?? undefined;
278 }
279
280 if (current === undefined) {
281 return yield* new EnterpriseNotResolved({
282 resourceName: output?.name ?? displayName,
283 });
284 }
285
286 const name = current.name ?? output?.name ?? "";
287 const displayChanged = !sameText(current.enterpriseDisplayName, displayName);
288 const colorChanged =
289 news.primaryColor !== undefined && current.primaryColor !== news.primaryColor;
290 const topicChanged =
291 news.pubsubTopic !== undefined && !sameText(current.pubsubTopic, news.pubsubTopic);
292 const notificationsChanged =
293 news.enabledNotificationTypes !== undefined &&
294 !sameStringList(current.enabledNotificationTypes, news.enabledNotificationTypes);
295 const logoChanged = news.logo !== undefined && !jsonEqual(current.logo, news.logo);
296 const termsChanged =
297 news.termsAndConditions !== undefined &&
298 !jsonEqual(current.termsAndConditions, news.termsAndConditions);
299 const signinChanged =
300 news.signinDetails !== undefined && !jsonEqual(current.signinDetails, news.signinDetails);
301 const contactChanged =
302 news.contactInfo !== undefined && !jsonEqual(current.contactInfo, news.contactInfo);
303
304 const updateMask = updateMaskOf(
305 displayChanged ? "enterpriseDisplayName" : undefined,
306 colorChanged ? "primaryColor" : undefined,
307 topicChanged ? "pubsubTopic" : undefined,
308 notificationsChanged ? "enabledNotificationTypes" : undefined,
309 logoChanged ? "logo" : undefined,
310 termsChanged ? "termsAndConditions" : undefined,
311 signinChanged ? "signinDetails" : undefined,
312 contactChanged ? "contactInfo" : undefined,
313 );
314
315 if (updateMask.length > 0 && name.length > 0) {
316 current = yield* androidmanagement.patchEnterprises({
317 name,
318 updateMask,
319 body: desired,
320 });
321 }
322
323 const fresh = (yield* getEnterprise(name)) ?? current;
324 return toAttrs(fresh, projectId);
325 }),
326
327 delete: Effect.fn(function* ({ output }) {
328 if (output.name.length === 0) return;
329 yield* androidmanagement.deleteEnterprises({ name: output.name }).pipe(
330 Effect.catchTags({
331 NotFound: () => Effect.void,
332 // Customer-managed enterprises cannot be deleted through the API.
333 BadRequest: () => Effect.void,
334 }),
335 );
336 }),
337 });