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 type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | createOwnership, |
| 12 | defaultOrgName, |
| 13 | desiredAttributes, |
| 14 | encodeDescription, |
| 15 | hasOwnershipMarker, |
| 16 | jsonEqual, |
| 17 | lastSegment, |
| 18 | letterPrefixedId, |
| 19 | listOrgNames, |
| 20 | orgIdOf, |
| 21 | orgNameOf, |
| 22 | ownedBy, |
| 23 | parseDescription, |
| 24 | propertiesToRecord, |
| 25 | recordToProperties, |
| 26 | sameRecord, |
| 27 | userProperties, |
| 28 | waitForOperation, |
| 29 | } from "./operations.ts"; |
| 30 | |
| 31 | export type EnvironmentType = apigee.GoogleCloudApigeeV1EnvironmentTypeEnum | (string & {}); |
| 32 | export type EnvironmentApiProxyType = |
| 33 | | apigee.GoogleCloudApigeeV1EnvironmentApiProxyTypeEnum |
| 34 | | (string & {}); |
| 35 | export type EnvironmentDeploymentType = |
| 36 | | apigee.GoogleCloudApigeeV1EnvironmentDeploymentTypeEnum |
| 37 | | (string & {}); |
| 38 | |
| 39 | export type EnvironmentClientIPResolutionConfig = |
| 40 | apigee.GoogleCloudApigeeV1EnvironmentClientIPResolutionConfig; |
| 41 | export type EnvironmentNodeConfig = apigee.GoogleCloudApigeeV1NodeConfig; |
| 42 | |
| 43 | export type EnvironmentProps = { |
| 44 | /** |
| 45 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 46 | * current GCP project id. Immutable — changing it replaces the |
| 47 | * environment. |
| 48 | */ |
| 49 | organization?: string; |
| 50 | /** |
| 51 | * Environment id (the `{env}` segment of |
| 52 | * `organizations/{org}/environments/{env}`). Must match |
| 53 | * `^[.\\p{Alnum}-_]{1,255}$`. If omitted, a unique name is generated. |
| 54 | * Immutable — changing it replaces the environment. |
| 55 | */ |
| 56 | environmentId?: string; |
| 57 | /** |
| 58 | * Display name. |
| 59 | */ |
| 60 | displayName?: string; |
| 61 | /** |
| 62 | * Human-readable description. Alchemy ownership is stored in a |
| 63 | * `[alchemy …]` prefix and stripped from attributes. |
| 64 | */ |
| 65 | description?: string; |
| 66 | /** |
| 67 | * Environment type. Immutable — changing it replaces the environment. |
| 68 | */ |
| 69 | type?: EnvironmentType; |
| 70 | /** |
| 71 | * API proxy type. Immutable — changing it replaces the environment. |
| 72 | */ |
| 73 | apiProxyType?: EnvironmentApiProxyType; |
| 74 | /** |
| 75 | * Deployment type. Immutable — changing it replaces the environment. |
| 76 | */ |
| 77 | deploymentType?: EnvironmentDeploymentType; |
| 78 | /** |
| 79 | * Forward proxy URI (`http://{hostname}:{port}`). Empty string removes |
| 80 | * the setting. |
| 81 | */ |
| 82 | forwardProxyUri?: string; |
| 83 | /** |
| 84 | * Client IP resolution algorithm used by analytics and API security. |
| 85 | */ |
| 86 | clientIpResolutionConfig?: EnvironmentClientIPResolutionConfig; |
| 87 | /** |
| 88 | * Custom properties (name/value pairs). Alchemy ownership properties |
| 89 | * are merged in automatically. |
| 90 | */ |
| 91 | properties?: Record<string, string>; |
| 92 | /** |
| 93 | * Gateway node reservation. |
| 94 | */ |
| 95 | nodeConfig?: EnvironmentNodeConfig; |
| 96 | }; |
| 97 | |
| 98 | export type Environment = Resource< |
| 99 | "GCP.Apigee.Environment", |
| 100 | EnvironmentProps, |
| 101 | { |
| 102 | /** Full resource name `organizations/{org}/environments/{env}`. */ |
| 103 | name: string; |
| 104 | /** Environment id (last path segment). */ |
| 105 | environmentId: string; |
| 106 | /** Apigee organization id. */ |
| 107 | organization: string; |
| 108 | /** Display name. */ |
| 109 | displayName: string | undefined; |
| 110 | /** User description with the Alchemy ownership prefix stripped. */ |
| 111 | description: string | undefined; |
| 112 | /** Environment type. */ |
| 113 | type: string | undefined; |
| 114 | /** API proxy type. */ |
| 115 | apiProxyType: string | undefined; |
| 116 | /** Deployment type. */ |
| 117 | deploymentType: string | undefined; |
| 118 | /** Forward proxy URI. */ |
| 119 | forwardProxyUri: string | undefined; |
| 120 | /** Client IP resolution config. */ |
| 121 | clientIpResolutionConfig: EnvironmentClientIPResolutionConfig | undefined; |
| 122 | /** User properties (Alchemy ownership properties stripped). */ |
| 123 | properties: Record<string, string>; |
| 124 | /** Gateway node reservation. */ |
| 125 | nodeConfig: EnvironmentNodeConfig | undefined; |
| 126 | /** Whether flow hooks are attached. */ |
| 127 | hasAttachedFlowHooks: boolean | undefined; |
| 128 | /** Server-reported state (`CREATING`, `ACTIVE`, …). */ |
| 129 | state: string | undefined; |
| 130 | /** Creation time in milliseconds since epoch. */ |
| 131 | createdAt: string | undefined; |
| 132 | /** Last modification time in milliseconds since epoch. */ |
| 133 | lastModifiedAt: string | undefined; |
| 134 | }, |
| 135 | never, |
| 136 | Providers |
| 137 | >; |
| 138 | |
| 139 | /** |
| 140 | * An Apigee environment that hosts API proxies and shared flows. |
| 141 | * |
| 142 | * Apigee environments have no labels field, so Alchemy stamps ownership |
| 143 | * into the description (and properties) for `list` / nuke. Name, type, |
| 144 | * API proxy type, and deployment type are identity — changing them |
| 145 | * replaces the environment. Display name, description, forward proxy, |
| 146 | * IP resolution, properties, and node config update in place. |
| 147 | * |
| 148 | * ### Creating an Environment |
| 149 | * **Example:** Generated name |
| 150 | * ```typescript |
| 151 | * const environment = yield* GCP.Apigee.Environment("Runtime", { |
| 152 | * displayName: "runtime", |
| 153 | * }); |
| 154 | * ``` |
| 155 | * |
| 156 | * **Example:** Named environment with description |
| 157 | * ```typescript |
| 158 | * const environment = yield* GCP.Apigee.Environment("Runtime", { |
| 159 | * environmentId: "prod", |
| 160 | * displayName: "Production", |
| 161 | * description: "production API runtime", |
| 162 | * apiProxyType: "PROGRAMMABLE", |
| 163 | * }); |
| 164 | * ``` |
| 165 | * |
| 166 | * @resource |
| 167 | * @category Apigee |
| 168 | */ |
| 169 | export const Environment = Resource<Environment>("GCP.Apigee.Environment"); |
| 170 | |
| 171 | export class EnvironmentNotResolved extends Data.TaggedError("GCP.Apigee.EnvironmentNotResolved")<{ |
| 172 | resourceName: string; |
| 173 | }> {} |
| 174 | |
| 175 | const resourceName = (organization: string, environmentId: string) => |
| 176 | `${orgNameOf(organization)}/environments/${environmentId}`; |
| 177 | |
| 178 | const toAttrs = (environment: apigee.GoogleCloudApigeeV1Environment, organization: string) => { |
| 179 | const name = environment.name ?? ""; |
| 180 | const parsed = parseDescription(environment.description); |
| 181 | return { |
| 182 | name, |
| 183 | environmentId: lastSegment(name), |
| 184 | organization: orgIdOf(organization), |
| 185 | displayName: environment.displayName, |
| 186 | description: parsed.description, |
| 187 | type: environment.type, |
| 188 | apiProxyType: environment.apiProxyType, |
| 189 | deploymentType: environment.deploymentType, |
| 190 | forwardProxyUri: environment.forwardProxyUri, |
| 191 | clientIpResolutionConfig: environment.clientIpResolutionConfig, |
| 192 | properties: userProperties(environment.properties), |
| 193 | nodeConfig: environment.nodeConfig, |
| 194 | hasAttachedFlowHooks: environment.hasAttachedFlowHooks, |
| 195 | state: environment.state, |
| 196 | createdAt: environment.createdAt, |
| 197 | lastModifiedAt: environment.lastModifiedAt, |
| 198 | }; |
| 199 | }; |
| 200 | |
| 201 | const getByName = (name: string) => |
| 202 | apigee |
| 203 | .getOrganizationsEnvironments({ name }) |
| 204 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 205 | |
| 206 | export const EnvironmentProvider = () => |
| 207 | Provider.succeed(Environment, { |
| 208 | stables: [ |
| 209 | "name", |
| 210 | "environmentId", |
| 211 | "organization", |
| 212 | "type", |
| 213 | "apiProxyType", |
| 214 | "deploymentType", |
| 215 | "createdAt", |
| 216 | ], |
| 217 | |
| 218 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 219 | if (!isResolved(news)) return undefined; |
| 220 | const previousId = olds?.environmentId ?? output?.environmentId; |
| 221 | const previousOrg = olds?.organization ?? output?.organization; |
| 222 | const previousType = olds?.type ?? output?.type; |
| 223 | const previousProxy = olds?.apiProxyType ?? output?.apiProxyType; |
| 224 | const previousDeploy = olds?.deploymentType ?? output?.deploymentType; |
| 225 | const idChanged = |
| 226 | previousId !== undefined && |
| 227 | news.environmentId !== undefined && |
| 228 | news.environmentId !== previousId; |
| 229 | const orgChanged = |
| 230 | previousOrg !== undefined && |
| 231 | news.organization !== undefined && |
| 232 | orgIdOf(news.organization) !== orgIdOf(previousOrg); |
| 233 | const typeChanged = |
| 234 | previousType !== undefined && news.type !== undefined && news.type !== previousType; |
| 235 | const proxyChanged = |
| 236 | previousProxy !== undefined && |
| 237 | news.apiProxyType !== undefined && |
| 238 | news.apiProxyType !== previousProxy; |
| 239 | const deployChanged = |
| 240 | previousDeploy !== undefined && |
| 241 | news.deploymentType !== undefined && |
| 242 | news.deploymentType !== previousDeploy; |
| 243 | if (idChanged || orgChanged || typeChanged || proxyChanged || deployChanged) { |
| 244 | return { |
| 245 | action: "replace" as const, |
| 246 | deleteFirst: idChanged && !orgChanged, |
| 247 | }; |
| 248 | } |
| 249 | return undefined; |
| 250 | }), |
| 251 | |
| 252 | read: Effect.fn(function* ({ id, olds, output }) { |
| 253 | const env = yield* GcpEnvironment.current; |
| 254 | const organization = defaultOrgName(env.project, olds?.organization ?? output?.organization); |
| 255 | const environmentId = yield* letterPrefixedId( |
| 256 | id, |
| 257 | olds?.environmentId, |
| 258 | output?.environmentId, |
| 259 | 255, |
| 260 | ); |
| 261 | const name = output?.name ?? resourceName(organization, environmentId); |
| 262 | const existing = yield* getByName(name); |
| 263 | if (existing === undefined) return undefined; |
| 264 | const attrs = toAttrs(existing, organization); |
| 265 | const { labels } = parseDescription(existing.description); |
| 266 | return (yield* ownedBy(id, labels)) ? attrs : Unowned(attrs); |
| 267 | }), |
| 268 | |
| 269 | list: () => |
| 270 | Effect.gen(function* () { |
| 271 | const orgs = yield* listOrgNames(); |
| 272 | const rows: Environment["Attributes"][] = []; |
| 273 | for (const organization of orgs) { |
| 274 | const org = yield* apigee |
| 275 | .getOrganizations({ name: organization }) |
| 276 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 277 | for (const environmentId of org?.environments ?? []) { |
| 278 | const existing = yield* getByName(`${organization}/environments/${environmentId}`); |
| 279 | if (existing !== undefined && hasOwnershipMarker(existing.description)) { |
| 280 | rows.push(toAttrs(existing, organization)); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | return rows; |
| 285 | }), |
| 286 | |
| 287 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 288 | const env = yield* GcpEnvironment.current; |
| 289 | const organization = defaultOrgName(env.project, news.organization); |
| 290 | const environmentId = yield* letterPrefixedId( |
| 291 | id, |
| 292 | news.environmentId, |
| 293 | output?.environmentId, |
| 294 | 255, |
| 295 | ); |
| 296 | const name = resourceName(organization, environmentId); |
| 297 | const ownership = yield* createOwnership(id); |
| 298 | const desiredDescription = encodeDescription(ownership, news.description); |
| 299 | const properties = desiredAttributes(news.properties, ownership); |
| 300 | |
| 301 | let current = yield* getByName(output?.name ?? name); |
| 302 | |
| 303 | if (current === undefined) { |
| 304 | const operation = yield* apigee |
| 305 | .createOrganizationsEnvironments({ |
| 306 | parent: organization, |
| 307 | name: environmentId, |
| 308 | body: { |
| 309 | name: environmentId, |
| 310 | displayName: news.displayName, |
| 311 | description: desiredDescription, |
| 312 | type: news.type, |
| 313 | apiProxyType: news.apiProxyType, |
| 314 | deploymentType: news.deploymentType, |
| 315 | forwardProxyUri: news.forwardProxyUri, |
| 316 | clientIpResolutionConfig: news.clientIpResolutionConfig, |
| 317 | properties: recordToProperties(properties), |
| 318 | nodeConfig: news.nodeConfig, |
| 319 | }, |
| 320 | }) |
| 321 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 322 | if (operation !== undefined) { |
| 323 | yield* waitForOperation(operation); |
| 324 | } |
| 325 | current = yield* getByName(name); |
| 326 | } |
| 327 | |
| 328 | if (current === undefined) { |
| 329 | return yield* new EnvironmentNotResolved({ resourceName: name }); |
| 330 | } |
| 331 | |
| 332 | const observedProperties = propertiesToRecord(current.properties); |
| 333 | const displayChanged = (current.displayName ?? "") !== (news.displayName ?? ""); |
| 334 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 335 | const proxyUriChanged = (current.forwardProxyUri ?? "") !== (news.forwardProxyUri ?? ""); |
| 336 | const ipChanged = !jsonEqual(current.clientIpResolutionConfig, news.clientIpResolutionConfig); |
| 337 | const propertiesChanged = !sameRecord(observedProperties, properties); |
| 338 | const nodeChanged = !jsonEqual(current.nodeConfig, news.nodeConfig); |
| 339 | |
| 340 | if ( |
| 341 | displayChanged || |
| 342 | descriptionChanged || |
| 343 | proxyUriChanged || |
| 344 | ipChanged || |
| 345 | propertiesChanged || |
| 346 | nodeChanged |
| 347 | ) { |
| 348 | current = yield* apigee.updateOrganizationsEnvironments({ |
| 349 | name, |
| 350 | body: { |
| 351 | name: environmentId, |
| 352 | displayName: news.displayName, |
| 353 | description: desiredDescription, |
| 354 | type: current.type, |
| 355 | apiProxyType: current.apiProxyType, |
| 356 | deploymentType: current.deploymentType, |
| 357 | forwardProxyUri: news.forwardProxyUri, |
| 358 | clientIpResolutionConfig: news.clientIpResolutionConfig, |
| 359 | properties: recordToProperties(properties), |
| 360 | nodeConfig: news.nodeConfig ?? current.nodeConfig, |
| 361 | }, |
| 362 | }); |
| 363 | } |
| 364 | |
| 365 | return toAttrs(current, organization); |
| 366 | }), |
| 367 | |
| 368 | delete: Effect.fn(function* ({ output }) { |
| 369 | const operation = yield* apigee |
| 370 | .deleteOrganizationsEnvironments({ name: output.name }) |
| 371 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 372 | if (operation !== undefined) { |
| 373 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 374 | } |
| 375 | }), |
| 376 | }); |