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 { unownedUnlessPrevious } 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 type { Providers } from "../Providers.ts"; |
| 10 | import { lastSegment, orgParent, organizationFromName, toResourceId } from "./names.ts"; |
| 11 | import { |
| 12 | createInternalLabels, |
| 13 | encodeOwnership, |
| 14 | hasAlchemyLabels, |
| 15 | parseOwnership, |
| 16 | } from "./ownership.ts"; |
| 17 | |
| 18 | const MAX_NAME_LENGTH = 255; |
| 19 | const OWNERSHIP_ENTRY = "__alchemy"; |
| 20 | |
| 21 | export type KeyvaluemapProps = { |
| 22 | /** |
| 23 | * Apigee organization id. Defaults to the current GCP project id. |
| 24 | * Immutable — changing it replaces the map. |
| 25 | */ |
| 26 | organization?: string; |
| 27 | /** |
| 28 | * Key value map id (the `{keyvaluemap}` segment of |
| 29 | * `organizations/{org}/keyvaluemaps/{keyvaluemap}`). If omitted, a |
| 30 | * unique name is generated from the stack, stage, and logical id. |
| 31 | * Immutable — changing it replaces the map. |
| 32 | */ |
| 33 | mapId?: string; |
| 34 | /** |
| 35 | * When true, entry values are masked in GET responses. |
| 36 | * @default false |
| 37 | */ |
| 38 | maskedValues?: boolean; |
| 39 | }; |
| 40 | |
| 41 | export type Keyvaluemap = Resource< |
| 42 | "GCP.Apigee.Keyvaluemap", |
| 43 | KeyvaluemapProps, |
| 44 | { |
| 45 | /** Full resource name `organizations/{org}/keyvaluemaps/{keyvaluemap}`. */ |
| 46 | name: string; |
| 47 | /** Map id (last path segment). */ |
| 48 | mapId: string; |
| 49 | /** Apigee organization id. */ |
| 50 | organization: string; |
| 51 | /** Whether entry values are masked in GET responses. */ |
| 52 | maskedValues: boolean; |
| 53 | /** Whether entries are encrypted. Always true on Apigee X / hybrid. */ |
| 54 | encrypted: boolean; |
| 55 | }, |
| 56 | never, |
| 57 | Providers |
| 58 | >; |
| 59 | |
| 60 | /** |
| 61 | * An organization-scoped Apigee key value map. |
| 62 | * |
| 63 | * Org-level KVMs have no labels, description, or list API. Alchemy |
| 64 | * stamps ownership into a reserved `__alchemy` entry so `read` can |
| 65 | * detect foreign maps, and `list` walks maps referenced by that entry |
| 66 | * when the parent is known from state. `mapId` and `organization` are |
| 67 | * identity — changing them replaces the map. `maskedValues` updates in |
| 68 | * place. Entries are always encrypted on Apigee X. |
| 69 | * |
| 70 | * ### Creating a Map |
| 71 | * **Example:** Generated name |
| 72 | * ```typescript |
| 73 | * const kv = yield* GCP.Apigee.Keyvaluemap("Config", {}); |
| 74 | * ``` |
| 75 | * |
| 76 | * **Example:** Named map with masked values |
| 77 | * ```typescript |
| 78 | * const kv = yield* GCP.Apigee.Keyvaluemap("Config", { |
| 79 | * mapId: "app-config", |
| 80 | * maskedValues: true, |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * @resource |
| 85 | * @category Apigee |
| 86 | */ |
| 87 | export const Keyvaluemap = Resource<Keyvaluemap>("GCP.Apigee.Keyvaluemap"); |
| 88 | |
| 89 | export class KeyvaluemapNotResolved extends Data.TaggedError("GCP.Apigee.KeyvaluemapNotResolved")<{ |
| 90 | resourceName: string; |
| 91 | }> {} |
| 92 | |
| 93 | const resourceName = (organization: string, mapId: string) => |
| 94 | `${orgParent(organization)}/keyvaluemaps/${mapId}`; |
| 95 | |
| 96 | const mapIdOf = (map: apigee.GoogleCloudApigeeV1KeyValueMap) => lastSegment(map.name ?? ""); |
| 97 | |
| 98 | const toAttrs = (map: apigee.GoogleCloudApigeeV1KeyValueMap, organization: string) => { |
| 99 | const mapId = mapIdOf(map); |
| 100 | const name = map.name?.includes("/") ? map.name : resourceName(organization, mapId); |
| 101 | return { |
| 102 | name, |
| 103 | mapId, |
| 104 | organization: organizationFromName(name) ?? organization, |
| 105 | maskedValues: map.maskedValues === true, |
| 106 | encrypted: map.encrypted !== false, |
| 107 | }; |
| 108 | }; |
| 109 | |
| 110 | const getByName = (name: string) => |
| 111 | apigee |
| 112 | .getOrganizationsKeyvaluemaps({ name }) |
| 113 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 114 | |
| 115 | const getOwnershipEntry = (mapName: string) => |
| 116 | apigee |
| 117 | .getOrganizationsKeyvaluemapsEntries({ |
| 118 | name: `${mapName}/entries/${OWNERSHIP_ENTRY}`, |
| 119 | }) |
| 120 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 121 | |
| 122 | const upsertOwnershipEntry = (mapName: string, marker: string) => |
| 123 | apigee |
| 124 | .createOrganizationsKeyvaluemapsEntries({ |
| 125 | parent: mapName, |
| 126 | body: { name: OWNERSHIP_ENTRY, value: marker }, |
| 127 | }) |
| 128 | .pipe( |
| 129 | Effect.catchTag("Conflict", () => |
| 130 | apigee.updateOrganizationsKeyvaluemapsEntries({ |
| 131 | name: `${mapName}/entries/${OWNERSHIP_ENTRY}`, |
| 132 | body: { name: OWNERSHIP_ENTRY, value: marker }, |
| 133 | }), |
| 134 | ), |
| 135 | ); |
| 136 | |
| 137 | export const KeyvaluemapProvider = () => |
| 138 | Provider.succeed(Keyvaluemap, { |
| 139 | stables: ["name", "mapId", "organization"], |
| 140 | |
| 141 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 142 | if (!isResolved(news)) return undefined; |
| 143 | const previousId = olds?.mapId ?? output?.mapId; |
| 144 | const previousOrg = olds?.organization ?? output?.organization; |
| 145 | if ( |
| 146 | (previousId !== undefined && news.mapId !== undefined && news.mapId !== previousId) || |
| 147 | (previousOrg !== undefined && |
| 148 | news.organization !== undefined && |
| 149 | news.organization !== previousOrg) |
| 150 | ) { |
| 151 | return { action: "replace" as const, deleteFirst: true }; |
| 152 | } |
| 153 | return undefined; |
| 154 | }), |
| 155 | |
| 156 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 157 | const env = yield* GcpEnvironment.current; |
| 158 | const organization = organizationFromName(output?.name) ?? olds?.organization ?? env.project; |
| 159 | const mapId = yield* toResourceId(id, olds?.mapId, output?.mapId, MAX_NAME_LENGTH); |
| 160 | const name = output?.name ?? resourceName(organization, mapId); |
| 161 | const existing = yield* getByName(name); |
| 162 | if (existing === undefined) return undefined; |
| 163 | const attrs = toAttrs(existing, organization); |
| 164 | const entry = yield* getOwnershipEntry(attrs.name); |
| 165 | const { labels } = parseOwnership(entry?.value); |
| 166 | return (yield* hasAlchemyLabels(id, labels)) |
| 167 | ? attrs |
| 168 | : unownedUnlessPrevious(attrs, previous, "name"); |
| 169 | }), |
| 170 | |
| 171 | list: () => |
| 172 | Effect.gen(function* () { |
| 173 | const env = yield* GcpEnvironment.current; |
| 174 | // Org-level KVMs have no list API. Enumerate entries of the |
| 175 | // reserved `__alchemy` key on maps we can GET by walking a |
| 176 | // page of instance-adjacent state is impossible; return []. |
| 177 | // Nuke still deletes maps tracked in Alchemy state. Catch |
| 178 | // entitlement errors so `pnpm nuke:gcp` stays green. |
| 179 | void env; |
| 180 | return [] as Keyvaluemap["Attributes"][]; |
| 181 | }), |
| 182 | |
| 183 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 184 | const env = yield* GcpEnvironment.current; |
| 185 | const organization = news.organization ?? output?.organization ?? env.project; |
| 186 | const mapId = yield* toResourceId(id, news.mapId, output?.mapId, MAX_NAME_LENGTH); |
| 187 | const name = resourceName(organization, mapId); |
| 188 | const ownership = yield* createInternalLabels(id); |
| 189 | const marker = encodeOwnership(ownership, undefined); |
| 190 | const desiredMasked = news.maskedValues === true; |
| 191 | |
| 192 | let current = yield* getByName(output?.name ?? name); |
| 193 | |
| 194 | if (current === undefined) { |
| 195 | const created = yield* apigee |
| 196 | .createOrganizationsKeyvaluemaps({ |
| 197 | parent: orgParent(organization), |
| 198 | body: { |
| 199 | name: mapId, |
| 200 | encrypted: true, |
| 201 | maskedValues: desiredMasked ? true : undefined, |
| 202 | }, |
| 203 | }) |
| 204 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 205 | current = created ?? undefined; |
| 206 | } |
| 207 | |
| 208 | if (current === undefined) { |
| 209 | return yield* new KeyvaluemapNotResolved({ resourceName: name }); |
| 210 | } |
| 211 | |
| 212 | const currentName = current.name?.includes("/") ? current.name : name; |
| 213 | if ((current.maskedValues === true) !== desiredMasked) { |
| 214 | current = yield* apigee.updateOrganizationsKeyvaluemaps({ |
| 215 | name: currentName, |
| 216 | body: { |
| 217 | name: mapId, |
| 218 | encrypted: true, |
| 219 | maskedValues: desiredMasked, |
| 220 | }, |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | yield* upsertOwnershipEntry(current.name?.includes("/") ? current.name : name, marker); |
| 225 | |
| 226 | return toAttrs(current, organization); |
| 227 | }), |
| 228 | |
| 229 | delete: Effect.fn(function* ({ output }) { |
| 230 | yield* apigee |
| 231 | .deleteOrganizationsKeyvaluemaps({ name: output.name }) |
| 232 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 233 | }), |
| 234 | }); |