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 cloudcontrolspartner from "@distilled.cloud/gcp/cloudcontrolspartner_v1"; |
| 6 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 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 | const location = "us-central1"; |
| 15 | |
| 16 | const organizationOf = () => |
| 17 | Effect.gen(function* () { |
| 18 | const { project } = yield* GcpEnvironment.current; |
| 19 | |
| 20 | const fromEnv = |
| 21 | process.env.GOOGLE_CLOUDCONTROLSPARTNER_ORGANIZATION ?? process.env.GOOGLE_ORGANIZATION_ID; |
| 22 | if (fromEnv && fromEnv.length > 0) { |
| 23 | return fromEnv.startsWith("organizations/") ? fromEnv : `organizations/${fromEnv}`; |
| 24 | } |
| 25 | let current: string | undefined = `projects/${project}`; |
| 26 | for (let i = 0; i < 8; i++) { |
| 27 | if (current === undefined) return ""; |
| 28 | if (current.startsWith("organizations/")) return current; |
| 29 | current = current.startsWith("projects/") |
| 30 | ? yield* resourcemanager.getProjects({ name: current }).pipe( |
| 31 | Effect.map((resource) => resource.parent), |
| 32 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 33 | ) |
| 34 | : current.startsWith("folders/") |
| 35 | ? yield* resourcemanager.getFolders({ name: current }).pipe( |
| 36 | Effect.map((folder) => folder.parent), |
| 37 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 38 | ) |
| 39 | : undefined; |
| 40 | } |
| 41 | return ""; |
| 42 | }); |
| 43 | |
| 44 | const waitUntilGone = (name: string) => |
| 45 | cloudcontrolspartner.getOrganizationsLocationsCustomers({ name }).pipe( |
| 46 | Effect.as("found" as const), |
| 47 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 48 | Effect.repeat({ |
| 49 | schedule: Schedule.spaced("1 second"), |
| 50 | until: (status) => status === "gone", |
| 51 | times: 10, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const customerOrgId = (process.env.GOOGLE_CLOUDCONTROLSPARTNER_CUSTOMER_ID ?? "").trim(); |
| 56 | |
| 57 | // Cloud Controls Partner is for allow-listed Sovereign Cloud partners: without |
| 58 | // partner permissions on the organization every call answers 403 ("Permission |
| 59 | // ... denied ... (or it may not exist)"). Set GCP_TEST_CLOUDCONTROLSPARTNER=1 |
| 60 | // and GOOGLE_CLOUDCONTROLSPARTNER_CUSTOMER_ID on a partner organization. |
| 61 | const runLifecycle = |
| 62 | !process.env.FAST && |
| 63 | process.env.GCP_TEST_CLOUDCONTROLSPARTNER === "1" && |
| 64 | customerOrgId.length > 0; |
| 65 | |
| 66 | test.provider.skipIf(!runLifecycle)( |
| 67 | "getOrganizationsLocationsCustomers on a missing customer fails with NotFound", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | const organization = (yield* organizationOf()) || "organizations/0"; |
| 73 | const error = yield* Effect.flip( |
| 74 | cloudcontrolspartner.getOrganizationsLocationsCustomers({ |
| 75 | name: `${organization}/locations/${location}/customers/0`, |
| 76 | }), |
| 77 | ); |
| 78 | expect(error._tag).toEqual("NotFound"); |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | }).pipe(logLevel), |
| 82 | { |
| 83 | tags: ["provider:gcp", "provider:gcp:cloudcontrolspartner", "live"], |
| 84 | timeout: 90_000, |
| 85 | }, |
| 86 | ); |
| 87 | |
| 88 | test.provider.skipIf(runLifecycle)( |
| 89 | "createOrganizationsLocationsCustomers without partner permissions fails with Forbidden", |
| 90 | (stack) => |
| 91 | Effect.gen(function* () { |
| 92 | yield* stack.destroy(); |
| 93 | |
| 94 | const organization = (yield* organizationOf()) || "organizations/0"; |
| 95 | const error = yield* Effect.flip( |
| 96 | cloudcontrolspartner.createOrganizationsLocationsCustomers({ |
| 97 | parent: `${organization}/locations/${location}`, |
| 98 | customerId: "0", |
| 99 | body: { displayName: "Alchemy Cloudcontrolspartner Probe" }, |
| 100 | }), |
| 101 | ); |
| 102 | expect(error._tag).toEqual("Forbidden"); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | }).pipe(logLevel), |
| 106 | { |
| 107 | tags: ["provider:gcp", "provider:gcp:cloudcontrolspartner", "live"], |
| 108 | timeout: 90_000, |
| 109 | }, |
| 110 | ); |
| 111 | |
| 112 | test.provider.skipIf(!runLifecycle)( |
| 113 | "create, update, and delete a customer", |
| 114 | (stack) => |
| 115 | Effect.gen(function* () { |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const organization = (yield* organizationOf()) || "organizations/0"; |
| 119 | const parent = `${organization}/locations/${location}`; |
| 120 | |
| 121 | const created = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* GCP.CloudControlsPartner.Customer("Acme", { |
| 124 | organization, |
| 125 | location, |
| 126 | customerId: customerOrgId, |
| 127 | displayName: "Acme Corp", |
| 128 | }); |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | expect(created.name).toEqual(`${parent}/customers/${customerOrgId}`); |
| 133 | expect(created.customerId).toEqual(customerOrgId); |
| 134 | expect(created.displayName).toEqual("Acme Corp"); |
| 135 | expect(created.location).toEqual(location); |
| 136 | |
| 137 | const fetched = yield* cloudcontrolspartner.getOrganizationsLocationsCustomers({ |
| 138 | name: created.name, |
| 139 | }); |
| 140 | expect(fetched.name).toEqual(created.name); |
| 141 | expect(fetched.displayName).toContain("[alchemy "); |
| 142 | expect(fetched.displayName).toContain("Acme Corp"); |
| 143 | |
| 144 | const updated = yield* stack.deploy( |
| 145 | Effect.gen(function* () { |
| 146 | return yield* GCP.CloudControlsPartner.Customer("Acme", { |
| 147 | organization, |
| 148 | location, |
| 149 | customerId: created.customerId, |
| 150 | displayName: "Acme Corporation", |
| 151 | }); |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | expect(updated.name).toEqual(created.name); |
| 156 | expect(updated.displayName).toEqual("Acme Corporation"); |
| 157 | |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | const gone = yield* waitUntilGone(created.name); |
| 161 | expect(gone).toEqual("gone"); |
| 162 | }).pipe(logLevel), |
| 163 | { |
| 164 | tags: ["provider:gcp", "provider:gcp:cloudcontrolspartner", "live"], |
| 165 | timeout: 90_000, |
| 166 | }, |
| 167 | ); |