1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as beyondcorp from "@distilled.cloud/gcp/beyondcorp_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { logLevel, currentProject, runLifecycle } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 beyondcorp.getProjectsLocationsAppGateways({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("2 seconds"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22test.provider(
23 "getProjectsLocationsAppGateways on a missing gateway fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 const project = yield* currentProject;
27 yield* stack.destroy();
28
29 const error = yield* Effect.flip(
30 beyondcorp.getProjectsLocationsAppGateways({
31 name: `projects/${project}/locations/us-central1/appGateways/alchemy-missing-gw`,
32 }),
33 );
34 expect(error._tag).toEqual("NotFound");
35
36 const page = yield* beyondcorp.listProjectsLocationsAppGateways({
37 parent: `projects/${project}/locations/us-central1`,
38 pageSize: 10,
39 });
40 expect((page.appGateways ?? []).map((item) => item.name)).not.toContain(
41 `projects/${project}/locations/us-central1/appGateways/alchemy-missing-gw`,
42 );
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:beyondcorp", "live"],
48 timeout: 90_000,
49 },
50);
51
52test.provider.skipIf(runLifecycle)(
53 "createProjectsLocationsAppGateways without entitlement fails with a typed tag",
54 (stack) =>
55 Effect.gen(function* () {
56 const project = yield* currentProject;
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 beyondcorp.createProjectsLocationsAppGateways({
61 parent: `projects/${project}/locations/us-central1`,
62 appGatewayId: "alch-probe-gw",
63 validateOnly: true,
64 body: {
65 type: "TCP_PROXY",
66 hostType: "GCP_REGIONAL_MIG",
67 },
68 }),
69 );
70 expect(error._tag).toEqual("AppConnectorsNotImplemented");
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 {
75 tags: ["provider:gcp", "provider:gcp:beyondcorp", "live"],
76 timeout: 90_000,
77 },
78);
79
80test.provider.skipIf(!runLifecycle)(
81 "create, update, and delete an app gateway",
82 (stack) =>
83 Effect.gen(function* () {
84 yield* stack.destroy();
85
86 const created = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.BeyondCorp.AppGateway("Edge", {
89 location: "us-central1",
90 type: "TCP_PROXY",
91 hostType: "GCP_REGIONAL_MIG",
92 displayName: "gateway a",
93 labels: { env: "test" },
94 });
95 }),
96 );
97
98 expect(created.name).toContain("/appGateways/");
99 expect(created.location).toEqual("us-central1");
100 expect(created.type).toEqual("TCP_PROXY");
101 expect(created.hostType).toEqual("GCP_REGIONAL_MIG");
102 expect(created.labels).toMatchObject({ env: "test" });
103
104 const fetched = yield* beyondcorp.getProjectsLocationsAppGateways({
105 name: created.name,
106 });
107 expect(fetched.name).toEqual(created.name);
108 expect(fetched.labels?.env).toEqual("test");
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* GCP.BeyondCorp.AppGateway("Edge", {
113 appGatewayId: created.appGatewayId,
114 location: "us-central1",
115 type: "TCP_PROXY",
116 hostType: "GCP_REGIONAL_MIG",
117 displayName: "gateway b",
118 labels: { env: "prod" },
119 });
120 }),
121 );
122
123 expect(updated.name).toEqual(created.name);
124 expect(updated.appGatewayId).toEqual(created.appGatewayId);
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(created.name);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 {
132 tags: ["provider:gcp", "provider:gcp:beyondcorp", "live"],
133 timeout: 120_000,
134 },
135);