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.getOrganizationsAnalyticsDatastores({ 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 "getOrganizationsAnalyticsDatastores on a missing datastore 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.getOrganizationsAnalyticsDatastores({
48 name: `${org}/analytics/datastores/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 analytics datastore",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.Apigee.AnalyticsDatastore("Exports", {
69 targetType: "gcs",
70 displayName: "alchemy exports",
71 datastoreConfig: {
72 projectId: project,
73 bucketName: "apigee-analytics",
74 path: "exports",
75 },
76 });
77 }),
78 );
79
80 expect(created.datastoreId).toEqual(expect.any(String));
81 expect(created.displayName).toEqual("alchemy exports");
82 expect(created.targetType).toEqual("gcs");
83
84 const fetched = yield* apigee.getOrganizationsAnalyticsDatastores({
85 name: created.name,
86 });
87 expect(fetched.displayName).toContain("alchemy-id=");
88 expect(fetched.displayName).toContain("alchemy exports");
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.Apigee.AnalyticsDatastore("Exports", {
93 targetType: "gcs",
94 displayName: "alchemy exports updated",
95 datastoreConfig: {
96 projectId: project,
97 bucketName: "apigee-analytics",
98 path: "exports-v2",
99 },
100 });
101 }),
102 );
103
104 expect(updated.datastoreId).toEqual(created.datastoreId);
105 expect(updated.displayName).toEqual("alchemy exports updated");
106
107 yield* stack.destroy();
108
109 const gone = yield* waitUntilGone(created.name);
110 expect(gone).toEqual("gone");
111 }).pipe(logLevel),
112 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
113);