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
| 1 | import * as storagetransfer from "@distilled.cloud/gcp/storagetransfer_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 | createOwnership, |
| 12 | DEFAULT_JOB_STATUS, |
| 13 | encodeOwnership, |
| 14 | fieldMask, |
| 15 | getTransferJob, |
| 16 | hasOwnershipMarker, |
| 17 | isDeletedJob, |
| 18 | jobIdOf, |
| 19 | listTransferJobs, |
| 20 | ownedByAlchemy, |
| 21 | parseOwnership, |
| 22 | retryApiDisabled, |
| 23 | sameValue, |
| 24 | sanitizeJobId, |
| 25 | toJobId, |
| 26 | transferJobName, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type GcsData = storagetransfer.GcsData; |
| 30 | export type PosixFilesystem = storagetransfer.PosixFilesystem; |
| 31 | export type AwsS3Data = storagetransfer.AwsS3Data; |
| 32 | export type HttpData = storagetransfer.HttpData; |
| 33 | export type AzureBlobStorageData = storagetransfer.AzureBlobStorageData; |
| 34 | export type AwsS3CompatibleData = storagetransfer.AwsS3CompatibleData; |
| 35 | export type HdfsData = storagetransfer.HdfsData; |
| 36 | export type ObjectConditions = storagetransfer.ObjectConditions; |
| 37 | export type TransferOptions = storagetransfer.TransferOptions; |
| 38 | export type TransferManifest = storagetransfer.TransferManifest; |
| 39 | export type TransferSpec = storagetransfer.TransferSpec; |
| 40 | export type ReplicationSpec = storagetransfer.ReplicationSpec; |
| 41 | export type NotificationConfig = storagetransfer.NotificationConfig; |
| 42 | export type LoggingConfig = storagetransfer.LoggingConfig; |
| 43 | export type Schedule = storagetransfer.Schedule; |
| 44 | export type EventStream = storagetransfer.EventStream; |
| 45 | export type TransferJobStatus = storagetransfer.TransferJobStatusEnum | (string & {}); |
| 46 | |
| 47 | export type TransferJobProps = { |
| 48 | /** |
| 49 | * Job id (the `{job}` segment of `transferJobs/{job}`). If omitted, a |
| 50 | * unique name is generated from the stack, stage, and logical id. The |
| 51 | * API name is `transferJobs/{job}` (max 128 characters, must not start |
| 52 | * with `transferJobs/OPI` unless this is a POSIX transfer). Immutable |
| 53 | * — changing it replaces the job. |
| 54 | */ |
| 55 | jobId?: string; |
| 56 | /** |
| 57 | * User description (max 1024 bytes). Transfer jobs have no labels |
| 58 | * field, so Alchemy ownership is stored in a `[alchemy …]` prefix and |
| 59 | * stripped from attributes. |
| 60 | */ |
| 61 | description?: string; |
| 62 | /** |
| 63 | * Transfer specification (source, sink, filters, options). Required |
| 64 | * unless `replicationSpec` is set. Updates in place; a complete spec |
| 65 | * must be provided on each update. |
| 66 | */ |
| 67 | transferSpec?: TransferSpec; |
| 68 | /** |
| 69 | * Cross-bucket replication specification. Mutually exclusive with |
| 70 | * `transferSpec`. Immutable after create — changing it replaces the |
| 71 | * job. |
| 72 | */ |
| 73 | replicationSpec?: ReplicationSpec; |
| 74 | /** |
| 75 | * Recurring or one-time schedule. Optional; omit to create a job that |
| 76 | * never auto-runs (invoke `RunTransferJob` instead). Immutable after |
| 77 | * create — changing it replaces the job. |
| 78 | */ |
| 79 | schedule?: Schedule; |
| 80 | /** |
| 81 | * Event-driven transfer stream. When set, `schedule` is ignored. |
| 82 | * Immutable after create — changing it replaces the job. |
| 83 | */ |
| 84 | eventStream?: EventStream; |
| 85 | /** |
| 86 | * Pub/Sub notification configuration. Updates in place. |
| 87 | */ |
| 88 | notificationConfig?: NotificationConfig; |
| 89 | /** |
| 90 | * Cloud Logging configuration. Updates in place. |
| 91 | */ |
| 92 | loggingConfig?: LoggingConfig; |
| 93 | /** |
| 94 | * Job status. Must be `ENABLED` or `DISABLED` on create (the API |
| 95 | * rejects `STATUS_UNSPECIFIED`). `DELETED` is applied by delete. |
| 96 | * @default "ENABLED" |
| 97 | */ |
| 98 | status?: TransferJobStatus; |
| 99 | /** |
| 100 | * User-managed service account that receives bucket permissions |
| 101 | * instead of the Transfer Service agent. Email or unique id. Immutable |
| 102 | * after create — changing it replaces the job. |
| 103 | */ |
| 104 | serviceAccount?: string; |
| 105 | }; |
| 106 | |
| 107 | export type TransferJob = Resource< |
| 108 | "GCP.StorageTransfer.TransferJob", |
| 109 | TransferJobProps, |
| 110 | { |
| 111 | /** Full resource name `transferJobs/{job}`. */ |
| 112 | name: string; |
| 113 | /** Job id (path segment after `transferJobs/`). */ |
| 114 | jobId: string; |
| 115 | /** Project id. */ |
| 116 | project: string; |
| 117 | /** User description with the Alchemy ownership prefix stripped. */ |
| 118 | description: string | undefined; |
| 119 | /** Job status (`ENABLED`, `DISABLED`, `DELETED`). */ |
| 120 | status: string | undefined; |
| 121 | /** Transfer specification. */ |
| 122 | transferSpec: TransferSpec | undefined; |
| 123 | /** Replication specification. */ |
| 124 | replicationSpec: ReplicationSpec | undefined; |
| 125 | /** Notification configuration. */ |
| 126 | notificationConfig: NotificationConfig | undefined; |
| 127 | /** Logging configuration. */ |
| 128 | loggingConfig: LoggingConfig | undefined; |
| 129 | /** Schedule, if any. */ |
| 130 | schedule: Schedule | undefined; |
| 131 | /** Event stream, if any. */ |
| 132 | eventStream: EventStream | undefined; |
| 133 | /** Delegated service account, if any. */ |
| 134 | serviceAccount: string | undefined; |
| 135 | /** RFC3339 creation timestamp. */ |
| 136 | creationTime: string | undefined; |
| 137 | /** RFC3339 last-modification timestamp. */ |
| 138 | lastModificationTime: string | undefined; |
| 139 | /** Most recently started TransferOperation name, if any. */ |
| 140 | latestOperationName: string | undefined; |
| 141 | }, |
| 142 | never, |
| 143 | Providers |
| 144 | >; |
| 145 | |
| 146 | /** |
| 147 | * A Storage Transfer Service job that copies objects between Cloud |
| 148 | * Storage, S3, Azure, POSIX, or HTTP sources and sinks. |
| 149 | * |
| 150 | * Transfer jobs have no labels field, so Alchemy stamps ownership into |
| 151 | * `description` for `list` / nuke. Name is identity. `description`, |
| 152 | * `transferSpec`, `notificationConfig`, `loggingConfig`, and `status` |
| 153 | * update in place. `schedule`, `eventStream`, `serviceAccount`, and |
| 154 | * `replicationSpec` are immutable after create. |
| 155 | * |
| 156 | * ### Creating a Transfer Job |
| 157 | * **Example:** One-shot GCS-to-GCS copy (disabled until RunTransferJob) |
| 158 | * ```typescript |
| 159 | * const job = yield* GCP.StorageTransfer.TransferJob("Nightly", { |
| 160 | * status: "DISABLED", |
| 161 | * description: "copy uploads into the archive bucket", |
| 162 | * transferSpec: { |
| 163 | * gcsDataSource: { bucketName: source.bucketName }, |
| 164 | * gcsDataSink: { bucketName: archive.bucketName }, |
| 165 | * }, |
| 166 | * }); |
| 167 | * ``` |
| 168 | * |
| 169 | * **Example:** Recurring copy with a daily schedule |
| 170 | * ```typescript |
| 171 | * const job = yield* GCP.StorageTransfer.TransferJob("Nightly", { |
| 172 | * description: "daily archive", |
| 173 | * transferSpec: { |
| 174 | * gcsDataSource: { bucketName: "src-bucket", path: "uploads/" }, |
| 175 | * gcsDataSink: { bucketName: "dst-bucket" }, |
| 176 | * }, |
| 177 | * schedule: { |
| 178 | * scheduleStartDate: { year: 2026, month: 1, day: 1 }, |
| 179 | * startTimeOfDay: { hours: 3, minutes: 0 }, |
| 180 | * repeatInterval: "86400s", |
| 181 | * }, |
| 182 | * }); |
| 183 | * ``` |
| 184 | * |
| 185 | * ### Updating a Transfer Job |
| 186 | * Change props on the same logical id; the engine keeps the physical id. |
| 187 | * |
| 188 | * **Example:** Enable the job and change the source prefix |
| 189 | * ```typescript |
| 190 | * const job = yield* GCP.StorageTransfer.TransferJob("Nightly", { |
| 191 | * status: "ENABLED", |
| 192 | * description: "daily archive", |
| 193 | * transferSpec: { |
| 194 | * gcsDataSource: { bucketName: "src-bucket", path: "inbox/" }, |
| 195 | * gcsDataSink: { bucketName: "dst-bucket" }, |
| 196 | * }, |
| 197 | * }); |
| 198 | * ``` |
| 199 | * |
| 200 | * @resource |
| 201 | * @category StorageTransfer |
| 202 | */ |
| 203 | export const TransferJob = Resource<TransferJob>("GCP.StorageTransfer.TransferJob"); |
| 204 | |
| 205 | export class TransferJobNotResolved extends Data.TaggedError( |
| 206 | "GCP.StorageTransfer.TransferJobNotResolved", |
| 207 | )<{ |
| 208 | resourceName: string; |
| 209 | }> {} |
| 210 | |
| 211 | const statusOf = (status: string | undefined) => |
| 212 | status && status.length > 0 ? status : DEFAULT_JOB_STATUS; |
| 213 | |
| 214 | const toAttrs = (job: storagetransfer.TransferJob, project: string) => { |
| 215 | const name = job.name ?? ""; |
| 216 | return { |
| 217 | name, |
| 218 | jobId: jobIdOf(name), |
| 219 | project: job.projectId ?? project, |
| 220 | description: parseOwnership(job.description).text, |
| 221 | status: job.status, |
| 222 | transferSpec: job.transferSpec, |
| 223 | replicationSpec: job.replicationSpec, |
| 224 | notificationConfig: job.notificationConfig, |
| 225 | loggingConfig: job.loggingConfig, |
| 226 | schedule: job.schedule, |
| 227 | eventStream: job.eventStream, |
| 228 | serviceAccount: job.serviceAccount, |
| 229 | creationTime: job.creationTime, |
| 230 | lastModificationTime: job.lastModificationTime, |
| 231 | latestOperationName: job.latestOperationName, |
| 232 | }; |
| 233 | }; |
| 234 | |
| 235 | const replaceOnImmutable = ( |
| 236 | news: TransferJobProps, |
| 237 | previous: TransferJobProps | undefined, |
| 238 | output: |
| 239 | | { |
| 240 | jobId?: string; |
| 241 | schedule?: Schedule; |
| 242 | eventStream?: EventStream; |
| 243 | serviceAccount?: string; |
| 244 | replicationSpec?: ReplicationSpec; |
| 245 | } |
| 246 | | undefined, |
| 247 | ) => { |
| 248 | const previousId = previous?.jobId ?? output?.jobId; |
| 249 | const nextId = news.jobId !== undefined ? sanitizeJobId(news.jobId) : previousId; |
| 250 | if (previousId !== undefined && nextId !== undefined && previousId !== nextId) { |
| 251 | return { action: "replace" as const, deleteFirst: false }; |
| 252 | } |
| 253 | if (output === undefined && previous === undefined) return undefined; |
| 254 | const observedSchedule = previous?.schedule ?? output?.schedule; |
| 255 | const observedStream = previous?.eventStream ?? output?.eventStream; |
| 256 | const observedAccount = previous?.serviceAccount ?? output?.serviceAccount; |
| 257 | const observedReplication = previous?.replicationSpec ?? output?.replicationSpec; |
| 258 | if (!sameValue(news.schedule, observedSchedule)) { |
| 259 | return { action: "replace" as const, deleteFirst: false }; |
| 260 | } |
| 261 | if (!sameValue(news.eventStream, observedStream)) { |
| 262 | return { action: "replace" as const, deleteFirst: false }; |
| 263 | } |
| 264 | if ((news.serviceAccount ?? "") !== (observedAccount ?? "")) { |
| 265 | return { action: "replace" as const, deleteFirst: false }; |
| 266 | } |
| 267 | if (!sameValue(news.replicationSpec, observedReplication)) { |
| 268 | return { action: "replace" as const, deleteFirst: false }; |
| 269 | } |
| 270 | return undefined; |
| 271 | }; |
| 272 | |
| 273 | const toCreateBody = ( |
| 274 | name: string, |
| 275 | projectId: string, |
| 276 | news: TransferJobProps, |
| 277 | description: string, |
| 278 | ): storagetransfer.TransferJob => ({ |
| 279 | name, |
| 280 | projectId, |
| 281 | description, |
| 282 | transferSpec: news.transferSpec, |
| 283 | replicationSpec: news.replicationSpec, |
| 284 | notificationConfig: news.notificationConfig, |
| 285 | loggingConfig: news.loggingConfig, |
| 286 | schedule: news.schedule, |
| 287 | eventStream: news.eventStream, |
| 288 | serviceAccount: news.serviceAccount, |
| 289 | status: statusOf(news.status), |
| 290 | }); |
| 291 | |
| 292 | export const TransferJobProvider = () => |
| 293 | Provider.succeed(TransferJob, { |
| 294 | stables: ["name", "jobId", "project", "creationTime"], |
| 295 | |
| 296 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 297 | if (!isResolved(news)) return undefined; |
| 298 | return replaceOnImmutable(news, olds, output); |
| 299 | }), |
| 300 | |
| 301 | read: Effect.fn(function* ({ id, olds, output }) { |
| 302 | const env = yield* GcpEnvironment.current; |
| 303 | const jobId = yield* toJobId(id, olds?.jobId, output?.jobId); |
| 304 | const name = output?.name ?? transferJobName(jobId); |
| 305 | const existing = yield* getTransferJob(name, env.project); |
| 306 | if (existing === undefined || isDeletedJob(existing)) return undefined; |
| 307 | const attrs = toAttrs(existing, env.project); |
| 308 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 309 | }), |
| 310 | |
| 311 | list: () => |
| 312 | Effect.gen(function* () { |
| 313 | const env = yield* GcpEnvironment.current; |
| 314 | const jobs = yield* listTransferJobs(env.project); |
| 315 | return jobs |
| 316 | .filter((job) => !isDeletedJob(job) && hasOwnershipMarker(job.description)) |
| 317 | .map((job) => toAttrs(job, env.project)); |
| 318 | }), |
| 319 | |
| 320 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 321 | const env = yield* GcpEnvironment.current; |
| 322 | const jobId = yield* toJobId(id, news.jobId, output?.jobId); |
| 323 | const name = transferJobName(jobId); |
| 324 | const ownership = yield* createOwnership(id); |
| 325 | const desiredDescription = encodeOwnership(ownership, news.description); |
| 326 | const desiredStatus = statusOf(news.status); |
| 327 | |
| 328 | let current = yield* getTransferJob(output?.name ?? name, env.project); |
| 329 | |
| 330 | if (current === undefined) { |
| 331 | const created = yield* retryApiDisabled( |
| 332 | storagetransfer.createTransferJobs({ |
| 333 | body: toCreateBody(name, env.project, news, desiredDescription), |
| 334 | }), |
| 335 | ).pipe(Effect.catchTag("Conflict", () => getTransferJob(name, env.project))); |
| 336 | current = created ?? undefined; |
| 337 | } |
| 338 | |
| 339 | if (current === undefined) { |
| 340 | return yield* new TransferJobNotResolved({ resourceName: name }); |
| 341 | } |
| 342 | |
| 343 | if (isDeletedJob(current)) { |
| 344 | current = yield* storagetransfer.patchTransferJobs({ |
| 345 | jobName: current.name ?? name, |
| 346 | body: { |
| 347 | projectId: env.project, |
| 348 | updateTransferJobFieldMask: fieldMask([ |
| 349 | "description", |
| 350 | news.transferSpec !== undefined ? "transfer_spec" : undefined, |
| 351 | news.notificationConfig !== undefined ? "notification_config" : undefined, |
| 352 | news.loggingConfig !== undefined ? "logging_config" : undefined, |
| 353 | "status", |
| 354 | ]), |
| 355 | transferJob: { |
| 356 | description: desiredDescription, |
| 357 | transferSpec: news.transferSpec, |
| 358 | notificationConfig: news.notificationConfig, |
| 359 | loggingConfig: news.loggingConfig, |
| 360 | status: desiredStatus, |
| 361 | }, |
| 362 | }, |
| 363 | }); |
| 364 | } |
| 365 | |
| 366 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 367 | const specChanged = !sameValue(current.transferSpec, news.transferSpec); |
| 368 | const notificationChanged = !sameValue(current.notificationConfig, news.notificationConfig); |
| 369 | const loggingChanged = !sameValue(current.loggingConfig, news.loggingConfig); |
| 370 | const statusChanged = (current.status ?? "") !== desiredStatus; |
| 371 | const updateMask = fieldMask([ |
| 372 | descriptionChanged ? "description" : undefined, |
| 373 | specChanged && news.transferSpec !== undefined ? "transfer_spec" : undefined, |
| 374 | notificationChanged ? "notification_config" : undefined, |
| 375 | loggingChanged ? "logging_config" : undefined, |
| 376 | statusChanged ? "status" : undefined, |
| 377 | ]); |
| 378 | |
| 379 | if (updateMask.length > 0) { |
| 380 | current = yield* storagetransfer.patchTransferJobs({ |
| 381 | jobName: current.name ?? name, |
| 382 | body: { |
| 383 | projectId: env.project, |
| 384 | updateTransferJobFieldMask: updateMask, |
| 385 | transferJob: { |
| 386 | description: desiredDescription, |
| 387 | transferSpec: news.transferSpec, |
| 388 | notificationConfig: news.notificationConfig, |
| 389 | loggingConfig: news.loggingConfig, |
| 390 | status: desiredStatus, |
| 391 | }, |
| 392 | }, |
| 393 | }); |
| 394 | } |
| 395 | |
| 396 | return toAttrs(current, env.project); |
| 397 | }), |
| 398 | |
| 399 | delete: Effect.fn(function* ({ output }) { |
| 400 | yield* storagetransfer |
| 401 | .deleteTransferJobs({ |
| 402 | jobName: output.name, |
| 403 | projectId: output.project, |
| 404 | }) |
| 405 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 406 | }), |
| 407 | }); |