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.getProjectsLocationsEndpointPolicies({ 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
28const matcher = (value: string) => ({
29 metadataLabelMatcher: {
30 metadataLabelMatchCriteria: "MATCH_ANY" as const,
31 metadataLabels: [{ labelName: "app", labelValue: value }],
32 },
33});
34
35test.provider(
36 "getProjectsLocationsEndpointPolicies on a missing policy fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const { project } = yield* GcpEnvironment.current;
42 const error = yield* Effect.flip(
43 networkservices.getProjectsLocationsEndpointPolicies({
44 name: `projects/${project}/locations/global/endpointPolicies/alchemy-missing`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider(
58 "create, update, and delete an endpoint policy",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 return yield* GCP.NetworkServices.EndpointPolicy("Sidecar", {
66 location: "global",
67 type: "SIDECAR_PROXY",
68 description: "endpoint policy a",
69 labels: { env: "test" },
70 endpointMatcher: matcher("web"),
71 trafficPortSelector: { ports: ["8080"] },
72 });
73 }),
74 );
75
76 expect(created.name).toContain("/endpointPolicies/");
77 expect(created.endpointPolicyId).toEqual(expect.any(String));
78 expect(created.location).toEqual("global");
79 expect(created.type).toEqual("SIDECAR_PROXY");
80 expect(created.description).toEqual("endpoint policy a");
81 expect(created.labels).toMatchObject({ env: "test" });
82 expect(created.createTime).toEqual(expect.any(String));
83
84 const fetched =
85 yield* networkservices.getProjectsLocationsEndpointPolicies({
86 name: created.name,
87 });
88 expect(fetched.name).toEqual(created.name);
89 expect(fetched.description).toEqual("endpoint policy a");
90 expect(fetched.labels?.env).toEqual("test");
91 expect(
92 Object.keys(fetched.labels ?? {}).some((key) =>
93 key.startsWith("alchemy-"),
94 ),
95 ).toEqual(true);
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* GCP.NetworkServices.EndpointPolicy("Sidecar", {
100 endpointPolicyId: created.endpointPolicyId,
101 location: "global",
102 type: "SIDECAR_PROXY",
103 description: "endpoint policy b",
104 labels: { env: "prod", role: "sidecar" },
105 endpointMatcher: matcher("api"),
106 trafficPortSelector: { ports: ["8081"] },
107 });
108 }),
109 );
110
111 expect(updated.name).toEqual(created.name);
112 expect(updated.description).toEqual("endpoint policy b");
113 expect(updated.labels).toMatchObject({ env: "prod", role: "sidecar" });
114
115 const refetched =
116 yield* networkservices.getProjectsLocationsEndpointPolicies({
117 name: created.name,
118 });
119 expect(refetched.description).toEqual("endpoint policy b");
120 expect(refetched.labels?.env).toEqual("prod");
121 expect(refetched.labels?.role).toEqual("sidecar");
122
123 yield* stack.destroy();
124
125 const gone = yield* waitUntilGone(created.name);
126 expect(gone).toEqual("gone");
127 }).pipe(logLevel),
128 {
129 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
130 timeout: 90_000,
131 },
132);