The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as biglake from "@distilled.cloud/gcp/biglake_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { GcpEnvironment } from "../Environment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | BiglakeNotResolved, |
| 11 | listLocations, |
| 12 | MAX_ID_LENGTH, |
| 13 | hasAlchemyLabels, |
| 14 | hasOwnershipMarker, |
| 15 | ignoreGone, |
| 16 | listCatalogs, |
| 17 | listDatabases, |
| 18 | listTables, |
| 19 | locationParent, |
| 20 | missingGet, |
| 21 | namedOf, |
| 22 | normalizeLocation, |
| 23 | parseOwnershipMarker, |
| 24 | parseResourceName, |
| 25 | replaceOnIdentity, |
| 26 | retryTransient, |
| 27 | rfc1035, |
| 28 | toPhysicalId, |
| 29 | waitUntilGone, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | export type CatalogProps = { |
| 33 | /** |
| 34 | * Catalog id (the `{catalog}` segment of |
| 35 | * `projects/{project}/locations/{location}/catalogs/{catalog}`). If |
| 36 | * omitted, a unique id is generated. Immutable — changing it replaces |
| 37 | * the catalog. |
| 38 | */ |
| 39 | catalogId?: string; |
| 40 | /** |
| 41 | * Location (`us-central1`, `US`, …). Immutable — changing it replaces |
| 42 | * the catalog. `US-CENTRAL1` is accepted and normalized to |
| 43 | * `us-central1`. |
| 44 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 45 | */ |
| 46 | location?: string; |
| 47 | }; |
| 48 | |
| 49 | export type Catalog = Resource< |
| 50 | "GCP.BigLake.Catalog", |
| 51 | CatalogProps, |
| 52 | { |
| 53 | /** Full resource name. */ |
| 54 | name: string; |
| 55 | /** Catalog id (last path segment). */ |
| 56 | catalogId: string; |
| 57 | /** Project id. */ |
| 58 | project: string; |
| 59 | /** Location id. */ |
| 60 | location: string; |
| 61 | /** RFC3339 creation timestamp. */ |
| 62 | createTime: string | undefined; |
| 63 | /** RFC3339 last-update timestamp. */ |
| 64 | updateTime: string | undefined; |
| 65 | /** RFC3339 deletion timestamp, if the catalog is soft-deleted. */ |
| 66 | deleteTime: string | undefined; |
| 67 | /** RFC3339 expire timestamp after delete. */ |
| 68 | expireTime: string | undefined; |
| 69 | }, |
| 70 | never, |
| 71 | Providers |
| 72 | >; |
| 73 | |
| 74 | /** |
| 75 | * A BigLake Metastore catalog — the top-level container for Hive |
| 76 | * databases and tables. |
| 77 | * |
| 78 | * Catalogs have no labels, description, or annotations, and Alchemy does |
| 79 | * not write ownership into the catalog IAM policy: generated ids carry an |
| 80 | * `alc_` prefix that `list` / nuke match, and `read` reports an explicit |
| 81 | * id it has no state for as unowned. |
| 82 | * Location and catalog id are immutable. There is nothing else to |
| 83 | * update in place. |
| 84 | * |
| 85 | * ### Creating a Catalog |
| 86 | * **Example:** Generated id |
| 87 | * ```typescript |
| 88 | * const catalog = yield* GCP.BigLake.Catalog("Lake", {}); |
| 89 | * ``` |
| 90 | * |
| 91 | * **Example:** Named catalog in a multi-region |
| 92 | * ```typescript |
| 93 | * const catalog = yield* GCP.BigLake.Catalog("Lake", { |
| 94 | * catalogId: "analytics", |
| 95 | * location: "US", |
| 96 | * }); |
| 97 | * ``` |
| 98 | * |
| 99 | * @resource |
| 100 | * @category BigLake |
| 101 | */ |
| 102 | export const Catalog = Resource<Catalog>("GCP.BigLake.Catalog"); |
| 103 | |
| 104 | const OWNERSHIP_TITLE = "alchemy-ownership"; |
| 105 | const resourceName = (project: string, location: string, catalogId: string) => |
| 106 | `${locationParent(project, location)}/catalogs/${catalogId}`; |
| 107 | |
| 108 | const toAttrs = (catalog: biglake.Catalog, project: string) => { |
| 109 | const name = catalog.name ?? ""; |
| 110 | const parsed = parseResourceName(name, "catalogs"); |
| 111 | return { |
| 112 | name, |
| 113 | catalogId: parsed.id, |
| 114 | project, |
| 115 | location: parsed.location, |
| 116 | createTime: catalog.createTime, |
| 117 | updateTime: catalog.updateTime, |
| 118 | deleteTime: catalog.deleteTime, |
| 119 | expireTime: catalog.expireTime, |
| 120 | }; |
| 121 | }; |
| 122 | |
| 123 | const getByName = missingGet(biglake.getProjectsLocationsCatalogs); |
| 124 | |
| 125 | const policyHasOwnership = (policy: biglake.Policy | undefined) => |
| 126 | (policy?.bindings ?? []).some( |
| 127 | (binding) => |
| 128 | binding.condition?.title === OWNERSHIP_TITLE || |
| 129 | hasOwnershipMarker(binding.condition?.description), |
| 130 | ); |
| 131 | |
| 132 | const getPolicy = (name: string) => |
| 133 | name.length === 0 |
| 134 | ? Effect.undefined |
| 135 | : biglake |
| 136 | .getIamPolicyProjectsCatalogs({ |
| 137 | resource: name, |
| 138 | "options.requestedPolicyVersion": 3, |
| 139 | }) |
| 140 | .pipe(Effect.catchTag(["NotFound", "BigLakeResourceNotFound"], () => Effect.undefined)); |
| 141 | |
| 142 | const toCatalogId = (id: string, explicit: string | undefined, existing: string | undefined) => |
| 143 | Effect.gen(function* () { |
| 144 | if (explicit !== undefined) return explicit; |
| 145 | if (existing !== undefined) return existing; |
| 146 | const generated = yield* toPhysicalId(id, undefined, undefined); |
| 147 | if (generated.startsWith("alc_")) return generated; |
| 148 | return rfc1035(`alc_${generated}`, MAX_ID_LENGTH); |
| 149 | }); |
| 150 | |
| 151 | const lastIdOwned = (name: string | undefined) => { |
| 152 | const id = parseResourceName(name ?? "", "catalogs").id; |
| 153 | return id.startsWith("alc_"); |
| 154 | }; |
| 155 | |
| 156 | const isOwnedCatalog = (catalog: biglake.Catalog, policy?: biglake.Policy) => |
| 157 | policyHasOwnership(policy) || lastIdOwned(catalog.name); |
| 158 | |
| 159 | const ownedFromPolicy = (id: string, policy: biglake.Policy | undefined) => |
| 160 | Effect.gen(function* () { |
| 161 | for (const binding of policy?.bindings ?? []) { |
| 162 | const labels = parseOwnershipMarker(binding.condition?.description); |
| 163 | if (yield* hasAlchemyLabels(id, labels)) return true; |
| 164 | } |
| 165 | return false; |
| 166 | }); |
| 167 | |
| 168 | const emptyCatalog = (name: string) => |
| 169 | Effect.gen(function* () { |
| 170 | const databases = yield* listDatabases(name); |
| 171 | yield* Effect.forEach( |
| 172 | namedOf(databases), |
| 173 | (database) => emptyAndDeleteDatabase(database.name!), |
| 174 | { concurrency: 1 }, |
| 175 | ); |
| 176 | }); |
| 177 | |
| 178 | const emptyAndDeleteDatabase = (name: string) => |
| 179 | Effect.gen(function* () { |
| 180 | const tables = yield* listTables(name); |
| 181 | yield* Effect.forEach( |
| 182 | namedOf(tables), |
| 183 | (table) => |
| 184 | ignoreGone( |
| 185 | biglake.deleteProjectsLocationsCatalogsDatabasesTables({ |
| 186 | name: table.name!, |
| 187 | }), |
| 188 | ), |
| 189 | { concurrency: 4 }, |
| 190 | ); |
| 191 | yield* ignoreGone(biglake.deleteProjectsLocationsCatalogsDatabases({ name })); |
| 192 | }); |
| 193 | |
| 194 | export const CatalogProvider = () => |
| 195 | Provider.succeed(Catalog, { |
| 196 | stables: ["name", "catalogId", "project", "location", "createTime"], |
| 197 | |
| 198 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 199 | const env = yield* GcpEnvironment.current; |
| 200 | if (!isResolved(news)) return undefined; |
| 201 | return replaceOnIdentity({ |
| 202 | previousId: olds?.catalogId ?? output?.catalogId, |
| 203 | nextId: news.catalogId, |
| 204 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 205 | nextLocation: normalizeLocation( |
| 206 | news.location ?? olds?.location ?? output?.location, |
| 207 | env.region, |
| 208 | ), |
| 209 | }); |
| 210 | }), |
| 211 | |
| 212 | read: Effect.fn(function* ({ id, olds, output }) { |
| 213 | const env = yield* GcpEnvironment.current; |
| 214 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 215 | const catalogId = yield* toCatalogId(id, olds?.catalogId, output?.catalogId); |
| 216 | const name = output?.name ?? resourceName(env.project, location, catalogId); |
| 217 | const existing = yield* getByName(name); |
| 218 | if (existing === undefined) return undefined; |
| 219 | const attrs = toAttrs(existing, env.project); |
| 220 | // Catalogs have no labels. A generated `alc_` id derives from this |
| 221 | // stack, stage, logical id and instance; an explicit id is ours when |
| 222 | // state has it or an earlier Alchemy version stamped the IAM policy. |
| 223 | if (output !== undefined || olds?.catalogId === undefined) return attrs; |
| 224 | const policy = yield* getPolicy(existing.name ?? name); |
| 225 | return (yield* ownedFromPolicy(id, policy)) ? attrs : Unowned(attrs); |
| 226 | }), |
| 227 | |
| 228 | list: () => |
| 229 | Effect.gen(function* () { |
| 230 | const env = yield* GcpEnvironment.current; |
| 231 | const catalogs = (yield* Effect.forEach(listLocations(env.region), (location) => |
| 232 | listCatalogs(locationParent(env.project, location)), |
| 233 | )).flat(); |
| 234 | const owned = yield* Effect.forEach( |
| 235 | namedOf(catalogs), |
| 236 | (catalog) => |
| 237 | Effect.gen(function* () { |
| 238 | const policy = yield* getPolicy(catalog.name!); |
| 239 | return isOwnedCatalog(catalog, policy) ? catalog : undefined; |
| 240 | }), |
| 241 | { concurrency: 4 }, |
| 242 | ); |
| 243 | return owned |
| 244 | .filter((item): item is biglake.Catalog => item !== undefined) |
| 245 | .map((item) => toAttrs(item, env.project)); |
| 246 | }), |
| 247 | |
| 248 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 249 | const env = yield* GcpEnvironment.current; |
| 250 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 251 | const catalogId = yield* toCatalogId(id, news.catalogId, output?.catalogId); |
| 252 | const parent = locationParent(env.project, location); |
| 253 | const name = output?.name ?? resourceName(env.project, location, catalogId); |
| 254 | let current = yield* getByName(name); |
| 255 | |
| 256 | if (current === undefined) { |
| 257 | const created = yield* retryTransient( |
| 258 | biglake.createProjectsLocationsCatalogs({ |
| 259 | parent, |
| 260 | catalogId, |
| 261 | body: {}, |
| 262 | }), |
| 263 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 264 | current = created ?? undefined; |
| 265 | } |
| 266 | |
| 267 | if (current === undefined) { |
| 268 | return yield* new BiglakeNotResolved({ resourceName: name }); |
| 269 | } |
| 270 | |
| 271 | return toAttrs(current, env.project); |
| 272 | }), |
| 273 | |
| 274 | delete: Effect.fn(function* ({ output }) { |
| 275 | if (!output.name) return; |
| 276 | yield* emptyCatalog(output.name); |
| 277 | yield* ignoreGone(biglake.deleteProjectsLocationsCatalogs({ name: output.name })); |
| 278 | yield* waitUntilGone(getByName(output.name)); |
| 279 | }), |
| 280 | }); |