1import { GcpEnvironment } from "@/GCP/Environment";
2import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
3import * as Effect from "effect/Effect";
4import { MinimumLogLevel } from "effect/References";
5
6export const logLevel = Effect.provideService(
7 MinimumLogLevel,
8 process.env.DEBUG ? "Debug" : "Info",
9);
10
11export const currentProject = GcpEnvironment.current.pipe(
12 Effect.map((env) => env.project),
13);
14
15// Access policies live on the organization and need Access Context Manager
16// admin there; the testing credentials get Forbidden "The caller does not have
17// permission". Set GOOGLE_ORGANIZATION_ID when the credentials administer the
18// org to run the lifecycles.
19export const runLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID;
20
21export const runProbe = !runLifecycle;
22
23export const lastSegment = (value: string) => {
24 const trimmed = value.replace(/\/+$/, "");
25 const parts = trimmed.split("/");
26 return parts[parts.length - 1] || trimmed;
27};
28
29export const projectContext = () =>
30 currentProject.pipe(
31 Effect.flatMap((project) =>
32 resourcemanager.getProjects({ name: `projects/${project}` }),
33 ),
34 Effect.map((resource) => {
35 const parent = resource.parent ?? "";
36 return {
37 projectNumber: lastSegment(resource.name ?? ""),
38 parent,
39 organization: parent.startsWith("organizations/") ? parent : undefined,
40 };
41 }),
42 Effect.catchTag("NotFound", () =>
43 Effect.succeed({
44 projectNumber: "",
45 parent: "",
46 organization: undefined as string | undefined,
47 }),
48 ),
49 );