The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import { GcpEnvironment } from "../Environment.ts"; |
| 6 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 7 | |
| 8 | export const FOLDER_DISPLAY_MAX = 30; |
| 9 | export const PROJECT_ID_MIN = 6; |
| 10 | export const PROJECT_ID_MAX = 30; |
| 11 | export const PROJECT_DISPLAY_MIN = 4; |
| 12 | export const PROJECT_DISPLAY_MAX = 30; |
| 13 | export const LIEN_REASON_MAX = 200; |
| 14 | export const LIEN_ORIGIN_MAX = 200; |
| 15 | export const DEFAULT_LIEN_ORIGIN = "alchemy.effect"; |
| 16 | export const DEFAULT_LIEN_RESTRICTIONS = ["resourcemanager.projects.delete"] as const; |
| 17 | |
| 18 | export class ParentRequired extends Data.TaggedError("GCP.ResourceManager.ParentRequired")<{ |
| 19 | project: string; |
| 20 | }> {} |
| 21 | |
| 22 | export const lastSegment = (value: string) => { |
| 23 | const trimmed = value.replace(/\/+$/, ""); |
| 24 | const parts = trimmed.split("/"); |
| 25 | return parts[parts.length - 1] || trimmed; |
| 26 | }; |
| 27 | |
| 28 | export const organizationParent = (value: string) => |
| 29 | value.startsWith("organizations/") ? value : `organizations/${lastSegment(value)}`; |
| 30 | |
| 31 | export const folderParent = (value: string) => |
| 32 | value.startsWith("folders/") ? value : `folders/${lastSegment(value)}`; |
| 33 | |
| 34 | export const projectParent = (value: string) => |
| 35 | value.startsWith("projects/") ? value : `projects/${lastSegment(value)}`; |
| 36 | |
| 37 | export const normalizeHierarchyParent = (value: string) => { |
| 38 | if ( |
| 39 | value.startsWith("organizations/") || |
| 40 | value.startsWith("folders/") || |
| 41 | value.startsWith("projects/") |
| 42 | ) { |
| 43 | return value; |
| 44 | } |
| 45 | if (value.startsWith("orgs/")) { |
| 46 | return `organizations/${value.slice("orgs/".length)}`; |
| 47 | } |
| 48 | return folderParent(value); |
| 49 | }; |
| 50 | |
| 51 | export const sameHierarchyParent = (left: string | undefined, right: string | undefined) => { |
| 52 | if (left === undefined || right === undefined) return left === right; |
| 53 | if (left === right) return true; |
| 54 | const leftKind = left.split("/")[0]; |
| 55 | const rightKind = right.split("/")[0]; |
| 56 | return leftKind === rightKind && lastSegment(left) === lastSegment(right); |
| 57 | }; |
| 58 | |
| 59 | export const isDeleteRequested = (state: string | undefined) => state === "DELETE_REQUESTED"; |
| 60 | |
| 61 | export const resourceNameFromOperation = ( |
| 62 | operation: resourcemanager.Operation, |
| 63 | prefix: string, |
| 64 | ): string | undefined => { |
| 65 | const name = operation.response?.name; |
| 66 | return typeof name === "string" && name.startsWith(prefix) ? name : undefined; |
| 67 | }; |
| 68 | |
| 69 | export const projectIdFromOperation = ( |
| 70 | operation: resourcemanager.Operation, |
| 71 | ): string | undefined => { |
| 72 | const projectId = operation.response?.projectId; |
| 73 | return typeof projectId === "string" && projectId.length > 0 ? projectId : undefined; |
| 74 | }; |
| 75 | |
| 76 | const parentOf = (name: string) => |
| 77 | name.startsWith("projects/") |
| 78 | ? resourcemanager.getProjects({ name }).pipe( |
| 79 | Effect.map((resource) => resource.parent), |
| 80 | Effect.catchTag(["NotFound", "Forbidden", "ProjectNotFound"], () => Effect.undefined), |
| 81 | ) |
| 82 | : name.startsWith("folders/") |
| 83 | ? resourcemanager.getFolders({ name }).pipe( |
| 84 | Effect.map((folder) => folder.parent), |
| 85 | Effect.catchTag(["NotFound", "Forbidden", "FolderNotFound"], () => Effect.undefined), |
| 86 | ) |
| 87 | : Effect.undefined; |
| 88 | |
| 89 | export const tryResolveParent = () => |
| 90 | Effect.gen(function* () { |
| 91 | const env = yield* GcpEnvironment.current; |
| 92 | return yield* parentOf(`projects/${env.project}`).pipe( |
| 93 | Effect.map((parent) => { |
| 94 | if (parent === undefined || parent.length === 0) return undefined; |
| 95 | if (parent.startsWith("folders/") || parent.startsWith("organizations/")) { |
| 96 | return parent; |
| 97 | } |
| 98 | return undefined; |
| 99 | }), |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | export const resolveParent = (explicit: string | undefined, existing: string | undefined) => |
| 104 | Effect.gen(function* () { |
| 105 | if (explicit !== undefined && explicit.length > 0) { |
| 106 | return normalizeHierarchyParent(explicit); |
| 107 | } |
| 108 | if (existing !== undefined && existing.length > 0) { |
| 109 | return normalizeHierarchyParent(existing); |
| 110 | } |
| 111 | const resolved = yield* tryResolveParent(); |
| 112 | if (resolved === undefined) { |
| 113 | const env = yield* GcpEnvironment.current; |
| 114 | return yield* new ParentRequired({ project: env.project }); |
| 115 | } |
| 116 | return resolved; |
| 117 | }); |
| 118 | |
| 119 | export const projectNumberOf = (project: string) => |
| 120 | resourcemanager.getProjects({ name: `projects/${project}` }).pipe( |
| 121 | Effect.map((resource) => { |
| 122 | const number = lastSegment(resource.name ?? ""); |
| 123 | return /^\d+$/.test(number) ? number : project; |
| 124 | }), |
| 125 | Effect.catchTag("NotFound", () => Effect.succeed(project)), |
| 126 | ); |
| 127 | |
| 128 | export const sortedStrings = (values: readonly string[] | undefined) => |
| 129 | [...(values ?? [])].slice().sort(); |
| 130 | |
| 131 | export const sameStringList = ( |
| 132 | left: readonly string[] | undefined, |
| 133 | right: readonly string[] | undefined, |
| 134 | ) => JSON.stringify(sortedStrings(left)) === JSON.stringify(sortedStrings(right)); |
| 135 | |
| 136 | export const collectPages = <Page, A, E, R>( |
| 137 | pages: Stream.Stream<Page, E, R>, |
| 138 | items: (page: Page) => readonly A[] | undefined, |
| 139 | ) => |
| 140 | pages.pipe( |
| 141 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 142 | Stream.runCollect, |
| 143 | Effect.map((chunk) => Array.from(chunk)), |
| 144 | ); |
| 145 | |
| 146 | /** |
| 147 | * Wait for a Resource Manager operation; project and folder operations |
| 148 | * settle within a minute or two. With `allowAlreadyExists`, ALREADY_EXISTS |
| 149 | * (code 6) counts as success (create race); with `notFoundOk`, so does |
| 150 | * NOT_FOUND (code 5, delete race) and an operation that is already gone. |
| 151 | */ |
| 152 | export const waitForOperation = ( |
| 153 | operation: resourcemanager.Operation, |
| 154 | options?: { notFoundOk?: boolean; allowAlreadyExists?: boolean }, |
| 155 | ) => |
| 156 | waitForGcpOperation(operation, (name) => resourcemanager.getOperations({ name }), { |
| 157 | budget: "10 minutes", |
| 158 | }).pipe( |
| 159 | Effect.catchIf( |
| 160 | (error) => |
| 161 | (error._tag === "GCP.OperationFailed" && |
| 162 | ((options?.allowAlreadyExists === true && error.code === 6) || |
| 163 | (options?.notFoundOk === true && error.code === 5))) || |
| 164 | (options?.notFoundOk === true && error._tag === "NotFound"), |
| 165 | () => Effect.succeed(operation), |
| 166 | ), |
| 167 | ); |
| 168 | |
| 169 | /** |
| 170 | * Wait for a create operation, then re-read it for its typed `response` |
| 171 | * (the created resource's name / project id). |
| 172 | */ |
| 173 | export const waitForCreate = (operation: resourcemanager.Operation) => |
| 174 | waitForOperation(operation, { allowAlreadyExists: true }).pipe( |
| 175 | Effect.andThen(() => |
| 176 | operation.name === undefined || operation.name.length === 0 |
| 177 | ? Effect.succeed(operation) |
| 178 | : resourcemanager.getOperations({ name: operation.name }), |
| 179 | ), |
| 180 | ); |