1import * as GCP from "@/GCP";
2import * as Output from "@/Output";
3import * as Test from "@/Test/Alchemy";
4import { Credentials } from "@distilled.cloud/gcp/Credentials";
5import * as networkservices from "@distilled.cloud/gcp/networkservices_v1";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import * as Redacted from "effect/Redacted";
9import { MinimumLogLevel } from "effect/References";
10import * as Schedule from "effect/Schedule";
11import * as HttpClient from "effect/http/HttpClient";
12import * as HttpClientRequest from "effect/http/HttpClientRequest";
13import { GcpEnvironment } from "@/GCP/Environment";
14
15const { test } = Test.make({ providers: GCP.providers() });
16
17const logLevel = Effect.provideService(
18 MinimumLogLevel,
19 process.env.DEBUG ? "Debug" : "Info",
20);
21
22const WASM = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
23
24const waitUntilGone = (name: string) =>
25 networkservices.getProjectsLocationsWasmPluginsVersions({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35const imageUriOf = (repo: { name: any }) =>
36 Output.interpolate`${repo.name}/genericArtifacts/plugin:v1`;
37
38const uploadPluginWasm = (repositoryName: string) =>
39 Effect.gen(function* () {
40 const creds = yield* yield* Credentials;
41 const client = yield* HttpClient.HttpClient;
42 const url =
43 `https://artifactregistry.googleapis.com/upload/v1/${repositoryName}/genericArtifacts:create` +
44 `?uploadType=media&filename=plugin.wasm&packageId=plugin&versionId=v1`;
45 const response = yield* client.execute(
46 HttpClientRequest.post(url).pipe(
47 HttpClientRequest.setHeader(
48 "Authorization",
49 `Bearer ${Redacted.value(creds.accessToken)}`,
50 ),
51 HttpClientRequest.bodyUint8Array(WASM, "application/octet-stream"),
52 ),
53 );
54 if (response.status === 409) return;
55 if (response.status < 200 || response.status >= 300) {
56 const body = yield* response.text.pipe(
57 Effect.catch(() => Effect.succeed("")),
58 );
59 return yield* Effect.fail(
60 new Error(`generic artifact upload failed: ${response.status} ${body}`),
61 );
62 }
63 }).pipe(
64 Effect.retry({
65 times: 5,
66 schedule: Schedule.spaced("2 seconds"),
67 }),
68 );
69
70test.provider(
71 "getProjectsLocationsWasmPluginsVersions on a missing version fails with a typed tag",
72 (stack) =>
73 Effect.gen(function* () {
74 yield* stack.destroy();
75
76 const { project } = yield* GcpEnvironment.current;
77 const error = yield* Effect.flip(
78 networkservices.getProjectsLocationsWasmPluginsVersions({
79 name: `projects/${project}/locations/global/wasmPlugins/alchemy-missing/versions/missing`,
80 }),
81 );
82 expect(error._tag).toEqual("NotFound");
83
84 yield* stack.destroy();
85 }).pipe(logLevel),
86 {
87 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
88 timeout: 90_000,
89 },
90);
91
92test.provider.skipIf(!!process.env.FAST)(
93 "create and delete a wasm plugin version",
94 (stack) =>
95 Effect.gen(function* () {
96 yield* stack.destroy();
97
98 const repo = yield* stack.deploy(
99 Effect.gen(function* () {
100 return yield* GCP.ArtifactRegistry.Repository("WasmVer", {
101 location: "us-central1",
102 format: "GENERIC",
103 description: "wasm version artifacts",
104 });
105 }),
106 );
107
108 yield* uploadPluginWasm(repo.name);
109
110 const created = yield* stack.deploy(
111 Effect.gen(function* () {
112 const artifacts = yield* GCP.ArtifactRegistry.Repository("WasmVer", {
113 repositoryId: repo.repositoryId,
114 location: "us-central1",
115 format: "GENERIC",
116 description: "wasm version artifacts",
117 });
118 const plugin = yield* GCP.NetworkServices.WasmPlugin("Edge", {
119 location: "global",
120 description: "version parent",
121 labels: { env: "test" },
122 mainVersionId: "v1",
123 versions: {
124 v1: {
125 description: "inline v1",
126 imageUri: imageUriOf(artifacts),
127 },
128 },
129 });
130 const version = yield* GCP.NetworkServices.WasmPluginsVersion("V2", {
131 wasmPlugin: plugin.name,
132 location: "global",
133 wasmPluginVersionId: "v2",
134 description: "wasm version a",
135 labels: { env: "test" },
136 imageUri: imageUriOf(artifacts),
137 });
138 return { artifacts, plugin, version };
139 }),
140 );
141
142 expect(created.version.name).toContain("/versions/");
143 expect(created.version.wasmPlugin).toEqual(created.plugin.name);
144 expect(created.version.wasmPluginVersionId).toEqual("v2");
145 expect(created.version.description).toEqual("wasm version a");
146 expect(created.version.labels).toMatchObject({ env: "test" });
147 expect(created.version.createTime).toEqual(expect.any(String));
148
149 const fetched =
150 yield* networkservices.getProjectsLocationsWasmPluginsVersions({
151 name: created.version.name,
152 });
153 expect(fetched.name).toEqual(created.version.name);
154 expect(fetched.description).toEqual("wasm version a");
155 expect(fetched.labels?.env).toEqual("test");
156 expect(
157 Object.keys(fetched.labels ?? {}).some((key) =>
158 key.startsWith("alchemy-"),
159 ),
160 ).toEqual(true);
161
162 const serving = yield* stack.deploy(
163 Effect.gen(function* () {
164 const artifacts = yield* GCP.ArtifactRegistry.Repository("WasmVer", {
165 repositoryId: repo.repositoryId,
166 location: "us-central1",
167 format: "GENERIC",
168 description: "wasm version artifacts",
169 });
170 const plugin = yield* GCP.NetworkServices.WasmPlugin("Edge", {
171 wasmPluginId: created.plugin.wasmPluginId,
172 location: "global",
173 description: "version parent",
174 labels: { env: "test" },
175 mainVersionId: "v2",
176 });
177 const version = yield* GCP.NetworkServices.WasmPluginsVersion("V2", {
178 wasmPlugin: plugin.name,
179 wasmPluginVersionId: created.version.wasmPluginVersionId,
180 location: "global",
181 description: "wasm version a",
182 labels: { env: "test" },
183 imageUri: imageUriOf(artifacts),
184 });
185 return { artifacts, plugin, version };
186 }),
187 );
188
189 expect(serving.plugin.name).toEqual(created.plugin.name);
190 expect(serving.plugin.mainVersionId).toEqual("v2");
191 expect(serving.version.name).toEqual(created.version.name);
192
193 const refetched = yield* networkservices.getProjectsLocationsWasmPlugins({
194 name: created.plugin.name,
195 });
196 expect(refetched.mainVersionId).toEqual("v2");
197
198 yield* stack.destroy();
199
200 const gone = yield* waitUntilGone(created.version.name);
201 expect(gone).toEqual("gone");
202 }).pipe(logLevel),
203 {
204 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
205 timeout: 120_000,
206 },
207);