1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (project: string, region: string, addressName: string) =>
17 compute.getAddresses({ project, region, address: addressName }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("2 seconds"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider(
28 "create, update, and delete a regional address",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.Compute.Address("Ingress", {
36 region: "us-central1",
37 labels: { env: "test" },
38 });
39 }),
40 );
41
42 expect(created.addressName).toEqual(expect.any(String));
43 expect(created.address).toEqual(expect.any(String));
44 expect(created.region).toEqual("us-central1");
45 expect(created.addressType).toEqual("EXTERNAL");
46 expect(created.networkTier).toEqual("PREMIUM");
47 expect(created.labels).toMatchObject({ env: "test" });
48 expect(created.status).toEqual(expect.stringMatching(/RESERVED|IN_USE/));
49
50 const fetched = yield* compute.getAddresses({
51 project: created.project,
52 region: created.region,
53 address: created.addressName,
54 });
55 expect(fetched.name).toEqual(created.addressName);
56 expect(fetched.address).toEqual(created.address);
57 expect(fetched.labels?.env).toEqual("test");
58
59 const updated = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.Compute.Address("Ingress", {
62 addressName: created.addressName,
63 region: "us-central1",
64 labels: { env: "prod", role: "ingress" },
65 });
66 }),
67 );
68
69 expect(updated.addressName).toEqual(created.addressName);
70 expect(updated.address).toEqual(created.address);
71 expect(updated.labels).toMatchObject({ env: "prod", role: "ingress" });
72
73 const fetchedUpdated = yield* compute.getAddresses({
74 project: updated.project,
75 region: updated.region,
76 address: updated.addressName,
77 });
78 expect(fetchedUpdated.labels?.env).toEqual("prod");
79 expect(fetchedUpdated.labels?.role).toEqual("ingress");
80
81 yield* stack.destroy();
82
83 const gone = yield* waitUntilGone(
84 created.project,
85 created.region,
86 created.addressName,
87 );
88 expect(gone).toEqual("gone");
89 }).pipe(logLevel),
90 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
91);