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.getProjectsLocationsTlsRoutes({ 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 "getProjectsLocationsTlsRoutes on a missing route fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const { project } = yield* GcpEnvironment.current;
35 const error = yield* Effect.flip(
36 networkservices.getProjectsLocationsTlsRoutes({
37 name: `projects/${project}/locations/global/tlsRoutes/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 "create, update, and delete a tls route",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
59 location: "global",
60 description: "tls mesh",
61 labels: { env: "test" },
62 });
63 const backend = yield* GCP.Compute.BackendService("Backend", {
64 loadBalancingScheme: "INTERNAL_SELF_MANAGED",
65 protocol: "HTTP",
66 description: "tls backend",
67 });
68 const route = yield* GCP.NetworkServices.TlsRoute("Secure", {
69 location: "global",
70 description: "tls route a",
71 labels: { env: "test" },
72 meshes: [mesh.name],
73 rules: [
74 {
75 matches: [{ sniHost: ["api.example.com"], alpn: ["h2"] }],
76 action: {
77 destinations: [
78 {
79 serviceName: backend.selfLink ?? backend.name,
80 weight: 1,
81 },
82 ],
83 },
84 },
85 ],
86 });
87 return { mesh, backend, route };
88 }),
89 );
90
91 expect(created.route.name).toContain("/tlsRoutes/");
92 expect(created.route.tlsRouteId).toEqual(expect.any(String));
93 expect(created.route.location).toEqual("global");
94 expect(created.route.description).toEqual("tls route a");
95 expect(created.route.meshes).toContain(created.mesh.name);
96 expect(created.route.rules[0]?.matches?.[0]?.sniHost).toContain(
97 "api.example.com",
98 );
99 expect(created.route.labels).toMatchObject({ env: "test" });
100 expect(created.route.createTime).toEqual(expect.any(String));
101
102 const fetched = yield* networkservices.getProjectsLocationsTlsRoutes({
103 name: created.route.name,
104 });
105 expect(fetched.name).toEqual(created.route.name);
106 expect(fetched.description).toEqual("tls route a");
107 expect(fetched.labels?.env).toEqual("test");
108 expect(
109 Object.keys(fetched.labels ?? {}).some((key) =>
110 key.startsWith("alchemy-"),
111 ),
112 ).toEqual(true);
113
114 const updated = yield* stack.deploy(
115 Effect.gen(function* () {
116 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
117 meshId: created.mesh.meshId,
118 location: "global",
119 description: "tls mesh",
120 labels: { env: "test" },
121 });
122 const backend = yield* GCP.Compute.BackendService("Backend", {
123 name: created.backend.name,
124 loadBalancingScheme: "INTERNAL_SELF_MANAGED",
125 protocol: "HTTP",
126 description: "tls backend",
127 });
128 const route = yield* GCP.NetworkServices.TlsRoute("Secure", {
129 tlsRouteId: created.route.tlsRouteId,
130 location: "global",
131 description: "tls route b",
132 labels: { env: "prod", role: "tls" },
133 meshes: [mesh.name],
134 rules: [
135 {
136 matches: [{ sniHost: ["secure.example.com"], alpn: ["h2"] }],
137 action: {
138 destinations: [
139 {
140 serviceName: backend.selfLink ?? backend.name,
141 weight: 1,
142 },
143 ],
144 idleTimeout: "60s",
145 },
146 },
147 ],
148 });
149 return { mesh, backend, route };
150 }),
151 );
152
153 expect(updated.route.name).toEqual(created.route.name);
154 expect(updated.route.description).toEqual("tls route b");
155 expect(updated.route.labels).toMatchObject({ env: "prod", role: "tls" });
156 expect(updated.route.rules[0]?.matches?.[0]?.sniHost).toContain(
157 "secure.example.com",
158 );
159
160 const refetched = yield* networkservices.getProjectsLocationsTlsRoutes({
161 name: created.route.name,
162 });
163 expect(refetched.description).toEqual("tls route b");
164 expect(refetched.labels?.env).toEqual("prod");
165 expect(refetched.labels?.role).toEqual("tls");
166
167 yield* stack.destroy();
168
169 const gone = yield* waitUntilGone(created.route.name);
170 expect(gone).toEqual("gone");
171 }).pipe(logLevel),
172 {
173 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
174 timeout: 180_000,
175 },
176);