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.getOrganizationsEnvironmentsKeystoresAliases({ 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 "getOrganizationsEnvironmentsKeystoresAliases on a missing alias 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.getOrganizationsEnvironmentsKeystoresAliases({
48 name: `${org}/environments/alchemy-missing/keystores/alchemy-missing/aliases/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 keystore alias",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const environment = yield* GCP.Apigee.Environment("Runtime", {
67 displayName: "runtime",
68 });
69 const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
70 environment: environment.environmentId,
71 });
72 const alias = yield* GCP.Apigee.EnvironmentsKeystoresAlias("Server", {
73 environment: environment.environmentId,
74 keystore: keystore.keystoreId,
75 subject: { commonName: "api.example.com" },
76 certValidityInDays: 365,
77 });
78 return { environment, keystore, alias };
79 }),
80 );
81
82 expect(created.alias.aliasId).toEqual(expect.any(String));
83 expect(created.alias.keystoreId).toEqual(created.keystore.keystoreId);
84 expect(created.alias.environmentId).toEqual(
85 created.environment.environmentId,
86 );
87
88 const fetched =
89 yield* apigee.getOrganizationsEnvironmentsKeystoresAliases({
90 name: created.alias.name,
91 });
92 expect(fetched.alias).toEqual(created.alias.aliasId);
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 const environment = yield* GCP.Apigee.Environment("Runtime", {
97 environmentId: created.environment.environmentId,
98 displayName: "runtime",
99 });
100 const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
101 environment: environment.environmentId,
102 keystoreId: created.keystore.keystoreId,
103 });
104 const alias = yield* GCP.Apigee.EnvironmentsKeystoresAlias("Server", {
105 environment: environment.environmentId,
106 keystore: keystore.keystoreId,
107 aliasId: created.alias.aliasId,
108 subject: { commonName: "api.example.com" },
109 certValidityInDays: 30,
110 });
111 return { environment, keystore, alias };
112 }),
113 );
114
115 expect(updated.alias.name).toEqual(created.alias.name);
116
117 yield* stack.destroy();
118
119 const gone = yield* waitUntilGone(created.alias.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
123);