The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as eventarc from "@distilled.cloud/gcp/eventarc_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | compact, |
| 14 | collectPages, |
| 15 | expandResource, |
| 16 | hasAlchemyLabelKeys, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | resourceName, |
| 21 | rfc1035, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | retryOnTransient, |
| 25 | waitForOperation, |
| 26 | waitUntilGone, |
| 27 | waitUntilPresent, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "channelConnections"; |
| 31 | |
| 32 | export type ChannelConnectionProps = { |
| 33 | /** |
| 34 | * Channel connection id (the `{channelConnection}` segment of |
| 35 | * `projects/{project}/locations/{location}/channelConnections/{channelConnection}`). |
| 36 | * If omitted, a unique name is generated. Must match |
| 37 | * `[a-z]([a-z0-9-]*[a-z0-9])?` and be 1-63 characters. Immutable — |
| 38 | * changing it replaces the connection. |
| 39 | */ |
| 40 | channelConnectionId?: string; |
| 41 | /** |
| 42 | * Eventarc location (`us-central1`, `us-east1`, …). Immutable — |
| 43 | * changing it replaces the connection. `US-CENTRAL1` is accepted and |
| 44 | * normalized to `us-central1`. |
| 45 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 46 | * region, else `us-central1`) |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * Subscriber Channel this connection binds, as |
| 51 | * `projects/{project}/locations/{location}/channels/{channel}` or the |
| 52 | * `{channel}` segment. Created in the provider project; the channel |
| 53 | * lives in the subscriber project. Immutable — changing it replaces |
| 54 | * the connection. |
| 55 | */ |
| 56 | channel: string; |
| 57 | /** |
| 58 | * Activation token from the subscriber Channel. Input only — used |
| 59 | * during create to bind the channel to this provider project, then |
| 60 | * discarded. Omit when the API no longer requires it. |
| 61 | */ |
| 62 | activationToken?: string; |
| 63 | /** |
| 64 | * User labels. Alchemy ownership labels are merged in automatically. |
| 65 | * The ChannelConnection API has no update method, so changing labels |
| 66 | * replaces the connection. |
| 67 | */ |
| 68 | labels?: Record<string, string>; |
| 69 | }; |
| 70 | |
| 71 | export type ChannelConnection = Resource< |
| 72 | "GCP.Eventarc.ChannelConnection", |
| 73 | ChannelConnectionProps, |
| 74 | { |
| 75 | /** Full resource name `projects/{project}/locations/{location}/channelConnections/{channelConnection}`. */ |
| 76 | name: string; |
| 77 | /** Channel connection id (last path segment). */ |
| 78 | channelConnectionId: string; |
| 79 | /** Project id. */ |
| 80 | project: string; |
| 81 | /** Location id (`us-central1`, …). */ |
| 82 | location: string; |
| 83 | /** Bound subscriber Channel resource name. */ |
| 84 | channel: string | undefined; |
| 85 | /** User labels (Alchemy ownership labels stripped). */ |
| 86 | labels: Record<string, string>; |
| 87 | /** Server-assigned UUID4, stable until delete. */ |
| 88 | uid: string | undefined; |
| 89 | /** RFC3339 creation timestamp. */ |
| 90 | createTime: string | undefined; |
| 91 | /** RFC3339 last-update timestamp. */ |
| 92 | updateTime: string | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | >; |
| 97 | |
| 98 | /** |
| 99 | * An Eventarc ChannelConnection that binds a third-party event provider |
| 100 | * to a subscriber Channel. Created in the provider project using the |
| 101 | * subscriber Channel's activation token. |
| 102 | * |
| 103 | * There is no patch API — `channelConnectionId`, `location`, `channel`, |
| 104 | * and labels are all identity. Changing any of them replaces the |
| 105 | * connection. |
| 106 | * |
| 107 | * ### Creating a ChannelConnection |
| 108 | * **Example:** Bind a subscriber channel |
| 109 | * ```typescript |
| 110 | * const connection = yield* GCP.Eventarc.ChannelConnection("Partner", { |
| 111 | * location: "us-central1", |
| 112 | * channel: |
| 113 | * "projects/subscriber/locations/us-central1/channels/datadog", |
| 114 | * activationToken: channel.activationToken, |
| 115 | * labels: { env: "prod" }, |
| 116 | * }); |
| 117 | * ``` |
| 118 | * |
| 119 | * @resource |
| 120 | * @category Eventarc |
| 121 | */ |
| 122 | export const ChannelConnection = Resource<ChannelConnection>("GCP.Eventarc.ChannelConnection"); |
| 123 | |
| 124 | const toAttrs = (connection: eventarc.ChannelConnection, project: string, region: string) => { |
| 125 | const name = connection.name ?? ""; |
| 126 | const parsed = parseName(name, COLLECTION, region); |
| 127 | return { |
| 128 | name, |
| 129 | channelConnectionId: parsed.id, |
| 130 | project: parsed.project || project, |
| 131 | location: parsed.location, |
| 132 | channel: connection.channel, |
| 133 | labels: userLabels(connection.labels), |
| 134 | uid: connection.uid, |
| 135 | createTime: connection.createTime, |
| 136 | updateTime: connection.updateTime, |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | const getByName = (name: string) => |
| 141 | eventarc |
| 142 | .getProjectsLocationsChannelConnections({ name }) |
| 143 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 144 | |
| 145 | export const ChannelConnectionProvider = () => |
| 146 | Provider.succeed(ChannelConnection, { |
| 147 | stables: ["name", "channelConnectionId", "project", "location", "channel", "uid", "createTime"], |
| 148 | |
| 149 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 150 | if (!isResolved(news)) return undefined; |
| 151 | const env = yield* GcpEnvironment.current; |
| 152 | const previousId = olds?.channelConnectionId ?? output?.channelConnectionId; |
| 153 | const nextId = news.channelConnectionId |
| 154 | ? rfc1035(news.channelConnectionId, "channel-connection") |
| 155 | : previousId; |
| 156 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 157 | const nextLocation = normalizeLocation( |
| 158 | news.location ?? olds?.location ?? output?.location, |
| 159 | env.region, |
| 160 | ); |
| 161 | const previousChannel = olds?.channel ?? output?.channel ?? ""; |
| 162 | const nextChannel = news.channel; |
| 163 | const previousLabels = toLabels(olds?.labels ?? output?.labels); |
| 164 | const nextLabels = toLabels(news.labels); |
| 165 | const { upsert, removed } = diffLabels(previousLabels, nextLabels); |
| 166 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 167 | const identityChanged = |
| 168 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 169 | previousLocation !== nextLocation || |
| 170 | (previousChannel.length > 0 && previousChannel !== nextChannel); |
| 171 | if (!identityChanged && !labelsChanged) return undefined; |
| 172 | const samePhysical = |
| 173 | previousId !== undefined && |
| 174 | nextId !== undefined && |
| 175 | previousId === nextId && |
| 176 | previousLocation === nextLocation; |
| 177 | return { |
| 178 | action: "replace" as const, |
| 179 | deleteFirst: samePhysical, |
| 180 | }; |
| 181 | }), |
| 182 | |
| 183 | read: Effect.fn(function* ({ id, olds, output }) { |
| 184 | const env = yield* GcpEnvironment.current; |
| 185 | const channelConnectionId = yield* toPhysicalId( |
| 186 | id, |
| 187 | olds?.channelConnectionId, |
| 188 | output?.channelConnectionId, |
| 189 | "channel-connection", |
| 190 | ); |
| 191 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 192 | const name = |
| 193 | output?.name ?? resourceName(env.project, location, COLLECTION, channelConnectionId); |
| 194 | const existing = yield* getByName(name); |
| 195 | if (existing === undefined) return undefined; |
| 196 | const attrs = toAttrs(existing, env.project, env.region); |
| 197 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 198 | }), |
| 199 | |
| 200 | list: () => |
| 201 | Effect.gen(function* () { |
| 202 | const env = yield* GcpEnvironment.current; |
| 203 | const items = yield* collectPages( |
| 204 | eventarc.listProjectsLocationsChannelConnections.pages({ |
| 205 | parent: parentOf(env.project, "-"), |
| 206 | pageSize: 1000, |
| 207 | }), |
| 208 | (page) => page.channelConnections, |
| 209 | ); |
| 210 | return items |
| 211 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 212 | .map((item) => toAttrs(item, env.project, env.region)); |
| 213 | }), |
| 214 | |
| 215 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const channelConnectionId = yield* toPhysicalId( |
| 218 | id, |
| 219 | news.channelConnectionId, |
| 220 | output?.channelConnectionId, |
| 221 | "channel-connection", |
| 222 | ); |
| 223 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 224 | const name = resourceName(env.project, location, COLLECTION, channelConnectionId); |
| 225 | const desiredLabels = { |
| 226 | ...toLabels(news.labels), |
| 227 | ...(yield* createInternalLabels(id)), |
| 228 | }; |
| 229 | const channel = expandResource(news.channel, env.project, location, "channels"); |
| 230 | |
| 231 | let current = yield* getByName(name); |
| 232 | |
| 233 | if (current === undefined) { |
| 234 | const created = yield* eventarc |
| 235 | .createProjectsLocationsChannelConnections({ |
| 236 | parent: parentOf(env.project, location), |
| 237 | channelConnectionId, |
| 238 | body: compact({ |
| 239 | name, |
| 240 | channel, |
| 241 | activationToken: news.activationToken, |
| 242 | labels: desiredLabels, |
| 243 | }), |
| 244 | }) |
| 245 | .pipe( |
| 246 | Effect.retry({ |
| 247 | while: (error) => error._tag === "Conflict", |
| 248 | times: 5, |
| 249 | schedule: Schedule.spaced("2 seconds"), |
| 250 | }), |
| 251 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 252 | ); |
| 253 | if (created !== undefined) { |
| 254 | yield* waitForOperation(created); |
| 255 | } |
| 256 | current = yield* waitUntilPresent(getByName(name), name); |
| 257 | } |
| 258 | |
| 259 | return toAttrs(current, env.project, env.region); |
| 260 | }), |
| 261 | |
| 262 | delete: Effect.fn(function* ({ output }) { |
| 263 | yield* retryOnTransient( |
| 264 | Effect.gen(function* () { |
| 265 | const existing = yield* getByName(output.name); |
| 266 | if (existing === undefined) return; |
| 267 | const operation = yield* eventarc |
| 268 | .deleteProjectsLocationsChannelConnections({ |
| 269 | name: output.name, |
| 270 | }) |
| 271 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 272 | if (operation !== undefined) { |
| 273 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 274 | } |
| 275 | }), |
| 276 | ).pipe( |
| 277 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 278 | getByName(output.name).pipe( |
| 279 | Effect.flatMap((current) => (current === undefined ? Effect.void : Effect.fail(error))), |
| 280 | ), |
| 281 | ), |
| 282 | ); |
| 283 | yield* waitUntilGone(getByName(output.name), output.name); |
| 284 | }), |
| 285 | }); |