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 cnr from "@distilled.cloud/gcp/cloudnumberregistry_v1alpha"; |
| 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 | DEFAULT_LOCATION, |
| 14 | ResourceNotResolved, |
| 15 | fieldMask, |
| 16 | listAtLocation, |
| 17 | listLabeledPages, |
| 18 | normalizeLocation, |
| 19 | parentOf, |
| 20 | parseName, |
| 21 | replaceOnIdentity, |
| 22 | resourceName, |
| 23 | sameStringList, |
| 24 | toPhysicalId, |
| 25 | userLabels, |
| 26 | waitForDeleteOperation, |
| 27 | waitForOperation, |
| 28 | waitUntilExists, |
| 29 | waitUntilGone, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | const COLLECTION = "registryBooks"; |
| 33 | |
| 34 | export type RegistryBookAggregatedData = { |
| 35 | discoveredRangesCount: number | undefined; |
| 36 | uniqueScopesCount: number | undefined; |
| 37 | customRealmsCount: number | undefined; |
| 38 | customRangesCount: number | undefined; |
| 39 | discoveredRealmsCount: number | undefined; |
| 40 | }; |
| 41 | |
| 42 | export type RegistryBookProps = { |
| 43 | /** |
| 44 | * Registry book id (the `{registryBook}` segment of |
| 45 | * `projects/{project}/locations/{location}/registryBooks/{registryBook}`). |
| 46 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 47 | * it replaces the book. |
| 48 | */ |
| 49 | registryBookId?: string; |
| 50 | /** |
| 51 | * Location of the registry book. Cloud Number Registry is global — |
| 52 | * `global` is the only supported value. Immutable — changing it |
| 53 | * replaces the book. |
| 54 | * @default "global" |
| 55 | */ |
| 56 | location?: string; |
| 57 | /** |
| 58 | * Scopes claimed by this book. In Preview only project scopes are |
| 59 | * supported (`projects/{project}`). Each scope can be claimed once. |
| 60 | * Omit to organize only user-managed realms. |
| 61 | */ |
| 62 | claimedScopes?: string[]; |
| 63 | /** |
| 64 | * User labels. Alchemy ownership labels are merged in automatically. |
| 65 | */ |
| 66 | labels?: Record<string, string>; |
| 67 | }; |
| 68 | |
| 69 | export type RegistryBook = Resource< |
| 70 | "GCP.CloudNumberRegistry.RegistryBook", |
| 71 | RegistryBookProps, |
| 72 | { |
| 73 | /** Full resource name `projects/{project}/locations/{location}/registryBooks/{registryBook}`. */ |
| 74 | name: string; |
| 75 | /** Registry book id (last path segment). */ |
| 76 | registryBookId: string; |
| 77 | /** Project id. */ |
| 78 | project: string; |
| 79 | /** Location id of the resource. */ |
| 80 | location: string; |
| 81 | /** Scopes claimed by this book. */ |
| 82 | claimedScopes: string[]; |
| 83 | /** User labels (Alchemy ownership labels stripped). */ |
| 84 | labels: Record<string, string>; |
| 85 | /** Whether this is the default registry book created with the IPAM admin scope. */ |
| 86 | isDefault: boolean | undefined; |
| 87 | /** Aggregated counts, populated when the view is AGGREGATE. */ |
| 88 | aggregatedData: RegistryBookAggregatedData | undefined; |
| 89 | /** RFC3339 creation timestamp. */ |
| 90 | createTime: string | undefined; |
| 91 | /** RFC3339 last-update timestamp. */ |
| 92 | updateTime: string | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | >; |
| 97 | |
| 98 | /** |
| 99 | * A Cloud Number Registry book — a container for IP address management |
| 100 | * information. Creating an IPAM admin scope also creates a default book; |
| 101 | * additional books organize discovered or user-managed realms. |
| 102 | * |
| 103 | * `registryBookId` and `location` replace the resource. `claimedScopes` |
| 104 | * and labels update in place. |
| 105 | * |
| 106 | * ### Creating a Registry Book |
| 107 | * **Example:** Generated name |
| 108 | * ```typescript |
| 109 | * const book = yield* GCP.CloudNumberRegistry.RegistryBook("Inventory", {}); |
| 110 | * ``` |
| 111 | * |
| 112 | * **Example:** Named book that claims a project |
| 113 | * ```typescript |
| 114 | * const book = yield* GCP.CloudNumberRegistry.RegistryBook("Inventory", { |
| 115 | * registryBookId: "inventory", |
| 116 | * claimedScopes: ["projects/my-project"], |
| 117 | * labels: { env: "prod" }, |
| 118 | * }); |
| 119 | * ``` |
| 120 | * |
| 121 | * ### Updating a Registry Book |
| 122 | * **Example:** Labels |
| 123 | * ```typescript |
| 124 | * const book = yield* GCP.CloudNumberRegistry.RegistryBook("Inventory", { |
| 125 | * registryBookId: "inventory", |
| 126 | * labels: { env: "prod", role: "ipam" }, |
| 127 | * }); |
| 128 | * ``` |
| 129 | * |
| 130 | * @resource |
| 131 | * @category CloudNumberRegistry |
| 132 | */ |
| 133 | export const RegistryBook = Resource<RegistryBook>("GCP.CloudNumberRegistry.RegistryBook"); |
| 134 | |
| 135 | const expandScopes = (scopes: readonly string[] | undefined, project: string) => |
| 136 | (scopes ?? []).map((scope) => (scope.includes("/") ? scope : `projects/${scope || project}`)); |
| 137 | |
| 138 | const toAttrs = (item: cnr.RegistryBook, project: string) => { |
| 139 | const name = item.name ?? ""; |
| 140 | const parsed = parseName(name, COLLECTION); |
| 141 | return { |
| 142 | name, |
| 143 | registryBookId: parsed.id, |
| 144 | project: parsed.project || project, |
| 145 | location: parsed.location || DEFAULT_LOCATION, |
| 146 | claimedScopes: item.claimedScopes ?? [], |
| 147 | labels: userLabels(item.labels), |
| 148 | isDefault: item.isDefault, |
| 149 | aggregatedData: item.aggregatedData |
| 150 | ? { |
| 151 | discoveredRangesCount: item.aggregatedData.discoveredRangesCount, |
| 152 | uniqueScopesCount: item.aggregatedData.uniqueScopesCount, |
| 153 | customRealmsCount: item.aggregatedData.customRealmsCount, |
| 154 | customRangesCount: item.aggregatedData.customRangesCount, |
| 155 | discoveredRealmsCount: item.aggregatedData.discoveredRealmsCount, |
| 156 | } |
| 157 | : undefined, |
| 158 | createTime: item.createTime, |
| 159 | updateTime: item.updateTime, |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | const getByName = (name: string) => |
| 164 | name.length === 0 |
| 165 | ? Effect.undefined |
| 166 | : cnr |
| 167 | .getProjectsLocationsRegistryBooks({ name }) |
| 168 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 169 | |
| 170 | const listOwned = (project: string) => |
| 171 | listAtLocation(project, (parent) => |
| 172 | listLabeledPages( |
| 173 | cnr.listProjectsLocationsRegistryBooks.pages({ |
| 174 | parent, |
| 175 | pageSize: 1000, |
| 176 | }), |
| 177 | (page) => page.registryBooks, |
| 178 | (item) => item.labels, |
| 179 | ), |
| 180 | ); |
| 181 | |
| 182 | export const RegistryBookProvider = () => |
| 183 | Provider.succeed(RegistryBook, { |
| 184 | stables: ["name", "registryBookId", "project", "location", "isDefault", "createTime"], |
| 185 | |
| 186 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 187 | if (!isResolved(news)) return undefined; |
| 188 | return replaceOnIdentity({ |
| 189 | previousId: olds?.registryBookId ?? output?.registryBookId, |
| 190 | nextId: news.registryBookId ?? olds?.registryBookId ?? output?.registryBookId, |
| 191 | previousLocation: normalizeLocation(olds?.location ?? output?.location), |
| 192 | nextLocation: normalizeLocation(news.location ?? olds?.location ?? output?.location), |
| 193 | }); |
| 194 | }), |
| 195 | |
| 196 | read: Effect.fn(function* ({ id, olds, output }) { |
| 197 | const env = yield* GcpEnvironment.current; |
| 198 | const registryBookId = yield* toPhysicalId( |
| 199 | id, |
| 200 | olds?.registryBookId, |
| 201 | output?.registryBookId, |
| 202 | "book", |
| 203 | ); |
| 204 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 205 | const name = output?.name ?? resourceName(env.project, location, COLLECTION, registryBookId); |
| 206 | const existing = yield* getByName(name); |
| 207 | if (existing === undefined) return undefined; |
| 208 | const attrs = toAttrs(existing, env.project); |
| 209 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 210 | }), |
| 211 | |
| 212 | list: () => |
| 213 | Effect.gen(function* () { |
| 214 | const env = yield* GcpEnvironment.current; |
| 215 | const items = yield* listOwned(env.project); |
| 216 | return items.map((item) => toAttrs(item, env.project)); |
| 217 | }), |
| 218 | |
| 219 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 220 | const env = yield* GcpEnvironment.current; |
| 221 | const registryBookId = yield* toPhysicalId( |
| 222 | id, |
| 223 | news.registryBookId, |
| 224 | output?.registryBookId, |
| 225 | "book", |
| 226 | ); |
| 227 | const location = normalizeLocation(news.location ?? output?.location); |
| 228 | const name = resourceName(env.project, location, COLLECTION, registryBookId); |
| 229 | const desiredLabels = { |
| 230 | ...toLabels(news.labels), |
| 231 | ...(yield* createInternalLabels(id)), |
| 232 | }; |
| 233 | const claimedScopes = expandScopes(news.claimedScopes, env.project); |
| 234 | |
| 235 | let current = yield* getByName(output?.name ?? name); |
| 236 | |
| 237 | if (current === undefined) { |
| 238 | const created = yield* cnr |
| 239 | .createProjectsLocationsRegistryBooks({ |
| 240 | parent: parentOf(env.project, location), |
| 241 | registryBookId, |
| 242 | body: { |
| 243 | labels: desiredLabels, |
| 244 | claimedScopes: claimedScopes.length > 0 ? claimedScopes : undefined, |
| 245 | }, |
| 246 | }) |
| 247 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 248 | if (created !== undefined) { |
| 249 | yield* waitForOperation(created); |
| 250 | } |
| 251 | current = yield* waitUntilExists(getByName(name), name); |
| 252 | } |
| 253 | |
| 254 | if (current === undefined) { |
| 255 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 256 | } |
| 257 | |
| 258 | const observedLabels = tagRecord(current.labels); |
| 259 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 260 | const mask = fieldMask([ |
| 261 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 262 | news.claimedScopes !== undefined && |
| 263 | !sameStringList(current.claimedScopes, claimedScopes) && |
| 264 | "claimedScopes", |
| 265 | ]); |
| 266 | |
| 267 | if (mask.length > 0) { |
| 268 | const operation = yield* cnr.patchProjectsLocationsRegistryBooks({ |
| 269 | name: current.name ?? name, |
| 270 | updateMask: mask, |
| 271 | body: { |
| 272 | name: current.name ?? name, |
| 273 | labels: desiredLabels, |
| 274 | claimedScopes, |
| 275 | }, |
| 276 | }); |
| 277 | yield* waitForOperation(operation); |
| 278 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 279 | } |
| 280 | |
| 281 | return toAttrs(current, env.project); |
| 282 | }), |
| 283 | |
| 284 | delete: Effect.fn(function* ({ output }) { |
| 285 | const operation = yield* cnr |
| 286 | .deleteProjectsLocationsRegistryBooks({ |
| 287 | name: output.name, |
| 288 | force: true, |
| 289 | }) |
| 290 | .pipe( |
| 291 | Effect.retry({ |
| 292 | while: (error) => error._tag === "Conflict", |
| 293 | times: 8, |
| 294 | schedule: Schedule.spaced("2 seconds"), |
| 295 | }), |
| 296 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 297 | ); |
| 298 | if (operation !== undefined) { |
| 299 | yield* waitForDeleteOperation(operation); |
| 300 | } |
| 301 | yield* waitUntilGone(getByName(output.name), output.name); |
| 302 | }), |
| 303 | }); |