1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as networkservices from "@distilled.cloud/gcp/networkservices_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";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 networkservices.getProjectsLocationsLbRouteExtensions({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsLbRouteExtensions on a missing extension fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 networkservices.getProjectsLocationsLbRouteExtensions({
37 name: `projects/${project}/locations/us-central1/lbRouteExtensions/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "createProjectsLocationsLbRouteExtensions without a forwarding rule fails with BadRequest",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const error = yield* Effect.flip(
58 networkservices.createProjectsLocationsLbRouteExtensions({
59 parent: `projects/${project}/locations/us-central1`,
60 lbRouteExtensionId: "alchemy-missing-lb-route",
61 body: {
62 loadBalancingScheme: "INTERNAL_MANAGED",
63 forwardingRules: [
64 `//compute.googleapis.com/projects/${project}/regions/us-central1/forwardingRules/alchemy-missing-fr`,
65 ],
66 extensionChains: [
67 {
68 name: "chain1",
69 matchCondition: { celExpression: "true" },
70 extensions: [
71 {
72 name: "ext1",
73 authority: "ext1.example.com",
74 service: `https://www.googleapis.com/compute/v1/projects/${project}/regions/us-central1/backendServices/alchemy-missing-be`,
75 timeout: "0.1s",
76 supportedEvents: ["REQUEST_HEADERS"],
77 },
78 ],
79 },
80 ],
81 },
82 }),
83 );
84 expect(error._tag).toBe("BadRequest");
85
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 {
89 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
90 timeout: 90_000,
91 },
92);
93
94test.provider.skipIf(
95 !!process.env.FAST || !process.env.GCP_TEST_LB_ROUTE_EXTENSION,
96)(
97 "create, update, and delete an lb route extension",
98 (stack) =>
99 Effect.gen(function* () {
100 yield* stack.destroy();
101
102 const forwardingRule = process.env.GCP_TEST_LB_ROUTE_EXTENSION_TARGET!;
103 const service = process.env.GCP_TEST_LB_ROUTE_EXTENSION_SERVICE!;
104
105 const created = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.NetworkServices.LbRouteExtension("Route", {
108 location: "us-central1",
109 description: "lb route a",
110 labels: { env: "test" },
111 loadBalancingScheme: "INTERNAL_MANAGED",
112 forwardingRules: [forwardingRule],
113 extensionChains: [
114 {
115 name: "chain1",
116 matchCondition: { celExpression: "true" },
117 extensions: [
118 {
119 name: "ext1",
120 authority: "ext1.example.com",
121 service,
122 timeout: "0.1s",
123 supportedEvents: ["REQUEST_HEADERS"],
124 },
125 ],
126 },
127 ],
128 });
129 }),
130 );
131
132 expect(created.name).toContain("/lbRouteExtensions/");
133 expect(created.location).toEqual("us-central1");
134 expect(created.labels).toMatchObject({ env: "test" });
135
136 const fetched =
137 yield* networkservices.getProjectsLocationsLbRouteExtensions({
138 name: created.name,
139 });
140 expect(fetched.name).toEqual(created.name);
141 expect(fetched.labels?.env).toEqual("test");
142
143 const updated = yield* stack.deploy(
144 Effect.gen(function* () {
145 return yield* GCP.NetworkServices.LbRouteExtension("Route", {
146 lbRouteExtensionId: created.lbRouteExtensionId,
147 location: "us-central1",
148 description: "lb route b",
149 labels: { env: "prod", role: "route" },
150 loadBalancingScheme: "INTERNAL_MANAGED",
151 forwardingRules: [forwardingRule],
152 extensionChains: [
153 {
154 name: "chain1",
155 matchCondition: { celExpression: "true" },
156 extensions: [
157 {
158 name: "ext1",
159 authority: "ext1.example.com",
160 service,
161 timeout: "0.2s",
162 failOpen: true,
163 supportedEvents: ["REQUEST_HEADERS"],
164 },
165 ],
166 },
167 ],
168 });
169 }),
170 );
171
172 expect(updated.name).toEqual(created.name);
173 expect(updated.description).toEqual("lb route b");
174 expect(updated.labels).toMatchObject({ env: "prod", role: "route" });
175
176 yield* stack.destroy();
177
178 const gone = yield* waitUntilGone(created.name);
179 expect(gone).toEqual("gone");
180 }).pipe(logLevel),
181 {
182 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
183 timeout: 120_000,
184 },
185);