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";
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 region = "us-central1";
18
19const waitUntilGone = (
20 projectId: string,
21 regionName: string,
22 templateName: string,
23) =>
24 compute
25 .getRegionInstanceTemplates({
26 project: projectId,
27 region: regionName,
28 instanceTemplate: templateName,
29 })
30 .pipe(
31 Effect.as("found" as const),
32 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
33 Effect.repeat({
34 schedule: Schedule.spaced("1 second"),
35 until: (status) => status === "gone",
36 times: 10,
37 }),
38 );
39
40test.provider(
41 "create, replace, and delete a regional instance template",
42 (stack) =>
43 Effect.gen(function* () {
44 const { project } = yield* GcpEnvironment.current;
45 yield* stack.destroy();
46
47 const created = yield* stack.deploy(
48 Effect.gen(function* () {
49 return yield* GCP.Compute.RegionInstanceTemplate("Web", {
50 region,
51 machineType: "e2-micro",
52 labels: { env: "test" },
53 disks: [
54 {
55 boot: true,
56 autoDelete: true,
57 sourceImage:
58 "projects/debian-cloud/global/images/family/debian-12",
59 diskSizeGb: 10,
60 },
61 ],
62 networkInterfaces: [{ network: "global/networks/default" }],
63 });
64 }),
65 );
66
67 expect(created.templateName).toEqual(expect.any(String));
68 expect(created.region).toEqual(region);
69 expect(created.machineType).toEqual("e2-micro");
70 expect(created.labels).toMatchObject({ env: "test" });
71 expect(created.project).toEqual(project);
72
73 const fetched = yield* compute.getRegionInstanceTemplates({
74 project,
75 region,
76 instanceTemplate: created.templateName,
77 });
78 expect(fetched.name).toEqual(created.templateName);
79 expect(fetched.properties?.machineType).toEqual("e2-micro");
80 expect(fetched.properties?.labels?.env).toEqual("test");
81 expect(fetched.properties?.labels?.["alchemy-id"]).toEqual(
82 expect.any(String),
83 );
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Compute.RegionInstanceTemplate("Web", {
88 templateName: created.templateName,
89 region,
90 machineType: "e2-small",
91 labels: { env: "prod", role: "web" },
92 disks: [
93 {
94 boot: true,
95 autoDelete: true,
96 sourceImage:
97 "projects/debian-cloud/global/images/family/debian-12",
98 diskSizeGb: 10,
99 },
100 ],
101 networkInterfaces: [{ network: "global/networks/default" }],
102 });
103 }),
104 );
105
106 expect(updated.templateName).toEqual(created.templateName);
107 expect(updated.machineType).toEqual("e2-small");
108 expect(updated.labels).toMatchObject({ env: "prod", role: "web" });
109
110 const refetched = yield* compute.getRegionInstanceTemplates({
111 project,
112 region,
113 instanceTemplate: updated.templateName,
114 });
115 expect(refetched.properties?.machineType).toEqual("e2-small");
116 expect(refetched.properties?.labels?.env).toEqual("prod");
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(project, region, created.templateName);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
124);