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 { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { orgIdOf, orgParent } from "./ownership.ts"; |
| 12 | |
| 13 | const MAX_NAME_LENGTH = 255; |
| 14 | |
| 15 | export type SitesApicategoryProps = { |
| 16 | /** |
| 17 | * Portal site id (the `{site}` segment of |
| 18 | * `organizations/{org}/sites/{site}`). Required. Immutable — changing |
| 19 | * it replaces the category. |
| 20 | */ |
| 21 | siteId: string; |
| 22 | /** |
| 23 | * Apigee organization id. Defaults to the stack GCP project. Immutable — |
| 24 | * changing it replaces the category. |
| 25 | */ |
| 26 | organization?: string; |
| 27 | /** |
| 28 | * Server-assigned category UUID. Set when adopting an existing category. |
| 29 | * Immutable — changing it replaces the category. |
| 30 | */ |
| 31 | categoryId?: string; |
| 32 | /** |
| 33 | * User-facing category name shown on the portal. If omitted, a unique |
| 34 | * name is generated from the stack, stage, and logical id. |
| 35 | */ |
| 36 | name?: string; |
| 37 | }; |
| 38 | |
| 39 | export type SitesApicategory = Resource< |
| 40 | "GCP.Apigee.SitesApicategory", |
| 41 | SitesApicategoryProps, |
| 42 | { |
| 43 | /** Full resource name `organizations/{org}/sites/{site}/apicategories/{id}`. */ |
| 44 | name: string; |
| 45 | /** Server-assigned category UUID. */ |
| 46 | categoryId: string; |
| 47 | /** Apigee organization id. */ |
| 48 | organization: string; |
| 49 | /** Portal site id. */ |
| 50 | siteId: string; |
| 51 | /** User-facing category name. */ |
| 52 | categoryName: string | undefined; |
| 53 | /** Last-modified time in milliseconds since epoch. */ |
| 54 | updateTime: string | undefined; |
| 55 | }, |
| 56 | never, |
| 57 | Providers |
| 58 | >; |
| 59 | |
| 60 | /** |
| 61 | * An API category on an Apigee integrated portal. Catalog items can be |
| 62 | * tagged with categories so portal users can browse by topic. |
| 63 | * |
| 64 | * API categories have no labels field and the name is shown to portal |
| 65 | * users, so Alchemy finds its category by name: `read` reports a |
| 66 | * category as owned only when it carries the generated name (an explicit |
| 67 | * `name` is reported as unowned — adopt it with `--adopt`), and nuke |
| 68 | * cannot discover categories. `siteId` and `organization` are identity — |
| 69 | * changing either replaces the category. The display name updates in |
| 70 | * place. |
| 71 | * |
| 72 | * ### Creating an API Category |
| 73 | * **Example:** Named category |
| 74 | * ```typescript |
| 75 | * const category = yield* GCP.Apigee.SitesApicategory("Payments", { |
| 76 | * siteId: portal.siteId, |
| 77 | * name: "Payments", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * ### Updating an API Category |
| 82 | * **Example:** Rename |
| 83 | * ```typescript |
| 84 | * const category = yield* GCP.Apigee.SitesApicategory("Payments", { |
| 85 | * siteId: portal.siteId, |
| 86 | * name: "Billing", |
| 87 | * }); |
| 88 | * ``` |
| 89 | * |
| 90 | * @resource |
| 91 | * @category Apigee |
| 92 | */ |
| 93 | export const SitesApicategory = Resource<SitesApicategory>("GCP.Apigee.SitesApicategory"); |
| 94 | |
| 95 | export class SitesApicategoryNotResolved extends Data.TaggedError( |
| 96 | "GCP.Apigee.SitesApicategoryNotResolved", |
| 97 | )<{ |
| 98 | resourceName: string; |
| 99 | }> {} |
| 100 | |
| 101 | const siteParent = (org: string, siteId: string) => `${orgParent(org)}/sites/${siteId}`; |
| 102 | |
| 103 | const resourceName = (org: string, siteId: string, categoryId: string) => |
| 104 | `${siteParent(org, siteId)}/apicategories/${categoryId}`; |
| 105 | |
| 106 | const toDisplayName = (id: string, name: string | undefined, existing?: string) => |
| 107 | Effect.gen(function* () { |
| 108 | if (name !== undefined) return name; |
| 109 | if (existing !== undefined) return existing; |
| 110 | return yield* createPhysicalName({ |
| 111 | id, |
| 112 | maxLength: MAX_NAME_LENGTH, |
| 113 | lowercase: true, |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | const unwrap = ( |
| 118 | response: apigee.GoogleCloudApigeeV1ApiCategoryResponse | undefined, |
| 119 | ): apigee.GoogleCloudApigeeV1ApiCategory | undefined => response?.data; |
| 120 | |
| 121 | const toAttrs = (category: apigee.GoogleCloudApigeeV1ApiCategory, org: string, siteId: string) => { |
| 122 | const categoryId = category.id ?? ""; |
| 123 | return { |
| 124 | name: categoryId ? resourceName(org, siteId, categoryId) : "", |
| 125 | categoryId, |
| 126 | organization: org, |
| 127 | siteId: category.siteId ?? siteId, |
| 128 | categoryName: category.name, |
| 129 | updateTime: category.updateTime, |
| 130 | }; |
| 131 | }; |
| 132 | |
| 133 | const getByName = (name: string) => |
| 134 | apigee.getOrganizationsSitesApicategories({ name }).pipe( |
| 135 | Effect.map((response) => unwrap(response)), |
| 136 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined), |
| 137 | ); |
| 138 | |
| 139 | const listBySite = (org: string, siteId: string) => |
| 140 | apigee |
| 141 | .listOrganizationsSitesApicategories({ |
| 142 | parent: siteParent(org, siteId), |
| 143 | }) |
| 144 | .pipe( |
| 145 | Effect.map((page) => page.data ?? []), |
| 146 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.succeed([])), |
| 147 | ); |
| 148 | |
| 149 | const findByDisplayName = (org: string, siteId: string, displayName: string) => |
| 150 | listBySite(org, siteId).pipe( |
| 151 | Effect.map((categories) => categories.find((category) => category.name === displayName)), |
| 152 | ); |
| 153 | |
| 154 | export const SitesApicategoryProvider = () => |
| 155 | Provider.succeed(SitesApicategory, { |
| 156 | stables: ["name", "categoryId", "organization", "siteId"], |
| 157 | |
| 158 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 159 | if (!isResolved(news)) return undefined; |
| 160 | const previousSite = olds?.siteId ?? output?.siteId; |
| 161 | const siteChanged = previousSite !== undefined && news.siteId !== previousSite; |
| 162 | const previousOrg = olds?.organization ?? output?.organization; |
| 163 | const orgChanged = |
| 164 | previousOrg !== undefined && |
| 165 | news.organization !== undefined && |
| 166 | orgIdOf(news.organization, previousOrg) !== previousOrg; |
| 167 | const previousId = olds?.categoryId ?? output?.categoryId; |
| 168 | const idChanged = |
| 169 | previousId !== undefined && news.categoryId !== undefined && news.categoryId !== previousId; |
| 170 | if (siteChanged || orgChanged || idChanged) { |
| 171 | return { action: "replace" as const, deleteFirst: false }; |
| 172 | } |
| 173 | return undefined; |
| 174 | }), |
| 175 | |
| 176 | read: Effect.fn(function* ({ id, olds, output }) { |
| 177 | const env = yield* GcpEnvironment.current; |
| 178 | const org = orgIdOf(olds?.organization ?? output?.organization, env.project); |
| 179 | const siteId = olds?.siteId ?? output?.siteId; |
| 180 | if (siteId === undefined) return undefined; |
| 181 | const categoryId = olds?.categoryId ?? output?.categoryId; |
| 182 | const name = |
| 183 | output?.name ?? |
| 184 | (categoryId !== undefined ? resourceName(org, siteId, categoryId) : undefined); |
| 185 | const generated = yield* toDisplayName(id, undefined); |
| 186 | const displayName = olds?.name ?? output?.categoryName ?? generated; |
| 187 | const existing = |
| 188 | name !== undefined |
| 189 | ? yield* getByName(name) |
| 190 | : yield* findByDisplayName(org, siteId, displayName); |
| 191 | if (existing === undefined) return undefined; |
| 192 | const attrs = toAttrs(existing, org, siteId); |
| 193 | return existing.name === generated ? attrs : Unowned(attrs); |
| 194 | }), |
| 195 | |
| 196 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 197 | const env = yield* GcpEnvironment.current; |
| 198 | const org = orgIdOf(news.organization ?? output?.organization, env.project); |
| 199 | const siteId = news.siteId; |
| 200 | const displayName = yield* toDisplayName(id, news.name, output?.categoryName); |
| 201 | const desiredName = displayName; |
| 202 | |
| 203 | let current = |
| 204 | output?.name !== undefined |
| 205 | ? yield* getByName(output.name) |
| 206 | : news.categoryId !== undefined |
| 207 | ? yield* getByName(resourceName(org, siteId, news.categoryId)) |
| 208 | : yield* findByDisplayName(org, siteId, desiredName); |
| 209 | |
| 210 | if (current === undefined) { |
| 211 | const created = yield* apigee |
| 212 | .createOrganizationsSitesApicategories({ |
| 213 | parent: siteParent(org, siteId), |
| 214 | body: { |
| 215 | name: desiredName, |
| 216 | siteId, |
| 217 | }, |
| 218 | }) |
| 219 | .pipe( |
| 220 | Effect.map((response) => unwrap(response)), |
| 221 | Effect.catchTag("Conflict", () => findByDisplayName(org, siteId, desiredName)), |
| 222 | ); |
| 223 | current = created ?? undefined; |
| 224 | } |
| 225 | |
| 226 | if (current === undefined || !(current.id ?? "").length) { |
| 227 | return yield* new SitesApicategoryNotResolved({ |
| 228 | resourceName: siteParent(org, siteId), |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | const resource = resourceName(org, siteId, current.id ?? ""); |
| 233 | if ((current.name ?? "") !== desiredName) { |
| 234 | const patched = yield* apigee.patchOrganizationsSitesApicategories({ |
| 235 | name: resource, |
| 236 | body: { |
| 237 | name: desiredName, |
| 238 | siteId, |
| 239 | id: current.id, |
| 240 | }, |
| 241 | }); |
| 242 | current = unwrap(patched) ?? current; |
| 243 | } |
| 244 | |
| 245 | return toAttrs(current, org, siteId); |
| 246 | }), |
| 247 | |
| 248 | delete: Effect.fn(function* ({ output }) { |
| 249 | yield* apigee |
| 250 | .deleteOrganizationsSitesApicategories({ name: output.name }) |
| 251 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 252 | }), |
| 253 | }); |