The best way to control Claude Code, Codex and more from any computer or phone
| 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 * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DataplexNotResolved, |
| 14 | collectPages, |
| 15 | hasAlchemyLabelMap, |
| 16 | listAtLocation, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | retryQuota, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type EntryGroupProps = { |
| 30 | /** |
| 31 | * Entry group id (the `{entryGroup}` segment of |
| 32 | * `projects/{project}/locations/{location}/entryGroups/{entryGroup}`). |
| 33 | * If omitted, a unique name is generated from the stack, stage, and |
| 34 | * logical id. Must be 1-63 characters, start with a letter, and match |
| 35 | * `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable — changing it replaces the |
| 36 | * entry group. |
| 37 | */ |
| 38 | entryGroupId?: string; |
| 39 | /** |
| 40 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 41 | * replaces the entry group. `US-CENTRAL1` is accepted and normalized |
| 42 | * to `us-central1`. |
| 43 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * Human-readable description. |
| 48 | */ |
| 49 | description?: string; |
| 50 | /** |
| 51 | * User-friendly display name. |
| 52 | */ |
| 53 | displayName?: string; |
| 54 | /** |
| 55 | * User labels. Alchemy ownership labels are merged in automatically. |
| 56 | */ |
| 57 | labels?: Record<string, string>; |
| 58 | }; |
| 59 | |
| 60 | export type EntryGroup = Resource< |
| 61 | "GCP.Dataplex.EntryGroup", |
| 62 | EntryGroupProps, |
| 63 | { |
| 64 | /** Full resource name. */ |
| 65 | name: string; |
| 66 | /** Entry group id (last path segment). */ |
| 67 | entryGroupId: string; |
| 68 | /** Project id. */ |
| 69 | project: string; |
| 70 | /** Location id. */ |
| 71 | location: string; |
| 72 | /** User-provided description. */ |
| 73 | description: string | undefined; |
| 74 | /** User-friendly display name. */ |
| 75 | displayName: string | undefined; |
| 76 | /** User labels (Alchemy ownership labels stripped). */ |
| 77 | labels: Record<string, string>; |
| 78 | /** Transfer status for migrated Data Catalog groups. */ |
| 79 | transferStatus: string | undefined; |
| 80 | /** Server etag. */ |
| 81 | etag: string | undefined; |
| 82 | /** System-generated uid. */ |
| 83 | uid: string | undefined; |
| 84 | /** RFC3339 creation timestamp. */ |
| 85 | createTime: string | undefined; |
| 86 | /** RFC3339 last-update timestamp. */ |
| 87 | updateTime: string | undefined; |
| 88 | }, |
| 89 | never, |
| 90 | Providers |
| 91 | >; |
| 92 | |
| 93 | /** |
| 94 | * A Dataplex Universal Catalog Entry Group — a logical grouping of |
| 95 | * Entries. |
| 96 | * |
| 97 | * Changing `entryGroupId` or `location` replaces the group. Description, |
| 98 | * display name, and labels update in place. |
| 99 | * |
| 100 | * ### Creating an Entry Group |
| 101 | * **Example:** Generated name |
| 102 | * ```typescript |
| 103 | * const group = yield* GCP.Dataplex.EntryGroup("Catalog", {}); |
| 104 | * ``` |
| 105 | * |
| 106 | * **Example:** Explicit id and labels |
| 107 | * ```typescript |
| 108 | * const group = yield* GCP.Dataplex.EntryGroup("Catalog", { |
| 109 | * entryGroupId: "app-catalog", |
| 110 | * displayName: "App catalog", |
| 111 | * labels: { env: "prod" }, |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * ### Updating an Entry Group |
| 116 | * **Example:** Description and labels |
| 117 | * ```typescript |
| 118 | * // Same logical id as before; only the changed props differ. |
| 119 | * const group = yield* GCP.Dataplex.EntryGroup("Catalog", { |
| 120 | * entryGroupId: "app-catalog", |
| 121 | * description: "catalog v2", |
| 122 | * labels: { env: "prod", team: "data" }, |
| 123 | * }); |
| 124 | * ``` |
| 125 | * |
| 126 | * @resource |
| 127 | * @category Dataplex |
| 128 | */ |
| 129 | export const EntryGroup = Resource<EntryGroup>("GCP.Dataplex.EntryGroup"); |
| 130 | |
| 131 | const resourceName = (project: string, location: string, entryGroupId: string) => |
| 132 | `projects/${project}/locations/${location}/entryGroups/${entryGroupId}`; |
| 133 | |
| 134 | const toAttrs = (group: dataplex.GoogleCloudDataplexV1EntryGroup, project: string) => { |
| 135 | const name = group.name ?? ""; |
| 136 | const parsed = parseName(name, "entryGroups"); |
| 137 | return { |
| 138 | name, |
| 139 | entryGroupId: parsed.id, |
| 140 | project: parsed.project || project, |
| 141 | location: parsed.location, |
| 142 | description: group.description, |
| 143 | displayName: group.displayName, |
| 144 | labels: userLabels(group.labels), |
| 145 | transferStatus: group.transferStatus, |
| 146 | etag: group.etag, |
| 147 | uid: group.uid, |
| 148 | createTime: group.createTime, |
| 149 | updateTime: group.updateTime, |
| 150 | }; |
| 151 | }; |
| 152 | |
| 153 | const getByName = (name: string) => |
| 154 | retryQuota(dataplex.getProjectsLocationsEntryGroups({ name })).pipe( |
| 155 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 156 | ); |
| 157 | |
| 158 | const listGroups = (project: string, region: string) => { |
| 159 | const collect = (parent: string) => |
| 160 | collectPages( |
| 161 | dataplex.listProjectsLocationsEntryGroups.pages({ |
| 162 | parent, |
| 163 | pageSize: 1000, |
| 164 | }), |
| 165 | (page) => page.entryGroups, |
| 166 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))); |
| 167 | return listAtLocation(project, region, collect); |
| 168 | }; |
| 169 | |
| 170 | export const listAlchemyEntryGroups = (project: string, region: string) => |
| 171 | listGroups(project, region); |
| 172 | |
| 173 | export const EntryGroupProvider = () => |
| 174 | Provider.succeed(EntryGroup, { |
| 175 | stables: ["name", "entryGroupId", "project", "location", "createTime"], |
| 176 | |
| 177 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 178 | if (!isResolved(news)) return undefined; |
| 179 | const env = yield* GcpEnvironment.current; |
| 180 | return replaceOnIdentity({ |
| 181 | previousId: olds?.entryGroupId ?? output?.entryGroupId, |
| 182 | nextId: news.entryGroupId ?? olds?.entryGroupId ?? output?.entryGroupId, |
| 183 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 184 | nextLocation: normalizeLocation( |
| 185 | news.location ?? olds?.location ?? output?.location, |
| 186 | env.region, |
| 187 | ), |
| 188 | }); |
| 189 | }), |
| 190 | |
| 191 | read: Effect.fn(function* ({ id, olds, output }) { |
| 192 | const env = yield* GcpEnvironment.current; |
| 193 | const entryGroupId = yield* toPhysicalId( |
| 194 | id, |
| 195 | olds?.entryGroupId, |
| 196 | output?.entryGroupId, |
| 197 | "entrygroup", |
| 198 | ); |
| 199 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 200 | const name = output?.name ?? resourceName(env.project, location, entryGroupId); |
| 201 | const existing = yield* getByName(name); |
| 202 | if (existing === undefined) return undefined; |
| 203 | const attrs = toAttrs(existing, env.project); |
| 204 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 205 | }), |
| 206 | |
| 207 | list: () => |
| 208 | Effect.gen(function* () { |
| 209 | const env = yield* GcpEnvironment.current; |
| 210 | const items = yield* listGroups(env.project, env.region); |
| 211 | return items.map((item) => toAttrs(item, env.project)); |
| 212 | }), |
| 213 | |
| 214 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 215 | const env = yield* GcpEnvironment.current; |
| 216 | const entryGroupId = yield* toPhysicalId( |
| 217 | id, |
| 218 | news.entryGroupId, |
| 219 | output?.entryGroupId, |
| 220 | "entrygroup", |
| 221 | ); |
| 222 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 223 | const name = resourceName(env.project, location, entryGroupId); |
| 224 | const desiredLabels = { |
| 225 | ...toLabels(news.labels), |
| 226 | ...(yield* createInternalLabels(id)), |
| 227 | }; |
| 228 | |
| 229 | let current = yield* getByName(output?.name ?? name); |
| 230 | |
| 231 | if (current === undefined) { |
| 232 | const created = yield* dataplex |
| 233 | .createProjectsLocationsEntryGroups({ |
| 234 | parent: parentOf(env.project, location), |
| 235 | entryGroupId, |
| 236 | body: { |
| 237 | description: news.description, |
| 238 | displayName: news.displayName, |
| 239 | labels: desiredLabels, |
| 240 | }, |
| 241 | }) |
| 242 | .pipe( |
| 243 | retryQuota, |
| 244 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 245 | ); |
| 246 | if (created !== undefined) { |
| 247 | yield* waitForOperation(created); |
| 248 | } |
| 249 | current = yield* waitUntilExists(getByName(name), name); |
| 250 | } |
| 251 | |
| 252 | if (current === undefined) { |
| 253 | return yield* new DataplexNotResolved({ resourceName: name }); |
| 254 | } |
| 255 | |
| 256 | const observedLabels = tagRecord(current.labels); |
| 257 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 258 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 259 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 260 | const displayNameChanged = (current.displayName ?? "") !== (news.displayName ?? ""); |
| 261 | |
| 262 | if (labelsChanged || descriptionChanged || displayNameChanged) { |
| 263 | const operation = yield* retryQuota( |
| 264 | dataplex.patchProjectsLocationsEntryGroups({ |
| 265 | name: current.name ?? name, |
| 266 | updateMask: [ |
| 267 | labelsChanged ? "labels" : undefined, |
| 268 | descriptionChanged ? "description" : undefined, |
| 269 | displayNameChanged ? "displayName" : undefined, |
| 270 | ] |
| 271 | .filter((field): field is string => field !== undefined) |
| 272 | .join(","), |
| 273 | body: { |
| 274 | name: current.name ?? name, |
| 275 | etag: current.etag, |
| 276 | labels: desiredLabels, |
| 277 | description: news.description, |
| 278 | displayName: news.displayName, |
| 279 | }, |
| 280 | }), |
| 281 | ); |
| 282 | yield* waitForOperation(operation); |
| 283 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 284 | } |
| 285 | |
| 286 | return toAttrs(current, env.project); |
| 287 | }), |
| 288 | |
| 289 | delete: Effect.fn(function* ({ output }) { |
| 290 | const operation = yield* dataplex |
| 291 | .deleteProjectsLocationsEntryGroups({ |
| 292 | name: output.name, |
| 293 | }) |
| 294 | .pipe( |
| 295 | Effect.retry({ |
| 296 | while: (error) => error._tag === "Conflict" || error._tag === "TooManyRequests", |
| 297 | times: 8, |
| 298 | schedule: Schedule.spaced("2 seconds"), |
| 299 | }), |
| 300 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 301 | ); |
| 302 | if (operation !== undefined) { |
| 303 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 304 | } |
| 305 | yield* waitUntilGone(getByName(output.name), output.name); |
| 306 | }), |
| 307 | }); |