@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 257 lines 8.95 KB
Raw
1 import * as androidmanagement from "@distilled.cloud/gcp/androidmanagement_v1";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import { GcpEnvironment } from "../Environment.ts";
7 import type { Providers } from "../Providers.ts";
8 import {
9 DEFAULT_DURATION,
10 getEnrollmentToken,
11 jsonEqual,
12 lastSegment,
13 parentOf,
14 replaceOnIdentity,
15 toEnterpriseName,
16 toEnrollmentTokenName,
17 } from "./internal.ts";
18
19 export type EnterprisesEnrollmentTokenProps = {
20 /**
21 * Parent enterprise (`enterprises/{enterprise}` or `{enterprise}`).
22 * Immutable — changing it replaces the token.
23 */
24 parent: string;
25 /**
26 * Policy initially applied to the enrolled device
27 * (`enterprises/{enterprise}/policies/{policy}` or `{policy}`).
28 * Immutable — changing it replaces the token.
29 */
30 policyName?: string;
31 /**
32 * Whether personal usage is allowed on a device provisioned with this
33 * token. Immutable — changing it replaces the token.
34 */
35 allowPersonalUsage?: androidmanagement.EnrollmentTokenAllowPersonalUsageEnum | (string & {});
36 /**
37 * Google authentication options during enrollment. Immutable —
38 * changing them replaces the token.
39 */
40 googleAuthenticationOptions?: androidmanagement.GoogleAuthenticationOptions;
41 /**
42 * One-time-use token. Immutable — changing it replaces the token.
43 */
44 oneTimeOnly?: boolean;
45 /**
46 * Token lifetime as a duration string (for example `3600s`). Defaults
47 * to ten years so the resource stays usable until deleted.
48 * Immutable — changing it replaces the token.
49 * @default "315360000s"
50 */
51 duration?: string;
52 /**
53 * Arbitrary data exposed on the Device after enrollment (max 1024
54 * characters). List/get return only a partial view and omit this
55 * field. Immutable — changing it replaces the token.
56 */
57 additionalData?: string;
58 };
59
60 export type EnterprisesEnrollmentToken = Resource<
61 "GCP.AndroidManagement.EnterprisesEnrollmentToken",
62 EnterprisesEnrollmentTokenProps,
63 {
64 /** Resource name `enterprises/{enterprise}/enrollmentTokens/{token}`. */
65 name: string;
66 /** Token id (last path segment). */
67 enrollmentTokenId: string;
68 /** Parent enterprise name. */
69 parent: string;
70 /** Project id used when the token was reconciled. */
71 project: string;
72 /** Initial policy name. */
73 policyName: string | undefined;
74 /** Personal-usage setting. */
75 allowPersonalUsage: string | undefined;
76 /** Google authentication options. */
77 googleAuthenticationOptions: androidmanagement.GoogleAuthenticationOptions | undefined;
78 /** One-time-use flag. */
79 oneTimeOnly: boolean | undefined;
80 /** Requested lifetime. */
81 duration: string | undefined;
82 /** Additional data exposed on the enrolled device. */
83 additionalData: string | undefined;
84 /** Token value passed to the device. */
85 value: string | undefined;
86 /** QR code payload for enrollment. */
87 qrCode: string | undefined;
88 /** RFC3339 expiration timestamp. */
89 expirationTimestamp: string | undefined;
90 },
91 never,
92 Providers
93 >;
94
95 /**
96 * An Android Management enrollment token.
97 *
98 * Tokens are create-only (no patch API). Identity is the parent
99 * enterprise plus the server-assigned name. Changing parent, policy,
100 * duration, personal-usage, or additional data replaces the token.
101 * List/get return a partial view (`name`, `expirationTimestamp`,
102 * `allowPersonalUsage`, `value`, `qrCode`) and tokens have no labels,
103 * so Alchemy tracks a token only by the name recorded in state.
104 *
105 * ### Creating an Enrollment Token
106 * **Example:** Long-lived token
107 * ```typescript
108 * const token = yield* GCP.AndroidManagement.EnterprisesEnrollmentToken(
109 * "Enroll",
110 * { parent: enterprise.name },
111 * );
112 * ```
113 *
114 * **Example:** One-time token for a policy
115 * ```typescript
116 * const token = yield* GCP.AndroidManagement.EnterprisesEnrollmentToken(
117 * "Enroll",
118 * {
119 * parent: enterprise.name,
120 * policyName: "default",
121 * oneTimeOnly: true,
122 * duration: "86400s",
123 * },
124 * );
125 * ```
126 *
127 * @resource
128 * @category AndroidManagement
129 */
130 export const EnterprisesEnrollmentToken = Resource<EnterprisesEnrollmentToken>(
131 "GCP.AndroidManagement.EnterprisesEnrollmentToken",
132 );
133
134 const toAttrs = (
135 token: androidmanagement.EnrollmentToken,
136 project: string,
137 extras?: {
138 parent?: string;
139 policyName?: string;
140 googleAuthenticationOptions?: androidmanagement.GoogleAuthenticationOptions;
141 oneTimeOnly?: boolean;
142 duration?: string;
143 additionalData?: string;
144 },
145 ) => {
146 const name = token.name ?? "";
147 return {
148 name,
149 enrollmentTokenId: lastSegment(name),
150 parent: extras?.parent ?? parentOf(name),
151 project,
152 policyName: extras?.policyName ?? token.policyName,
153 allowPersonalUsage: token.allowPersonalUsage,
154 googleAuthenticationOptions:
155 extras?.googleAuthenticationOptions ?? token.googleAuthenticationOptions,
156 oneTimeOnly: extras?.oneTimeOnly ?? token.oneTimeOnly,
157 duration: extras?.duration ?? token.duration,
158 additionalData: extras?.additionalData ?? token.additionalData,
159 value: token.value,
160 qrCode: token.qrCode,
161 expirationTimestamp: token.expirationTimestamp,
162 };
163 };
164
165 export const EnterprisesEnrollmentTokenProvider = () =>
166 Provider.succeed(EnterprisesEnrollmentToken, {
167 stables: ["name", "enrollmentTokenId", "parent", "project", "expirationTimestamp"],
168
169 diff: Effect.fn(function* ({ news, olds, output }) {
170 if (!isResolved(news)) return undefined;
171 const identity = replaceOnIdentity({
172 previousParent: olds?.parent ?? output?.parent,
173 nextParent: news.parent,
174 extra:
175 (news.policyName !== undefined &&
176 olds?.policyName !== undefined &&
177 news.policyName !== olds.policyName) ||
178 (news.duration !== undefined &&
179 olds?.duration !== undefined &&
180 news.duration !== olds.duration) ||
181 (news.oneTimeOnly !== undefined &&
182 olds?.oneTimeOnly !== undefined &&
183 news.oneTimeOnly !== olds.oneTimeOnly) ||
184 (news.allowPersonalUsage !== undefined &&
185 output?.allowPersonalUsage !== undefined &&
186 news.allowPersonalUsage !== output.allowPersonalUsage) ||
187 (news.additionalData !== undefined &&
188 olds?.additionalData !== undefined &&
189 news.additionalData !== olds.additionalData) ||
190 (news.googleAuthenticationOptions !== undefined &&
191 olds?.googleAuthenticationOptions !== undefined &&
192 !jsonEqual(news.googleAuthenticationOptions, olds.googleAuthenticationOptions)),
193 });
194 return identity;
195 }),
196
197 read: Effect.fn(function* ({ olds, output }) {
198 const env = yield* GcpEnvironment.current;
199 const parent = olds?.parent ?? output?.parent ?? "";
200 const existing = yield* getEnrollmentToken(output?.name ?? "");
201 if (existing === undefined) return undefined;
202 return toAttrs(existing, env.project, {
203 parent,
204 policyName: olds?.policyName ?? output?.policyName,
205 googleAuthenticationOptions:
206 olds?.googleAuthenticationOptions ?? output?.googleAuthenticationOptions,
207 oneTimeOnly: olds?.oneTimeOnly ?? output?.oneTimeOnly,
208 duration: olds?.duration ?? output?.duration,
209 additionalData: olds?.additionalData ?? output?.additionalData,
210 });
211 }),
212
213 reconcile: Effect.fn(function* ({ news, output }) {
214 const env = yield* GcpEnvironment.current;
215 const parent = toEnterpriseName(news.parent);
216 const additionalData = news.additionalData;
217 const duration = news.duration ?? output?.duration ?? DEFAULT_DURATION;
218 const desired: androidmanagement.EnrollmentToken = {
219 policyName: news.policyName,
220 allowPersonalUsage: news.allowPersonalUsage,
221 googleAuthenticationOptions: news.googleAuthenticationOptions,
222 oneTimeOnly: news.oneTimeOnly,
223 duration,
224 additionalData,
225 };
226
227 let current = yield* getEnrollmentToken(toEnrollmentTokenName(parent, output?.name));
228
229 if (current === undefined) {
230 current = yield* androidmanagement.createEnterprisesEnrollmentTokens({
231 parent,
232 body: desired,
233 });
234 }
235
236 const name = current.name ?? output?.name ?? "";
237 const fresh = (yield* getEnrollmentToken(name)) ?? current;
238 return toAttrs(fresh, env.project, {
239 parent,
240 policyName: news.policyName ?? output?.policyName,
241 googleAuthenticationOptions:
242 news.googleAuthenticationOptions ?? output?.googleAuthenticationOptions,
243 oneTimeOnly: news.oneTimeOnly ?? output?.oneTimeOnly,
244 duration,
245 additionalData,
246 });
247 }),
248
249 delete: Effect.fn(function* ({ output }) {
250 if (output.name.length === 0) return;
251 yield* androidmanagement
252 .deleteEnterprisesEnrollmentTokens({
253 name: output.name,
254 })
255 .pipe(Effect.catchTag("NotFound", () => Effect.void));
256 }),
257 });