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 = "ipamAdminScopes"; |
| 33 | const DEFAULT_PLATFORM: cnr.IpamAdminScopeEnabledAddonPlatformsItemEnum = "COMPUTE_ENGINE"; |
| 34 | |
| 35 | export type IpamAdminScopeProps = { |
| 36 | /** |
| 37 | * IPAM admin scope id (the `{ipamAdminScope}` segment of |
| 38 | * `projects/{project}/locations/{location}/ipamAdminScopes/{ipamAdminScope}`). |
| 39 | * If omitted, a unique RFC1035 name is generated. Immutable — |
| 40 | * changing it replaces the scope. Only one admin scope may exist per |
| 41 | * organization. |
| 42 | */ |
| 43 | ipamAdminScopeId?: string; |
| 44 | /** |
| 45 | * Location of the scope. Cloud Number Registry is global — `global` |
| 46 | * is the only supported value. Immutable — changing it replaces the |
| 47 | * scope. |
| 48 | * @default "global" |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * Administrative scopes enabled for discovery. Preview allows a |
| 53 | * single organization (`organizations/{org}`). Immutable — changing |
| 54 | * it replaces the scope. |
| 55 | */ |
| 56 | scopes: string[]; |
| 57 | /** |
| 58 | * Platforms whose IP addresses Cloud Number Registry discovers. |
| 59 | * @default ["COMPUTE_ENGINE"] |
| 60 | */ |
| 61 | enabledAddonPlatforms?: Array<cnr.IpamAdminScopeEnabledAddonPlatformsItemEnum | (string & {})>; |
| 62 | /** |
| 63 | * User labels. Alchemy ownership labels are merged in automatically. |
| 64 | */ |
| 65 | labels?: Record<string, string>; |
| 66 | }; |
| 67 | |
| 68 | export type IpamAdminScope = Resource< |
| 69 | "GCP.CloudNumberRegistry.IpamAdminScope", |
| 70 | IpamAdminScopeProps, |
| 71 | { |
| 72 | /** Full resource name `projects/{project}/locations/{location}/ipamAdminScopes/{ipamAdminScope}`. */ |
| 73 | name: string; |
| 74 | /** IPAM admin scope id (last path segment). */ |
| 75 | ipamAdminScopeId: string; |
| 76 | /** Project id. */ |
| 77 | project: string; |
| 78 | /** Location id of the resource. */ |
| 79 | location: string; |
| 80 | /** Administrative scopes. */ |
| 81 | scopes: string[]; |
| 82 | /** Enabled discovery platforms. */ |
| 83 | enabledAddonPlatforms: string[]; |
| 84 | /** User labels (Alchemy ownership labels stripped). */ |
| 85 | labels: Record<string, string>; |
| 86 | /** Discovery pipeline state (`SETUP_IN_PROGRESS`, `READY_FOR_USE`, …). */ |
| 87 | state: string | undefined; |
| 88 | /** RFC3339 creation timestamp. */ |
| 89 | createTime: string | undefined; |
| 90 | /** RFC3339 last-update timestamp. */ |
| 91 | updateTime: string | undefined; |
| 92 | }, |
| 93 | never, |
| 94 | Providers |
| 95 | >; |
| 96 | |
| 97 | /** |
| 98 | * A Cloud Number Registry IPAM admin scope — the organization-wide |
| 99 | * boundary for IP address discovery. Only one project in an |
| 100 | * organization may own the admin scope. Creating it also creates a |
| 101 | * default registry book and starts discovery (up to 24 hours). |
| 102 | * |
| 103 | * `ipamAdminScopeId`, `location`, and `scopes` replace the resource. |
| 104 | * Platforms and labels update in place. |
| 105 | * |
| 106 | * ### Creating an IPAM Admin Scope |
| 107 | * **Example:** Organization scope |
| 108 | * ```typescript |
| 109 | * const scope = yield* GCP.CloudNumberRegistry.IpamAdminScope("Org", { |
| 110 | * scopes: ["organizations/1234567890"], |
| 111 | * enabledAddonPlatforms: ["COMPUTE_ENGINE"], |
| 112 | * labels: { env: "prod" }, |
| 113 | * }); |
| 114 | * ``` |
| 115 | * |
| 116 | * ### Updating an IPAM Admin Scope |
| 117 | * **Example:** Labels |
| 118 | * ```typescript |
| 119 | * const scope = yield* GCP.CloudNumberRegistry.IpamAdminScope("Org", { |
| 120 | * scopes: ["organizations/1234567890"], |
| 121 | * labels: { env: "prod", role: "ipam" }, |
| 122 | * }); |
| 123 | * ``` |
| 124 | * |
| 125 | * @resource |
| 126 | * @category CloudNumberRegistry |
| 127 | */ |
| 128 | export const IpamAdminScope = Resource<IpamAdminScope>("GCP.CloudNumberRegistry.IpamAdminScope"); |
| 129 | |
| 130 | const expandScopes = (scopes: readonly string[]) => |
| 131 | scopes.map((scope) => (scope.includes("/") ? scope : `organizations/${scope}`)); |
| 132 | |
| 133 | const toAttrs = (item: cnr.IpamAdminScope, project: string) => { |
| 134 | const name = item.name ?? ""; |
| 135 | const parsed = parseName(name, COLLECTION); |
| 136 | return { |
| 137 | name, |
| 138 | ipamAdminScopeId: parsed.id, |
| 139 | project: parsed.project || project, |
| 140 | location: parsed.location || DEFAULT_LOCATION, |
| 141 | scopes: item.scopes ?? [], |
| 142 | enabledAddonPlatforms: (item.enabledAddonPlatforms ?? []).map((value) => String(value)), |
| 143 | labels: userLabels(item.labels), |
| 144 | state: item.state, |
| 145 | createTime: item.createTime, |
| 146 | updateTime: item.updateTime, |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | const getByName = (name: string) => |
| 151 | name.length === 0 |
| 152 | ? Effect.undefined |
| 153 | : cnr |
| 154 | .getProjectsLocationsIpamAdminScopes({ name }) |
| 155 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 156 | |
| 157 | const listOwned = (project: string) => |
| 158 | listAtLocation(project, (parent) => |
| 159 | listLabeledPages( |
| 160 | cnr.listProjectsLocationsIpamAdminScopes.pages({ |
| 161 | parent, |
| 162 | pageSize: 1000, |
| 163 | }), |
| 164 | (page) => page.ipamAdminScopes, |
| 165 | (item) => item.labels, |
| 166 | ), |
| 167 | ); |
| 168 | |
| 169 | export const IpamAdminScopeProvider = () => |
| 170 | Provider.succeed(IpamAdminScope, { |
| 171 | stables: ["name", "ipamAdminScopeId", "project", "location", "scopes", "createTime"], |
| 172 | |
| 173 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 174 | if (!isResolved(news)) return undefined; |
| 175 | const previousScopes = olds?.scopes ?? output?.scopes; |
| 176 | const nextScopes = expandScopes(news.scopes); |
| 177 | return replaceOnIdentity({ |
| 178 | previousId: olds?.ipamAdminScopeId ?? output?.ipamAdminScopeId, |
| 179 | nextId: news.ipamAdminScopeId ?? olds?.ipamAdminScopeId ?? output?.ipamAdminScopeId, |
| 180 | previousLocation: normalizeLocation(olds?.location ?? output?.location), |
| 181 | nextLocation: normalizeLocation(news.location ?? olds?.location ?? output?.location), |
| 182 | extra: previousScopes !== undefined && !sameStringList(previousScopes, nextScopes), |
| 183 | }); |
| 184 | }), |
| 185 | |
| 186 | read: Effect.fn(function* ({ id, olds, output }) { |
| 187 | const env = yield* GcpEnvironment.current; |
| 188 | const ipamAdminScopeId = yield* toPhysicalId( |
| 189 | id, |
| 190 | olds?.ipamAdminScopeId, |
| 191 | output?.ipamAdminScopeId, |
| 192 | "scope", |
| 193 | ); |
| 194 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 195 | const name = |
| 196 | output?.name ?? resourceName(env.project, location, COLLECTION, ipamAdminScopeId); |
| 197 | const existing = yield* getByName(name); |
| 198 | if (existing === undefined) return undefined; |
| 199 | const attrs = toAttrs(existing, env.project); |
| 200 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 201 | }), |
| 202 | |
| 203 | list: () => |
| 204 | Effect.gen(function* () { |
| 205 | const env = yield* GcpEnvironment.current; |
| 206 | const items = yield* listOwned(env.project); |
| 207 | return items.map((item) => toAttrs(item, env.project)); |
| 208 | }), |
| 209 | |
| 210 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 211 | const env = yield* GcpEnvironment.current; |
| 212 | const ipamAdminScopeId = yield* toPhysicalId( |
| 213 | id, |
| 214 | news.ipamAdminScopeId, |
| 215 | output?.ipamAdminScopeId, |
| 216 | "scope", |
| 217 | ); |
| 218 | const location = normalizeLocation(news.location ?? output?.location); |
| 219 | const name = resourceName(env.project, location, COLLECTION, ipamAdminScopeId); |
| 220 | const desiredLabels = { |
| 221 | ...toLabels(news.labels), |
| 222 | ...(yield* createInternalLabels(id)), |
| 223 | }; |
| 224 | const scopes = expandScopes(news.scopes); |
| 225 | const enabledAddonPlatforms = news.enabledAddonPlatforms ?? [DEFAULT_PLATFORM]; |
| 226 | |
| 227 | let current = yield* getByName(output?.name ?? name); |
| 228 | |
| 229 | if (current === undefined) { |
| 230 | const created = yield* cnr |
| 231 | .createProjectsLocationsIpamAdminScopes({ |
| 232 | parent: parentOf(env.project, location), |
| 233 | ipamAdminScopeId, |
| 234 | body: { |
| 235 | scopes, |
| 236 | enabledAddonPlatforms, |
| 237 | labels: desiredLabels, |
| 238 | }, |
| 239 | }) |
| 240 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 241 | if (created !== undefined) { |
| 242 | yield* waitForOperation(created); |
| 243 | } |
| 244 | current = yield* waitUntilExists(getByName(name), name); |
| 245 | } |
| 246 | |
| 247 | if (current === undefined) { |
| 248 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 249 | } |
| 250 | |
| 251 | const observedLabels = tagRecord(current.labels); |
| 252 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 253 | const mask = fieldMask([ |
| 254 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 255 | news.enabledAddonPlatforms !== undefined && |
| 256 | !sameStringList( |
| 257 | current.enabledAddonPlatforms, |
| 258 | enabledAddonPlatforms.map((value) => String(value)), |
| 259 | ) && |
| 260 | "enabledAddonPlatforms", |
| 261 | ]); |
| 262 | |
| 263 | if (mask.length > 0) { |
| 264 | const operation = yield* cnr.patchProjectsLocationsIpamAdminScopes({ |
| 265 | name: current.name ?? name, |
| 266 | updateMask: mask, |
| 267 | body: { |
| 268 | name: current.name ?? name, |
| 269 | labels: desiredLabels, |
| 270 | enabledAddonPlatforms, |
| 271 | }, |
| 272 | }); |
| 273 | yield* waitForOperation(operation); |
| 274 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 275 | } |
| 276 | |
| 277 | return toAttrs(current, env.project); |
| 278 | }), |
| 279 | |
| 280 | delete: Effect.fn(function* ({ output }) { |
| 281 | const disable = yield* cnr |
| 282 | .disableProjectsLocationsIpamAdminScopes({ |
| 283 | name: output.name, |
| 284 | body: {}, |
| 285 | }) |
| 286 | .pipe(Effect.catchTag(["NotFound", "BadRequest", "Conflict"], () => Effect.undefined)); |
| 287 | if (disable !== undefined) { |
| 288 | yield* waitForDeleteOperation(disable); |
| 289 | } |
| 290 | |
| 291 | const operation = yield* cnr |
| 292 | .deleteProjectsLocationsIpamAdminScopes({ |
| 293 | name: output.name, |
| 294 | force: true, |
| 295 | }) |
| 296 | .pipe( |
| 297 | Effect.retry({ |
| 298 | while: (error) => error._tag === "Conflict", |
| 299 | times: 8, |
| 300 | schedule: Schedule.spaced("2 seconds"), |
| 301 | }), |
| 302 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 303 | ); |
| 304 | if (operation !== undefined) { |
| 305 | yield* waitForDeleteOperation(operation); |
| 306 | } |
| 307 | yield* waitUntilGone(getByName(output.name), output.name); |
| 308 | }), |
| 309 | }); |