1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9import { DEFAULT_NETWORK, defaultNetworkSelfLink } from "../networkQuota.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const runLifecycle = !process.env.FAST;
19
20const waitUntilGone = (name: string) =>
21 networkconnectivity.getProjectsLocationsRegionalEndpoints({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsRegionalEndpoints on a missing endpoint fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 networkconnectivity.getProjectsLocationsRegionalEndpoints({
40 name: `projects/${project}/locations/us-central1/regionalEndpoints/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
49 timeout: 90_000,
50 },
51);
52
53test.provider.skipIf(!runLifecycle)(
54 "create and delete a regional endpoint",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const subnet = yield* GCP.Compute.Subnetwork("RepSubnet", {
63 network: DEFAULT_NETWORK,
64 ipCidrRange: "172.20.11.0/24",
65 privateIpGoogleAccess: true,
66 });
67 const endpoint = yield* GCP.NetworkConnectivity.RegionalEndpoint(
68 "Storage",
69 {
70 targetGoogleApi: "storage.us-central1.p.rep.googleapis.com",
71 accessType: "REGIONAL",
72 network: defaultNetworkSelfLink(project),
73 subnetwork: subnet.selfLink.as<string>(),
74 description: "rep a",
75 labels: { env: "test" },
76 },
77 );
78 return { subnet, endpoint };
79 }),
80 );
81
82 expect(created.endpoint.name).toContain("/regionalEndpoints/");
83 expect(created.endpoint.location).toEqual("us-central1");
84 expect(created.endpoint.targetGoogleApi).toEqual(
85 "storage.us-central1.p.rep.googleapis.com",
86 );
87 expect(created.endpoint.accessType).toEqual("REGIONAL");
88 expect(created.endpoint.labels).toMatchObject({ env: "test" });
89
90 const fetched =
91 yield* networkconnectivity.getProjectsLocationsRegionalEndpoints({
92 name: created.endpoint.name,
93 });
94 expect(fetched.name).toEqual(created.endpoint.name);
95 expect(fetched.targetGoogleApi).toEqual(
96 "storage.us-central1.p.rep.googleapis.com",
97 );
98 expect(fetched.labels?.env).toEqual("test");
99 expect(
100 Object.keys(fetched.labels ?? {}).some((key) =>
101 key.startsWith("alchemy-"),
102 ),
103 ).toEqual(true);
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.endpoint.name);
108 expect(gone).toEqual("gone");
109 }).pipe(logLevel),
110 {
111 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
112 timeout: 600_000,
113 },
114);