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 apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | createInternalLabels, |
| 14 | encodeOwnership, |
| 15 | hasAlchemyLabels, |
| 16 | hasOwnershipMarker, |
| 17 | lastSegment, |
| 18 | orgIdOf, |
| 19 | orgParent, |
| 20 | parseOwnership, |
| 21 | } from "./ownership.ts"; |
| 22 | |
| 23 | const MAX_ID_LENGTH = 63; |
| 24 | |
| 25 | export type SpaceProps = { |
| 26 | /** |
| 27 | * Space id (the `{space}` segment of `organizations/{org}/spaces/{space}`). |
| 28 | * If omitted, a unique name is generated from the stack, stage, and |
| 29 | * logical id. Must be 1-63 characters, start with a letter, and match |
| 30 | * `[a-z][a-z0-9-]*`. Immutable — changing it replaces the space. |
| 31 | */ |
| 32 | spaceId?: string; |
| 33 | /** |
| 34 | * Apigee organization id. Defaults to the stack GCP project. Immutable — |
| 35 | * changing it replaces the space. |
| 36 | */ |
| 37 | organization?: string; |
| 38 | /** |
| 39 | * Human-readable display name. Spaces have no labels field, so Alchemy |
| 40 | * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is stored |
| 41 | * in a `[alchemy …]` prefix for `list` / nuke. |
| 42 | */ |
| 43 | displayName?: string; |
| 44 | }; |
| 45 | |
| 46 | export type Space = Resource< |
| 47 | "GCP.Apigee.Space", |
| 48 | SpaceProps, |
| 49 | { |
| 50 | /** Full resource name `organizations/{org}/spaces/{space}`. */ |
| 51 | name: string; |
| 52 | /** Space id (last path segment). */ |
| 53 | spaceId: string; |
| 54 | /** Apigee organization id. */ |
| 55 | organization: string; |
| 56 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 57 | displayName: string | undefined; |
| 58 | /** RFC3339 creation timestamp. */ |
| 59 | createTime: string | undefined; |
| 60 | /** RFC3339 last-update timestamp. */ |
| 61 | updateTime: string | undefined; |
| 62 | }, |
| 63 | never, |
| 64 | Providers |
| 65 | >; |
| 66 | |
| 67 | /** |
| 68 | * An Apigee organization space used to group API proxies, shared flows, |
| 69 | * and products for IAM. |
| 70 | * |
| 71 | * Spaces have no labels field, so Alchemy stamps ownership into |
| 72 | * `displayName` for `list` / nuke. `spaceId` and `organization` are |
| 73 | * identity — changing either replaces the space. `displayName` updates |
| 74 | * in place. |
| 75 | * |
| 76 | * ### Creating a Space |
| 77 | * **Example:** Generated id |
| 78 | * ```typescript |
| 79 | * const space = yield* GCP.Apigee.Space("Payments", { |
| 80 | * displayName: "payments team", |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * **Example:** Explicit id |
| 85 | * ```typescript |
| 86 | * const space = yield* GCP.Apigee.Space("Payments", { |
| 87 | * spaceId: "payments", |
| 88 | * displayName: "payments team", |
| 89 | * }); |
| 90 | * ``` |
| 91 | * |
| 92 | * ### Updating a Space |
| 93 | * **Example:** Change the display name |
| 94 | * ```typescript |
| 95 | * const space = yield* GCP.Apigee.Space("Payments", { |
| 96 | * displayName: "payments and billing", |
| 97 | * }); |
| 98 | * ``` |
| 99 | * |
| 100 | * @resource |
| 101 | * @category Apigee |
| 102 | */ |
| 103 | export const Space = Resource<Space>("GCP.Apigee.Space"); |
| 104 | |
| 105 | export class SpaceNotResolved extends Data.TaggedError("GCP.Apigee.SpaceNotResolved")<{ |
| 106 | resourceName: string; |
| 107 | }> {} |
| 108 | |
| 109 | const rfc1035 = (name: string): string => { |
| 110 | let next = name |
| 111 | .toLowerCase() |
| 112 | .replace(/[^a-z0-9-]/g, "-") |
| 113 | .replace(/-+/g, "-") |
| 114 | .replace(/^-+/, "") |
| 115 | .replace(/-+$/, ""); |
| 116 | if (!/^[a-z]/.test(next)) { |
| 117 | next = `s${next}`; |
| 118 | } |
| 119 | next = next.slice(0, MAX_ID_LENGTH).replace(/-+$/, ""); |
| 120 | return next.length > 0 ? next : "space"; |
| 121 | }; |
| 122 | |
| 123 | const resourceName = (org: string, spaceId: string) => `${orgParent(org)}/spaces/${spaceId}`; |
| 124 | |
| 125 | const spaceIdOf = (space: apigee.GoogleCloudApigeeV1Space) => { |
| 126 | const raw = space.name ?? ""; |
| 127 | return raw.includes("/") ? lastSegment(raw) : raw; |
| 128 | }; |
| 129 | |
| 130 | const toId = (id: string, spaceId: string | undefined, existing?: string) => |
| 131 | Effect.gen(function* () { |
| 132 | if (spaceId !== undefined) return rfc1035(spaceId); |
| 133 | if (existing !== undefined) return existing; |
| 134 | return rfc1035( |
| 135 | yield* createPhysicalName({ |
| 136 | id, |
| 137 | maxLength: MAX_ID_LENGTH, |
| 138 | lowercase: true, |
| 139 | }), |
| 140 | ); |
| 141 | }); |
| 142 | |
| 143 | const toAttrs = (space: apigee.GoogleCloudApigeeV1Space, org: string) => { |
| 144 | const spaceId = spaceIdOf(space); |
| 145 | const parsed = parseOwnership(space.displayName); |
| 146 | const name = space.name ?? (spaceId ? resourceName(org, spaceId) : ""); |
| 147 | return { |
| 148 | name, |
| 149 | spaceId, |
| 150 | organization: orgIdOf(name.startsWith("organizations/") ? name : org, org), |
| 151 | displayName: parsed.text, |
| 152 | createTime: space.createTime, |
| 153 | updateTime: space.updateTime, |
| 154 | }; |
| 155 | }; |
| 156 | |
| 157 | const getByName = (name: string) => |
| 158 | apigee |
| 159 | .getOrganizationsSpaces({ name }) |
| 160 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 161 | |
| 162 | export const SpaceProvider = () => |
| 163 | Provider.succeed(Space, { |
| 164 | stables: ["name", "spaceId", "organization", "createTime"], |
| 165 | |
| 166 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 167 | if (!isResolved(news)) return undefined; |
| 168 | const previousId = olds?.spaceId ?? output?.spaceId; |
| 169 | const idChanged = |
| 170 | previousId !== undefined && |
| 171 | news.spaceId !== undefined && |
| 172 | rfc1035(news.spaceId) !== previousId; |
| 173 | const previousOrg = olds?.organization ?? output?.organization; |
| 174 | const orgChanged = |
| 175 | previousOrg !== undefined && |
| 176 | news.organization !== undefined && |
| 177 | orgIdOf(news.organization, previousOrg) !== previousOrg; |
| 178 | if (idChanged || orgChanged) { |
| 179 | return { action: "replace" as const, deleteFirst: false }; |
| 180 | } |
| 181 | return undefined; |
| 182 | }), |
| 183 | |
| 184 | read: Effect.fn(function* ({ id, olds, output }) { |
| 185 | const env = yield* GcpEnvironment.current; |
| 186 | const org = orgIdOf(olds?.organization ?? output?.organization, env.project); |
| 187 | const spaceId = yield* toId(id, olds?.spaceId, output?.spaceId); |
| 188 | const name = output?.name ?? resourceName(org, spaceId); |
| 189 | const existing = yield* getByName(name); |
| 190 | if (existing === undefined) return undefined; |
| 191 | const attrs = toAttrs(existing, org); |
| 192 | const { labels } = parseOwnership(existing.displayName); |
| 193 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 194 | }), |
| 195 | |
| 196 | list: () => |
| 197 | Effect.gen(function* () { |
| 198 | const env = yield* GcpEnvironment.current; |
| 199 | const org = env.project; |
| 200 | return yield* apigee.listOrganizationsSpaces |
| 201 | .pages({ |
| 202 | parent: orgParent(org), |
| 203 | pageSize: 1000, |
| 204 | }) |
| 205 | .pipe( |
| 206 | Stream.flatMap((page) => Stream.fromIterable(page.spaces ?? [])), |
| 207 | Stream.filter((space) => hasOwnershipMarker(space.displayName)), |
| 208 | Stream.map((space) => toAttrs(space, org)), |
| 209 | Stream.runCollect, |
| 210 | Effect.map((chunk) => Array.from(chunk)), |
| 211 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.succeed([])), |
| 212 | ); |
| 213 | }), |
| 214 | |
| 215 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const org = orgIdOf(news.organization ?? output?.organization, env.project); |
| 218 | const spaceId = yield* toId(id, news.spaceId, output?.spaceId); |
| 219 | const name = resourceName(org, spaceId); |
| 220 | const ownership = yield* createInternalLabels(id); |
| 221 | const desiredDisplayName = encodeOwnership(ownership, news.displayName); |
| 222 | |
| 223 | let current = yield* getByName(output?.name ?? name); |
| 224 | |
| 225 | if (current === undefined) { |
| 226 | const created = yield* apigee |
| 227 | .createOrganizationsSpaces({ |
| 228 | parent: orgParent(org), |
| 229 | spaceId, |
| 230 | body: { |
| 231 | displayName: desiredDisplayName, |
| 232 | }, |
| 233 | }) |
| 234 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 235 | current = created ?? undefined; |
| 236 | } |
| 237 | |
| 238 | if (current === undefined) { |
| 239 | return yield* new SpaceNotResolved({ resourceName: name }); |
| 240 | } |
| 241 | |
| 242 | if ((current.displayName ?? "") !== desiredDisplayName) { |
| 243 | current = yield* apigee.patchOrganizationsSpaces({ |
| 244 | name: current.name ?? name, |
| 245 | updateMask: "display_name", |
| 246 | body: { |
| 247 | displayName: desiredDisplayName, |
| 248 | }, |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | return toAttrs(current, org); |
| 253 | }), |
| 254 | |
| 255 | delete: Effect.fn(function* ({ output }) { |
| 256 | yield* apigee |
| 257 | .deleteOrganizationsSpaces({ name: output.name }) |
| 258 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 259 | }), |
| 260 | }); |