The best way to control Claude Code, Codex and more from any computer or phone
| 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 | expandName, |
| 16 | fieldMask, |
| 17 | fingerprint, |
| 18 | listAtLocation, |
| 19 | listLabeledPages, |
| 20 | normalizeLocation, |
| 21 | parentOf, |
| 22 | parseName, |
| 23 | replaceOnIdentity, |
| 24 | resourceName, |
| 25 | sameRef, |
| 26 | sameText, |
| 27 | toPhysicalId, |
| 28 | userLabels, |
| 29 | waitForDeleteOperation, |
| 30 | waitForOperation, |
| 31 | waitUntilExists, |
| 32 | waitUntilGone, |
| 33 | } from "./internal.ts"; |
| 34 | |
| 35 | const COLLECTION = "customRanges"; |
| 36 | |
| 37 | export type CustomRangeAttribute = { |
| 38 | /** Attribute key. */ |
| 39 | key?: string; |
| 40 | /** Attribute value. */ |
| 41 | value?: string; |
| 42 | }; |
| 43 | |
| 44 | export type CustomRangeProps = { |
| 45 | /** |
| 46 | * Custom range id (the `{customRange}` segment of |
| 47 | * `projects/{project}/locations/{location}/customRanges/{customRange}`). |
| 48 | * If omitted, a unique RFC1035 name is generated. Immutable — |
| 49 | * changing it replaces the range. |
| 50 | */ |
| 51 | customRangeId?: string; |
| 52 | /** |
| 53 | * Location of the range. Cloud Number Registry is global — `global` |
| 54 | * is the only supported value. Immutable — changing it replaces the |
| 55 | * range. |
| 56 | * @default "global" |
| 57 | */ |
| 58 | location?: string; |
| 59 | /** |
| 60 | * Parent realm. Accepts a realm id or a full resource name. Mutually |
| 61 | * exclusive with `parentRange`. Immutable — changing it replaces the |
| 62 | * range. |
| 63 | */ |
| 64 | realm?: string; |
| 65 | /** |
| 66 | * Parent custom range. Accepts a range id or a full resource name. |
| 67 | * Mutually exclusive with `realm` — the child inherits the parent's |
| 68 | * realm. Immutable — changing it replaces the range. |
| 69 | */ |
| 70 | parentRange?: string; |
| 71 | /** |
| 72 | * IPv4 CIDR (for example `10.0.0.0/22`). Mutually exclusive with |
| 73 | * `ipv6CidrRange`. Immutable — changing it replaces the range. |
| 74 | */ |
| 75 | ipv4CidrRange?: string; |
| 76 | /** |
| 77 | * IPv6 CIDR. Mutually exclusive with `ipv4CidrRange`. Immutable — |
| 78 | * changing it replaces the range. |
| 79 | */ |
| 80 | ipv6CidrRange?: string; |
| 81 | /** |
| 82 | * Human-readable description. |
| 83 | */ |
| 84 | description?: string; |
| 85 | /** |
| 86 | * Searchable attributes (`key` / `value` pairs). |
| 87 | */ |
| 88 | attributes?: CustomRangeAttribute[]; |
| 89 | /** |
| 90 | * User labels. Alchemy ownership labels are merged in automatically. |
| 91 | */ |
| 92 | labels?: Record<string, string>; |
| 93 | }; |
| 94 | |
| 95 | export type CustomRange = Resource< |
| 96 | "GCP.CloudNumberRegistry.CustomRange", |
| 97 | CustomRangeProps, |
| 98 | { |
| 99 | /** Full resource name `projects/{project}/locations/{location}/customRanges/{customRange}`. */ |
| 100 | name: string; |
| 101 | /** Custom range id (last path segment). */ |
| 102 | customRangeId: string; |
| 103 | /** Project id. */ |
| 104 | project: string; |
| 105 | /** Location id of the resource. */ |
| 106 | location: string; |
| 107 | /** Parent realm, if this range is attached directly to a realm. */ |
| 108 | realm: string | undefined; |
| 109 | /** Parent custom range, if this is a child range. */ |
| 110 | parentRange: string | undefined; |
| 111 | /** Inherited registry book. */ |
| 112 | registryBook: string | undefined; |
| 113 | /** IPv4 CIDR, if this is an IPv4 range. */ |
| 114 | ipv4CidrRange: string | undefined; |
| 115 | /** IPv6 CIDR, if this is an IPv6 range. */ |
| 116 | ipv6CidrRange: string | undefined; |
| 117 | /** Human-readable description. */ |
| 118 | description: string | undefined; |
| 119 | /** Searchable attributes. */ |
| 120 | attributes: CustomRangeAttribute[]; |
| 121 | /** User labels (Alchemy ownership labels stripped). */ |
| 122 | labels: Record<string, string>; |
| 123 | }, |
| 124 | never, |
| 125 | Providers |
| 126 | >; |
| 127 | |
| 128 | /** |
| 129 | * A user-defined Cloud Number Registry IP range. Custom ranges track |
| 130 | * address space that discovery does not cover (on-prem subnets, and so |
| 131 | * on) and can only be added to user-managed realms. |
| 132 | * |
| 133 | * `customRangeId`, `location`, `realm`, `parentRange`, and the CIDR |
| 134 | * replace the resource. Description, attributes, and labels update in |
| 135 | * place. |
| 136 | * |
| 137 | * ### Creating a Custom Range |
| 138 | * **Example:** Range in a realm |
| 139 | * ```typescript |
| 140 | * const book = yield* GCP.CloudNumberRegistry.RegistryBook("Inventory", {}); |
| 141 | * const realm = yield* GCP.CloudNumberRegistry.Realm("Private", { |
| 142 | * registryBook: book.name, |
| 143 | * }); |
| 144 | * const range = yield* GCP.CloudNumberRegistry.CustomRange("OnPrem", { |
| 145 | * realm: realm.name, |
| 146 | * ipv4CidrRange: "10.0.0.0/22", |
| 147 | * description: "on-prem campus", |
| 148 | * }); |
| 149 | * ``` |
| 150 | * |
| 151 | * **Example:** Child range |
| 152 | * ```typescript |
| 153 | * const child = yield* GCP.CloudNumberRegistry.CustomRange("Building", { |
| 154 | * parentRange: parent.name, |
| 155 | * ipv4CidrRange: "10.0.0.0/24", |
| 156 | * }); |
| 157 | * ``` |
| 158 | * |
| 159 | * ### Updating a Custom Range |
| 160 | * **Example:** Description and labels |
| 161 | * ```typescript |
| 162 | * const range = yield* GCP.CloudNumberRegistry.CustomRange("OnPrem", { |
| 163 | * realm: realm.name, |
| 164 | * ipv4CidrRange: "10.0.0.0/22", |
| 165 | * description: "on-prem campus v2", |
| 166 | * labels: { env: "prod" }, |
| 167 | * }); |
| 168 | * ``` |
| 169 | * |
| 170 | * @resource |
| 171 | * @category CloudNumberRegistry |
| 172 | */ |
| 173 | export const CustomRange = Resource<CustomRange>("GCP.CloudNumberRegistry.CustomRange"); |
| 174 | |
| 175 | const toAttributes = (attributes: readonly cnr.Attribute[] | undefined): CustomRangeAttribute[] => |
| 176 | (attributes ?? []).map((attribute) => ({ |
| 177 | key: attribute.key, |
| 178 | value: attribute.value, |
| 179 | })); |
| 180 | |
| 181 | const toAttrs = (item: cnr.CustomRange, project: string) => { |
| 182 | const name = item.name ?? ""; |
| 183 | const parsed = parseName(name, COLLECTION); |
| 184 | return { |
| 185 | name, |
| 186 | customRangeId: parsed.id, |
| 187 | project: parsed.project || project, |
| 188 | location: parsed.location || DEFAULT_LOCATION, |
| 189 | realm: item.realm, |
| 190 | parentRange: item.parentRange, |
| 191 | registryBook: item.registryBook, |
| 192 | ipv4CidrRange: item.ipv4CidrRange, |
| 193 | ipv6CidrRange: item.ipv6CidrRange, |
| 194 | description: item.description, |
| 195 | attributes: toAttributes(item.attributes), |
| 196 | labels: userLabels(item.labels), |
| 197 | }; |
| 198 | }; |
| 199 | |
| 200 | const getByName = (name: string) => |
| 201 | name.length === 0 |
| 202 | ? Effect.undefined |
| 203 | : cnr |
| 204 | .getProjectsLocationsCustomRanges({ name }) |
| 205 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 206 | |
| 207 | const listOwned = (project: string) => |
| 208 | listAtLocation(project, (parent) => |
| 209 | listLabeledPages( |
| 210 | cnr.listProjectsLocationsCustomRanges.pages({ |
| 211 | parent, |
| 212 | pageSize: 1000, |
| 213 | }), |
| 214 | (page) => page.customRanges, |
| 215 | (item) => item.labels, |
| 216 | ), |
| 217 | ); |
| 218 | |
| 219 | export const CustomRangeProvider = () => |
| 220 | Provider.succeed(CustomRange, { |
| 221 | stables: [ |
| 222 | "name", |
| 223 | "customRangeId", |
| 224 | "project", |
| 225 | "location", |
| 226 | "realm", |
| 227 | "parentRange", |
| 228 | "registryBook", |
| 229 | "ipv4CidrRange", |
| 230 | "ipv6CidrRange", |
| 231 | ], |
| 232 | |
| 233 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 234 | if (!isResolved(news)) return undefined; |
| 235 | const previousRealm = olds?.realm ?? output?.realm; |
| 236 | const previousParent = olds?.parentRange ?? output?.parentRange; |
| 237 | const previousV4 = olds?.ipv4CidrRange ?? output?.ipv4CidrRange; |
| 238 | const nextV4 = news.ipv4CidrRange ?? previousV4; |
| 239 | const previousV6 = olds?.ipv6CidrRange ?? output?.ipv6CidrRange; |
| 240 | const nextV6 = news.ipv6CidrRange ?? previousV6; |
| 241 | return replaceOnIdentity({ |
| 242 | previousId: olds?.customRangeId ?? output?.customRangeId, |
| 243 | nextId: news.customRangeId ?? olds?.customRangeId ?? output?.customRangeId, |
| 244 | previousLocation: normalizeLocation(olds?.location ?? output?.location), |
| 245 | nextLocation: normalizeLocation(news.location ?? olds?.location ?? output?.location), |
| 246 | extra: |
| 247 | (news.realm !== undefined && |
| 248 | previousRealm !== undefined && |
| 249 | !sameRef(previousRealm, news.realm)) || |
| 250 | (news.parentRange !== undefined && |
| 251 | previousParent !== undefined && |
| 252 | !sameRef(previousParent, news.parentRange)) || |
| 253 | (news.realm !== undefined && previousParent !== undefined) || |
| 254 | (news.parentRange !== undefined && previousRealm !== undefined) || |
| 255 | (previousV4 !== undefined && nextV4 !== undefined && previousV4 !== nextV4) || |
| 256 | (previousV6 !== undefined && nextV6 !== undefined && previousV6 !== nextV6), |
| 257 | }); |
| 258 | }), |
| 259 | |
| 260 | read: Effect.fn(function* ({ id, olds, output }) { |
| 261 | const env = yield* GcpEnvironment.current; |
| 262 | const customRangeId = yield* toPhysicalId( |
| 263 | id, |
| 264 | olds?.customRangeId, |
| 265 | output?.customRangeId, |
| 266 | "range", |
| 267 | ); |
| 268 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 269 | const name = output?.name ?? resourceName(env.project, location, COLLECTION, customRangeId); |
| 270 | const existing = yield* getByName(name); |
| 271 | if (existing === undefined) return undefined; |
| 272 | const attrs = toAttrs(existing, env.project); |
| 273 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 274 | }), |
| 275 | |
| 276 | list: () => |
| 277 | Effect.gen(function* () { |
| 278 | const env = yield* GcpEnvironment.current; |
| 279 | const items = yield* listOwned(env.project); |
| 280 | return items.map((item) => toAttrs(item, env.project)); |
| 281 | }), |
| 282 | |
| 283 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 284 | const env = yield* GcpEnvironment.current; |
| 285 | const customRangeId = yield* toPhysicalId( |
| 286 | id, |
| 287 | news.customRangeId, |
| 288 | output?.customRangeId, |
| 289 | "range", |
| 290 | ); |
| 291 | const location = normalizeLocation(news.location ?? output?.location); |
| 292 | const name = resourceName(env.project, location, COLLECTION, customRangeId); |
| 293 | const desiredLabels = { |
| 294 | ...toLabels(news.labels), |
| 295 | ...(yield* createInternalLabels(id)), |
| 296 | }; |
| 297 | const realm = |
| 298 | news.parentRange === undefined && news.realm !== undefined |
| 299 | ? expandName(news.realm, env.project, location, "realms") |
| 300 | : undefined; |
| 301 | const parentRange = |
| 302 | news.parentRange !== undefined |
| 303 | ? expandName(news.parentRange, env.project, location, COLLECTION) |
| 304 | : undefined; |
| 305 | const attributes = news.attributes; |
| 306 | |
| 307 | let current = yield* getByName(output?.name ?? name); |
| 308 | |
| 309 | if (current === undefined) { |
| 310 | const created = yield* cnr |
| 311 | .createProjectsLocationsCustomRanges({ |
| 312 | parent: parentOf(env.project, location), |
| 313 | customRangeId, |
| 314 | body: { |
| 315 | realm, |
| 316 | parentRange, |
| 317 | ipv4CidrRange: news.ipv4CidrRange, |
| 318 | ipv6CidrRange: news.ipv6CidrRange, |
| 319 | description: news.description, |
| 320 | attributes, |
| 321 | labels: desiredLabels, |
| 322 | }, |
| 323 | }) |
| 324 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 325 | if (created !== undefined) { |
| 326 | yield* waitForOperation(created); |
| 327 | } |
| 328 | current = yield* waitUntilExists(getByName(name), name); |
| 329 | } |
| 330 | |
| 331 | if (current === undefined) { |
| 332 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 333 | } |
| 334 | |
| 335 | const observedLabels = tagRecord(current.labels); |
| 336 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 337 | const mask = fieldMask([ |
| 338 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 339 | !sameText(current.description, news.description) && "description", |
| 340 | attributes !== undefined && |
| 341 | fingerprint(toAttributes(current.attributes)) !== fingerprint(attributes) && |
| 342 | "attributes", |
| 343 | ]); |
| 344 | |
| 345 | if (mask.length > 0) { |
| 346 | const operation = yield* cnr.patchProjectsLocationsCustomRanges({ |
| 347 | name: current.name ?? name, |
| 348 | updateMask: mask, |
| 349 | body: { |
| 350 | name: current.name ?? name, |
| 351 | labels: desiredLabels, |
| 352 | description: news.description, |
| 353 | attributes, |
| 354 | }, |
| 355 | }); |
| 356 | yield* waitForOperation(operation); |
| 357 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 358 | } |
| 359 | |
| 360 | return toAttrs(current, env.project); |
| 361 | }), |
| 362 | |
| 363 | delete: Effect.fn(function* ({ output }) { |
| 364 | const operation = yield* cnr |
| 365 | .deleteProjectsLocationsCustomRanges({ |
| 366 | name: output.name, |
| 367 | force: true, |
| 368 | }) |
| 369 | .pipe( |
| 370 | Effect.retry({ |
| 371 | while: (error) => error._tag === "Conflict", |
| 372 | times: 8, |
| 373 | schedule: Schedule.spaced("2 seconds"), |
| 374 | }), |
| 375 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 376 | ); |
| 377 | if (operation !== undefined) { |
| 378 | yield* waitForDeleteOperation(operation); |
| 379 | } |
| 380 | yield* waitUntilGone(getByName(output.name), output.name); |
| 381 | }), |
| 382 | }); |