The best way to control Claude Code, Codex and more from any computer or phone

main
ts 284 lines 9.56 KB
Raw
1 import * as dlp from "@distilled.cloud/gcp/dlp_v2";
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 * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 collectPages,
14 encodeOwnership,
15 hasHybridOwnership,
16 hybridLabels,
17 jsonEqual,
18 lastSegment,
19 projectOf,
20 replaceOnIdentity,
21 toResourceId,
22 } from "./internal.ts";
23
24 export type RiskAnalysisJobConfig = dlp.GooglePrivacyDlpV2RiskAnalysisJobConfig;
25
26 export type DlpJobProps = {
27 /**
28 * Job id (the `{dlpJob}` segment of `projects/{project}/dlpJobs/{dlpJob}`).
29 * If omitted, a unique name is generated. Must match `[a-zA-Z0-9_-]+`
30 * and is at most 100 characters. Immutable — changing it replaces the
31 * job.
32 */
33 jobId?: string;
34 /**
35 * Inspect-job configuration. Mutually exclusive with `riskJob`.
36 * Hybrid storage is the recommended test shape: Alchemy stamps
37 * ownership into `hybridOptions.labels` for `list` / nuke.
38 */
39 inspectJob?: dlp.GooglePrivacyDlpV2InspectJobConfig;
40 /**
41 * Risk-analysis job configuration. Mutually exclusive with
42 * `inspectJob`.
43 */
44 riskJob?: RiskAnalysisJobConfig;
45 };
46
47 export type DlpJob = Resource<
48 "GCP.DLP.DlpJob",
49 DlpJobProps,
50 {
51 /** Full resource name `projects/{project}/dlpJobs/{id}`. */
52 name: string;
53 /** Job id (last path segment). */
54 jobId: string;
55 /** Project id. */
56 project: string;
57 /** Job type (`INSPECT_JOB` or `RISK_ANALYSIS_JOB`). */
58 type: string | undefined;
59 /** Job state (`PENDING`, `RUNNING`, `DONE`, …). */
60 state: string | undefined;
61 /** Inspect-job snapshot, if this is an inspect job. */
62 inspectJob: dlp.GooglePrivacyDlpV2InspectJobConfig | undefined;
63 /** Trigger that created the job, if any. */
64 jobTriggerName: string | undefined;
65 /** RFC3339 creation timestamp. */
66 createTime: string | undefined;
67 /** RFC3339 start timestamp. */
68 startTime: string | undefined;
69 /** RFC3339 end timestamp. */
70 endTime: string | undefined;
71 },
72 never,
73 Providers
74 >;
75
76 /**
77 * A project-scoped Cloud DLP inspect or risk-analysis job.
78 *
79 * DLP jobs have no labels or description field. For inspect jobs Alchemy
80 * stamps ownership into `storageConfig.hybridOptions.labels` so `list` /
81 * nuke can find them. Jobs are not updatable — changing `jobId`,
82 * `inspectJob`, or `riskJob` replaces the job.
83 *
84 * ### Creating a DLP Job
85 * **Example:** Hybrid inspect job
86 * ```typescript
87 * const job = yield* GCP.DLP.DlpJob("Scan", {
88 * inspectJob: {
89 * inspectConfig: { infoTypes: [{ name: "EMAIL_ADDRESS" }] },
90 * storageConfig: { hybridOptions: { description: "hybrid scan" } },
91 * },
92 * });
93 * ```
94 *
95 * @resource
96 * @category DLP
97 */
98 export const DlpJob = Resource<DlpJob>("GCP.DLP.DlpJob");
99
100 export class DlpJobNotResolved extends Data.TaggedError("GCP.DLP.DlpJobNotResolved")<{
101 resourceName: string;
102 }> {}
103
104 const MAX_JOB_ID_LENGTH = 64;
105
106 const stripJobPrefix = (jobId: string) => jobId.replace(/^[ir]-/, "");
107
108 const prefixedJobId = (jobId: string, risk: boolean) => {
109 const bare = stripJobPrefix(jobId).slice(0, MAX_JOB_ID_LENGTH - 2);
110 return `${risk ? "r" : "i"}-${bare}`;
111 };
112
113 const resourceName = (project: string, jobId: string, risk: boolean) =>
114 `projects/${project}/dlpJobs/${prefixedJobId(jobId, risk)}`;
115
116 const observedInspectJob = (job: dlp.GooglePrivacyDlpV2DlpJob) =>
117 job.inspectDetails?.requestedOptions?.jobConfig;
118
119 const observedHybridLabels = (job: dlp.GooglePrivacyDlpV2DlpJob) =>
120 observedInspectJob(job)?.storageConfig?.hybridOptions?.labels;
121
122 const stampInspectJob = (
123 inspectJob: dlp.GooglePrivacyDlpV2InspectJobConfig | undefined,
124 ownership: Record<string, string>,
125 ): dlp.GooglePrivacyDlpV2InspectJobConfig | undefined => {
126 if (inspectJob === undefined) return undefined;
127 const hybrid = inspectJob.storageConfig?.hybridOptions;
128 const storage = inspectJob.storageConfig;
129 const usingOtherStorage =
130 storage?.cloudStorageOptions !== undefined ||
131 storage?.bigQueryOptions !== undefined ||
132 storage?.datastoreOptions !== undefined;
133 if (usingOtherStorage && hybrid === undefined) return inspectJob;
134 return {
135 ...inspectJob,
136 storageConfig: {
137 ...storage,
138 hybridOptions: {
139 ...hybrid,
140 labels: {
141 ...hybrid?.labels,
142 ...hybridLabels(ownership),
143 },
144 description: encodeOwnership(ownership, hybrid?.description),
145 },
146 },
147 };
148 };
149
150 const toAttrs = (job: dlp.GooglePrivacyDlpV2DlpJob, project: string) => {
151 const name = job.name ?? "";
152 return {
153 name,
154 jobId: lastSegment(name),
155 project: projectOf(name) || project,
156 type: job.type,
157 state: job.state,
158 inspectJob: observedInspectJob(job),
159 jobTriggerName: job.jobTriggerName,
160 createTime: job.createTime,
161 startTime: job.startTime,
162 endTime: job.endTime,
163 };
164 };
165
166 const getByName = (name: string) =>
167 name.length === 0
168 ? Effect.undefined
169 : dlp.getProjectsDlpJobs({ name }).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
170
171 export const DlpJobProvider = () =>
172 Provider.succeed(DlpJob, {
173 stables: ["name", "jobId", "project", "type", "createTime"],
174
175 diff: Effect.fn(function* ({ news, olds, output }) {
176 if (!isResolved(news)) return undefined;
177 const previous = olds?.jobId ?? output?.jobId;
178 const idChanged =
179 previous !== undefined && news.jobId !== undefined && news.jobId !== previous;
180 const inspectChanged = olds !== undefined && !jsonEqual(olds.inspectJob, news.inspectJob);
181 const riskChanged = olds !== undefined && !jsonEqual(olds.riskJob, news.riskJob);
182 return replaceOnIdentity(idChanged || inspectChanged || riskChanged);
183 }),
184
185 read: Effect.fn(function* ({ id, olds, output }) {
186 const env = yield* GcpEnvironment.current;
187 const jobId = yield* toResourceId(
188 id,
189 olds?.jobId !== undefined ? stripJobPrefix(olds.jobId) : undefined,
190 output?.jobId !== undefined ? stripJobPrefix(output.jobId) : undefined,
191 );
192 const risk = olds?.riskJob !== undefined || output?.type === "RISK_ANALYSIS_JOB";
193 const name = output?.name ?? resourceName(env.project, jobId, risk);
194 const existing = yield* getByName(name);
195 if (existing === undefined) return undefined;
196 const attrs = toAttrs(existing, env.project);
197 if (output !== undefined && output.name === attrs.name) return attrs;
198 const labels = observedHybridLabels(existing);
199 return hasHybridOwnership(labels) ? attrs : Unowned(attrs);
200 }),
201
202 list: () =>
203 Effect.gen(function* () {
204 const env = yield* GcpEnvironment.current;
205 const items = yield* collectPages(
206 dlp.listProjectsDlpJobs.pages({
207 parent: `projects/${env.project}`,
208 pageSize: 100,
209 type: "INSPECT_JOB",
210 }),
211 (page) => page.jobs,
212 ).pipe(
213 Effect.catchTag("NotFound", () => Effect.succeed([] as dlp.GooglePrivacyDlpV2DlpJob[])),
214 );
215 return items
216 .filter((job) => hasHybridOwnership(observedHybridLabels(job)))
217 .map((job) => toAttrs(job, env.project));
218 }),
219
220 reconcile: Effect.fn(function* ({ id, news, output }) {
221 const env = yield* GcpEnvironment.current;
222 const jobId = yield* toResourceId(
223 id,
224 news.jobId !== undefined ? stripJobPrefix(news.jobId) : undefined,
225 output?.jobId !== undefined ? stripJobPrefix(output.jobId) : undefined,
226 );
227 const risk = news.riskJob !== undefined;
228 const name = resourceName(env.project, jobId, risk);
229 const ownership = yield* createInternalLabels(id);
230 const inspectJob = stampInspectJob(news.inspectJob, ownership);
231
232 let current = yield* getByName(output?.name ?? name);
233
234 if (current === undefined) {
235 const created = yield* dlp
236 .createProjectsDlpJobs({
237 parent: `projects/${env.project}`,
238 body: {
239 jobId: prefixedJobId(jobId, risk),
240 inspectJob,
241 riskJob: news.riskJob,
242 },
243 })
244 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
245 current = created ?? undefined;
246 }
247
248 if (current === undefined) {
249 return yield* new DlpJobNotResolved({ resourceName: name });
250 }
251
252 if (inspectJob !== undefined && observedHybridLabels(current) === undefined) {
253 current = yield* getByName(current.name ?? name).pipe(
254 Effect.filterOrFail(
255 (job) =>
256 job !== undefined &&
257 (observedHybridLabels(job) !== undefined ||
258 job.state === "FAILED" ||
259 job.state === "DONE" ||
260 job.state === "CANCELED"),
261 () => new DlpJobNotResolved({ resourceName: name }),
262 ),
263 Effect.retry({
264 while: (error) => error._tag === "GCP.DLP.DlpJobNotResolved",
265 times: 8,
266 schedule: Schedule.spaced("1 second"),
267 }),
268 Effect.catchTag("GCP.DLP.DlpJobNotResolved", () => Effect.succeed(current)),
269 );
270 }
271
272 if (current === undefined) {
273 return yield* new DlpJobNotResolved({ resourceName: name });
274 }
275
276 return toAttrs(current, env.project);
277 }),
278
279 delete: Effect.fn(function* ({ output }) {
280 yield* dlp
281 .deleteProjectsDlpJobs({ name: output.name })
282 .pipe(Effect.catchTag("NotFound", () => Effect.void));
283 }),
284 });