@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 191 lines 5.73 KB
Raw
1 import * as jobs from "@distilled.cloud/gcp/jobs_v4";
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 deleteTenant,
12 encodeOwnershipLine,
13 findOwnedTenant,
14 getTenant,
15 listOwnedTenants,
16 MAX_EXTERNAL_ID_LENGTH,
17 ownedByAlchemy,
18 ownershipLabels,
19 parseOwnership,
20 parseTenantName,
21 projectParent,
22 replaceOnIdentity,
23 sameText,
24 tenantNameOf,
25 toGeneratedName,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type TenantProps = {
30 /**
31 * Server-assigned tenant id (the `{tenant}` segment of
32 * `projects/{project}/tenants/{tenant}`). Leave blank on create.
33 * Immutable — changing it replaces the tenant.
34 */
35 tenantId?: string;
36 /**
37 * Client-side tenant identifier (max 255 characters). Cloud Talent
38 * tenants have no labels field, so Alchemy ownership is stored in a
39 * `[alchemy …]` prefix and stripped from attributes.
40 */
41 externalId?: string;
42 };
43
44 export type Tenant = Resource<
45 "GCP.Jobs.Tenant",
46 TenantProps,
47 {
48 /** Full resource name `projects/{project}/tenants/{tenant}`. */
49 name: string;
50 /** Server-assigned tenant id. */
51 tenantId: string;
52 /** Project id. */
53 project: string;
54 /** Client identifier with the Alchemy ownership prefix stripped. */
55 externalId: string | undefined;
56 },
57 never,
58 Providers
59 >;
60
61 /**
62 * A Cloud Talent Solution tenant. Tenants isolate jobs and companies
63 * for different customer groups.
64 *
65 * Tenants have no labels field, so Alchemy stamps ownership into
66 * `externalId` for `list` / nuke. The server-assigned tenant id is
67 * identity — changing `tenantId` replaces the tenant. `externalId`
68 * updates in place.
69 *
70 * ### Creating a Tenant
71 * **Example:** Generated identifier
72 * ```typescript
73 * const tenant = yield* GCP.Jobs.Tenant("Acme", {});
74 * ```
75 *
76 * **Example:** Explicit external id
77 * ```typescript
78 * const tenant = yield* GCP.Jobs.Tenant("Acme", {
79 * externalId: "acme-corp",
80 * });
81 * ```
82 *
83 * ### Updating a Tenant
84 * Re-declare the same logical id with changed props; the engine keeps the
85 * physical resource and updates it in place.
86 *
87 * **Example:** Change the client identifier
88 * ```typescript
89 * const tenant = yield* GCP.Jobs.Tenant("Acme", {
90 * externalId: "acme-holdings",
91 * });
92 * ```
93 *
94 * @resource
95 * @category Jobs
96 */
97 export const Tenant = Resource<Tenant>("GCP.Jobs.Tenant");
98
99 export class TenantNotResolved extends Data.TaggedError("GCP.Jobs.TenantNotResolved")<{
100 resourceName: string;
101 }> {}
102
103 const toAttrs = (tenant: jobs.Tenant, project: string) => {
104 const name = tenant.name ?? "";
105 const parsed = parseTenantName(name, project);
106 return {
107 name,
108 tenantId: parsed.tenantId,
109 project: parsed.project || project,
110 externalId: parseOwnership(tenant.externalId).text,
111 };
112 };
113
114 export const TenantProvider = () =>
115 Provider.succeed(Tenant, {
116 stables: ["name", "tenantId", "project"],
117
118 diff: Effect.fn(function* ({ news, olds, output }) {
119 if (!isResolved(news)) return undefined;
120 return replaceOnIdentity({
121 previousId: olds?.tenantId ?? output?.tenantId,
122 nextId: news.tenantId,
123 });
124 }),
125
126 read: Effect.fn(function* ({ id, olds, output }) {
127 const env = yield* GcpEnvironment.current;
128 const name =
129 output?.name ?? tenantNameOf(env.project, olds?.tenantId ?? output?.tenantId ?? "");
130 let existing = yield* getTenant(name);
131 if (existing === undefined) {
132 existing = yield* findOwnedTenant(env.project, id);
133 }
134 if (existing === undefined) return undefined;
135 const attrs = toAttrs(existing, env.project);
136 return (yield* ownedByAlchemy(id, existing.externalId)) ? attrs : Unowned(attrs);
137 }),
138
139 list: () =>
140 Effect.gen(function* () {
141 const env = yield* GcpEnvironment.current;
142 const tenants = yield* listOwnedTenants(env.project);
143 return tenants.map((tenant) => toAttrs(tenant, env.project));
144 }),
145
146 reconcile: Effect.fn(function* ({ id, news, output }) {
147 const env = yield* GcpEnvironment.current;
148 const ownership = yield* ownershipLabels(id);
149 const clientId = yield* toGeneratedName(id, news.externalId, output?.externalId);
150 const externalId = encodeOwnershipLine(ownership, clientId, MAX_EXTERNAL_ID_LENGTH);
151 const name = output?.name ?? tenantNameOf(env.project, news.tenantId ?? "");
152
153 let current = yield* getTenant(name);
154 if (current === undefined) {
155 current = yield* findOwnedTenant(env.project, id);
156 }
157
158 if (current === undefined) {
159 const created = yield* jobs
160 .createProjectsTenants({
161 parent: projectParent(env.project),
162 body: { externalId },
163 })
164 .pipe(Effect.catchTag("Conflict", () => findOwnedTenant(env.project, id)));
165 current = created ?? undefined;
166 }
167
168 if (current === undefined) {
169 return yield* new TenantNotResolved({
170 resourceName: name || `projects/${env.project}/tenants/${clientId}`,
171 });
172 }
173
174 const currentName = current.name ?? name;
175 if (!sameText(current.externalId, externalId)) {
176 current = yield* jobs.patchProjectsTenants({
177 name: currentName,
178 updateMask: "externalId",
179 body: { name: currentName, externalId },
180 });
181 }
182
183 return toAttrs(current, env.project);
184 }),
185
186 delete: Effect.fn(function* ({ output }) {
187 if (!output.name) return;
188 yield* deleteTenant(output.name);
189 yield* waitUntilGone(getTenant(output.name));
190 }),
191 });