@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 346 lines 10.9 KB
Raw
1 import * as chromemanagement from "@distilled.cloud/gcp/chromemanagement_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 detailsNeedSync,
12 encodeOwnershipLine,
13 findConnectorConfigByName,
14 findOwnedConnectorConfig,
15 getConnectorConfig,
16 hasOwnershipMarker,
17 lastSegment,
18 listOwnedConnectorConfigs,
19 MAX_DISPLAY_NAME_LENGTH,
20 ownedByAlchemy,
21 ownershipLabels,
22 parentOf,
23 parseOwnership,
24 replaceOnIdentity,
25 sameText,
26 toConnectorConfigId,
27 toConnectorConfigName,
28 toCustomerName,
29 toDisplayName,
30 updateMaskOf,
31 } from "./internal.ts";
32
33 export type ConnectorConfigDetails =
34 chromemanagement.GoogleChromeManagementVersionsV1ConnectorConfigDetails;
35 export type ConnectorConfigType =
36 | chromemanagement.GoogleChromeManagementVersionsV1ConnectorConfigTypeEnum
37 | (string & {});
38 export type ConnectorConfigStatus =
39 chromemanagement.GoogleChromeManagementVersionsV1ConnectorConfigStatus;
40
41 export type CustomersConnectorConfigProps = {
42 /**
43 * Parent customer (`customers/{customer}` or `{customer}`). Immutable
44 * — changing it replaces the connector config.
45 */
46 parent: string;
47 /**
48 * Connector config id (last path segment). If omitted, a unique id is
49 * generated from the stack, stage, and logical id. Immutable —
50 * changing it replaces the connector config.
51 */
52 connectorConfigId?: string;
53 /**
54 * Admin-facing display name. Connector configs have no labels field,
55 * so Alchemy stores ownership in a `[alchemy …]` prefix and strips it
56 * from attributes.
57 */
58 displayName?: string;
59 /**
60 * Connector type (`REPORTING`, `DEVICE_TRUST`, `XDR`,
61 * `IDENTITY_BASED_ENROLLMENT`, `CERTIFICATE_AUTHORITY`, `ROOT_STORE`,
62 * `CONTENT_ANALYSIS`). Immutable — changing it replaces the connector
63 * config.
64 */
65 type: ConnectorConfigType;
66 /**
67 * Type-specific connector details (Pub/Sub, Splunk, CrowdStrike,
68 * device trust, MIP, …). `apiKey` and `hecToken` fields are input-only
69 * and are not returned by get.
70 */
71 details: ConnectorConfigDetails;
72 };
73
74 export type CustomersConnectorConfig = Resource<
75 "GCP.ChromeManagement.CustomersConnectorConfig",
76 CustomersConnectorConfigProps,
77 {
78 /** Resource name `customers/{customer}/connectorConfigs/{connector_config}`. */
79 name: string;
80 /** Connector config id (last path segment). */
81 connectorConfigId: string;
82 /** Parent customer name `customers/{customer}`. */
83 parent: string;
84 /** Project id used when the connector config was reconciled. */
85 project: string;
86 /** Display name with the Alchemy ownership prefix stripped. */
87 displayName: string | undefined;
88 /** Connector type. */
89 type: string | undefined;
90 /** Type-specific connector details. Input-only secrets are omitted. */
91 details: ConnectorConfigDetails | undefined;
92 /** Server-reported connector status. */
93 status: ConnectorConfigStatus | undefined;
94 },
95 never,
96 Providers
97 >;
98
99 /**
100 * A Chrome Management connector config
101 * (`customers/{customer}/connectorConfigs/{connector_config}`).
102 *
103 * Connector configs have no labels field, so Alchemy stamps ownership
104 * into `displayName` for `list` / nuke. Parent, connector config id, and
105 * type are identity — changing any of them replaces the config. Display
106 * name and details update in place. Create requires a Chrome Enterprise
107 * customer the caller can administer.
108 *
109 * ### Creating a Connector Config
110 * **Example:** Pub/Sub reporting connector
111 * ```typescript
112 * const topic = yield* GCP.PubSub.Topic("chrome-events", {});
113 * const config = yield* GCP.ChromeManagement.CustomersConnectorConfig(
114 * "Reporting",
115 * {
116 * parent: "customers/my_customer",
117 * type: "REPORTING",
118 * displayName: "Alchemy reporting",
119 * details: {
120 * pubSubConfig: {
121 * topicFullPath: topic.name,
122 * reportingSettings: {
123 * enabledDefaultEvents: ["ALL_DEFAULT_EVENTS"],
124 * },
125 * },
126 * },
127 * },
128 * );
129 * ```
130 *
131 * **Example:** Device trust connector
132 * ```typescript
133 * const config = yield* GCP.ChromeManagement.CustomersConnectorConfig(
134 * "Trust",
135 * {
136 * parent: "customers/C01234567",
137 * type: "DEVICE_TRUST",
138 * displayName: "Okta device trust",
139 * details: {
140 * deviceTrustConfig: {
141 * serviceProvider: "OKTA",
142 * urlMatchers: ["https://login.example.com"],
143 * serviceAccounts: ["trust@example.com"],
144 * scope: "BROWSERS_ONLY",
145 * },
146 * },
147 * },
148 * );
149 * ```
150 *
151 * ### Updating a Connector Config
152 * **Example:** Rename and change reporting events
153 * ```typescript
154 * const config = yield* GCP.ChromeManagement.CustomersConnectorConfig(
155 * "Reporting",
156 * {
157 * parent: "customers/my_customer",
158 * type: "REPORTING",
159 * displayName: "Alchemy reporting (prod)",
160 * details: {
161 * pubSubConfig: {
162 * topicFullPath: topic.name,
163 * reportingSettings: {
164 * enabledDefaultEvents: ["BROWSER_CRASH_EVENT"],
165 * enabledOptInEvents: ["ALL_OPT_IN_EVENTS"],
166 * },
167 * },
168 * },
169 * },
170 * );
171 * ```
172 *
173 * @resource
174 * @category ChromeManagement
175 */
176 export const CustomersConnectorConfig = Resource<CustomersConnectorConfig>(
177 "GCP.ChromeManagement.CustomersConnectorConfig",
178 );
179
180 export class CustomersConnectorConfigNotResolved extends Data.TaggedError(
181 "GCP.ChromeManagement.CustomersConnectorConfigNotResolved",
182 )<{
183 parent: string;
184 resourceName: string;
185 }> {}
186
187 const lookupName = (
188 parent: string,
189 connectorConfigId: string | undefined,
190 existingName: string | undefined,
191 ) => {
192 if (existingName && existingName.length > 0) return existingName;
193 return toConnectorConfigName(parent, connectorConfigId);
194 };
195
196 const toAttrs = (
197 row: chromemanagement.GoogleChromeManagementVersionsV1ConnectorConfig,
198 project: string,
199 ) => {
200 const name = row.name ?? "";
201 return {
202 name,
203 connectorConfigId: lastSegment(name),
204 parent: parentOf(name) || toCustomerName(name),
205 project,
206 displayName: parseOwnership(row.displayName).text,
207 type: row.type,
208 details: row.details,
209 status: row.status,
210 };
211 };
212
213 const desiredBody = (input: {
214 news: CustomersConnectorConfigProps;
215 displayName: string;
216 }): chromemanagement.GoogleChromeManagementVersionsV1ConnectorConfig => ({
217 displayName: input.displayName,
218 type: input.news.type,
219 details: input.news.details,
220 });
221
222 export const CustomersConnectorConfigProvider = () =>
223 Provider.succeed(CustomersConnectorConfig, {
224 stables: ["name", "connectorConfigId", "parent", "project"],
225
226 diff: Effect.fn(function* ({ news, olds, output }) {
227 if (!isResolved(news)) return undefined;
228 return replaceOnIdentity({
229 previousParent: olds?.parent ?? output?.parent,
230 nextParent: news.parent,
231 previousId: olds?.connectorConfigId ?? output?.connectorConfigId,
232 nextId: news.connectorConfigId,
233 previousType: olds?.type ?? output?.type,
234 nextType: news.type,
235 });
236 }),
237
238 read: Effect.fn(function* ({ id, olds, output }) {
239 const env = yield* GcpEnvironment.current;
240 const parent = toCustomerName(olds?.parent ?? output?.parent ?? "");
241 const connectorConfigId = yield* toConnectorConfigId(
242 id,
243 olds?.connectorConfigId ?? output?.connectorConfigId,
244 output?.connectorConfigId,
245 );
246 const name = lookupName(parent, connectorConfigId, output?.name);
247 let existing = yield* getConnectorConfig(name);
248 if (existing === undefined) {
249 existing = yield* findOwnedConnectorConfig(id, parent);
250 }
251 if (existing === undefined) return undefined;
252 const attrs = toAttrs(existing, env.project);
253 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
254 }),
255
256 list: () =>
257 Effect.gen(function* () {
258 const env = yield* GcpEnvironment.current;
259 const rows = yield* listOwnedConnectorConfigs();
260 return rows
261 .filter((row) => hasOwnershipMarker(row.displayName))
262 .map((row) => toAttrs(row, env.project));
263 }),
264
265 reconcile: Effect.fn(function* ({ id, news, output, olds }) {
266 const env = yield* GcpEnvironment.current;
267 const parent = toCustomerName(news.parent);
268 const ownership = yield* ownershipLabels(id);
269 const rawName = yield* toDisplayName(id, news.displayName, output?.displayName);
270 const displayName = encodeOwnershipLine(ownership, rawName, MAX_DISPLAY_NAME_LENGTH);
271 const connectorConfigId = yield* toConnectorConfigId(
272 id,
273 news.connectorConfigId,
274 output?.connectorConfigId,
275 );
276 const name = lookupName(parent, connectorConfigId, output?.name);
277 const desired = desiredBody({ news, displayName });
278
279 let current = yield* getConnectorConfig(name);
280 if (current === undefined) {
281 current = yield* findOwnedConnectorConfig(id, parent);
282 }
283 if (current === undefined) {
284 current = yield* findConnectorConfigByName(displayName, parent);
285 }
286
287 if (current === undefined) {
288 const created = yield* chromemanagement
289 .createCustomersConnectorConfigs({
290 parent,
291 connectorConfigId,
292 body: desired,
293 })
294 .pipe(
295 Effect.catchTag("Conflict", () =>
296 getConnectorConfig(name).pipe(
297 Effect.flatMap((row) =>
298 row !== undefined
299 ? Effect.succeed(row)
300 : findConnectorConfigByName(displayName, parent),
301 ),
302 ),
303 ),
304 );
305 current = created ?? undefined;
306 }
307
308 if (current === undefined) {
309 return yield* new CustomersConnectorConfigNotResolved({
310 parent,
311 resourceName: name || displayName,
312 });
313 }
314
315 const currentName = current.name ?? name;
316 const updateMask = updateMaskOf(
317 !sameText(current.displayName, desired.displayName) ? "displayName" : undefined,
318 detailsNeedSync(current.details, desired.details, olds?.details) ? "details" : undefined,
319 );
320
321 if (updateMask.length > 0 && currentName.length > 0) {
322 current = yield* chromemanagement.patchCustomersConnectorConfigs({
323 name: currentName,
324 updateMask,
325 body: desired,
326 });
327 }
328
329 const fresh = (yield* getConnectorConfig(current.name ?? currentName)) ?? current;
330 return toAttrs(fresh, env.project);
331 }),
332
333 delete: Effect.fn(function* ({ output }) {
334 if (!output.name) return;
335 yield* chromemanagement
336 .deleteCustomersConnectorConfigs({
337 name: output.name,
338 })
339 .pipe(
340 Effect.catchTags({
341 NotFound: () => Effect.void,
342 BadRequest: () => Effect.void,
343 }),
344 );
345 }),
346 });