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
23const waitUntilGone = (name: string) =>
24 apigee.getOrganizationsAppgroupsAppsKeys({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.catchTag("ApigeeResourceNotFound", () =>
28 Effect.succeed("gone" as const),
29 ),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37test.provider(
38 "getOrganizationsAppgroupsAppsKeys on a missing key fails with ApigeeResourceNotFound",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42 const org = `organizations/${project}`;
43
44 yield* stack.destroy();
45
46 const error = yield* Effect.flip(
47 apigee.getOrganizationsAppgroupsAppsKeys({
48 name: `${org}/appgroups/missing-group/apps/missing-app/keys/missing-key`,
49 }),
50 );
51 expect(error._tag).toEqual("ApigeeResourceNotFound");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
56);
57
58test.provider.skipIf(!runLifecycle)(
59 "create, update, and delete an appgroup app key",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63 const org = `organizations/${project}`;
64
65 yield* stack.destroy();
66
67 const group = yield* apigee.createOrganizationsAppgroups({
68 parent: org,
69 body: { name: "alchemy-appgroup", displayName: "alchemy-appgroup" },
70 });
71 const groupId = group.name ?? "alchemy-appgroup";
72 const groupParent = groupId.includes("/appgroups/")
73 ? groupId
74 : `${org}/appgroups/${groupId}`;
75
76 const app = yield* apigee.createOrganizationsAppgroupsApps({
77 parent: groupParent,
78 body: { name: "alchemy-app" },
79 });
80 const appName = app.name ?? "alchemy-app";
81
82 const created = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.Apigee.AppgroupsAppsKey("GroupKey", {
85 appGroup: groupId,
86 app: appName,
87 attributes: { team: "platform" },
88 });
89 }),
90 );
91
92 expect(created.consumerKey).toEqual(expect.any(String));
93 expect(created.attributes).toMatchObject({ team: "platform" });
94
95 const fetched = yield* apigee.getOrganizationsAppgroupsAppsKeys({
96 name: created.name,
97 });
98 expect(fetched.consumerKey).toEqual(created.consumerKey);
99
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.Apigee.AppgroupsAppsKey("GroupKey", {
103 appGroup: groupId,
104 app: appName,
105 consumerKey: created.consumerKey,
106 consumerSecret: created.consumerSecret,
107 attributes: { team: "runtime" },
108 });
109 }),
110 );
111
112 expect(updated.name).toEqual(created.name);
113 expect(updated.attributes).toMatchObject({ team: "runtime" });
114
115 yield* stack.destroy();
116
117 const gone = yield* waitUntilGone(created.name);
118 expect(gone).toEqual("gone");
119
120 yield* apigee
121 .deleteOrganizationsAppgroupsApps({
122 name: `${groupParent}/apps/${appName}`,
123 })
124 .pipe(Effect.catchTag("NotFound", () => Effect.void));
125 yield* apigee
126 .deleteOrganizationsAppgroups({ name: groupParent })
127 .pipe(Effect.catchTag("NotFound", () => Effect.void));
128 }).pipe(logLevel),
129 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
130);