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.getOrganizationsSecurityProfiles({ 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 "getOrganizationsSecurityProfiles on a missing profile 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.getOrganizationsSecurityProfiles({
48 name: `${org}/securityProfiles/alchemy-missing`,
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 security profile",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.Apigee.SecurityProfile("Baseline", {
67 description: "default scoring",
68 profileConfig: {
69 categories: [{ authorization: true, threat: true }],
70 },
71 });
72 }),
73 );
74
75 expect(created.securityProfileId).toEqual(expect.any(String));
76 expect(created.description).toEqual("default scoring");
77
78 const fetched = yield* apigee.getOrganizationsSecurityProfiles({
79 name: created.name,
80 });
81 expect(fetched.description).toContain("alchemy-id=");
82 expect(fetched.description).toContain("default scoring");
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.Apigee.SecurityProfile("Baseline", {
87 securityProfileId: created.securityProfileId,
88 description: "updated scoring",
89 profileConfig: {
90 categories: [{ authorization: true, threat: true, cors: true }],
91 },
92 });
93 }),
94 );
95
96 expect(updated.name).toEqual(created.name);
97 expect(updated.description).toEqual("updated scoring");
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(created.name);
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
105);