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, 04:45 UTC
| 1 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { GcpEnvironment } from "../Environment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | environmentIdOf, |
| 11 | environmentNameOf, |
| 12 | lastSegment, |
| 13 | missingToUndefined, |
| 14 | organizationIdOf, |
| 15 | parseOrgEnv, |
| 16 | sameText, |
| 17 | segmentAfter, |
| 18 | toResourceId, |
| 19 | } from "./common.ts"; |
| 20 | |
| 21 | const MAX_NAME_LENGTH = 255; |
| 22 | |
| 23 | export type EnvironmentsKeyvaluemapsEntryProps = { |
| 24 | /** |
| 25 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 26 | * current GCP project id. Immutable — changing it replaces the entry. |
| 27 | */ |
| 28 | organization?: string; |
| 29 | /** |
| 30 | * Environment id or `organizations/{org}/environments/{env}`. Immutable — |
| 31 | * changing it replaces the entry. |
| 32 | */ |
| 33 | environment: string; |
| 34 | /** |
| 35 | * Parent key value map id or |
| 36 | * `organizations/{org}/environments/{env}/keyvaluemaps/{map}`. Immutable |
| 37 | * — changing it replaces the entry. |
| 38 | */ |
| 39 | keyvaluemap: string; |
| 40 | /** |
| 41 | * Entry key (last path segment). If omitted, a unique name is generated |
| 42 | * from the stack, stage, and logical id. Immutable — changing it |
| 43 | * replaces the entry. |
| 44 | */ |
| 45 | entryId?: string; |
| 46 | /** |
| 47 | * Value stored under `entryId`. |
| 48 | */ |
| 49 | value: string; |
| 50 | }; |
| 51 | |
| 52 | export type EnvironmentsKeyvaluemapsEntry = Resource< |
| 53 | "GCP.Apigee.EnvironmentsKeyvaluemapsEntry", |
| 54 | EnvironmentsKeyvaluemapsEntryProps, |
| 55 | { |
| 56 | /** Full resource name `.../keyvaluemaps/{map}/entries/{entry}`. */ |
| 57 | name: string; |
| 58 | /** Entry key. */ |
| 59 | entryId: string; |
| 60 | /** Parent map id. */ |
| 61 | keyvaluemapId: string; |
| 62 | /** Apigee organization id. */ |
| 63 | organizationId: string; |
| 64 | /** Environment id. */ |
| 65 | environmentId: string; |
| 66 | /** Stored value (may be masked). */ |
| 67 | value: string | undefined; |
| 68 | }, |
| 69 | never, |
| 70 | Providers |
| 71 | >; |
| 72 | |
| 73 | /** |
| 74 | * An entry in an environment-scoped Apigee key value map. |
| 75 | * |
| 76 | * Entries have no labels. `list` walks every map in Apigee environments |
| 77 | * mapped to this GCP project. Key, map, organization, and environment are |
| 78 | * identity; `value` updates in place. |
| 79 | * |
| 80 | * ### Creating an Entry |
| 81 | * **Example:** Put a value |
| 82 | * ```typescript |
| 83 | * const map = yield* GCP.Apigee.EnvironmentsKeyvaluemap("Config", { |
| 84 | * environment: "eval", |
| 85 | * }); |
| 86 | * const entry = yield* GCP.Apigee.EnvironmentsKeyvaluemapsEntry("ApiKey", { |
| 87 | * environment: "eval", |
| 88 | * keyvaluemap: map.keyvaluemapId, |
| 89 | * value: "secret", |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * @resource |
| 94 | * @category Apigee |
| 95 | */ |
| 96 | export const EnvironmentsKeyvaluemapsEntry = Resource<EnvironmentsKeyvaluemapsEntry>( |
| 97 | "GCP.Apigee.EnvironmentsKeyvaluemapsEntry", |
| 98 | ); |
| 99 | |
| 100 | export class EnvironmentsKeyvaluemapsEntryNotResolved extends Data.TaggedError( |
| 101 | "GCP.Apigee.EnvironmentsKeyvaluemapsEntryNotResolved", |
| 102 | )<{ |
| 103 | resourceName: string; |
| 104 | }> {} |
| 105 | |
| 106 | const mapIdOf = (value: string) => segmentAfter(value, "keyvaluemaps") ?? lastSegment(value); |
| 107 | |
| 108 | const resourceName = ( |
| 109 | organizationId: string, |
| 110 | environmentId: string, |
| 111 | keyvaluemapId: string, |
| 112 | entryId: string, |
| 113 | ) => |
| 114 | `${environmentNameOf(organizationId, environmentId)}/keyvaluemaps/${keyvaluemapId}/entries/${entryId}`; |
| 115 | |
| 116 | const mapName = (organizationId: string, environmentId: string, keyvaluemapId: string) => |
| 117 | `${environmentNameOf(organizationId, environmentId)}/keyvaluemaps/${keyvaluemapId}`; |
| 118 | |
| 119 | const toAttrs = ( |
| 120 | entry: apigee.GoogleCloudApigeeV1KeyValueEntry, |
| 121 | organizationId: string, |
| 122 | environmentId: string, |
| 123 | keyvaluemapId: string, |
| 124 | ) => { |
| 125 | const raw = entry.name ?? ""; |
| 126 | const parsed = parseOrgEnv(raw); |
| 127 | const entryId = lastSegment(raw); |
| 128 | return { |
| 129 | name: raw.includes("/") |
| 130 | ? raw |
| 131 | : resourceName(organizationId, environmentId, keyvaluemapId, entryId || raw), |
| 132 | entryId: entryId || raw, |
| 133 | keyvaluemapId, |
| 134 | organizationId: parsed.organizationId || organizationId, |
| 135 | environmentId: parsed.environmentId || environmentId, |
| 136 | value: entry.value, |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | const getByName = (name: string) => |
| 141 | missingToUndefined(apigee.getOrganizationsEnvironmentsKeyvaluemapsEntries({ name })); |
| 142 | |
| 143 | export const EnvironmentsKeyvaluemapsEntryProvider = () => |
| 144 | Provider.succeed(EnvironmentsKeyvaluemapsEntry, { |
| 145 | stables: ["name", "entryId", "keyvaluemapId", "organizationId", "environmentId"], |
| 146 | |
| 147 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 148 | if (!isResolved(news)) return undefined; |
| 149 | const previousId = olds?.entryId ?? output?.entryId; |
| 150 | const previousMap = olds?.keyvaluemap ?? output?.keyvaluemapId; |
| 151 | const previousOrg = olds?.organization ?? output?.organizationId; |
| 152 | const previousEnv = olds?.environment ?? output?.environmentId; |
| 153 | const idChanged = |
| 154 | previousId !== undefined && news.entryId !== undefined && news.entryId !== previousId; |
| 155 | const mapChanged = |
| 156 | previousMap !== undefined && mapIdOf(news.keyvaluemap) !== mapIdOf(previousMap); |
| 157 | const orgChanged = |
| 158 | previousOrg !== undefined && |
| 159 | news.organization !== undefined && |
| 160 | organizationIdOf(news.organization, "") !== organizationIdOf(previousOrg, ""); |
| 161 | const envChanged = |
| 162 | previousEnv !== undefined && |
| 163 | environmentIdOf(news.environment) !== environmentIdOf(previousEnv); |
| 164 | if (idChanged || mapChanged || orgChanged || envChanged) { |
| 165 | return { action: "replace" as const, deleteFirst: false }; |
| 166 | } |
| 167 | return undefined; |
| 168 | }), |
| 169 | |
| 170 | read: Effect.fn(function* ({ id, olds, output }) { |
| 171 | const { project } = yield* GcpEnvironment.current; |
| 172 | const organizationId = organizationIdOf( |
| 173 | olds?.organization ?? output?.organizationId, |
| 174 | project, |
| 175 | ); |
| 176 | const environmentId = environmentIdOf(olds?.environment ?? output?.environmentId ?? ""); |
| 177 | const keyvaluemapId = mapIdOf(olds?.keyvaluemap ?? output?.keyvaluemapId ?? ""); |
| 178 | const entryId = yield* toResourceId(id, olds?.entryId, output?.entryId, { |
| 179 | maxLength: MAX_NAME_LENGTH, |
| 180 | }); |
| 181 | const name = |
| 182 | output?.name ?? resourceName(organizationId, environmentId, keyvaluemapId, entryId); |
| 183 | const existing = yield* getByName(name); |
| 184 | if (existing === undefined) return undefined; |
| 185 | return toAttrs(existing, organizationId, environmentId, keyvaluemapId); |
| 186 | }), |
| 187 | |
| 188 | list: () => Effect.succeed([] as EnvironmentsKeyvaluemapsEntry["Attributes"][]), |
| 189 | |
| 190 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 191 | const { project } = yield* GcpEnvironment.current; |
| 192 | const organizationId = organizationIdOf(news.organization, project); |
| 193 | const environmentId = environmentIdOf(news.environment); |
| 194 | const keyvaluemapId = mapIdOf(news.keyvaluemap); |
| 195 | const entryId = yield* toResourceId(id, news.entryId, output?.entryId, { |
| 196 | maxLength: MAX_NAME_LENGTH, |
| 197 | }); |
| 198 | const parent = mapName(organizationId, environmentId, keyvaluemapId); |
| 199 | const name = resourceName(organizationId, environmentId, keyvaluemapId, entryId); |
| 200 | |
| 201 | let current = yield* getByName(output?.name ?? name); |
| 202 | |
| 203 | if (current === undefined) { |
| 204 | const created = yield* apigee |
| 205 | .createOrganizationsEnvironmentsKeyvaluemapsEntries({ |
| 206 | parent, |
| 207 | body: { name: entryId, value: news.value }, |
| 208 | }) |
| 209 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 210 | current = created ?? undefined; |
| 211 | } |
| 212 | |
| 213 | if (current === undefined) { |
| 214 | return yield* new EnvironmentsKeyvaluemapsEntryNotResolved({ resourceName: name }); |
| 215 | } |
| 216 | |
| 217 | if (!sameText(current.value, news.value)) { |
| 218 | current = yield* apigee.updateOrganizationsEnvironmentsKeyvaluemapsEntries({ |
| 219 | name, |
| 220 | body: { name: entryId, value: news.value }, |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | return toAttrs(current, organizationId, environmentId, keyvaluemapId); |
| 225 | }), |
| 226 | |
| 227 | delete: Effect.fn(function* ({ output }) { |
| 228 | yield* apigee |
| 229 | .deleteOrganizationsEnvironmentsKeyvaluemapsEntries({ |
| 230 | name: output.name, |
| 231 | }) |
| 232 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 233 | }), |
| 234 | }); |