@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, 04:45 UTC

main
ts 322 lines 10.9 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 * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 encodeOwnershipLine,
14 expandParent,
15 hasOwnershipMarker,
16 locationParent,
17 normalizeLocation,
18 ownedByAlchemy,
19 parseName,
20 parseOwnership,
21 replaceOnIdentity,
22 ResourceNotResolved,
23 toPhysicalId,
24 waitForOperation,
25 waitUntilExists,
26 waitUntilGone,
27 } from "./internal.ts";
28
29 export type ImportDataFileFormat = mc.ImportDataFileFormatEnum | (string & {});
30 export type ImportDataFileState = mc.ImportDataFileStateEnum | (string & {});
31
32 export type ImportJobsImportDataFileProps = {
33 /**
34 * Parent import job. Full name
35 * `projects/{project}/locations/{location}/importJobs/{importJob}` or
36 * the import job id (combined with `location`). Immutable — changing it
37 * replaces the file.
38 */
39 importJob: string;
40 /**
41 * Region used when `importJob` is a bare id.
42 * @default the stack's GCP region (`GCP.Region`, else the profile
43 * region, else `us-central1`)
44 */
45 location?: string;
46 /**
47 * Import data file id (the `{importDataFile}` segment). If omitted, a
48 * unique RFC1035 name is generated. Immutable — changing it replaces
49 * the file.
50 */
51 importDataFileId?: string;
52 /**
53 * Payload format. Immutable — changing it replaces the file.
54 * @default "IMPORT_JOB_FORMAT_RVTOOLS_CSV"
55 */
56 format?: ImportDataFileFormat;
57 /**
58 * User-friendly display name. Maximum length is 63 characters including
59 * Alchemy's ownership marker. Import data files have no labels field,
60 * so ownership is stored in a `[alchemy …]` prefix and stripped from
61 * attributes.
62 */
63 displayName?: string;
64 };
65
66 export type ImportJobsImportDataFile = Resource<
67 "GCP.MigrationCenter.ImportJobsImportDataFile",
68 ImportJobsImportDataFileProps,
69 {
70 /** Full resource name. */
71 name: string;
72 /** Import data file id (last path segment). */
73 importDataFileId: string;
74 /** Parent import job resource name. */
75 importJob: string;
76 /** Project id. */
77 project: string;
78 /** Location id. */
79 location: string;
80 /** Payload format. */
81 format: string | undefined;
82 /** User display name with the Alchemy ownership prefix stripped. */
83 displayName: string | undefined;
84 /** Server-reported state. */
85 state: string | undefined;
86 /** Signed upload URI. */
87 signedUri: string | undefined;
88 /** Upload URI expiration. */
89 uriExpirationTime: string | undefined;
90 /** RFC3339 creation timestamp. */
91 createTime: string | undefined;
92 },
93 never,
94 Providers
95 >;
96
97 /**
98 * A payload file attached to a Migration Center import job. Creating the
99 * file returns a signed URI that the client uploads inventory CSV/XLSX
100 * into.
101 *
102 * Import data files have no labels field — Alchemy stamps ownership into
103 * the display name so `list` / nuke can find them. The API has no patch
104 * method; changing format or display name replaces the file.
105 *
106 * ### Creating an Import Data File
107 * **Example:** RVTools CSV payload
108 * ```typescript
109 * const job = yield* GCP.MigrationCenter.ImportJob("Rvtools", {
110 * assetSource: source.name,
111 * });
112 * const file = yield* GCP.MigrationCenter.ImportJobsImportDataFile("Payload", {
113 * importJob: job.name,
114 * format: "IMPORT_JOB_FORMAT_RVTOOLS_CSV",
115 * displayName: "inventory",
116 * });
117 * ```
118 *
119 * @resource
120 * @category MigrationCenter
121 */
122 export const ImportJobsImportDataFile = Resource<ImportJobsImportDataFile>(
123 "GCP.MigrationCenter.ImportJobsImportDataFile",
124 );
125
126 const DEFAULT_FORMAT: ImportDataFileFormat = "IMPORT_JOB_FORMAT_RVTOOLS_CSV";
127
128 const jobNameOf = (value: string, project: string, location: string) =>
129 expandParent(value, project, location, "importJobs");
130
131 const resourceName = (importJob: string, importDataFileId: string) =>
132 `${importJob}/importDataFiles/${importDataFileId}`;
133
134 const toAttrs = (item: mc.ImportDataFile, project: string, region: string) => {
135 const name = item.name ?? "";
136 const parsed = parseName(name, "importDataFiles", region);
137 const ownership = parseOwnership(item.displayName);
138 return {
139 name,
140 importDataFileId: parsed.id,
141 importJob: parsed.parent,
142 project: parsed.project || project,
143 location: parsed.location,
144 format: item.format,
145 displayName: ownership.text,
146 state: item.state,
147 signedUri: item.uploadFileInfo?.signedUri,
148 uriExpirationTime: item.uploadFileInfo?.uriExpirationTime,
149 createTime: item.createTime,
150 };
151 };
152
153 const getByName = (name: string) =>
154 name.length === 0
155 ? Effect.undefined
156 : mc
157 .getProjectsLocationsImportJobsImportDataFiles({ name })
158 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
159
160 const listFiles = (parent: string) =>
161 mc.listProjectsLocationsImportJobsImportDataFiles.pages({ parent, pageSize: 1000 }).pipe(
162 Stream.flatMap((page) => Stream.fromIterable(page.importDataFiles ?? [])),
163 Stream.runCollect,
164 Effect.map((chunk) => Array.from(chunk)),
165 Effect.catchTag("NotFound", () => Effect.succeed([] as mc.ImportDataFile[])),
166 );
167
168 const listJobs = (project: string, region: string) =>
169 mc.listProjectsLocationsImportJobs
170 .pages({
171 parent: `projects/${project}/locations/-`,
172 pageSize: 1000,
173 view: "IMPORT_JOB_VIEW_BASIC",
174 })
175 .pipe(
176 Stream.flatMap((page) => Stream.fromIterable(page.importJobs ?? [])),
177 Stream.runCollect,
178 Effect.map((chunk) => Array.from(chunk)),
179 Effect.catchTag("NotFound", () =>
180 mc.listProjectsLocationsImportJobs
181 .pages({
182 parent: locationParent(project, region),
183 pageSize: 1000,
184 view: "IMPORT_JOB_VIEW_BASIC",
185 })
186 .pipe(
187 Stream.flatMap((page) => Stream.fromIterable(page.importJobs ?? [])),
188 Stream.runCollect,
189 Effect.map((chunk) => Array.from(chunk)),
190 Effect.catchTag("NotFound", () => Effect.succeed([] as mc.ImportJob[])),
191 ),
192 ),
193 );
194
195 const listOwned = (project: string, region: string) =>
196 Effect.gen(function* () {
197 const jobs = yield* listJobs(project, region);
198 const files: mc.ImportDataFile[] = [];
199 for (const job of jobs) {
200 if (job.name === undefined) continue;
201 const nested = yield* listFiles(job.name);
202 for (const file of nested) {
203 if (hasOwnershipMarker(file.displayName)) files.push(file);
204 }
205 }
206 return files;
207 });
208
209 export const ImportJobsImportDataFileProvider = () =>
210 Provider.succeed(ImportJobsImportDataFile, {
211 stables: ["name", "importDataFileId", "importJob", "project", "location", "createTime"],
212
213 diff: Effect.fn(function* ({ news, olds, output }) {
214 if (!isResolved(news)) return undefined;
215 const env = yield* GcpEnvironment.current;
216 const previousFormat = olds?.format ?? output?.format ?? DEFAULT_FORMAT;
217 const nextFormat = news.format ?? previousFormat;
218 const previousJob = olds?.importJob ?? output?.importJob;
219 const nextJob = news.importJob;
220 return replaceOnIdentity({
221 previousId: olds?.importDataFileId ?? output?.importDataFileId,
222 nextId: news.importDataFileId ?? olds?.importDataFileId ?? output?.importDataFileId,
223 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
224 nextLocation: normalizeLocation(
225 news.location ?? olds?.location ?? output?.location,
226 env.region,
227 ),
228 previousParent: previousJob,
229 nextParent: nextJob,
230 extra: previousFormat !== nextFormat,
231 });
232 }),
233
234 read: Effect.fn(function* ({ id, olds, output }) {
235 const env = yield* GcpEnvironment.current;
236 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
237 const importJob = jobNameOf(
238 olds?.importJob ?? output?.importJob ?? "",
239 env.project,
240 location,
241 );
242 const importDataFileId = yield* toPhysicalId(
243 id,
244 olds?.importDataFileId,
245 output?.importDataFileId,
246 "importdata",
247 );
248 const name =
249 output?.name ?? (importJob.length > 0 ? resourceName(importJob, importDataFileId) : "");
250 const existing = yield* getByName(name);
251 if (existing === undefined) return undefined;
252 const attrs = toAttrs(existing, env.project, env.region);
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 items = yield* listOwned(env.project, env.region);
260 return items.map((item) => toAttrs(item, env.project, env.region));
261 }),
262
263 reconcile: Effect.fn(function* ({ id, news, output }) {
264 const env = yield* GcpEnvironment.current;
265 const location = normalizeLocation(news.location ?? output?.location, env.region);
266 const importJob = jobNameOf(news.importJob, env.project, location);
267 const importDataFileId = yield* toPhysicalId(
268 id,
269 news.importDataFileId,
270 output?.importDataFileId,
271 "importdata",
272 );
273 const name = resourceName(importJob, importDataFileId);
274 const ownership = yield* createInternalLabels(id);
275 const displayName = encodeOwnershipLine(ownership, news.displayName);
276 const format = news.format ?? DEFAULT_FORMAT;
277
278 let current = yield* getByName(output?.name ?? name);
279
280 if (current === undefined) {
281 const created = yield* mc
282 .createProjectsLocationsImportJobsImportDataFiles({
283 parent: importJob,
284 importDataFileId,
285 body: {
286 displayName,
287 format,
288 },
289 })
290 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
291 if (created !== undefined) {
292 yield* waitForOperation(created);
293 }
294 current = yield* waitUntilExists(getByName(name), name);
295 }
296
297 if (current === undefined) {
298 return yield* new ResourceNotResolved({ resourceName: name });
299 }
300
301 return toAttrs(current, env.project, env.region);
302 }),
303
304 delete: Effect.fn(function* ({ output }) {
305 const operation = yield* mc
306 .deleteProjectsLocationsImportJobsImportDataFiles({
307 name: output.name,
308 })
309 .pipe(
310 Effect.retry({
311 while: (error) => error._tag === "Conflict",
312 times: 8,
313 schedule: Schedule.spaced("2 seconds"),
314 }),
315 Effect.catchTag("NotFound", () => Effect.undefined),
316 );
317 if (operation !== undefined) {
318 yield* waitForOperation(operation, { notFoundOk: true });
319 }
320 yield* waitUntilGone(getByName(output.name), output.name);
321 }),
322 });