1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as vmwareengine from "@distilled.cloud/gcp/vmwareengine_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
17// Private clouds need VMware Engine node quota and bill thousands of dollars
18// a month; set GCP_TEST_VMWAREENGINE=1 on an entitled project to opt in.
19const runLifecycle = !!process.env.GCP_TEST_VMWAREENGINE && !process.env.FAST;
20const serviceNetworkOf = (project: string) =>
21 process.env.GCP_TEST_VMWAREENGINE_SERVICE_NETWORK ??
22 `projects/${project}/global/networks/default`;
23
24const waitUntilGone = (name: string) =>
25 vmwareengine.getProjectsLocationsPrivateConnections({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsPrivateConnections on a missing connection fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 vmwareengine.getProjectsLocationsPrivateConnections({
45 name: `projects/${project}/locations/us-central1/privateConnections/alchemy-pc-missing`,
46 }),
47 );
48 expect(error._tag).toEqual("NotFound");
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 {
53 tags: ["provider:gcp", "provider:gcp:vmwareengine", "live"],
54 timeout: 90_000,
55 },
56);
57
58test.provider.skipIf(!runLifecycle)(
59 "create, update, and delete a private connection",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 const ven = yield* GCP.VMwareEngine.VmwareEngineNetwork("Ven", {
69 type: "STANDARD",
70 description: "alchemy-test-pc-ven",
71 });
72 const connection = yield* GCP.VMwareEngine.PrivateConnection("Peer", {
73 location: "us-central1",
74 type: "THIRD_PARTY_SERVICE",
75 vmwareEngineNetwork: ven.name,
76 serviceNetwork: serviceNetworkOf(project),
77 description: "alchemy-test-pc",
78 });
79 return { ven, connection };
80 }),
81 );
82
83 expect(created.connection.name).toContain("/privateConnections/");
84 expect(created.connection.location).toEqual("us-central1");
85 expect(created.connection.type).toEqual("THIRD_PARTY_SERVICE");
86 expect(created.connection.description).toEqual("alchemy-test-pc");
87 expect(created.connection.vmwareEngineNetwork).toEqual(created.ven.name);
88 expect(created.connection.createTime).toEqual(expect.any(String));
89
90 const fetched =
91 yield* vmwareengine.getProjectsLocationsPrivateConnections({
92 name: created.connection.name,
93 });
94 expect(fetched.name).toEqual(created.connection.name);
95 expect(fetched.description).toContain("alchemy-id=");
96 expect(fetched.description).toContain("alchemy-test-pc");
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 const ven = yield* GCP.VMwareEngine.VmwareEngineNetwork("Ven", {
101 vmwareEngineNetworkId: created.ven.vmwareEngineNetworkId,
102 location: "global",
103 type: "STANDARD",
104 description: "alchemy-test-pc-ven",
105 });
106 const connection = yield* GCP.VMwareEngine.PrivateConnection("Peer", {
107 privateConnectionId: created.connection.privateConnectionId,
108 location: "us-central1",
109 type: "THIRD_PARTY_SERVICE",
110 vmwareEngineNetwork: ven.name,
111 serviceNetwork: serviceNetworkOf(project),
112 description: "alchemy-prod-pc",
113 });
114 return { ven, connection };
115 }),
116 );
117
118 expect(updated.connection.name).toEqual(created.connection.name);
119 expect(updated.connection.description).toEqual("alchemy-prod-pc");
120
121 const refetched =
122 yield* vmwareengine.getProjectsLocationsPrivateConnections({
123 name: created.connection.name,
124 });
125 expect(refetched.description).toContain("alchemy-prod-pc");
126 expect(refetched.description).toContain("alchemy-id=");
127
128 yield* stack.destroy();
129
130 const gone = yield* waitUntilGone(created.connection.name);
131 expect(gone).toEqual("gone");
132 }).pipe(logLevel),
133 {
134 tags: ["provider:gcp", "provider:gcp:vmwareengine", "live"],
135 timeout: 120_000,
136 },
137);