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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as dataplex from "@distilled.cloud/gcp/dataplex_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { withDataplexSlot } from "./quota.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID ?? ""; |
| 17 | const runLifecycle = !process.env.FAST && organizationId.length > 0; |
| 18 | |
| 19 | const waitUntilGone = (name: string) => |
| 20 | dataplex.getOrganizationsLocationsEncryptionConfigs({ name }).pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("2 seconds"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "getOrganizationsLocationsEncryptionConfigs on a missing config fails with a typed tag", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const { project } = yield* GcpEnvironment.current; |
| 37 | // Probe the test project's own organization so the name is valid. |
| 38 | const projectInfo = yield* resourcemanager.getProjects({ |
| 39 | name: `projects/${project}`, |
| 40 | }); |
| 41 | const org = |
| 42 | organizationId.length > 0 |
| 43 | ? organizationId |
| 44 | : (projectInfo.parent ?? "").replace(/^organizations\//, ""); |
| 45 | const error = yield* Effect.flip( |
| 46 | dataplex.getOrganizationsLocationsEncryptionConfigs({ |
| 47 | name: `organizations/${org}/locations/us-central1/encryptionConfigs/alchemy-missing`, |
| 48 | }), |
| 49 | ); |
| 50 | // Without an organization-level Dataplex role the API answers |
| 51 | // "Permission 'dataplex.encryptionConfig.get' denied". |
| 52 | expect(error._tag).toEqual(runLifecycle ? "NotFound" : "Forbidden"); |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | }).pipe(logLevel, withDataplexSlot), |
| 56 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 57 | ); |
| 58 | |
| 59 | test.provider.skipIf(!runLifecycle)( |
| 60 | "create, update, and delete an encryption config", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | yield* stack.destroy(); |
| 64 | |
| 65 | const created = yield* stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | return yield* GCP.Dataplex.EncryptionConfig("Default", { |
| 68 | organizationId, |
| 69 | location: "us-central1", |
| 70 | encryptionConfigId: "default", |
| 71 | enableMetastoreEncryption: false, |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.name).toContain("/encryptionConfigs/"); |
| 77 | expect(created.encryptionConfigId).toEqual("default"); |
| 78 | expect(created.organizationId).toEqual(organizationId); |
| 79 | expect(created.location).toEqual("us-central1"); |
| 80 | |
| 81 | const fetched = yield* dataplex.getOrganizationsLocationsEncryptionConfigs({ |
| 82 | name: created.name, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.name); |
| 85 | |
| 86 | const updated = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* GCP.Dataplex.EncryptionConfig("Default", { |
| 89 | organizationId, |
| 90 | location: "us-central1", |
| 91 | encryptionConfigId: "default", |
| 92 | enableMetastoreEncryption: false, |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(updated.name).toEqual(created.name); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | const gone = yield* waitUntilGone(created.name); |
| 101 | expect(gone).toEqual("gone"); |
| 102 | }).pipe(logLevel, withDataplexSlot), |
| 103 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 104 | ); |