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.getOrganizationsApimServiceExtensions({ 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("2 seconds"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37test.provider(
38 "getOrganizationsApimServiceExtensions on a missing extension 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.getOrganizationsApimServiceExtensions({
48 name: `${org}/apimServiceExtensions/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 an apim service extension",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63
64 yield* stack.destroy();
65
66 const lbForwardingRule = `projects/${project}/regions/us-central1/forwardingRules/https`;
67 const network = `projects/${project}/global/networks/default`;
68 const networkConfigs = [
69 {
70 region: "us-central1",
71 subnet: `projects/${project}/regions/us-central1/subnetworks/default`,
72 },
73 ];
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* GCP.Apigee.ApimServiceExtension("Edge", {
78 lbForwardingRule,
79 network,
80 networkConfigs,
81 extensionProcessor: "ext-processor",
82 });
83 }),
84 );
85
86 expect(created.apimServiceExtensionId).toEqual(expect.any(String));
87 expect(created.lbForwardingRule).toEqual(lbForwardingRule);
88 expect(created.extensionProcessor).toEqual("ext-processor");
89
90 const fetched = yield* apigee.getOrganizationsApimServiceExtensions({
91 name: created.name,
92 });
93 expect(fetched.name).toEqual(created.name);
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.Apigee.ApimServiceExtension("Edge", {
98 apimServiceExtensionId: created.apimServiceExtensionId,
99 lbForwardingRule,
100 network,
101 networkConfigs,
102 extensionProcessor: "ext-processor-v2",
103 });
104 }),
105 );
106
107 expect(updated.name).toEqual(created.name);
108 expect(updated.extensionProcessor).toEqual("ext-processor-v2");
109
110 yield* stack.destroy();
111
112 const gone = yield* waitUntilGone(created.name);
113 expect(gone).toEqual("gone");
114 }).pipe(logLevel),
115 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
116);