1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as osconfig from "@distilled.cloud/gcp/osconfig_v2";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
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 osconfig.getProjectsLocationsGlobalPolicyOrchestrators({ 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
28// The OS Config API is disabled on the testing project (every call fails
29// with ServiceDisabled). Set GCP_TEST_OSCONFIG=1 on a project with OS Config
30// enabled.
31const runLifecycle = !!process.env.GCP_TEST_OSCONFIG;
32
33test.provider(
34 "getProjectsLocationsGlobalPolicyOrchestrators on a missing orchestrator fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38 const parent = `projects/${project}/locations/global`;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 osconfig.getProjectsLocationsGlobalPolicyOrchestrators({
43 name: `${parent}/policyOrchestrators/alchemy-missing-orch`,
44 }),
45 );
46 expect(error._tag).toEqual("ServiceDisabled");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:osconfig", "live"], timeout: 90_000 },
51);
52
53test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a project policy orchestrator",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 const parent = `projects/${project}/locations/global`;
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.OSConfig.ProjectsLocationsGlobalPolicyOrchestrator(
64 "Debian",
65 {
66 description: "validation only",
67 labels: { env: "test" },
68 state: "STOPPED",
69 },
70 );
71 }),
72 );
73
74 expect(created.policyOrchestratorId).toEqual(expect.any(String));
75 expect(created.parent).toEqual(parent);
76 expect(created.name).toEqual(
77 `${parent}/policyOrchestrators/${created.policyOrchestratorId}`,
78 );
79 expect(created.action).toEqual("UPSERT");
80 expect(created.state).toEqual("STOPPED");
81 expect(created.description).toEqual("validation only");
82 expect(created.labels).toMatchObject({ env: "test" });
83
84 const fetched =
85 yield* osconfig.getProjectsLocationsGlobalPolicyOrchestrators({
86 name: created.name,
87 });
88 expect(fetched.name).toEqual(created.name);
89 expect(fetched.labels?.env).toEqual("test");
90 expect(fetched.state).toEqual("STOPPED");
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.OSConfig.ProjectsLocationsGlobalPolicyOrchestrator(
95 "Debian",
96 {
97 policyOrchestratorId: created.policyOrchestratorId,
98 description: "updated",
99 labels: { env: "prod", role: "os" },
100 state: "STOPPED",
101 },
102 );
103 }),
104 );
105
106 expect(updated.name).toEqual(created.name);
107 expect(updated.description).toEqual("updated");
108 expect(updated.labels).toMatchObject({ env: "prod", role: "os" });
109
110 const refetched =
111 yield* osconfig.getProjectsLocationsGlobalPolicyOrchestrators({
112 name: created.name,
113 });
114 expect(refetched.description).toEqual("updated");
115 expect(refetched.labels?.env).toEqual("prod");
116
117 yield* stack.destroy();
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:osconfig", "live"], timeout: 90_000 },
122);