1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as iap from "@distilled.cloud/gcp/iap_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 iap.getProjectsIap_tunnelLocationsDestGroups({ 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 "getProjectsIap_tunnelLocationsDestGroups on a missing group 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 iap.getProjectsIap_tunnelLocationsDestGroups({
37 name: `projects/${project}/iap_tunnel/locations/us-central1/destGroups/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 { tags: ["provider:gcp", "provider:gcp:iap", "live"], timeout: 90_000 },
45);
46
47test.provider(
48 "create, update, and delete an IAP tunnel dest group",
49 (stack) =>
50 Effect.gen(function* () {
51 const { project } = yield* GcpEnvironment.current;
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 return yield* GCP.IAP.IapDestGroup("SshHosts", {
57 location: "us-central1",
58 cidrs: ["10.241.0.0/24"],
59 });
60 }),
61 );
62
63 expect(created.name).toContain("/destGroups/");
64 expect(created.destGroupId.length).toBeGreaterThanOrEqual(4);
65 expect(created.location).toEqual("us-central1");
66 expect(created.project).toEqual(project);
67 expect(created.cidrs).toEqual(["10.241.0.0/24"]);
68 expect(created.fqdns).toEqual([]);
69
70 const fetched = yield* iap.getProjectsIap_tunnelLocationsDestGroups({
71 name: created.name,
72 });
73 expect(fetched.name).toEqual(created.name);
74 expect(fetched.cidrs).toContain("10.241.0.0/24");
75 expect(fetched.fqdns ?? []).toEqual([]);
76
77 const updated = yield* stack.deploy(
78 Effect.gen(function* () {
79 return yield* GCP.IAP.IapDestGroup("SshHosts", {
80 location: "us-central1",
81 cidrs: ["10.241.0.0/24", "10.241.1.0/24"],
82 fqdns: ["db.internal.example.com"],
83 });
84 }),
85 );
86
87 expect(updated.name).toEqual(created.name);
88 expect(updated.cidrs.sort()).toEqual(
89 ["10.241.0.0/24", "10.241.1.0/24"].sort(),
90 );
91 expect(updated.fqdns).toEqual(["db.internal.example.com"]);
92
93 const fetchedUpdate = yield* iap.getProjectsIap_tunnelLocationsDestGroups(
94 {
95 name: created.name,
96 },
97 );
98 expect(fetchedUpdate.cidrs?.sort()).toEqual(
99 ["10.241.0.0/24", "10.241.1.0/24"].sort(),
100 );
101 expect(fetchedUpdate.fqdns).toEqual(["db.internal.example.com"]);
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 { tags: ["provider:gcp", "provider:gcp:iap", "live"], timeout: 90_000 },
109);