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 serviceAccountOf = (project: string) =>
18 `alchemy-testing@${project}.iam.gserviceaccount.com`;
19
20const waitUntilGone = (name: string) =>
21 migrationcenter.getProjectsLocationsDiscoveryClients({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("1 second"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsDiscoveryClients on a missing client fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 migrationcenter.getProjectsLocationsDiscoveryClients({
40 name: `projects/${project}/locations/us-central1/discoveryClients/alchemy-missing-client`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
49 timeout: 90_000,
50 },
51);
52
53test.provider(
54 "create, update, and delete a migration center discovery client",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 const serviceAccount = serviceAccountOf(project);
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 const source = yield* GCP.MigrationCenter.Source("Scanner", {
64 location: "us-central1",
65 type: "SOURCE_TYPE_DISCOVERY_CLIENT",
66 displayName: "scanner-source",
67 });
68 const client = yield* GCP.MigrationCenter.DiscoveryClient("Agent", {
69 location: "us-central1",
70 source: source.name,
71 serviceAccount,
72 displayName: "on-prem-agent",
73 description: "test scanner",
74 labels: { env: "test" },
75 });
76 return { source, client };
77 }),
78 );
79
80 expect(created.client.discoveryClientId).toEqual(expect.any(String));
81 expect(created.client.source).toEqual(created.source.name);
82 expect(created.client.serviceAccount).toEqual(serviceAccount);
83 expect(created.client.displayName).toEqual("on-prem-agent");
84 expect(created.client.labels).toMatchObject({ env: "test" });
85
86 const fetched =
87 yield* migrationcenter.getProjectsLocationsDiscoveryClients({
88 name: created.client.name,
89 });
90 expect(fetched.name).toEqual(created.client.name);
91 expect(fetched.source).toEqual(created.source.name);
92 expect(fetched.labels?.env).toEqual("test");
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 const source = yield* GCP.MigrationCenter.Source("Scanner", {
97 sourceId: created.source.sourceId,
98 location: "us-central1",
99 type: "SOURCE_TYPE_DISCOVERY_CLIENT",
100 displayName: "scanner-source",
101 });
102 const client = yield* GCP.MigrationCenter.DiscoveryClient("Agent", {
103 discoveryClientId: created.client.discoveryClientId,
104 location: "us-central1",
105 source: source.name,
106 serviceAccount,
107 displayName: "on-prem-agent-v2",
108 description: "test scanner v2",
109 labels: { env: "prod" },
110 });
111 return { source, client };
112 }),
113 );
114
115 expect(updated.client.name).toEqual(created.client.name);
116 expect(updated.client.displayName).toEqual("on-prem-agent-v2");
117 expect(updated.client.description).toEqual("test scanner v2");
118 expect(updated.client.labels).toMatchObject({ env: "prod" });
119
120 yield* stack.destroy();
121 const gone = yield* waitUntilGone(created.client.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
126 timeout: 180_000,
127 },
128);