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 dataplex from "@distilled.cloud/gcp/dataplex_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DataplexNotResolved, |
| 13 | collectPages, |
| 14 | expandParent, |
| 15 | hasAlchemyLabelMap, |
| 16 | lastSegment, |
| 17 | listAtLocation, |
| 18 | normalizeLocation, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | retryQuota, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitUntilExists, |
| 25 | waitUntilGone, |
| 26 | } from "./internal.ts"; |
| 27 | |
| 28 | export type GlossariesCategoryProps = { |
| 29 | /** |
| 30 | * Parent Glossary. Full name |
| 31 | * `projects/{project}/locations/{location}/glossaries/{glossary}` or |
| 32 | * the glossary id (combined with `location`). Immutable — changing it |
| 33 | * replaces the category. |
| 34 | */ |
| 35 | glossary: string; |
| 36 | /** |
| 37 | * Category id (the `{category}` segment of |
| 38 | * `.../glossaries/{glossary}/categories/{category}`). If omitted, a |
| 39 | * unique name is generated from the stack, stage, and logical id. |
| 40 | * Must be 1-63 characters, start with a letter, and match |
| 41 | * `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable — changing it replaces the |
| 42 | * category. |
| 43 | */ |
| 44 | categoryId?: string; |
| 45 | /** |
| 46 | * Region used when `glossary` is a bare id. Immutable — changing it |
| 47 | * replaces the category. |
| 48 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * Immediate parent in the glossary hierarchy — the glossary itself |
| 53 | * or another category. Defaults to the parent glossary. |
| 54 | */ |
| 55 | parent?: string; |
| 56 | /** |
| 57 | * Human-readable description. |
| 58 | */ |
| 59 | description?: string; |
| 60 | /** |
| 61 | * User-friendly display name. |
| 62 | */ |
| 63 | displayName?: string; |
| 64 | /** |
| 65 | * User labels. Alchemy ownership labels are merged in automatically. |
| 66 | */ |
| 67 | labels?: Record<string, string>; |
| 68 | }; |
| 69 | |
| 70 | export type GlossariesCategory = Resource< |
| 71 | "GCP.Dataplex.GlossariesCategory", |
| 72 | GlossariesCategoryProps, |
| 73 | { |
| 74 | /** Full resource name. */ |
| 75 | name: string; |
| 76 | /** Category id (last path segment). */ |
| 77 | categoryId: string; |
| 78 | /** Parent glossary resource name. */ |
| 79 | glossary: string; |
| 80 | /** Parent glossary id. */ |
| 81 | glossaryId: string; |
| 82 | /** Immediate hierarchy parent. */ |
| 83 | parent: string | undefined; |
| 84 | /** Project id. */ |
| 85 | project: string; |
| 86 | /** Location id. */ |
| 87 | location: string; |
| 88 | /** User-provided description. */ |
| 89 | description: string | undefined; |
| 90 | /** User-friendly display name. */ |
| 91 | displayName: string | undefined; |
| 92 | /** User labels (Alchemy ownership labels stripped). */ |
| 93 | labels: Record<string, string>; |
| 94 | /** System-generated uid. */ |
| 95 | uid: string | undefined; |
| 96 | /** RFC3339 creation timestamp. */ |
| 97 | createTime: string | undefined; |
| 98 | /** RFC3339 last-update timestamp. */ |
| 99 | updateTime: string | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | >; |
| 104 | |
| 105 | /** |
| 106 | * A Dataplex Glossary Category nested under a Glossary. |
| 107 | * |
| 108 | * Changing `categoryId`, `glossary`, or `location` replaces the |
| 109 | * category. Description, display name, labels, and hierarchy `parent` |
| 110 | * update in place. |
| 111 | * |
| 112 | * ### Creating a Glossary Category |
| 113 | * **Example:** Category under a glossary |
| 114 | * ```typescript |
| 115 | * const category = yield* GCP.Dataplex.GlossariesCategory("Finance", { |
| 116 | * glossary: glossary.name, |
| 117 | * displayName: "Finance", |
| 118 | * labels: { env: "prod" }, |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * ### Updating a Glossary Category |
| 123 | * **Example:** Description and labels |
| 124 | * ```typescript |
| 125 | * // Same logical id as before; only the changed props differ. |
| 126 | * const category = yield* GCP.Dataplex.GlossariesCategory("Finance", { |
| 127 | * glossary: glossary.name, |
| 128 | * description: "finance terms", |
| 129 | * labels: { env: "prod", team: "data" }, |
| 130 | * }); |
| 131 | * ``` |
| 132 | * |
| 133 | * @resource |
| 134 | * @category Dataplex |
| 135 | */ |
| 136 | export const GlossariesCategory = Resource<GlossariesCategory>("GCP.Dataplex.GlossariesCategory"); |
| 137 | |
| 138 | const resolveParent = ( |
| 139 | project: string, |
| 140 | glossary: string, |
| 141 | location: string | undefined, |
| 142 | defaultLocation: string, |
| 143 | ) => { |
| 144 | const parent = expandParent( |
| 145 | glossary, |
| 146 | project, |
| 147 | normalizeLocation(location, defaultLocation), |
| 148 | "glossaries", |
| 149 | ); |
| 150 | const parsed = parseName(`${parent}/categories/_`, "categories"); |
| 151 | return { |
| 152 | parent: parsed.parent, |
| 153 | location: parsed.location, |
| 154 | project: parsed.project || project, |
| 155 | glossaryId: lastSegment(parsed.parent), |
| 156 | }; |
| 157 | }; |
| 158 | |
| 159 | const resourceName = (parent: string, categoryId: string) => `${parent}/categories/${categoryId}`; |
| 160 | |
| 161 | const toAttrs = (category: dataplex.GoogleCloudDataplexV1GlossaryCategory, project: string) => { |
| 162 | const name = category.name ?? ""; |
| 163 | const parsed = parseName(name, "categories"); |
| 164 | const glossary = parseName(parsed.parent, "glossaries"); |
| 165 | return { |
| 166 | name, |
| 167 | categoryId: parsed.id, |
| 168 | glossary: parsed.parent, |
| 169 | glossaryId: glossary.id, |
| 170 | parent: category.parent, |
| 171 | project: parsed.project || project, |
| 172 | location: parsed.location, |
| 173 | description: category.description, |
| 174 | displayName: category.displayName, |
| 175 | labels: userLabels(category.labels), |
| 176 | uid: category.uid, |
| 177 | createTime: category.createTime, |
| 178 | updateTime: category.updateTime, |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | const getByName = (name: string) => |
| 183 | retryQuota(dataplex.getProjectsLocationsGlossariesCategories({ name })).pipe( |
| 184 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 185 | ); |
| 186 | |
| 187 | const listCategoriesUnder = (parent: string, project: string) => |
| 188 | collectPages( |
| 189 | dataplex.listProjectsLocationsGlossariesCategories.pages({ |
| 190 | parent, |
| 191 | pageSize: 1000, |
| 192 | }), |
| 193 | (page) => page.categories, |
| 194 | ).pipe( |
| 195 | Effect.map((items) => |
| 196 | items.filter((item) => hasAlchemyLabelMap(item.labels)).map((item) => toAttrs(item, project)), |
| 197 | ), |
| 198 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 199 | ); |
| 200 | |
| 201 | export const GlossariesCategoryProvider = () => |
| 202 | Provider.succeed(GlossariesCategory, { |
| 203 | stables: ["name", "categoryId", "glossary", "glossaryId", "project", "location", "createTime"], |
| 204 | |
| 205 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 206 | if (!isResolved(news)) return undefined; |
| 207 | const env = yield* GcpEnvironment.current; |
| 208 | return replaceOnIdentity({ |
| 209 | previousId: olds?.categoryId ?? output?.categoryId, |
| 210 | nextId: news.categoryId ?? olds?.categoryId ?? output?.categoryId, |
| 211 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 212 | nextLocation: normalizeLocation( |
| 213 | news.location ?? olds?.location ?? output?.location, |
| 214 | env.region, |
| 215 | ), |
| 216 | previousParent: olds?.glossary ?? output?.glossary, |
| 217 | nextParent: news.glossary ?? olds?.glossary ?? output?.glossary, |
| 218 | }); |
| 219 | }), |
| 220 | |
| 221 | read: Effect.fn(function* ({ id, olds, output }) { |
| 222 | const env = yield* GcpEnvironment.current; |
| 223 | const resolved = resolveParent( |
| 224 | env.project, |
| 225 | olds?.glossary ?? output?.glossary ?? "", |
| 226 | olds?.location ?? output?.location, |
| 227 | env.region, |
| 228 | ); |
| 229 | const categoryId = yield* toPhysicalId(id, olds?.categoryId, output?.categoryId, "category"); |
| 230 | const name = output?.name ?? resourceName(resolved.parent, categoryId); |
| 231 | const existing = yield* getByName(name); |
| 232 | if (existing === undefined) return undefined; |
| 233 | const attrs = toAttrs(existing, env.project); |
| 234 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 235 | }), |
| 236 | |
| 237 | list: () => |
| 238 | Effect.gen(function* () { |
| 239 | const env = yield* GcpEnvironment.current; |
| 240 | const glossaries = yield* listAtLocation(env.project, env.region, (parent) => |
| 241 | collectPages( |
| 242 | dataplex.listProjectsLocationsGlossaries.pages({ |
| 243 | parent, |
| 244 | pageSize: 1000, |
| 245 | }), |
| 246 | (page) => page.glossaries, |
| 247 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))), |
| 248 | ); |
| 249 | const nested = yield* Effect.forEach( |
| 250 | glossaries, |
| 251 | (glossary) => |
| 252 | glossary.name ? listCategoriesUnder(glossary.name, env.project) : Effect.succeed([]), |
| 253 | { concurrency: 4 }, |
| 254 | ); |
| 255 | return nested.flat(); |
| 256 | }), |
| 257 | |
| 258 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 259 | const env = yield* GcpEnvironment.current; |
| 260 | const resolved = resolveParent( |
| 261 | env.project, |
| 262 | news.glossary, |
| 263 | news.location ?? output?.location, |
| 264 | env.region, |
| 265 | ); |
| 266 | const categoryId = yield* toPhysicalId(id, news.categoryId, output?.categoryId, "category"); |
| 267 | const name = resourceName(resolved.parent, categoryId); |
| 268 | const hierarchyParent = news.parent ?? resolved.parent; |
| 269 | const desiredLabels = { |
| 270 | ...toLabels(news.labels), |
| 271 | ...(yield* createInternalLabels(id)), |
| 272 | }; |
| 273 | |
| 274 | let current = yield* getByName(output?.name ?? name); |
| 275 | |
| 276 | if (current === undefined) { |
| 277 | const created = yield* dataplex |
| 278 | .createProjectsLocationsGlossariesCategories({ |
| 279 | parent: resolved.parent, |
| 280 | categoryId, |
| 281 | body: { |
| 282 | parent: hierarchyParent, |
| 283 | description: news.description, |
| 284 | displayName: news.displayName, |
| 285 | labels: desiredLabels, |
| 286 | }, |
| 287 | }) |
| 288 | .pipe( |
| 289 | retryQuota, |
| 290 | Effect.catchTag("Conflict", () => getByName(name)), |
| 291 | ); |
| 292 | current = created ?? undefined; |
| 293 | if (current === undefined) { |
| 294 | current = yield* waitUntilExists(getByName(name), name); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (current === undefined) { |
| 299 | return yield* new DataplexNotResolved({ resourceName: name }); |
| 300 | } |
| 301 | |
| 302 | const observedLabels = tagRecord(current.labels); |
| 303 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 304 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 305 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 306 | const displayNameChanged = (current.displayName ?? "") !== (news.displayName ?? ""); |
| 307 | const parentChanged = (current.parent ?? "") !== hierarchyParent; |
| 308 | |
| 309 | if (labelsChanged || descriptionChanged || displayNameChanged || parentChanged) { |
| 310 | current = yield* retryQuota( |
| 311 | dataplex.patchProjectsLocationsGlossariesCategories({ |
| 312 | name: current.name ?? name, |
| 313 | updateMask: [ |
| 314 | labelsChanged ? "labels" : undefined, |
| 315 | descriptionChanged ? "description" : undefined, |
| 316 | displayNameChanged ? "displayName" : undefined, |
| 317 | parentChanged ? "parent" : undefined, |
| 318 | ] |
| 319 | .filter((field): field is string => field !== undefined) |
| 320 | .join(","), |
| 321 | body: { |
| 322 | name: current.name ?? name, |
| 323 | parent: hierarchyParent, |
| 324 | labels: desiredLabels, |
| 325 | description: news.description, |
| 326 | displayName: news.displayName, |
| 327 | }, |
| 328 | }), |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | return toAttrs(current, env.project); |
| 333 | }), |
| 334 | |
| 335 | delete: Effect.fn(function* ({ output }) { |
| 336 | yield* retryQuota( |
| 337 | dataplex.deleteProjectsLocationsGlossariesCategories({ |
| 338 | name: output.name, |
| 339 | }), |
| 340 | ).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 341 | yield* waitUntilGone(getByName(output.name), output.name); |
| 342 | }), |
| 343 | }); |