| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | // Needs a provisioned Apigee organization on the testing project (paid, or |
| 18 | // ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission |
| 19 | // denied on resource \"organizations/{project}\" (or it may not exist)"). |
| 20 | // Set GCP_TEST_APIGEE_ORG=1 when the org exists. |
| 21 | const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG; |
| 22 | |
| 23 | const waitUntilGone = (name: string) => |
| 24 | apigee.getOrganizationsSpaces({ name }).pipe( |
| 25 | Effect.as("found" as const), |
| 26 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 27 | Effect.succeed("gone" as const), |
| 28 | ), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("1 second"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test.provider( |
| 37 | "getOrganizationsSpaces on a missing space fails with ApigeeResourceNotFound", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const { project } = yield* GcpEnvironment.current; |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const error = yield* Effect.flip( |
| 45 | apigee.getOrganizationsSpaces({ |
| 46 | name: `organizations/${project}/spaces/alchemy-apigee-missing-space`, |
| 47 | }), |
| 48 | ); |
| 49 | expect(error._tag).toEqual("ApigeeResourceNotFound"); |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | }).pipe(logLevel), |
| 53 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 54 | ); |
| 55 | |
| 56 | test.provider.skipIf(!runLifecycle)( |
| 57 | "create, update, and delete an Apigee space", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | const { project } = yield* GcpEnvironment.current; |
| 61 | |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const created = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* GCP.Apigee.Space("Team", { |
| 67 | displayName: "alchemy test space", |
| 68 | }); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(created.spaceId).toEqual(expect.any(String)); |
| 73 | expect(created.organization).toEqual(project); |
| 74 | expect(created.name).toEqual( |
| 75 | `organizations/${project}/spaces/${created.spaceId}`, |
| 76 | ); |
| 77 | expect(created.displayName).toEqual("alchemy test space"); |
| 78 | |
| 79 | const fetched = yield* apigee.getOrganizationsSpaces({ |
| 80 | name: created.name, |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.name); |
| 83 | expect(fetched.displayName).toContain("alchemy-id="); |
| 84 | expect(fetched.displayName).toContain("alchemy test space"); |
| 85 | |
| 86 | const updated = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* GCP.Apigee.Space("Team", { |
| 89 | spaceId: created.spaceId, |
| 90 | displayName: "alchemy updated space", |
| 91 | }); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(updated.spaceId).toEqual(created.spaceId); |
| 96 | expect(updated.displayName).toEqual("alchemy updated space"); |
| 97 | |
| 98 | const fetchedUpdate = yield* apigee.getOrganizationsSpaces({ |
| 99 | name: updated.name, |
| 100 | }); |
| 101 | expect(fetchedUpdate.displayName).toContain("alchemy updated space"); |
| 102 | expect(fetchedUpdate.displayName).toContain("alchemy-id="); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(created.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 110 | ); |