| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { MinimumLogLevel } from "effect/References"; |
| 5 | |
| 6 | export const logLevel = Effect.provideService( |
| 7 | MinimumLogLevel, |
| 8 | process.env.DEBUG ? "Debug" : "Info", |
| 9 | ); |
| 10 | |
| 11 | export 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. |
| 19 | export const runLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID; |
| 20 | |
| 21 | export const runProbe = !runLifecycle; |
| 22 | |
| 23 | export const lastSegment = (value: string) => { |
| 24 | const trimmed = value.replace(/\/+$/, ""); |
| 25 | const parts = trimmed.split("/"); |
| 26 | return parts[parts.length - 1] || trimmed; |
| 27 | }; |
| 28 | |
| 29 | export 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 | ); |