@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 313 lines 10.4 KB
Raw
1 import * as mc from "@distilled.cloud/gcp/migrationcenter_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 { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import {
14 expandParent,
15 fieldMask,
16 hasAlchemyLabelMap,
17 locationParent,
18 normalizeLocation,
19 parseName,
20 replaceOnIdentity,
21 ResourceNotResolved,
22 toPhysicalId,
23 userLabels,
24 waitForOperation,
25 waitUntilExists,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type ImportJobState = mc.ImportJobStateEnum | (string & {});
30
31 export type ImportJobProps = {
32 /**
33 * Import job id (the `{importJob}` segment of
34 * `projects/{project}/locations/{location}/importJobs/{importJob}`). If
35 * omitted, a unique RFC1035 name is generated. Immutable — changing it
36 * replaces the job.
37 */
38 importJobId?: string;
39 /**
40 * Region (`us-central1`, …). Immutable — changing it replaces the job.
41 * `US-CENTRAL1` is accepted and normalized to `us-central1`.
42 * @default the stack's GCP region (`GCP.Region`, else the profile
43 * region, else `us-central1`)
44 */
45 location?: string;
46 /**
47 * Source that receives imported frames. Full name or source id
48 * (combined with `location`). Immutable — changing it replaces the job.
49 */
50 assetSource: string;
51 /**
52 * User-friendly display name. Maximum length is 256 characters.
53 */
54 displayName?: string;
55 /**
56 * User labels. Alchemy ownership labels are merged in automatically.
57 */
58 labels?: Record<string, string>;
59 };
60
61 export type ImportJob = Resource<
62 "GCP.MigrationCenter.ImportJob",
63 ImportJobProps,
64 {
65 /** Full resource name. */
66 name: string;
67 /** Import job id (last path segment). */
68 importJobId: string;
69 /** Project id. */
70 project: string;
71 /** Location id. */
72 location: string;
73 /** Source that receives imported frames. */
74 assetSource: string | undefined;
75 /** User-friendly display name. */
76 displayName: string | undefined;
77 /** User labels (Alchemy ownership labels stripped). */
78 labels: Record<string, string>;
79 /** Server-reported state. */
80 state: string | undefined;
81 /** RFC3339 creation timestamp. */
82 createTime: string | undefined;
83 /** RFC3339 last-update timestamp. */
84 updateTime: string | undefined;
85 /** RFC3339 completion timestamp. */
86 completeTime: string | undefined;
87 },
88 never,
89 Providers
90 >;
91
92 /**
93 * A Migration Center import job that ingests asset frames from uploaded
94 * payload files into a source.
95 *
96 * `importJobId`, `location`, and `assetSource` are immutable. Display
97 * name and labels update in place. Nested import data files are force-
98 * deleted with the job.
99 *
100 * ### Creating an Import Job
101 * **Example:** Bind to an upload source
102 * ```typescript
103 * const source = yield* GCP.MigrationCenter.Source("Inventory", {
104 * type: "SOURCE_TYPE_UPLOAD",
105 * });
106 * const job = yield* GCP.MigrationCenter.ImportJob("Rvtools", {
107 * assetSource: source.name,
108 * displayName: "rvtools-import",
109 * });
110 * ```
111 *
112 * @resource
113 * @category MigrationCenter
114 */
115 export const ImportJob = Resource<ImportJob>("GCP.MigrationCenter.ImportJob");
116
117 const resourceName = (project: string, location: string, importJobId: string) =>
118 `${locationParent(project, location)}/importJobs/${importJobId}`;
119
120 const sourceOf = (value: string, project: string, location: string) =>
121 expandParent(value, project, location, "sources");
122
123 const toAttrs = (item: mc.ImportJob, project: string, region: string) => {
124 const name = item.name ?? "";
125 const parsed = parseName(name, "importJobs", region);
126 return {
127 name,
128 importJobId: parsed.id,
129 project: parsed.project || project,
130 location: parsed.location,
131 assetSource: item.assetSource,
132 displayName: item.displayName,
133 labels: userLabels(item.labels),
134 state: item.state,
135 createTime: item.createTime,
136 updateTime: item.updateTime,
137 completeTime: item.completeTime,
138 };
139 };
140
141 const getByName = (name: string) =>
142 name.length === 0
143 ? Effect.undefined
144 : mc
145 .getProjectsLocationsImportJobs({
146 name,
147 view: "IMPORT_JOB_VIEW_FULL",
148 })
149 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
150
151 const listOwned = (project: string, region: string) =>
152 mc.listProjectsLocationsImportJobs
153 .pages({
154 parent: `projects/${project}/locations/-`,
155 pageSize: 1000,
156 view: "IMPORT_JOB_VIEW_BASIC",
157 })
158 .pipe(
159 Stream.flatMap((page) => Stream.fromIterable(page.importJobs ?? [])),
160 Stream.filter((item) => hasAlchemyLabelMap(item.labels)),
161 Stream.runCollect,
162 Effect.map((chunk) => Array.from(chunk)),
163 Effect.catchTag("NotFound", () =>
164 mc.listProjectsLocationsImportJobs
165 .pages({
166 parent: locationParent(project, region),
167 pageSize: 1000,
168 view: "IMPORT_JOB_VIEW_BASIC",
169 })
170 .pipe(
171 Stream.flatMap((page) => Stream.fromIterable(page.importJobs ?? [])),
172 Stream.filter((item) => hasAlchemyLabelMap(item.labels)),
173 Stream.runCollect,
174 Effect.map((chunk) => Array.from(chunk)),
175 Effect.catchTag("NotFound", () => Effect.succeed([] as mc.ImportJob[])),
176 ),
177 ),
178 );
179
180 export const ImportJobProvider = () =>
181 Provider.succeed(ImportJob, {
182 stables: ["name", "importJobId", "project", "location", "createTime"],
183
184 diff: Effect.fn(function* ({ news, olds, output }) {
185 if (!isResolved(news)) return undefined;
186 const env = yield* GcpEnvironment.current;
187 const previousSource = olds?.assetSource ?? output?.assetSource;
188 const nextSource = news.assetSource;
189 const sourceChanged =
190 previousSource !== undefined &&
191 nextSource !== undefined &&
192 previousSource !== nextSource &&
193 !previousSource.endsWith(`/${nextSource}`) &&
194 !nextSource.endsWith(`/${previousSource}`);
195 return replaceOnIdentity({
196 previousId: olds?.importJobId ?? output?.importJobId,
197 nextId: news.importJobId ?? olds?.importJobId ?? output?.importJobId,
198 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
199 nextLocation: normalizeLocation(
200 news.location ?? olds?.location ?? output?.location,
201 env.region,
202 ),
203 extra: sourceChanged,
204 });
205 }),
206
207 read: Effect.fn(function* ({ id, olds, output }) {
208 const env = yield* GcpEnvironment.current;
209 const importJobId = yield* toPhysicalId(
210 id,
211 olds?.importJobId,
212 output?.importJobId,
213 "importjob",
214 );
215 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
216 const name = output?.name ?? resourceName(env.project, location, importJobId);
217 const existing = yield* getByName(name);
218 if (existing === undefined) return undefined;
219 const attrs = toAttrs(existing, env.project, env.region);
220 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
221 }),
222
223 list: () =>
224 Effect.gen(function* () {
225 const env = yield* GcpEnvironment.current;
226 const items = yield* listOwned(env.project, env.region);
227 return items.map((item) => toAttrs(item, env.project, env.region));
228 }),
229
230 reconcile: Effect.fn(function* ({ id, news, output }) {
231 const env = yield* GcpEnvironment.current;
232 const importJobId = yield* toPhysicalId(
233 id,
234 news.importJobId,
235 output?.importJobId,
236 "importjob",
237 );
238 const location = normalizeLocation(news.location ?? output?.location, env.region);
239 const name = resourceName(env.project, location, importJobId);
240 const assetSource = sourceOf(news.assetSource, env.project, location);
241 const desiredLabels = {
242 ...toLabels(news.labels),
243 ...(yield* createInternalLabels(id)),
244 };
245 const displayName = news.displayName ?? importJobId;
246
247 let current = yield* getByName(output?.name ?? name);
248
249 if (current === undefined) {
250 const created = yield* mc
251 .createProjectsLocationsImportJobs({
252 parent: locationParent(env.project, location),
253 importJobId,
254 body: {
255 assetSource,
256 displayName,
257 labels: desiredLabels,
258 },
259 })
260 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
261 if (created !== undefined) {
262 yield* waitForOperation(created);
263 }
264 current = yield* waitUntilExists(getByName(name), name);
265 }
266
267 if (current === undefined) {
268 return yield* new ResourceNotResolved({ resourceName: name });
269 }
270
271 const observedLabels = tagRecord(current.labels);
272 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
273 const labelsChanged = upsert.length > 0 || removed.length > 0;
274 const displayNameChanged = (current.displayName ?? "") !== displayName;
275 const mask = fieldMask([labelsChanged && "labels", displayNameChanged && "displayName"]);
276
277 if (mask.length > 0) {
278 const operation = yield* mc.patchProjectsLocationsImportJobs({
279 name: current.name ?? name,
280 updateMask: mask,
281 body: {
282 name: current.name ?? name,
283 displayName,
284 labels: desiredLabels,
285 },
286 });
287 yield* waitForOperation(operation);
288 current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name);
289 }
290
291 return toAttrs(current, env.project, env.region);
292 }),
293
294 delete: Effect.fn(function* ({ output }) {
295 const operation = yield* mc
296 .deleteProjectsLocationsImportJobs({
297 name: output.name,
298 force: true,
299 })
300 .pipe(
301 Effect.retry({
302 while: (error) => error._tag === "Conflict",
303 times: 8,
304 schedule: Schedule.spaced("2 seconds"),
305 }),
306 Effect.catchTag("NotFound", () => Effect.undefined),
307 );
308 if (operation !== undefined) {
309 yield* waitForOperation(operation, { notFoundOk: true });
310 }
311 yield* waitUntilGone(getByName(output.name), output.name);
312 }),
313 });