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 { GcpEnvironment } from "../Environment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | ApihubNotResolved, |
| 11 | type AttributeValuesMap, |
| 12 | createOwnership, |
| 13 | encodeOwnership, |
| 14 | hasOwnershipMarker, |
| 15 | listDependencies, |
| 16 | locationParent, |
| 17 | normalizeLocation, |
| 18 | ownedByAlchemy, |
| 19 | parseOwnership, |
| 20 | parseResourceName, |
| 21 | replaceOnIdentity, |
| 22 | sameJson, |
| 23 | sameText, |
| 24 | toPhysicalId, |
| 25 | updateMaskOf, |
| 26 | } from "./internal.ts"; |
| 27 | |
| 28 | export type DependencyEntity = { |
| 29 | /** |
| 30 | * Operation resource name |
| 31 | * `projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}`. |
| 32 | */ |
| 33 | operationResourceName?: string; |
| 34 | /** |
| 35 | * External API resource name |
| 36 | * `projects/{project}/locations/{location}/externalApis/{externalApi}`. |
| 37 | */ |
| 38 | externalApiResourceName?: string; |
| 39 | }; |
| 40 | |
| 41 | export type DependencyProps = { |
| 42 | /** |
| 43 | * Dependency id (the `{dependency}` segment of |
| 44 | * `projects/{project}/locations/{location}/dependencies/{dependency}`). |
| 45 | * If omitted, a unique id is generated. Immutable — changing it |
| 46 | * replaces the dependency. |
| 47 | */ |
| 48 | dependencyId?: string; |
| 49 | /** |
| 50 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 51 | * dependency. |
| 52 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 53 | */ |
| 54 | location?: string; |
| 55 | /** |
| 56 | * Human-readable description. Dependencies have no labels field, so |
| 57 | * Alchemy stamps ownership into a `[alchemy …]` prefix and strips it |
| 58 | * from attributes. |
| 59 | */ |
| 60 | description?: string; |
| 61 | /** |
| 62 | * Consumer entity. Immutable — changing it replaces the dependency. |
| 63 | */ |
| 64 | consumer: DependencyEntity; |
| 65 | /** |
| 66 | * Supplier entity. Immutable — changing it replaces the dependency. |
| 67 | */ |
| 68 | supplier: DependencyEntity; |
| 69 | /** |
| 70 | * User-defined attributes keyed by attribute resource name. |
| 71 | */ |
| 72 | attributes?: AttributeValuesMap; |
| 73 | }; |
| 74 | |
| 75 | export type Dependency = Resource< |
| 76 | "GCP.ApiHub.Dependency", |
| 77 | DependencyProps, |
| 78 | { |
| 79 | /** Full resource name. */ |
| 80 | name: string; |
| 81 | /** Dependency id (last path segment). */ |
| 82 | dependencyId: string; |
| 83 | /** Project id. */ |
| 84 | project: string; |
| 85 | /** Location id. */ |
| 86 | location: string; |
| 87 | /** User description with the Alchemy ownership prefix stripped. */ |
| 88 | description: string | undefined; |
| 89 | /** Consumer entity. */ |
| 90 | consumer: DependencyEntity | undefined; |
| 91 | /** Supplier entity. */ |
| 92 | supplier: DependencyEntity | undefined; |
| 93 | /** Discovery mode. */ |
| 94 | discoveryMode: string | undefined; |
| 95 | /** State. */ |
| 96 | state: string | undefined; |
| 97 | /** Error detail if the system detected a problem. */ |
| 98 | errorDetail: apihub.GoogleCloudApihubV1DependencyErrorDetail | undefined; |
| 99 | /** User-defined attributes. */ |
| 100 | attributes: AttributeValuesMap | undefined; |
| 101 | /** RFC3339 creation timestamp. */ |
| 102 | createTime: string | undefined; |
| 103 | /** RFC3339 last-update timestamp. */ |
| 104 | updateTime: string | undefined; |
| 105 | }, |
| 106 | never, |
| 107 | Providers |
| 108 | >; |
| 109 | |
| 110 | /** |
| 111 | * A directed dependency in API Hub from a consumer operation (or external |
| 112 | * API) to a supplier operation (or external API). |
| 113 | * |
| 114 | * Location, id, consumer, and supplier are immutable. Description updates |
| 115 | * in place. Dependencies have no labels field — Alchemy stamps ownership |
| 116 | * into the description. |
| 117 | * |
| 118 | * ### Creating a Dependency |
| 119 | * **Example:** Operation to operation |
| 120 | * ```typescript |
| 121 | * const dependency = yield* GCP.ApiHub.Dependency("Calls", { |
| 122 | * consumer: { operationResourceName: listPets.name }, |
| 123 | * supplier: { operationResourceName: getPet.name }, |
| 124 | * description: "listPets calls getPet", |
| 125 | * }); |
| 126 | * ``` |
| 127 | * |
| 128 | * @resource |
| 129 | * @category ApiHub |
| 130 | */ |
| 131 | export const Dependency = Resource<Dependency>("GCP.ApiHub.Dependency"); |
| 132 | |
| 133 | const resourceName = (project: string, location: string, dependencyId: string) => |
| 134 | `${locationParent(project, location)}/dependencies/${dependencyId}`; |
| 135 | |
| 136 | const toEntity = ( |
| 137 | entity: apihub.GoogleCloudApihubV1DependencyEntityReference | undefined, |
| 138 | ): DependencyEntity | undefined => { |
| 139 | if (entity === undefined) return undefined; |
| 140 | return { |
| 141 | operationResourceName: entity.operationResourceName, |
| 142 | externalApiResourceName: entity.externalApiResourceName, |
| 143 | }; |
| 144 | }; |
| 145 | |
| 146 | const toAttrs = ( |
| 147 | dependency: apihub.GoogleCloudApihubV1Dependency, |
| 148 | project: string, |
| 149 | region: string, |
| 150 | ): Dependency["Attributes"] => { |
| 151 | const name = dependency.name ?? ""; |
| 152 | const parsed = parseResourceName(name, "dependencies", region); |
| 153 | return { |
| 154 | name, |
| 155 | dependencyId: parsed.id, |
| 156 | project: parsed.project || project, |
| 157 | location: parsed.location, |
| 158 | description: parseOwnership(dependency.description).text, |
| 159 | consumer: toEntity(dependency.consumer), |
| 160 | supplier: toEntity(dependency.supplier), |
| 161 | discoveryMode: |
| 162 | dependency.discoveryMode === undefined ? undefined : `${dependency.discoveryMode}`, |
| 163 | state: dependency.state === undefined ? undefined : `${dependency.state}`, |
| 164 | errorDetail: dependency.errorDetail, |
| 165 | attributes: dependency.attributes, |
| 166 | createTime: dependency.createTime, |
| 167 | updateTime: dependency.updateTime, |
| 168 | }; |
| 169 | }; |
| 170 | |
| 171 | const getByName = (name: string) => |
| 172 | name.length === 0 |
| 173 | ? Effect.undefined |
| 174 | : apihub |
| 175 | .getProjectsLocationsDependencies({ name }) |
| 176 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 177 | |
| 178 | const entityOf = ( |
| 179 | entity: DependencyEntity, |
| 180 | ): apihub.GoogleCloudApihubV1DependencyEntityReference => ({ |
| 181 | operationResourceName: entity.operationResourceName, |
| 182 | externalApiResourceName: entity.externalApiResourceName, |
| 183 | }); |
| 184 | |
| 185 | export const DependencyProvider = () => |
| 186 | Provider.succeed(Dependency, { |
| 187 | stables: ["name", "dependencyId", "project", "location", "createTime"], |
| 188 | |
| 189 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 190 | if (!isResolved(news)) return undefined; |
| 191 | const env = yield* GcpEnvironment.current; |
| 192 | return replaceOnIdentity({ |
| 193 | previousId: olds?.dependencyId ?? output?.dependencyId, |
| 194 | nextId: news.dependencyId ?? olds?.dependencyId ?? output?.dependencyId, |
| 195 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 196 | nextLocation: normalizeLocation( |
| 197 | news.location ?? olds?.location ?? output?.location, |
| 198 | env.region, |
| 199 | ), |
| 200 | extra: |
| 201 | !sameJson(news.consumer ?? olds?.consumer, olds?.consumer ?? output?.consumer) || |
| 202 | !sameJson(news.supplier ?? olds?.supplier, olds?.supplier ?? output?.supplier), |
| 203 | }); |
| 204 | }), |
| 205 | |
| 206 | read: Effect.fn(function* ({ id, olds, output }) { |
| 207 | const env = yield* GcpEnvironment.current; |
| 208 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 209 | const dependencyId = yield* toPhysicalId(id, olds?.dependencyId, output?.dependencyId); |
| 210 | const name = output?.name ?? resourceName(env.project, location, dependencyId); |
| 211 | const existing = yield* getByName(name); |
| 212 | if (existing === undefined) return undefined; |
| 213 | const attrs = toAttrs(existing, env.project, env.region); |
| 214 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 215 | }), |
| 216 | |
| 217 | list: () => |
| 218 | Effect.gen(function* () { |
| 219 | const env = yield* GcpEnvironment.current; |
| 220 | const items = yield* listDependencies(locationParent(env.project, env.region)); |
| 221 | return items |
| 222 | .filter((item) => hasOwnershipMarker(item.description)) |
| 223 | .map((item) => toAttrs(item, env.project, env.region)); |
| 224 | }), |
| 225 | |
| 226 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 227 | const env = yield* GcpEnvironment.current; |
| 228 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 229 | const parent = locationParent(env.project, location); |
| 230 | const dependencyId = yield* toPhysicalId(id, news.dependencyId, output?.dependencyId); |
| 231 | const name = output?.name ?? resourceName(env.project, location, dependencyId); |
| 232 | const ownership = yield* createOwnership(id); |
| 233 | const description = encodeOwnership(ownership, news.description); |
| 234 | const consumer = entityOf(news.consumer); |
| 235 | const supplier = entityOf(news.supplier); |
| 236 | |
| 237 | let current = yield* getByName(name); |
| 238 | |
| 239 | if (current === undefined) { |
| 240 | const created = yield* apihub |
| 241 | .createProjectsLocationsDependencies({ |
| 242 | parent, |
| 243 | dependencyId, |
| 244 | body: { |
| 245 | description, |
| 246 | consumer, |
| 247 | supplier, |
| 248 | attributes: news.attributes, |
| 249 | }, |
| 250 | }) |
| 251 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 252 | current = created ?? undefined; |
| 253 | } |
| 254 | |
| 255 | if (current === undefined) { |
| 256 | return yield* new ApihubNotResolved({ resourceName: name }); |
| 257 | } |
| 258 | |
| 259 | const currentName = current.name ?? name; |
| 260 | const descriptionChanged = !sameText(current.description, description); |
| 261 | |
| 262 | if (descriptionChanged) { |
| 263 | current = yield* apihub.patchProjectsLocationsDependencies({ |
| 264 | name: currentName, |
| 265 | updateMask: updateMaskOf("description"), |
| 266 | body: { |
| 267 | name: currentName, |
| 268 | description, |
| 269 | }, |
| 270 | }); |
| 271 | } |
| 272 | |
| 273 | return toAttrs(current, env.project, env.region); |
| 274 | }), |
| 275 | |
| 276 | delete: Effect.fn(function* ({ output }) { |
| 277 | yield* apihub |
| 278 | .deleteProjectsLocationsDependencies({ name: output.name }) |
| 279 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 280 | }), |
| 281 | }); |