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 { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | collectPages, |
| 13 | defaultOrgName, |
| 14 | lastSegment, |
| 15 | listOrgNames, |
| 16 | orgIdOf, |
| 17 | orgNameOf, |
| 18 | waitForOperation, |
| 19 | } from "./operations.ts"; |
| 20 | |
| 21 | const MAX_ID_LENGTH = 32; |
| 22 | |
| 23 | export type EndpointAttachmentProps = { |
| 24 | /** |
| 25 | * Apigee organization id or `organizations/{org}`. Defaults to the |
| 26 | * current GCP project id. Immutable — changing it replaces the |
| 27 | * attachment. |
| 28 | */ |
| 29 | organization?: string; |
| 30 | /** |
| 31 | * Endpoint attachment id (the `{endpoint_attachment}` segment of |
| 32 | * `organizations/{org}/endpointAttachments/{endpoint_attachment}`). |
| 33 | * Must start with a lowercase letter, be 2-32 characters of lowercase |
| 34 | * letters, numbers, or hyphens, and must not end with a hyphen. If |
| 35 | * omitted, a unique `alc-` prefixed name is generated so `list` / nuke |
| 36 | * can find it (the API has no labels or description). Immutable — |
| 37 | * changing it replaces the attachment. |
| 38 | */ |
| 39 | endpointAttachmentId?: string; |
| 40 | /** |
| 41 | * Location of the endpoint attachment. Immutable — changing it replaces |
| 42 | * the attachment. `US-CENTRAL1` is accepted and normalized to |
| 43 | * `us-central1`. |
| 44 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 45 | */ |
| 46 | location?: string; |
| 47 | /** |
| 48 | * Service attachment in |
| 49 | * `projects/{project}/regions/{region}/serviceAttachments/{attachment}`. |
| 50 | * Immutable — changing it replaces the attachment. |
| 51 | */ |
| 52 | serviceAttachment: string; |
| 53 | }; |
| 54 | |
| 55 | export type EndpointAttachment = Resource< |
| 56 | "GCP.Apigee.EndpointAttachment", |
| 57 | EndpointAttachmentProps, |
| 58 | { |
| 59 | /** Full resource name `organizations/{org}/endpointAttachments/{id}`. */ |
| 60 | name: string; |
| 61 | /** Endpoint attachment id (last path segment). */ |
| 62 | endpointAttachmentId: string; |
| 63 | /** Apigee organization id. */ |
| 64 | organization: string; |
| 65 | /** Location. */ |
| 66 | location: string; |
| 67 | /** Service attachment resource name. */ |
| 68 | serviceAttachment: string | undefined; |
| 69 | /** Host that can be used as an HTTP target. */ |
| 70 | host: string | undefined; |
| 71 | /** Server-reported state (`CREATING`, `ACTIVE`, …). */ |
| 72 | state: string | undefined; |
| 73 | /** Connection state to the service attachment. */ |
| 74 | connectionState: string | undefined; |
| 75 | }, |
| 76 | never, |
| 77 | Providers |
| 78 | >; |
| 79 | |
| 80 | /** |
| 81 | * An Apigee endpoint attachment for southbound Private Service Connect. |
| 82 | * |
| 83 | * The API has no labels or description, so Alchemy prefixes generated ids |
| 84 | * with `alc-` so `list` / nuke can find them. All properties are identity — |
| 85 | * changing any of them replaces the attachment. |
| 86 | * |
| 87 | * ### Creating an Endpoint Attachment |
| 88 | * **Example:** Attach a service attachment in us-central1 |
| 89 | * ```typescript |
| 90 | * const attachment = yield* GCP.Apigee.EndpointAttachment("Backend", { |
| 91 | * location: "us-central1", |
| 92 | * serviceAttachment: |
| 93 | * "projects/my-project/regions/us-central1/serviceAttachments/backend", |
| 94 | * }); |
| 95 | * ``` |
| 96 | * |
| 97 | * @resource |
| 98 | * @category Apigee |
| 99 | */ |
| 100 | export const EndpointAttachment = Resource<EndpointAttachment>("GCP.Apigee.EndpointAttachment"); |
| 101 | |
| 102 | export class EndpointAttachmentNotResolved extends Data.TaggedError( |
| 103 | "GCP.Apigee.EndpointAttachmentNotResolved", |
| 104 | )<{ |
| 105 | resourceName: string; |
| 106 | }> {} |
| 107 | |
| 108 | const normalizeLocation = (location: string | undefined, defaultLocation: string) => |
| 109 | (location ?? defaultLocation).toLowerCase(); |
| 110 | |
| 111 | const toId = (id: string, explicit: string | undefined, existing?: string) => |
| 112 | Effect.gen(function* () { |
| 113 | if (explicit !== undefined) return explicit; |
| 114 | if (existing !== undefined) return existing; |
| 115 | const generated = yield* createPhysicalName({ |
| 116 | id, |
| 117 | maxLength: MAX_ID_LENGTH - 4, |
| 118 | lowercase: true, |
| 119 | }); |
| 120 | const body = generated.replace(/-+$/g, ""); |
| 121 | const prefixed = body.startsWith("alc") ? body : `alc-${body}`; |
| 122 | return prefixed.slice(0, MAX_ID_LENGTH).replace(/-+$/g, "") || "alc-a"; |
| 123 | }); |
| 124 | |
| 125 | const resourceName = (organization: string, endpointAttachmentId: string) => |
| 126 | `${orgNameOf(organization)}/endpointAttachments/${endpointAttachmentId}`; |
| 127 | |
| 128 | const toAttrs = ( |
| 129 | attachment: apigee.GoogleCloudApigeeV1EndpointAttachment, |
| 130 | organization: string, |
| 131 | region: string, |
| 132 | ) => { |
| 133 | const name = attachment.name ?? ""; |
| 134 | return { |
| 135 | name, |
| 136 | endpointAttachmentId: lastSegment(name), |
| 137 | organization: orgIdOf(organization), |
| 138 | location: attachment.location ?? region, |
| 139 | serviceAttachment: attachment.serviceAttachment, |
| 140 | host: attachment.host, |
| 141 | state: attachment.state, |
| 142 | connectionState: attachment.connectionState, |
| 143 | }; |
| 144 | }; |
| 145 | |
| 146 | const getByName = (name: string) => |
| 147 | apigee |
| 148 | .getOrganizationsEndpointAttachments({ name }) |
| 149 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 150 | |
| 151 | const isAlchemyId = (endpointAttachmentId: string) => endpointAttachmentId.startsWith("alc"); |
| 152 | |
| 153 | export const EndpointAttachmentProvider = () => |
| 154 | Provider.succeed(EndpointAttachment, { |
| 155 | stables: ["name", "endpointAttachmentId", "organization", "location", "serviceAttachment"], |
| 156 | |
| 157 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 158 | if (!isResolved(news)) return undefined; |
| 159 | const env = yield* GcpEnvironment.current; |
| 160 | const previousId = olds?.endpointAttachmentId ?? output?.endpointAttachmentId; |
| 161 | const previousOrg = olds?.organization ?? output?.organization; |
| 162 | const previousLocation = olds?.location ?? output?.location; |
| 163 | const previousSa = olds?.serviceAttachment ?? output?.serviceAttachment; |
| 164 | const idChanged = |
| 165 | previousId !== undefined && |
| 166 | news.endpointAttachmentId !== undefined && |
| 167 | news.endpointAttachmentId !== previousId; |
| 168 | const orgChanged = |
| 169 | previousOrg !== undefined && |
| 170 | news.organization !== undefined && |
| 171 | orgIdOf(news.organization) !== orgIdOf(previousOrg); |
| 172 | const locationChanged = |
| 173 | previousLocation !== undefined && |
| 174 | normalizeLocation(news.location ?? previousLocation, env.region) !== |
| 175 | normalizeLocation(previousLocation, env.region); |
| 176 | const saChanged = previousSa !== undefined && news.serviceAttachment !== previousSa; |
| 177 | if (idChanged || orgChanged || locationChanged || saChanged) { |
| 178 | return { |
| 179 | action: "replace" as const, |
| 180 | deleteFirst: idChanged && !orgChanged, |
| 181 | }; |
| 182 | } |
| 183 | return undefined; |
| 184 | }), |
| 185 | |
| 186 | read: Effect.fn(function* ({ id, olds, output }) { |
| 187 | const env = yield* GcpEnvironment.current; |
| 188 | const organization = defaultOrgName(env.project, olds?.organization ?? output?.organization); |
| 189 | const endpointAttachmentId = yield* toId( |
| 190 | id, |
| 191 | olds?.endpointAttachmentId, |
| 192 | output?.endpointAttachmentId, |
| 193 | ); |
| 194 | const name = output?.name ?? resourceName(organization, endpointAttachmentId); |
| 195 | const existing = yield* getByName(name); |
| 196 | if (existing === undefined) return undefined; |
| 197 | const attrs = toAttrs(existing, organization, env.region); |
| 198 | return isAlchemyId(attrs.endpointAttachmentId) ? attrs : Unowned(attrs); |
| 199 | }), |
| 200 | |
| 201 | list: () => |
| 202 | Effect.gen(function* () { |
| 203 | const env = yield* GcpEnvironment.current; |
| 204 | const orgs = yield* listOrgNames(); |
| 205 | const rows: EndpointAttachment["Attributes"][] = []; |
| 206 | for (const organization of orgs) { |
| 207 | const attachments = yield* collectPages( |
| 208 | apigee.listOrganizationsEndpointAttachments.pages({ |
| 209 | parent: organization, |
| 210 | pageSize: 1000, |
| 211 | }), |
| 212 | (page) => page.endpointAttachments, |
| 213 | ).pipe( |
| 214 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 215 | Effect.succeed([] as apigee.GoogleCloudApigeeV1EndpointAttachment[]), |
| 216 | ), |
| 217 | ); |
| 218 | for (const attachment of attachments) { |
| 219 | const attrs = toAttrs(attachment, organization, env.region); |
| 220 | if (isAlchemyId(attrs.endpointAttachmentId)) { |
| 221 | rows.push(attrs); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return rows; |
| 226 | }), |
| 227 | |
| 228 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const organization = defaultOrgName(env.project, news.organization); |
| 231 | const endpointAttachmentId = yield* toId( |
| 232 | id, |
| 233 | news.endpointAttachmentId, |
| 234 | output?.endpointAttachmentId, |
| 235 | ); |
| 236 | const name = resourceName(organization, endpointAttachmentId); |
| 237 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 238 | |
| 239 | let current = yield* getByName(output?.name ?? name); |
| 240 | |
| 241 | if (current === undefined) { |
| 242 | const operation = yield* apigee |
| 243 | .createOrganizationsEndpointAttachments({ |
| 244 | parent: organization, |
| 245 | endpointAttachmentId, |
| 246 | body: { |
| 247 | location, |
| 248 | serviceAttachment: news.serviceAttachment, |
| 249 | }, |
| 250 | }) |
| 251 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 252 | if (operation !== undefined) { |
| 253 | yield* waitForOperation(operation); |
| 254 | } |
| 255 | current = yield* getByName(name); |
| 256 | } |
| 257 | |
| 258 | if (current === undefined) { |
| 259 | return yield* new EndpointAttachmentNotResolved({ resourceName: name }); |
| 260 | } |
| 261 | |
| 262 | return toAttrs(current, organization, env.region); |
| 263 | }), |
| 264 | |
| 265 | delete: Effect.fn(function* ({ output }) { |
| 266 | const operation = yield* apigee |
| 267 | .deleteOrganizationsEndpointAttachments({ name: output.name }) |
| 268 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 269 | if (operation !== undefined) { |
| 270 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 271 | } |
| 272 | }), |
| 273 | }); |