The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as apihub from "@distilled.cloud/gcp/apihub_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 Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | ApihubNotResolved, |
| 12 | DEFAULT_DEPLOYMENT_TYPE, |
| 13 | encodeOwnership, |
| 14 | hasOwnershipMarker, |
| 15 | locationParent, |
| 16 | MAX_LONG_ID_LENGTH, |
| 17 | normalizeLocation, |
| 18 | ownedByAlchemy, |
| 19 | ownershipLabels, |
| 20 | parseName, |
| 21 | parseOwnership, |
| 22 | replaceOnIdentity, |
| 23 | sameJson, |
| 24 | sameStringList, |
| 25 | sameText, |
| 26 | toPhysicalId, |
| 27 | updateMaskOf, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | type AttributeValues = apihub.GoogleCloudApihubV1AttributeValues; |
| 31 | type AttributeValuesMap = apihub.GoogleCloudApihubV1AttributeValuesMap; |
| 32 | type Documentation = apihub.GoogleCloudApihubV1Documentation; |
| 33 | |
| 34 | export type DeploymentProps = { |
| 35 | /** |
| 36 | * Deployment id (the `{deployment}` segment of |
| 37 | * `projects/{project}/locations/{location}/deployments/{deployment}`). |
| 38 | * If omitted, a unique id is generated. Immutable — changing it replaces |
| 39 | * the deployment. |
| 40 | */ |
| 41 | deploymentId?: string; |
| 42 | /** |
| 43 | * Location of the API Hub instance (`us-central1`, …). Immutable — |
| 44 | * changing it replaces the deployment. `US-CENTRAL1` is accepted and |
| 45 | * normalized to `us-central1`. |
| 46 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * Human-readable display name. |
| 51 | */ |
| 52 | displayName?: string; |
| 53 | /** |
| 54 | * Human-readable description. API Hub deployments have no labels |
| 55 | * field, so Alchemy stamps ownership into a `[alchemy …]` prefix and |
| 56 | * strips it from attributes. |
| 57 | */ |
| 58 | description?: string; |
| 59 | /** |
| 60 | * Documentation URI for the deployment. |
| 61 | */ |
| 62 | documentation?: Documentation; |
| 63 | /** |
| 64 | * Deployment type (`system-deployment-type` enum). Defaults to the |
| 65 | * `others` allowed value. |
| 66 | */ |
| 67 | deploymentType?: AttributeValues; |
| 68 | /** |
| 69 | * Resource URI that identifies the deployment in its gateway. |
| 70 | */ |
| 71 | resourceUri: string; |
| 72 | /** |
| 73 | * Endpoints at which this deployment listens (URIs, hostnames, or IPs). |
| 74 | */ |
| 75 | endpoints: string[]; |
| 76 | /** |
| 77 | * SLO attribute values. |
| 78 | */ |
| 79 | slo?: AttributeValues; |
| 80 | /** |
| 81 | * Environment attribute values. |
| 82 | */ |
| 83 | environment?: AttributeValues; |
| 84 | /** |
| 85 | * User-defined attributes keyed by attribute resource name. |
| 86 | */ |
| 87 | attributes?: AttributeValuesMap; |
| 88 | /** |
| 89 | * Source project or org identifier. |
| 90 | */ |
| 91 | sourceProject?: string; |
| 92 | /** |
| 93 | * Source environment (`prod`, `dev`, `staging`, …). |
| 94 | */ |
| 95 | sourceEnvironment?: string; |
| 96 | /** |
| 97 | * Management URL attribute values. |
| 98 | */ |
| 99 | managementUrl?: AttributeValues; |
| 100 | /** |
| 101 | * Source URI attribute values. |
| 102 | */ |
| 103 | sourceUri?: AttributeValues; |
| 104 | }; |
| 105 | |
| 106 | export type Deployment = Resource< |
| 107 | "GCP.ApiHub.Deployment", |
| 108 | DeploymentProps, |
| 109 | { |
| 110 | /** Full resource name. */ |
| 111 | name: string; |
| 112 | /** Deployment id (last path segment). */ |
| 113 | deploymentId: string; |
| 114 | /** Project id. */ |
| 115 | project: string; |
| 116 | /** Location id. */ |
| 117 | location: string; |
| 118 | /** Display name. */ |
| 119 | displayName: string | undefined; |
| 120 | /** User description with the Alchemy ownership prefix stripped. */ |
| 121 | description: string | undefined; |
| 122 | /** Documentation. */ |
| 123 | documentation: Documentation | undefined; |
| 124 | /** Deployment type. */ |
| 125 | deploymentType: AttributeValues | undefined; |
| 126 | /** Gateway resource URI. */ |
| 127 | resourceUri: string | undefined; |
| 128 | /** Listening endpoints. */ |
| 129 | endpoints: string[]; |
| 130 | /** Linked API versions. */ |
| 131 | apiVersions: string[]; |
| 132 | /** SLO attribute. */ |
| 133 | slo: AttributeValues | undefined; |
| 134 | /** Environment attribute. */ |
| 135 | environment: AttributeValues | undefined; |
| 136 | /** User-defined attributes. */ |
| 137 | attributes: AttributeValuesMap | undefined; |
| 138 | /** Source metadata. */ |
| 139 | sourceMetadata: apihub.GoogleCloudApihubV1SourceMetadataList | undefined; |
| 140 | /** Source project. */ |
| 141 | sourceProject: string | undefined; |
| 142 | /** Source environment. */ |
| 143 | sourceEnvironment: string | undefined; |
| 144 | /** Management URL. */ |
| 145 | managementUrl: AttributeValues | undefined; |
| 146 | /** Source URI. */ |
| 147 | sourceUri: AttributeValues | undefined; |
| 148 | /** RFC3339 creation timestamp. */ |
| 149 | createTime: string | undefined; |
| 150 | /** RFC3339 last-update timestamp. */ |
| 151 | updateTime: string | undefined; |
| 152 | }, |
| 153 | never, |
| 154 | Providers |
| 155 | >; |
| 156 | |
| 157 | /** |
| 158 | * An API Hub deployment — a gateway, proxy, or other runtime that hosts |
| 159 | * APIs. Deployments are root-level entities and exist independently of |
| 160 | * any API. |
| 161 | * |
| 162 | * API Hub deployments have no labels, so Alchemy stamps ownership into |
| 163 | * the description for `list` / nuke. Location and deployment id are |
| 164 | * identity — changing them replaces the deployment. Display name, |
| 165 | * description, type, URI, endpoints, and related attributes update in |
| 166 | * place. |
| 167 | * |
| 168 | * ### Creating a Deployment |
| 169 | * **Example:** Generated id |
| 170 | * ```typescript |
| 171 | * const deployment = yield* GCP.ApiHub.Deployment("Orders", { |
| 172 | * resourceUri: "organizations/cymbal/environments/staging/apis/orders", |
| 173 | * endpoints: ["https://orders.example.com"], |
| 174 | * }); |
| 175 | * ``` |
| 176 | * |
| 177 | * **Example:** Named deployment with type and description |
| 178 | * ```typescript |
| 179 | * const deployment = yield* GCP.ApiHub.Deployment("Orders", { |
| 180 | * deploymentId: "orders-staging", |
| 181 | * displayName: "orders staging", |
| 182 | * description: "checkout proxy", |
| 183 | * resourceUri: "organizations/cymbal/environments/staging/apis/orders", |
| 184 | * endpoints: ["https://orders.example.com"], |
| 185 | * deploymentType: { enumValues: { values: [{ id: "apigee" }] } }, |
| 186 | * }); |
| 187 | * ``` |
| 188 | * |
| 189 | * ### Updating a Deployment |
| 190 | * **Example:** Change description and endpoints |
| 191 | * ```typescript |
| 192 | * const deployment = yield* GCP.ApiHub.Deployment("Orders", { |
| 193 | * resourceUri: "organizations/cymbal/environments/staging/apis/orders", |
| 194 | * endpoints: ["https://orders.example.com", "https://orders-alt.example.com"], |
| 195 | * description: "checkout proxy (updated)", |
| 196 | * }); |
| 197 | * ``` |
| 198 | * |
| 199 | * @resource |
| 200 | * @category ApiHub |
| 201 | */ |
| 202 | export const Deployment = Resource<Deployment>("GCP.ApiHub.Deployment"); |
| 203 | |
| 204 | const resourceName = (project: string, location: string, deploymentId: string) => |
| 205 | `${locationParent(project, location)}/deployments/${deploymentId}`; |
| 206 | |
| 207 | const toAttrs = ( |
| 208 | deployment: apihub.GoogleCloudApihubV1Deployment, |
| 209 | project: string, |
| 210 | region: string, |
| 211 | ) => { |
| 212 | const name = deployment.name ?? ""; |
| 213 | const parsed = parseName(name, "deployments", region); |
| 214 | const { text } = parseOwnership(deployment.description); |
| 215 | return { |
| 216 | name, |
| 217 | deploymentId: parsed.id, |
| 218 | project: parsed.project || project, |
| 219 | location: parsed.location, |
| 220 | displayName: deployment.displayName, |
| 221 | description: text, |
| 222 | documentation: deployment.documentation, |
| 223 | deploymentType: deployment.deploymentType, |
| 224 | resourceUri: deployment.resourceUri, |
| 225 | endpoints: [...(deployment.endpoints ?? [])], |
| 226 | apiVersions: [...(deployment.apiVersions ?? [])], |
| 227 | slo: deployment.slo, |
| 228 | environment: deployment.environment, |
| 229 | attributes: deployment.attributes, |
| 230 | sourceMetadata: deployment.sourceMetadata, |
| 231 | sourceProject: deployment.sourceProject, |
| 232 | sourceEnvironment: deployment.sourceEnvironment, |
| 233 | managementUrl: deployment.managementUrl, |
| 234 | sourceUri: deployment.sourceUri, |
| 235 | createTime: deployment.createTime, |
| 236 | updateTime: deployment.updateTime, |
| 237 | }; |
| 238 | }; |
| 239 | |
| 240 | const getByName = (name: string) => |
| 241 | name.length === 0 |
| 242 | ? Effect.undefined |
| 243 | : apihub |
| 244 | .getProjectsLocationsDeployments({ name }) |
| 245 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 246 | |
| 247 | const listAt = (parent: string, project: string, region: string) => |
| 248 | apihub.listProjectsLocationsDeployments.pages({ parent, pageSize: 1000 }).pipe( |
| 249 | Stream.flatMap((page) => Stream.fromIterable(page.deployments ?? [])), |
| 250 | Stream.filter((item) => hasOwnershipMarker(item.description)), |
| 251 | Stream.map((item) => toAttrs(item, project, region)), |
| 252 | Stream.runCollect, |
| 253 | Effect.map((chunk) => Array.from(chunk)), |
| 254 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 255 | ); |
| 256 | |
| 257 | const toBody = ( |
| 258 | news: DeploymentProps, |
| 259 | description: string, |
| 260 | displayName: string, |
| 261 | ): apihub.GoogleCloudApihubV1Deployment => ({ |
| 262 | displayName, |
| 263 | description, |
| 264 | documentation: news.documentation, |
| 265 | deploymentType: news.deploymentType ?? DEFAULT_DEPLOYMENT_TYPE, |
| 266 | resourceUri: news.resourceUri, |
| 267 | endpoints: news.endpoints, |
| 268 | slo: news.slo, |
| 269 | environment: news.environment, |
| 270 | attributes: news.attributes, |
| 271 | sourceProject: news.sourceProject, |
| 272 | sourceEnvironment: news.sourceEnvironment, |
| 273 | managementUrl: news.managementUrl, |
| 274 | sourceUri: news.sourceUri, |
| 275 | }); |
| 276 | |
| 277 | export const DeploymentProvider = () => |
| 278 | Provider.succeed(Deployment, { |
| 279 | stables: ["name", "deploymentId", "project", "location", "createTime"], |
| 280 | |
| 281 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 282 | if (!isResolved(news)) return undefined; |
| 283 | const env = yield* GcpEnvironment.current; |
| 284 | return replaceOnIdentity({ |
| 285 | previousId: olds?.deploymentId ?? output?.deploymentId, |
| 286 | nextId: news.deploymentId ?? olds?.deploymentId ?? output?.deploymentId, |
| 287 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 288 | nextLocation: normalizeLocation( |
| 289 | news.location ?? olds?.location ?? output?.location, |
| 290 | env.region, |
| 291 | ), |
| 292 | }); |
| 293 | }), |
| 294 | |
| 295 | read: Effect.fn(function* ({ id, olds, output }) { |
| 296 | const env = yield* GcpEnvironment.current; |
| 297 | const deploymentId = yield* toPhysicalId( |
| 298 | id, |
| 299 | olds?.deploymentId, |
| 300 | output?.deploymentId, |
| 301 | MAX_LONG_ID_LENGTH, |
| 302 | ); |
| 303 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 304 | const name = output?.name ?? resourceName(env.project, location, deploymentId); |
| 305 | const existing = yield* getByName(name); |
| 306 | if (existing === undefined) return undefined; |
| 307 | const attrs = toAttrs(existing, env.project, env.region); |
| 308 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 309 | }), |
| 310 | |
| 311 | list: () => |
| 312 | Effect.gen(function* () { |
| 313 | const env = yield* GcpEnvironment.current; |
| 314 | return yield* listAt(locationParent(env.project, env.region), env.project, env.region); |
| 315 | }), |
| 316 | |
| 317 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 318 | const env = yield* GcpEnvironment.current; |
| 319 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 320 | const deploymentId = yield* toPhysicalId( |
| 321 | id, |
| 322 | news.deploymentId, |
| 323 | output?.deploymentId, |
| 324 | MAX_LONG_ID_LENGTH, |
| 325 | ); |
| 326 | const name = resourceName(env.project, location, deploymentId); |
| 327 | const ownership = yield* ownershipLabels(id); |
| 328 | const description = encodeOwnership(ownership, news.description); |
| 329 | const displayName = news.displayName ?? deploymentId; |
| 330 | const parent = locationParent(env.project, location); |
| 331 | |
| 332 | let current = yield* getByName(output?.name ?? name); |
| 333 | |
| 334 | if (current === undefined) { |
| 335 | const created = yield* apihub |
| 336 | .createProjectsLocationsDeployments({ |
| 337 | parent, |
| 338 | deploymentId, |
| 339 | body: toBody(news, description, displayName), |
| 340 | }) |
| 341 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 342 | current = created ?? (yield* getByName(name)); |
| 343 | } |
| 344 | |
| 345 | if (current === undefined) { |
| 346 | return yield* new ApihubNotResolved({ resourceName: name }); |
| 347 | } |
| 348 | |
| 349 | const currentName = current.name ?? name; |
| 350 | const observed = parseOwnership(current.description).text; |
| 351 | const displayChanged = !sameText(current.displayName, displayName); |
| 352 | const descriptionChanged = !sameText(observed, news.description); |
| 353 | const documentationChanged = !sameJson(current.documentation, news.documentation); |
| 354 | const typeChanged = !sameJson( |
| 355 | current.deploymentType, |
| 356 | news.deploymentType ?? DEFAULT_DEPLOYMENT_TYPE, |
| 357 | ); |
| 358 | const uriChanged = !sameText(current.resourceUri, news.resourceUri); |
| 359 | const endpointsChanged = !sameStringList(current.endpoints, news.endpoints); |
| 360 | const sloChanged = !sameJson(current.slo, news.slo); |
| 361 | const environmentChanged = !sameJson(current.environment, news.environment); |
| 362 | const attributesChanged = !sameJson(current.attributes, news.attributes); |
| 363 | const sourceProjectChanged = !sameText(current.sourceProject, news.sourceProject); |
| 364 | const sourceEnvironmentChanged = !sameText(current.sourceEnvironment, news.sourceEnvironment); |
| 365 | const managementUrlChanged = !sameJson(current.managementUrl, news.managementUrl); |
| 366 | const sourceUriChanged = !sameJson(current.sourceUri, news.sourceUri); |
| 367 | |
| 368 | const updateMask = updateMaskOf( |
| 369 | displayChanged ? "display_name" : undefined, |
| 370 | descriptionChanged ? "description" : undefined, |
| 371 | documentationChanged ? "documentation" : undefined, |
| 372 | typeChanged ? "deployment_type" : undefined, |
| 373 | uriChanged ? "resource_uri" : undefined, |
| 374 | endpointsChanged ? "endpoints" : undefined, |
| 375 | sloChanged ? "slo" : undefined, |
| 376 | environmentChanged ? "environment" : undefined, |
| 377 | attributesChanged ? "attributes" : undefined, |
| 378 | sourceProjectChanged ? "source_project" : undefined, |
| 379 | sourceEnvironmentChanged ? "source_environment" : undefined, |
| 380 | managementUrlChanged ? "management_url" : undefined, |
| 381 | sourceUriChanged ? "source_uri" : undefined, |
| 382 | ); |
| 383 | |
| 384 | if (updateMask.length > 0) { |
| 385 | current = yield* apihub.patchProjectsLocationsDeployments({ |
| 386 | name: currentName, |
| 387 | updateMask, |
| 388 | body: { |
| 389 | name: currentName, |
| 390 | ...toBody(news, description, displayName), |
| 391 | }, |
| 392 | }); |
| 393 | } |
| 394 | |
| 395 | return toAttrs(current, env.project, env.region); |
| 396 | }), |
| 397 | |
| 398 | delete: Effect.fn(function* ({ output }) { |
| 399 | yield* apihub |
| 400 | .deleteProjectsLocationsDeployments({ name: output.name }) |
| 401 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 402 | }), |
| 403 | }); |