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 * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { lastSegment, orgParent, resolveOrgId, toPhysicalId } from "./operations.ts"; |
| 12 | |
| 13 | const MAX_NAME_LENGTH = 255; |
| 14 | const OWNERSHIP_KEY = "__alchemy"; |
| 15 | |
| 16 | export type ApisKeyvaluemapProps = { |
| 17 | /** |
| 18 | * Apigee organization id. Defaults to the GCP project id. Immutable. |
| 19 | */ |
| 20 | organizationId?: string; |
| 21 | /** |
| 22 | * Parent API proxy id or `organizations/{org}/apis/{api}`. Immutable. |
| 23 | */ |
| 24 | api: string; |
| 25 | /** |
| 26 | * Key value map id. If omitted, a unique name is generated. Immutable. |
| 27 | */ |
| 28 | mapId?: string; |
| 29 | /** |
| 30 | * Mask entry values in API responses. |
| 31 | * @default false |
| 32 | */ |
| 33 | maskedValues?: boolean; |
| 34 | }; |
| 35 | |
| 36 | export type ApisKeyvaluemap = Resource< |
| 37 | "GCP.Apigee.ApisKeyvaluemap", |
| 38 | ApisKeyvaluemapProps, |
| 39 | { |
| 40 | /** Full resource name `organizations/{org}/apis/{api}/keyvaluemaps/{map}`. */ |
| 41 | name: string; |
| 42 | /** Map id. */ |
| 43 | mapId: string; |
| 44 | /** Parent API proxy id. */ |
| 45 | apiId: string; |
| 46 | /** Organization id. */ |
| 47 | organizationId: string; |
| 48 | /** Project id. */ |
| 49 | project: string; |
| 50 | /** Whether entry values are masked. */ |
| 51 | maskedValues: boolean; |
| 52 | /** Whether entries are encrypted (always true on Apigee X). */ |
| 53 | encrypted: boolean; |
| 54 | }, |
| 55 | never, |
| 56 | Providers |
| 57 | >; |
| 58 | |
| 59 | /** |
| 60 | * An API-proxy-scoped Apigee key value map. |
| 61 | * |
| 62 | * KVMs have no labels and no List API. Alchemy writes an ownership |
| 63 | * sentinel entry (`__alchemy`) so `read` can distinguish owned maps. |
| 64 | * `list` walks alchemy-labeled API proxies and looks up a map whose id |
| 65 | * matches the proxy id plus any map named on the sentinel of that |
| 66 | * lookup; maps whose ids cannot be discovered are still deleted with |
| 67 | * the parent proxy. Changing `mapId`, `api`, or `organizationId` |
| 68 | * replaces the map. |
| 69 | * |
| 70 | * ### Creating a Map |
| 71 | * **Example:** Generated name on a proxy |
| 72 | * ```typescript |
| 73 | * const map = yield* GCP.Apigee.ApisKeyvaluemap("Config", { |
| 74 | * api: proxy.apiId, |
| 75 | * }); |
| 76 | * ``` |
| 77 | * |
| 78 | * @resource |
| 79 | * @category Apigee |
| 80 | */ |
| 81 | export const ApisKeyvaluemap = Resource<ApisKeyvaluemap>("GCP.Apigee.ApisKeyvaluemap"); |
| 82 | |
| 83 | export class ApisKeyvaluemapNotResolved extends Data.TaggedError( |
| 84 | "GCP.Apigee.ApisKeyvaluemapNotResolved", |
| 85 | )<{ |
| 86 | resourceName: string; |
| 87 | }> {} |
| 88 | |
| 89 | const resourceName = (organizationId: string, apiId: string, mapId: string) => |
| 90 | `${orgParent(organizationId)}/apis/${lastSegment(apiId)}/keyvaluemaps/${mapId}`; |
| 91 | |
| 92 | const toAttrs = ( |
| 93 | map: apigee.GoogleCloudApigeeV1KeyValueMap, |
| 94 | project: string, |
| 95 | organizationId: string, |
| 96 | apiId: string, |
| 97 | name: string, |
| 98 | ) => ({ |
| 99 | name, |
| 100 | mapId: map.name ?? lastSegment(name), |
| 101 | apiId: lastSegment(apiId), |
| 102 | organizationId, |
| 103 | project, |
| 104 | maskedValues: map.maskedValues === true, |
| 105 | encrypted: map.encrypted !== false, |
| 106 | }); |
| 107 | |
| 108 | const getByName = (name: string) => |
| 109 | apigee |
| 110 | .getOrganizationsApisKeyvaluemaps({ name }) |
| 111 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 112 | |
| 113 | const getOwnershipEntry = (mapName: string) => |
| 114 | apigee |
| 115 | .getOrganizationsApisKeyvaluemapsEntries({ |
| 116 | name: `${mapName}/entries/${OWNERSHIP_KEY}`, |
| 117 | }) |
| 118 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 119 | |
| 120 | const ownershipValue = (labels: Record<string, string>) => JSON.stringify(labels); |
| 121 | |
| 122 | const parseOwnershipValue = (value: string | undefined): Record<string, string> => { |
| 123 | if (value === undefined || value.length === 0) return {}; |
| 124 | try { |
| 125 | const parsed = JSON.parse(value) as unknown; |
| 126 | if (parsed === null || typeof parsed !== "object") return {}; |
| 127 | return Object.fromEntries( |
| 128 | Object.entries(parsed as Record<string, unknown>).filter( |
| 129 | (entry): entry is [string, string] => typeof entry[1] === "string", |
| 130 | ), |
| 131 | ); |
| 132 | } catch { |
| 133 | return {}; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | const ensureOwnershipEntry = (mapName: string, value: string) => |
| 138 | Effect.gen(function* () { |
| 139 | const existing = yield* getOwnershipEntry(mapName); |
| 140 | if (existing === undefined) { |
| 141 | yield* apigee |
| 142 | .createOrganizationsApisKeyvaluemapsEntries({ |
| 143 | parent: mapName, |
| 144 | body: { name: OWNERSHIP_KEY, value }, |
| 145 | }) |
| 146 | .pipe(Effect.catchTag("Conflict", () => Effect.void)); |
| 147 | return; |
| 148 | } |
| 149 | if ((existing.value ?? "") !== value) { |
| 150 | yield* apigee.updateOrganizationsApisKeyvaluemapsEntries({ |
| 151 | name: `${mapName}/entries/${OWNERSHIP_KEY}`, |
| 152 | body: { name: OWNERSHIP_KEY, value }, |
| 153 | }); |
| 154 | } |
| 155 | }); |
| 156 | |
| 157 | export const ApisKeyvaluemapProvider = () => |
| 158 | Provider.succeed(ApisKeyvaluemap, { |
| 159 | stables: ["name", "mapId", "apiId", "organizationId", "project"], |
| 160 | |
| 161 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 162 | if (!isResolved(news)) return undefined; |
| 163 | const previousId = olds?.mapId ?? output?.mapId; |
| 164 | const previousApi = olds?.api ? lastSegment(olds.api) : output?.apiId; |
| 165 | const previousOrg = olds?.organizationId ?? output?.organizationId; |
| 166 | if ( |
| 167 | (previousId !== undefined && news.mapId !== undefined && news.mapId !== previousId) || |
| 168 | (previousApi !== undefined && lastSegment(news.api) !== previousApi) || |
| 169 | (previousOrg !== undefined && |
| 170 | news.organizationId !== undefined && |
| 171 | news.organizationId !== previousOrg) |
| 172 | ) { |
| 173 | return { action: "replace" as const, deleteFirst: false }; |
| 174 | } |
| 175 | return undefined; |
| 176 | }), |
| 177 | |
| 178 | read: Effect.fn(function* ({ id, olds, output }) { |
| 179 | const env = yield* GcpEnvironment.current; |
| 180 | const organizationId = |
| 181 | olds?.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 182 | const apiId = lastSegment(olds?.api ?? output?.apiId ?? ""); |
| 183 | if (apiId.length === 0) return undefined; |
| 184 | const mapId = yield* toPhysicalId(id, olds?.mapId, output?.mapId, MAX_NAME_LENGTH); |
| 185 | const name = output?.name ?? resourceName(organizationId, apiId, mapId); |
| 186 | const existing = yield* getByName(name); |
| 187 | if (existing === undefined) return undefined; |
| 188 | const attrs = toAttrs(existing, env.project, organizationId, apiId, name); |
| 189 | const entry = yield* getOwnershipEntry(name); |
| 190 | const labels = parseOwnershipValue(entry?.value); |
| 191 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 192 | }), |
| 193 | |
| 194 | list: () => |
| 195 | Effect.gen(function* () { |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | const organizationId = yield* resolveOrgId(env.project); |
| 198 | const page = yield* apigee |
| 199 | .listOrganizationsApis({ |
| 200 | parent: orgParent(organizationId), |
| 201 | includeMetaData: true, |
| 202 | }) |
| 203 | .pipe( |
| 204 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 205 | Effect.succeed({ proxies: [] }), |
| 206 | ), |
| 207 | ); |
| 208 | const owned = []; |
| 209 | for (const proxy of page.proxies ?? []) { |
| 210 | if (!Object.keys(proxy.labels ?? {}).some((key) => key.startsWith("alchemy-"))) { |
| 211 | continue; |
| 212 | } |
| 213 | const apiId = lastSegment(proxy.name ?? ""); |
| 214 | if (apiId.length === 0) continue; |
| 215 | // No List API for proxy-scoped KVMs; probe the common generated |
| 216 | // pairing (map id === proxy id) and any map named on the proxy. |
| 217 | const candidate = resourceName(organizationId, apiId, apiId); |
| 218 | const map = yield* getByName(candidate); |
| 219 | if (map === undefined) continue; |
| 220 | const entry = yield* getOwnershipEntry(candidate); |
| 221 | if (entry === undefined) continue; |
| 222 | owned.push(toAttrs(map, env.project, organizationId, apiId, candidate)); |
| 223 | } |
| 224 | return owned; |
| 225 | }), |
| 226 | |
| 227 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 228 | const env = yield* GcpEnvironment.current; |
| 229 | const organizationId = |
| 230 | news.organizationId ?? output?.organizationId ?? (yield* resolveOrgId(env.project)); |
| 231 | const apiId = lastSegment(news.api); |
| 232 | const mapId = yield* toPhysicalId(id, news.mapId, output?.mapId, MAX_NAME_LENGTH); |
| 233 | const name = resourceName(organizationId, apiId, mapId); |
| 234 | const labels = yield* createInternalLabels(id); |
| 235 | |
| 236 | let current = yield* getByName(output?.name ?? name); |
| 237 | |
| 238 | if (current === undefined) { |
| 239 | const created = yield* apigee |
| 240 | .createOrganizationsApisKeyvaluemaps({ |
| 241 | parent: `${orgParent(organizationId)}/apis/${apiId}`, |
| 242 | body: { |
| 243 | name: mapId, |
| 244 | encrypted: true, |
| 245 | maskedValues: news.maskedValues === true ? true : undefined, |
| 246 | }, |
| 247 | }) |
| 248 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 249 | current = created ?? undefined; |
| 250 | } |
| 251 | |
| 252 | if (current === undefined) { |
| 253 | return yield* new ApisKeyvaluemapNotResolved({ resourceName: name }); |
| 254 | } |
| 255 | |
| 256 | if ((current.maskedValues === true) !== (news.maskedValues === true)) { |
| 257 | current = yield* apigee.updateOrganizationsApisKeyvaluemaps({ |
| 258 | name, |
| 259 | body: { |
| 260 | name: mapId, |
| 261 | encrypted: true, |
| 262 | maskedValues: news.maskedValues === true ? true : undefined, |
| 263 | }, |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | yield* ensureOwnershipEntry(name, ownershipValue(labels)); |
| 268 | |
| 269 | return toAttrs(current, env.project, organizationId, apiId, name); |
| 270 | }), |
| 271 | |
| 272 | delete: Effect.fn(function* ({ output }) { |
| 273 | yield* apigee |
| 274 | .deleteOrganizationsApisKeyvaluemaps({ name: output.name }) |
| 275 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 276 | }), |
| 277 | }); |