1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as migrationcenter from "@distilled.cloud/gcp/migrationcenter_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 migrationcenter.getProjectsLocationsPreferenceSets({ 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 "getProjectsLocationsPreferenceSets on a missing set 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 migrationcenter.getProjectsLocationsPreferenceSets({
37 name: `projects/${project}/locations/us-central1/preferenceSets/alchemy-missing-prefset`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, and delete a migration center preference set",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 return yield* GCP.MigrationCenter.PreferenceSet("Prod", {
60 location: "us-central1",
61 displayName: "prod-gce",
62 description: "gce defaults",
63 });
64 }),
65 );
66
67 expect(created.preferenceSetId).toEqual(expect.any(String));
68 expect(created.name).toEqual(
69 `projects/${project}/locations/us-central1/preferenceSets/${created.preferenceSetId}`,
70 );
71 expect(created.displayName).toEqual("prod-gce");
72 expect(created.description).toEqual("gce defaults");
73 expect(created.virtualMachinePreferences?.targetProduct).toEqual(
74 "COMPUTE_MIGRATION_TARGET_PRODUCT_COMPUTE_ENGINE",
75 );
76
77 const fetched = yield* migrationcenter.getProjectsLocationsPreferenceSets(
78 { name: created.name },
79 );
80 expect(fetched.name).toEqual(created.name);
81 expect(fetched.displayName).toEqual("prod-gce");
82 expect(fetched.description).toContain("alchemy-id=");
83 expect(fetched.description).toContain("gce defaults");
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.MigrationCenter.PreferenceSet("Prod", {
88 preferenceSetId: created.preferenceSetId,
89 location: "us-central1",
90 displayName: "prod-gce-v2",
91 description: "gce moderate",
92 virtualMachinePreferences: {
93 targetProduct: "COMPUTE_MIGRATION_TARGET_PRODUCT_COMPUTE_ENGINE",
94 regionPreferences: { preferredRegions: ["us-central1"] },
95 sizingOptimizationStrategy:
96 "SIZING_OPTIMIZATION_STRATEGY_MODERATE",
97 commitmentPlan: "COMMITMENT_PLAN_ONE_YEAR",
98 },
99 });
100 }),
101 );
102
103 expect(updated.name).toEqual(created.name);
104 expect(updated.displayName).toEqual("prod-gce-v2");
105 expect(updated.description).toEqual("gce moderate");
106 expect(updated.virtualMachinePreferences?.commitmentPlan).toEqual(
107 "COMMITMENT_PLAN_ONE_YEAR",
108 );
109
110 yield* stack.destroy();
111 const gone = yield* waitUntilGone(created.name);
112 expect(gone).toEqual("gone");
113 }).pipe(logLevel),
114 {
115 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
116 timeout: 90_000,
117 },
118);