The best way to control Claude Code, Codex and more from any computer or phone
| 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 | attributesToRecord, |
| 12 | childName, |
| 13 | createOwnership, |
| 14 | defaultOrgName, |
| 15 | desiredAttributes, |
| 16 | lastSegment, |
| 17 | letterPrefixedId, |
| 18 | listOrgNames, |
| 19 | orgIdOf, |
| 20 | orgNameOf, |
| 21 | ownedBy, |
| 22 | recordToAttributes, |
| 23 | sameRecord, |
| 24 | sameStringList, |
| 25 | userAttributes, |
| 26 | } from "./operations.ts"; |
| 27 | |
| 28 | export type DevelopersAppProps = { |
| 29 | /** |
| 30 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 31 | * current GCP project id. Immutable — changing it replaces the app. |
| 32 | */ |
| 33 | organization?: string; |
| 34 | /** |
| 35 | * Parent developer email or `organizations/{org}/developers/{email}`. |
| 36 | * Immutable — changing it replaces the app. |
| 37 | */ |
| 38 | developer: string; |
| 39 | /** |
| 40 | * App name (the `{app}` segment of |
| 41 | * `organizations/{org}/developers/{email}/apps/{app}`). If omitted, a |
| 42 | * unique name is generated. Immutable — changing it replaces the app. |
| 43 | */ |
| 44 | appName?: string; |
| 45 | /** |
| 46 | * OAuth 2.0 callback URL. |
| 47 | */ |
| 48 | callbackUrl?: string; |
| 49 | /** |
| 50 | * API products associated with the app. |
| 51 | */ |
| 52 | apiProducts?: string[]; |
| 53 | /** |
| 54 | * Scopes to apply to the app. Must already exist on associated API |
| 55 | * products. |
| 56 | */ |
| 57 | scopes?: string[]; |
| 58 | /** |
| 59 | * Credential status (`approved` or `revoked`). |
| 60 | */ |
| 61 | status?: string; |
| 62 | /** |
| 63 | * Expiration time in milliseconds for the auto-generated consumer key. |
| 64 | * `-1` (default) never expires. Immutable after create. |
| 65 | */ |
| 66 | keyExpiresIn?: string; |
| 67 | /** |
| 68 | * Developer app family. |
| 69 | */ |
| 70 | appFamily?: string; |
| 71 | /** |
| 72 | * Custom attributes. Alchemy ownership attributes are merged in |
| 73 | * automatically. |
| 74 | */ |
| 75 | attributes?: Record<string, string>; |
| 76 | }; |
| 77 | |
| 78 | export type DevelopersApp = Resource< |
| 79 | "GCP.Apigee.DevelopersApp", |
| 80 | DevelopersAppProps, |
| 81 | { |
| 82 | /** Full resource name `organizations/{org}/developers/{email}/apps/{app}`. */ |
| 83 | name: string; |
| 84 | /** App name (last path segment). */ |
| 85 | appName: string; |
| 86 | /** Server-generated UUID. */ |
| 87 | appId: string | undefined; |
| 88 | /** Parent developer email. */ |
| 89 | developer: string; |
| 90 | /** Parent developer id. */ |
| 91 | developerId: string | undefined; |
| 92 | /** Apigee organization id. */ |
| 93 | organization: string; |
| 94 | /** OAuth callback URL. */ |
| 95 | callbackUrl: string | undefined; |
| 96 | /** Associated API products. */ |
| 97 | apiProducts: string[]; |
| 98 | /** Scopes. */ |
| 99 | scopes: string[]; |
| 100 | /** Credential status. */ |
| 101 | status: string | undefined; |
| 102 | /** Key expiration in milliseconds. */ |
| 103 | keyExpiresIn: string | undefined; |
| 104 | /** Developer app family. */ |
| 105 | appFamily: string | undefined; |
| 106 | /** User attributes (Alchemy ownership attributes stripped). */ |
| 107 | attributes: Record<string, string>; |
| 108 | /** Creation time in milliseconds since epoch. */ |
| 109 | createdAt: string | undefined; |
| 110 | /** Last modification time in milliseconds since epoch. */ |
| 111 | lastModifiedAt: string | undefined; |
| 112 | }, |
| 113 | never, |
| 114 | Providers |
| 115 | >; |
| 116 | |
| 117 | /** |
| 118 | * An Apigee app owned by a developer, used to obtain API keys. |
| 119 | * |
| 120 | * Apigee developer apps have no labels field, so Alchemy stamps ownership |
| 121 | * into custom attributes for `list` / nuke. Name and parent developer are |
| 122 | * identity — changing `appName` or `developer` replaces the app. Callback |
| 123 | * URL, products, scopes, status, and attributes update in place. |
| 124 | * |
| 125 | * ### Creating a Developer App |
| 126 | * **Example:** App under an existing developer |
| 127 | * ```typescript |
| 128 | * const app = yield* GCP.Apigee.DevelopersApp("Portal", { |
| 129 | * developer: developer.email, |
| 130 | * callbackUrl: "https://example.com/callback", |
| 131 | * }); |
| 132 | * ``` |
| 133 | * |
| 134 | * **Example:** Named app with attributes |
| 135 | * ```typescript |
| 136 | * const app = yield* GCP.Apigee.DevelopersApp("Portal", { |
| 137 | * developer: "ada@example.com", |
| 138 | * appName: "portal-app", |
| 139 | * attributes: { team: "platform" }, |
| 140 | * }); |
| 141 | * ``` |
| 142 | * |
| 143 | * @resource |
| 144 | * @category Apigee |
| 145 | */ |
| 146 | export const DevelopersApp = Resource<DevelopersApp>("GCP.Apigee.DevelopersApp"); |
| 147 | |
| 148 | export class DevelopersAppNotResolved extends Data.TaggedError( |
| 149 | "GCP.Apigee.DevelopersAppNotResolved", |
| 150 | )<{ |
| 151 | resourceName: string; |
| 152 | }> {} |
| 153 | |
| 154 | const developerEmailOf = (developer: string) => { |
| 155 | if (developer.includes("/developers/")) { |
| 156 | return lastSegment(developer); |
| 157 | } |
| 158 | return developer; |
| 159 | }; |
| 160 | |
| 161 | const developerNameOf = (organization: string, developer: string) => |
| 162 | childName(orgNameOf(organization), "developers", developerEmailOf(developer)); |
| 163 | |
| 164 | const resourceName = (organization: string, developer: string, appName: string) => |
| 165 | `${developerNameOf(organization, developer)}/apps/${appName}`; |
| 166 | |
| 167 | const toAttrs = ( |
| 168 | app: apigee.GoogleCloudApigeeV1DeveloperApp, |
| 169 | organization: string, |
| 170 | developer: string, |
| 171 | ) => { |
| 172 | const appName = app.name ?? ""; |
| 173 | const email = developerEmailOf(developer); |
| 174 | return { |
| 175 | name: resourceName(organization, email, appName), |
| 176 | appName, |
| 177 | appId: app.appId, |
| 178 | developer: email, |
| 179 | developerId: app.developerId, |
| 180 | organization: orgIdOf(organization), |
| 181 | callbackUrl: app.callbackUrl, |
| 182 | apiProducts: app.apiProducts ?? [], |
| 183 | scopes: app.scopes ?? [], |
| 184 | status: app.status, |
| 185 | keyExpiresIn: app.keyExpiresIn, |
| 186 | appFamily: app.appFamily, |
| 187 | attributes: userAttributes(attributesToRecord(app.attributes)), |
| 188 | createdAt: app.createdAt, |
| 189 | lastModifiedAt: app.lastModifiedAt, |
| 190 | }; |
| 191 | }; |
| 192 | |
| 193 | const getByName = (name: string) => |
| 194 | apigee |
| 195 | .getOrganizationsDevelopersApps({ name }) |
| 196 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 197 | |
| 198 | const listDeveloperEmails = (organization: string) => |
| 199 | apigee |
| 200 | .listOrganizationsDevelopers({ |
| 201 | parent: organization, |
| 202 | expand: true, |
| 203 | count: "1000", |
| 204 | }) |
| 205 | .pipe( |
| 206 | Effect.map((page) => |
| 207 | (page.developer ?? []) |
| 208 | .map((developer) => developer.email ?? "") |
| 209 | .filter((email) => email.length > 0), |
| 210 | ), |
| 211 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.succeed([] as string[])), |
| 212 | ); |
| 213 | |
| 214 | export const DevelopersAppProvider = () => |
| 215 | Provider.succeed(DevelopersApp, { |
| 216 | stables: ["name", "appName", "appId", "developer", "organization", "createdAt"], |
| 217 | |
| 218 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 219 | if (!isResolved(news)) return undefined; |
| 220 | const previousName = olds?.appName ?? output?.appName; |
| 221 | const previousDeveloper = olds?.developer ?? output?.developer; |
| 222 | const previousOrg = olds?.organization ?? output?.organization; |
| 223 | const nameChanged = |
| 224 | previousName !== undefined && news.appName !== undefined && news.appName !== previousName; |
| 225 | const developerChanged = |
| 226 | previousDeveloper !== undefined && |
| 227 | developerEmailOf(news.developer) !== developerEmailOf(previousDeveloper); |
| 228 | const orgChanged = |
| 229 | previousOrg !== undefined && |
| 230 | news.organization !== undefined && |
| 231 | orgIdOf(news.organization) !== orgIdOf(previousOrg); |
| 232 | if (nameChanged || developerChanged || orgChanged) { |
| 233 | return { |
| 234 | action: "replace" as const, |
| 235 | deleteFirst: nameChanged && !developerChanged && !orgChanged, |
| 236 | }; |
| 237 | } |
| 238 | return undefined; |
| 239 | }), |
| 240 | |
| 241 | read: Effect.fn(function* ({ id, olds, output }) { |
| 242 | const env = yield* GcpEnvironment.current; |
| 243 | const organization = defaultOrgName(env.project, olds?.organization ?? output?.organization); |
| 244 | const developer = olds?.developer ?? output?.developer; |
| 245 | if (developer === undefined) return undefined; |
| 246 | const appName = yield* letterPrefixedId(id, olds?.appName, output?.appName, 255); |
| 247 | const name = output?.name ?? resourceName(organization, developer, appName); |
| 248 | const existing = yield* getByName(name); |
| 249 | if (existing === undefined) return undefined; |
| 250 | const attrs = toAttrs(existing, organization, developer); |
| 251 | return (yield* ownedBy(id, attributesToRecord(existing.attributes))) ? attrs : Unowned(attrs); |
| 252 | }), |
| 253 | |
| 254 | list: () => |
| 255 | Effect.gen(function* () { |
| 256 | const orgs = yield* listOrgNames(); |
| 257 | const rows: DevelopersApp["Attributes"][] = []; |
| 258 | for (const organization of orgs) { |
| 259 | const emails = yield* listDeveloperEmails(organization); |
| 260 | for (const email of emails) { |
| 261 | const page = yield* apigee |
| 262 | .listOrganizationsDevelopersApps({ |
| 263 | parent: developerNameOf(organization, email), |
| 264 | expand: true, |
| 265 | count: "1000", |
| 266 | }) |
| 267 | .pipe( |
| 268 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 269 | Effect.succeed({ |
| 270 | app: [] as apigee.GoogleCloudApigeeV1DeveloperApp[], |
| 271 | }), |
| 272 | ), |
| 273 | ); |
| 274 | for (const app of page.app ?? []) { |
| 275 | const labels = attributesToRecord(app.attributes); |
| 276 | if (Object.keys(labels).some((key) => key.startsWith("alchemy-"))) { |
| 277 | rows.push(toAttrs(app, organization, email)); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | return rows; |
| 283 | }), |
| 284 | |
| 285 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const organization = defaultOrgName(env.project, news.organization); |
| 288 | const developer = developerEmailOf(news.developer); |
| 289 | const appName = yield* letterPrefixedId(id, news.appName, output?.appName, 255); |
| 290 | const parent = developerNameOf(organization, developer); |
| 291 | const name = `${parent}/apps/${appName}`; |
| 292 | const ownership = yield* createOwnership(id); |
| 293 | const attributes = desiredAttributes(news.attributes, ownership); |
| 294 | |
| 295 | let current = yield* getByName(output?.name ?? name); |
| 296 | |
| 297 | if (current === undefined) { |
| 298 | const created = yield* apigee |
| 299 | .createOrganizationsDevelopersApps({ |
| 300 | parent, |
| 301 | body: { |
| 302 | name: appName, |
| 303 | callbackUrl: news.callbackUrl, |
| 304 | apiProducts: news.apiProducts, |
| 305 | scopes: news.scopes, |
| 306 | status: news.status, |
| 307 | keyExpiresIn: news.keyExpiresIn, |
| 308 | appFamily: news.appFamily, |
| 309 | attributes: recordToAttributes(attributes), |
| 310 | }, |
| 311 | }) |
| 312 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 313 | current = created ?? undefined; |
| 314 | } |
| 315 | |
| 316 | if (current === undefined) { |
| 317 | return yield* new DevelopersAppNotResolved({ resourceName: name }); |
| 318 | } |
| 319 | |
| 320 | const observedAttributes = attributesToRecord(current.attributes); |
| 321 | const callbackChanged = (current.callbackUrl ?? "") !== (news.callbackUrl ?? ""); |
| 322 | const productsChanged = !sameStringList(current.apiProducts, news.apiProducts); |
| 323 | const scopesChanged = !sameStringList(current.scopes, news.scopes); |
| 324 | const statusChanged = news.status !== undefined && (current.status ?? "") !== news.status; |
| 325 | const familyChanged = (current.appFamily ?? "") !== (news.appFamily ?? ""); |
| 326 | const attributesChanged = !sameRecord(observedAttributes, attributes); |
| 327 | |
| 328 | if ( |
| 329 | callbackChanged || |
| 330 | productsChanged || |
| 331 | scopesChanged || |
| 332 | statusChanged || |
| 333 | familyChanged || |
| 334 | attributesChanged |
| 335 | ) { |
| 336 | current = yield* apigee.updateOrganizationsDevelopersApps({ |
| 337 | name, |
| 338 | body: { |
| 339 | name: appName, |
| 340 | callbackUrl: news.callbackUrl, |
| 341 | apiProducts: news.apiProducts, |
| 342 | scopes: news.scopes, |
| 343 | status: news.status ?? current.status, |
| 344 | appFamily: news.appFamily, |
| 345 | attributes: recordToAttributes(attributes), |
| 346 | }, |
| 347 | }); |
| 348 | } |
| 349 | |
| 350 | return toAttrs(current, organization, developer); |
| 351 | }), |
| 352 | |
| 353 | delete: Effect.fn(function* ({ output }) { |
| 354 | yield* apigee |
| 355 | .deleteOrganizationsDevelopersApps({ name: output.name }) |
| 356 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 357 | }), |
| 358 | }); |