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 =
22 !!process.env.GCP_TEST_APIGEE_ORG && !!process.env.GCP_TEST_APIGEE_ENDPOINT;
23
24const waitUntilGone = (name: string) =>
25 apigee.getOrganizationsEndpointAttachments({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.catchTag("ApigeeResourceNotFound", () =>
29 Effect.succeed("gone" as const),
30 ),
31 Effect.repeat({
32 schedule: Schedule.spaced("2 seconds"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38test.provider(
39 "getOrganizationsEndpointAttachments on a missing attachment fails with ApigeeResourceNotFound",
40 (stack) =>
41 Effect.gen(function* () {
42 const { project } = yield* GcpEnvironment.current;
43 const org = `organizations/${project}`;
44
45 yield* stack.destroy();
46
47 const error = yield* Effect.flip(
48 apigee.getOrganizationsEndpointAttachments({
49 name: `${org}/endpointAttachments/alchemy-missing`,
50 }),
51 );
52 expect(error._tag).toEqual("ApigeeResourceNotFound");
53
54 yield* stack.destroy();
55 }).pipe(logLevel),
56 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
57);
58
59test.provider.skipIf(!runLifecycle)(
60 "create and delete an endpoint attachment",
61 (stack) =>
62 Effect.gen(function* () {
63 const { project } = yield* GcpEnvironment.current;
64 const serviceAttachment =
65 process.env.GCP_TEST_APIGEE_SERVICE_ATTACHMENT ??
66 `projects/${project}/regions/us-central1/serviceAttachments/alchemy-backend`;
67
68 yield* stack.destroy();
69
70 const created = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* GCP.Apigee.EndpointAttachment("Backend", {
73 location: "us-central1",
74 serviceAttachment,
75 });
76 }),
77 );
78
79 expect(created.endpointAttachmentId.startsWith("alc")).toEqual(true);
80 expect(created.location).toEqual("us-central1");
81 expect(created.serviceAttachment).toEqual(serviceAttachment);
82
83 const fetched = yield* apigee.getOrganizationsEndpointAttachments({
84 name: created.name,
85 });
86 expect(fetched.name).toEqual(created.name);
87
88 yield* stack.destroy();
89
90 const gone = yield* waitUntilGone(created.name);
91 expect(gone).toEqual("gone");
92 }).pipe(logLevel),
93 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
94);