1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as apigee from "@distilled.cloud/gcp/apigee_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const 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.
21const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG;
22
23// Apigee X organizations are 1:1 with the GCP project. Deploying
24// GCP.Apigee.Organization against that project would adopt/update (and
25// destroy would delete) the account org. Keep lifecycle skipped.
26const runOrgLifecycle = false;
27
28const waitUntilGone = (name: string) =>
29 apigee.getOrganizations({ name }).pipe(
30 Effect.as("found" as const),
31 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
32 Effect.catchTag("ApigeeResourceNotFound", () =>
33 Effect.succeed("gone" as const),
34 ),
35 Effect.repeat({
36 schedule: Schedule.spaced("2 seconds"),
37 until: (status) => status === "gone",
38 times: 10,
39 }),
40 );
41
42test.provider(
43 "getOrganizations on a missing organization fails with ApigeeResourceNotFound",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 apigee.getOrganizations({
50 name: "organizations/alchemy-missing-org",
51 }),
52 );
53 expect(error._tag).toEqual("ApigeeResourceNotFound");
54
55 yield* stack.destroy();
56 }).pipe(logLevel),
57 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
58);
59
60test.provider.skipIf(!runLifecycle || !runOrgLifecycle)(
61 "create, update, and delete an organization",
62 (stack) =>
63 Effect.gen(function* () {
64 const { project } = yield* GcpEnvironment.current;
65 const org = `organizations/${project}`;
66
67 yield* stack.destroy();
68
69 const created = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.Apigee.Organization("Org", {
72 displayName: "alchemy-org",
73 description: "alchemy test org",
74 disableVpcPeering: true,
75 });
76 }),
77 );
78
79 expect(created.organizationId).toEqual(project);
80 expect(created.name).toEqual(org);
81 expect(created.displayName).toEqual("alchemy-org");
82 expect(created.description).toEqual("alchemy test org");
83
84 const fetched = yield* apigee.getOrganizations({
85 name: created.name,
86 });
87 expect(fetched.description).toContain("alchemy-id=");
88 expect(fetched.description).toContain("alchemy test org");
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.Apigee.Organization("Org", {
93 organizationId: created.organizationId,
94 displayName: "alchemy-org-updated",
95 description: "updated alchemy test org",
96 disableVpcPeering: true,
97 });
98 }),
99 );
100
101 expect(updated.name).toEqual(created.name);
102 expect(updated.displayName).toEqual("alchemy-org-updated");
103 expect(updated.description).toEqual("updated alchemy test org");
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.name);
108 expect(gone).toEqual("gone");
109 }).pipe(logLevel),
110 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
111);