The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 6 | import * as storagetransfer from "@distilled.cloud/gcp/storagetransfer_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (jobName: string) => |
| 16 | Effect.gen(function* () { |
| 17 | const { project } = yield* GcpEnvironment.current; |
| 18 | |
| 19 | return yield* storagetransfer.getTransferJobs({ jobName, projectId: project }).pipe( |
| 20 | Effect.map((job) => (job.status === "DELETED" ? ("gone" as const) : ("found" as const))), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | }); |
| 29 | |
| 30 | const grantBucketRole = (bucket: string, member: string, role: string) => |
| 31 | Effect.gen(function* () { |
| 32 | const policy = yield* storage.getIamPolicyBuckets({ |
| 33 | bucket, |
| 34 | optionsRequestedPolicyVersion: 3, |
| 35 | }); |
| 36 | const bindings = [...(policy.bindings ?? [])]; |
| 37 | const existing = bindings.find((binding) => binding.role === role); |
| 38 | if (existing?.members?.includes(member)) return; |
| 39 | if (existing) { |
| 40 | existing.members = [...(existing.members ?? []), member]; |
| 41 | } else { |
| 42 | bindings.push({ role, members: [member] }); |
| 43 | } |
| 44 | yield* storage.setIamPolicyBuckets({ |
| 45 | bucket, |
| 46 | body: { |
| 47 | ...policy, |
| 48 | bindings, |
| 49 | }, |
| 50 | }); |
| 51 | }).pipe( |
| 52 | Effect.retry({ |
| 53 | while: (error) => error._tag === "Conflict", |
| 54 | times: 4, |
| 55 | schedule: Schedule.spaced("500 millis"), |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | const grantTransferAccess = (sourceBucket: string, sinkBucket: string, email: string) => { |
| 60 | const member = `serviceAccount:${email}`; |
| 61 | return Effect.all( |
| 62 | [ |
| 63 | grantBucketRole(sourceBucket, member, "roles/storage.legacyBucketReader"), |
| 64 | grantBucketRole(sourceBucket, member, "roles/storage.objectViewer"), |
| 65 | grantBucketRole(sinkBucket, member, "roles/storage.legacyBucketReader"), |
| 66 | grantBucketRole(sinkBucket, member, "roles/storage.objectAdmin"), |
| 67 | ], |
| 68 | { concurrency: 1 }, |
| 69 | ); |
| 70 | }; |
| 71 | |
| 72 | test.provider( |
| 73 | "create, update, and delete a transfer job", |
| 74 | (stack) => |
| 75 | Effect.gen(function* () { |
| 76 | const { project } = yield* GcpEnvironment.current; |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | const buckets = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | const source = yield* GCP.Storage.Bucket("Src", { |
| 83 | location: "US-CENTRAL1", |
| 84 | forceDestroy: true, |
| 85 | }); |
| 86 | const sink = yield* GCP.Storage.Bucket("Dst", { |
| 87 | location: "US-CENTRAL1", |
| 88 | forceDestroy: true, |
| 89 | }); |
| 90 | return { |
| 91 | sourceBucket: source.bucketName.as<string>(), |
| 92 | sinkBucket: sink.bucketName.as<string>(), |
| 93 | }; |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | const account = yield* storagetransfer.getGoogleServiceAccounts({ |
| 98 | projectId: project, |
| 99 | }); |
| 100 | if (account.accountEmail) { |
| 101 | yield* grantTransferAccess(buckets.sourceBucket, buckets.sinkBucket, account.accountEmail); |
| 102 | } |
| 103 | |
| 104 | const created = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | yield* GCP.Storage.Bucket("Src", { |
| 107 | bucketName: buckets.sourceBucket, |
| 108 | location: "US-CENTRAL1", |
| 109 | forceDestroy: true, |
| 110 | }); |
| 111 | yield* GCP.Storage.Bucket("Dst", { |
| 112 | bucketName: buckets.sinkBucket, |
| 113 | location: "US-CENTRAL1", |
| 114 | forceDestroy: true, |
| 115 | }); |
| 116 | return yield* GCP.StorageTransfer.TransferJob("Copy", { |
| 117 | description: "copy uploads", |
| 118 | status: "DISABLED", |
| 119 | transferSpec: { |
| 120 | gcsDataSource: { bucketName: buckets.sourceBucket }, |
| 121 | gcsDataSink: { bucketName: buckets.sinkBucket }, |
| 122 | }, |
| 123 | }); |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | expect(created.jobId).toEqual(expect.any(String)); |
| 128 | expect(created.name).toEqual(`transferJobs/${created.jobId}`); |
| 129 | expect(created.project).toEqual(project); |
| 130 | expect(created.description).toEqual("copy uploads"); |
| 131 | expect(created.status).toEqual("DISABLED"); |
| 132 | expect(created.transferSpec?.gcsDataSource?.bucketName).toEqual(buckets.sourceBucket); |
| 133 | expect(created.transferSpec?.gcsDataSink?.bucketName).toEqual(buckets.sinkBucket); |
| 134 | |
| 135 | const fetched = yield* storagetransfer.getTransferJobs({ |
| 136 | jobName: created.name, |
| 137 | projectId: project, |
| 138 | }); |
| 139 | expect(fetched.name).toEqual(created.name); |
| 140 | expect(fetched.status).toEqual("DISABLED"); |
| 141 | expect(fetched.description).toContain("alchemy-id="); |
| 142 | expect(fetched.description).toContain("copy uploads"); |
| 143 | expect(fetched.transferSpec?.gcsDataSource?.bucketName).toEqual(buckets.sourceBucket); |
| 144 | |
| 145 | const updated = yield* stack.deploy( |
| 146 | Effect.gen(function* () { |
| 147 | yield* GCP.Storage.Bucket("Src", { |
| 148 | bucketName: buckets.sourceBucket, |
| 149 | location: "US-CENTRAL1", |
| 150 | forceDestroy: true, |
| 151 | }); |
| 152 | yield* GCP.Storage.Bucket("Dst", { |
| 153 | bucketName: buckets.sinkBucket, |
| 154 | location: "US-CENTRAL1", |
| 155 | forceDestroy: true, |
| 156 | }); |
| 157 | return yield* GCP.StorageTransfer.TransferJob("Copy", { |
| 158 | jobId: created.jobId, |
| 159 | description: "copy inbox", |
| 160 | status: "DISABLED", |
| 161 | transferSpec: { |
| 162 | gcsDataSource: { |
| 163 | bucketName: buckets.sourceBucket, |
| 164 | path: "inbox/", |
| 165 | }, |
| 166 | gcsDataSink: { bucketName: buckets.sinkBucket }, |
| 167 | }, |
| 168 | }); |
| 169 | }), |
| 170 | ); |
| 171 | |
| 172 | expect(updated.name).toEqual(created.name); |
| 173 | expect(updated.jobId).toEqual(created.jobId); |
| 174 | expect(updated.description).toEqual("copy inbox"); |
| 175 | expect(updated.transferSpec?.gcsDataSource?.path).toEqual("inbox/"); |
| 176 | |
| 177 | const fetchedUpdate = yield* storagetransfer.getTransferJobs({ |
| 178 | jobName: created.name, |
| 179 | projectId: project, |
| 180 | }); |
| 181 | expect(fetchedUpdate.description).toContain("copy inbox"); |
| 182 | expect(fetchedUpdate.transferSpec?.gcsDataSource?.path).toEqual("inbox/"); |
| 183 | expect(fetchedUpdate.status).toEqual("DISABLED"); |
| 184 | |
| 185 | yield* stack.destroy(); |
| 186 | |
| 187 | const gone = yield* waitUntilGone(created.name); |
| 188 | expect(gone).toEqual("gone"); |
| 189 | }).pipe(logLevel), |
| 190 | { |
| 191 | tags: ["provider:gcp", "provider:gcp:storagetransfer", "live"], |
| 192 | timeout: 120_000, |
| 193 | }, |
| 194 | ); |