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.getOrganizationsDevelopersAppsKeys({ 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 "getOrganizationsDevelopersAppsKeys 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.getOrganizationsDevelopersAppsKeys({
48 name: `${org}/developers/missing@alchemy.example/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 a developer app key",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const developer = yield* GCP.Apigee.Developer("Owner", {
67 firstName: "Ada",
68 lastName: "Lovelace",
69 });
70 const app = yield* GCP.Apigee.DevelopersApp("Portal", {
71 developer: developer.email,
72 });
73 const key = yield* GCP.Apigee.DevelopersAppsKey("PortalKey", {
74 developer: developer.email,
75 app: app.appName,
76 attributes: { team: "platform" },
77 });
78 return { developer, app, key };
79 }),
80 );
81
82 expect(created.key.consumerKey).toEqual(expect.any(String));
83 expect(created.key.attributes).toMatchObject({ team: "platform" });
84
85 const fetched = yield* apigee.getOrganizationsDevelopersAppsKeys({
86 name: created.key.name,
87 });
88 expect(fetched.consumerKey).toEqual(created.key.consumerKey);
89 expect(
90 fetched.attributes?.some((item) => item.name === "alchemy-id"),
91 ).toEqual(true);
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 const developer = yield* GCP.Apigee.Developer("Owner", {
96 email: created.developer.email,
97 firstName: "Ada",
98 lastName: "Lovelace",
99 });
100 const app = yield* GCP.Apigee.DevelopersApp("Portal", {
101 developer: developer.email,
102 appName: created.app.appName,
103 });
104 const key = yield* GCP.Apigee.DevelopersAppsKey("PortalKey", {
105 developer: developer.email,
106 app: app.appName,
107 consumerKey: created.key.consumerKey,
108 consumerSecret: created.key.consumerSecret,
109 attributes: { team: "runtime" },
110 });
111 return { developer, app, key };
112 }),
113 );
114
115 expect(updated.key.name).toEqual(created.key.name);
116 expect(updated.key.attributes).toMatchObject({ team: "runtime" });
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(created.key.name);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
124);