| 1 | import * as chromemanagement from "@distilled.cloud/gcp/chromemanagement_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { MinimumLogLevel } from "effect/References"; |
| 4 | import * as Schedule from "effect/Schedule"; |
| 5 | |
| 6 | export const logLevel = Effect.provideService( |
| 7 | MinimumLogLevel, |
| 8 | process.env.DEBUG ? "Debug" : "Info", |
| 9 | ); |
| 10 | |
| 11 | const lastSegment = (value: string) => { |
| 12 | const trimmed = value.replace(/\/+$/, ""); |
| 13 | const parts = trimmed.split("/"); |
| 14 | return parts[parts.length - 1] || trimmed; |
| 15 | }; |
| 16 | |
| 17 | export const toCustomerName = (value: string) => { |
| 18 | const trimmed = value.replace(/\/+$/, "").trim(); |
| 19 | if (trimmed.length === 0) return ""; |
| 20 | const idx = trimmed.lastIndexOf("/customers/"); |
| 21 | if (idx >= 0) { |
| 22 | const id = trimmed.slice(idx + "/customers/".length).split("/")[0] ?? ""; |
| 23 | return id.length > 0 ? `customers/${id}` : ""; |
| 24 | } |
| 25 | if (trimmed.startsWith("customers/")) { |
| 26 | const id = trimmed.slice("customers/".length).split("/")[0] ?? ""; |
| 27 | return id.length > 0 ? `customers/${id}` : trimmed; |
| 28 | } |
| 29 | return `customers/${lastSegment(trimmed)}`; |
| 30 | }; |
| 31 | |
| 32 | export const customerName = (() => { |
| 33 | const raw = |
| 34 | process.env.GCP_CHROMEMANAGEMENT_CUSTOMER?.trim() || |
| 35 | process.env.GCP_CHROMEMANAGEMENT_CUSTOMER_ID?.trim(); |
| 36 | return raw ? toCustomerName(raw) : "customers/my_customer"; |
| 37 | })(); |
| 38 | |
| 39 | // Needs a Chrome Enterprise customer the credentials administer |
| 40 | // (GCP_CHROMEMANAGEMENT_CUSTOMER); the service-account token fails with |
| 41 | // `InsufficientScopes` ("Request had insufficient authentication scopes.") |
| 42 | export const runLifecycle = !!( |
| 43 | process.env.GCP_CHROMEMANAGEMENT_CUSTOMER?.trim() || |
| 44 | process.env.GCP_CHROMEMANAGEMENT_CUSTOMER_ID?.trim() |
| 45 | ); |
| 46 | |
| 47 | export const probeParent = "customers/0"; |
| 48 | export const probeName = `${probeParent}/connectorConfigs/alchemy-missing`; |
| 49 | |
| 50 | export const waitUntilGone = (name: string) => |
| 51 | chromemanagement.getCustomersConnectorConfigs({ name }).pipe( |
| 52 | Effect.as("found" as const), |
| 53 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 54 | Effect.repeat({ |
| 55 | schedule: Schedule.spaced("1 second"), |
| 56 | until: (status) => status === "gone", |
| 57 | times: 10, |
| 58 | }), |
| 59 | ); |