The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as spanner from "@distilled.cloud/gcp/spanner_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 Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { |
| 13 | createInternalLabels, |
| 14 | diffLabels, |
| 15 | hasAlchemyLabels, |
| 16 | stripInternalLabels, |
| 17 | toLabels, |
| 18 | } from "../Labels.ts"; |
| 19 | import type { Providers } from "../Providers.ts"; |
| 20 | import { |
| 21 | configIdOf, |
| 22 | configNameOf, |
| 23 | instanceConfigName, |
| 24 | parseResourceName, |
| 25 | retryConcurrentChanges, |
| 26 | toConfigId, |
| 27 | waitForOperation, |
| 28 | } from "./operations.ts"; |
| 29 | |
| 30 | export type ReplicaInfo = { |
| 31 | /** Replica location (e.g. `us-central1`). */ |
| 32 | location?: string; |
| 33 | /** Replica type (`READ_WRITE`, `READ_ONLY`, `WITNESS`). */ |
| 34 | type?: spanner.ReplicaInfoTypeEnum | (string & {}); |
| 35 | /** Whether this location is the default leader. */ |
| 36 | defaultLeaderLocation?: boolean; |
| 37 | }; |
| 38 | |
| 39 | export type InstanceConfigProps = { |
| 40 | /** |
| 41 | * Instance config id (the `{config}` segment of |
| 42 | * `projects/{project}/instanceConfigs/{config}`). User-managed ids must |
| 43 | * start with `custom-` and match `custom-[-a-z0-9]*[a-z0-9]` (2–64 |
| 44 | * characters). If omitted, a unique `custom-` name is generated. |
| 45 | * Immutable — changing it replaces the config. |
| 46 | */ |
| 47 | instanceConfigId?: string; |
| 48 | /** |
| 49 | * User-facing display name. Defaults to the instance config id. |
| 50 | */ |
| 51 | displayName?: string; |
| 52 | /** |
| 53 | * Google-managed base config id (`regional-us-central1`) or full name. |
| 54 | * Immutable — changing it replaces the config. |
| 55 | * @default `regional-{region}` for the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 56 | */ |
| 57 | baseConfig?: string; |
| 58 | /** |
| 59 | * Replica placement. Must include every replica from `baseConfig` plus |
| 60 | * at least one of its optional replicas. When omitted, Alchemy copies |
| 61 | * the base replicas and appends the first optional replica. |
| 62 | * Immutable — changing them replaces the config. |
| 63 | */ |
| 64 | replicas?: ReplicaInfo[]; |
| 65 | /** |
| 66 | * User labels. Alchemy ownership labels are merged in automatically. |
| 67 | */ |
| 68 | labels?: Record<string, string>; |
| 69 | }; |
| 70 | |
| 71 | export type InstanceConfig = Resource< |
| 72 | "GCP.Spanner.InstanceConfig", |
| 73 | InstanceConfigProps, |
| 74 | { |
| 75 | /** Full resource name `projects/{project}/instanceConfigs/{config}`. */ |
| 76 | name: string; |
| 77 | /** Instance config id (last path segment). */ |
| 78 | instanceConfigId: string; |
| 79 | /** Project id. */ |
| 80 | project: string; |
| 81 | /** User-facing display name. */ |
| 82 | displayName: string | undefined; |
| 83 | /** `GOOGLE_MANAGED` or `USER_MANAGED`. */ |
| 84 | configType: string | undefined; |
| 85 | /** Replica placement. */ |
| 86 | replicas: ReplicaInfo[]; |
| 87 | /** Optional replicas advertised by Google-managed configs. */ |
| 88 | optionalReplicas: ReplicaInfo[]; |
| 89 | /** Full base config name. */ |
| 90 | baseConfig: string | undefined; |
| 91 | /** User labels (Alchemy ownership labels stripped). */ |
| 92 | labels: Record<string, string>; |
| 93 | /** Allowed `default_leader` values. */ |
| 94 | leaderOptions: string[]; |
| 95 | /** Whether a create/update is in flight. */ |
| 96 | reconciling: boolean | undefined; |
| 97 | /** Current state (`CREATING`, `READY`). */ |
| 98 | state: string | undefined; |
| 99 | /** Whether free instances can be created in this config. */ |
| 100 | freeInstanceAvailability: string | undefined; |
| 101 | /** Quorum type (`REGION`, `DUAL_REGION`, `MULTI_REGION`). */ |
| 102 | quorumType: string | undefined; |
| 103 | /** Storage limit in bytes per processing unit. */ |
| 104 | storageLimitPerProcessingUnit: string | undefined; |
| 105 | }, |
| 106 | never, |
| 107 | Providers |
| 108 | >; |
| 109 | |
| 110 | /** |
| 111 | * A user-managed Cloud Spanner instance configuration. |
| 112 | * |
| 113 | * User-managed configs clone a Google-managed `baseConfig` and add at |
| 114 | * least one optional read-only replica. Only `displayName` and `labels` |
| 115 | * update in place; changing `instanceConfigId`, `baseConfig`, or |
| 116 | * `replicas` replaces the config. Create, update, and delete are |
| 117 | * long-running. Google-managed configs cannot be created or deleted. |
| 118 | * |
| 119 | * ### Creating an Instance Config |
| 120 | * **Example:** Clone `regional-us-central1` with an optional replica |
| 121 | * ```typescript |
| 122 | * const config = yield* GCP.Spanner.InstanceConfig("Custom", { |
| 123 | * baseConfig: "regional-us-central1", |
| 124 | * displayName: "custom-us-central1", |
| 125 | * labels: { env: "test" }, |
| 126 | * }); |
| 127 | * ``` |
| 128 | * |
| 129 | * **Example:** Explicit id and replica list |
| 130 | * ```typescript |
| 131 | * const config = yield* GCP.Spanner.InstanceConfig("Custom", { |
| 132 | * instanceConfigId: "custom-us-central1-ro", |
| 133 | * baseConfig: "regional-us-central1", |
| 134 | * replicas: [ |
| 135 | * { location: "us-central1", type: "READ_WRITE" }, |
| 136 | * { location: "us-central1", type: "READ_WRITE" }, |
| 137 | * { location: "us-central1", type: "READ_WRITE" }, |
| 138 | * { location: "us-east1", type: "READ_ONLY" }, |
| 139 | * ], |
| 140 | * }); |
| 141 | * ``` |
| 142 | * |
| 143 | * @resource |
| 144 | * @category Spanner |
| 145 | */ |
| 146 | export const InstanceConfig = Resource<InstanceConfig>("GCP.Spanner.InstanceConfig"); |
| 147 | |
| 148 | export class InstanceConfigNotResolved extends Data.TaggedError( |
| 149 | "GCP.Spanner.InstanceConfigNotResolved", |
| 150 | )<{ |
| 151 | resourceName: string; |
| 152 | }> {} |
| 153 | |
| 154 | export class InstanceConfigNotReady extends Data.TaggedError("GCP.Spanner.InstanceConfigNotReady")<{ |
| 155 | resourceName: string; |
| 156 | state: string; |
| 157 | }> {} |
| 158 | |
| 159 | export class InstanceConfigStillExists extends Data.TaggedError( |
| 160 | "GCP.Spanner.InstanceConfigStillExists", |
| 161 | )<{ |
| 162 | resourceName: string; |
| 163 | }> {} |
| 164 | |
| 165 | export class InstanceConfigNoOptionalReplicas extends Data.TaggedError( |
| 166 | "GCP.Spanner.InstanceConfigNoOptionalReplicas", |
| 167 | )<{ |
| 168 | baseConfig: string; |
| 169 | }> {} |
| 170 | |
| 171 | const userLabels = ( |
| 172 | labels: Record<string, string | undefined> | null | undefined, |
| 173 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 174 | |
| 175 | const replicaOf = (replica: spanner.ReplicaInfo): ReplicaInfo => ({ |
| 176 | location: replica.location, |
| 177 | type: replica.type, |
| 178 | defaultLeaderLocation: replica.defaultLeaderLocation, |
| 179 | }); |
| 180 | |
| 181 | const replicasOf = (items: spanner.ReplicaInfoList | undefined): ReplicaInfo[] => |
| 182 | (items ?? []).map(replicaOf); |
| 183 | |
| 184 | const replicaKey = (replicas: ReplicaInfo[] | undefined) => |
| 185 | JSON.stringify( |
| 186 | (replicas ?? []) |
| 187 | .map((replica) => ({ |
| 188 | location: (replica.location ?? "").toLowerCase(), |
| 189 | type: (replica.type ?? "").toUpperCase(), |
| 190 | defaultLeaderLocation: replica.defaultLeaderLocation === true, |
| 191 | })) |
| 192 | .sort( |
| 193 | (left, right) => |
| 194 | left.location.localeCompare(right.location) || left.type.localeCompare(right.type), |
| 195 | ), |
| 196 | ); |
| 197 | |
| 198 | const toAttrs = (config: spanner.InstanceConfig, project: string): InstanceConfig["Attributes"] => { |
| 199 | const name = config.name ?? ""; |
| 200 | const parsed = parseResourceName(name); |
| 201 | return { |
| 202 | name, |
| 203 | instanceConfigId: parsed.instanceConfigId, |
| 204 | project, |
| 205 | displayName: config.displayName, |
| 206 | configType: config.configType, |
| 207 | replicas: replicasOf(config.replicas), |
| 208 | optionalReplicas: replicasOf(config.optionalReplicas), |
| 209 | baseConfig: config.baseConfig, |
| 210 | labels: userLabels(config.labels), |
| 211 | leaderOptions: config.leaderOptions ?? [], |
| 212 | reconciling: config.reconciling, |
| 213 | state: config.state, |
| 214 | freeInstanceAvailability: config.freeInstanceAvailability, |
| 215 | quorumType: config.quorumType, |
| 216 | storageLimitPerProcessingUnit: config.storageLimitPerProcessingUnit, |
| 217 | }; |
| 218 | }; |
| 219 | |
| 220 | const getByName = (name: string) => |
| 221 | spanner |
| 222 | .getProjectsInstanceConfigs({ name }) |
| 223 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 224 | |
| 225 | const waitUntilExists = (name: string) => |
| 226 | getByName(name).pipe( |
| 227 | Effect.flatMap((config) => |
| 228 | config |
| 229 | ? Effect.succeed(config) |
| 230 | : Effect.fail(new InstanceConfigNotResolved({ resourceName: name })), |
| 231 | ), |
| 232 | Effect.retry({ |
| 233 | while: (error) => error._tag === "GCP.Spanner.InstanceConfigNotResolved", |
| 234 | times: 8, |
| 235 | schedule: Schedule.spaced("2 seconds"), |
| 236 | }), |
| 237 | ); |
| 238 | |
| 239 | const isReady = (config: spanner.InstanceConfig) => |
| 240 | (config.state ?? "STATE_UNSPECIFIED") === "READY" && config.reconciling !== true; |
| 241 | |
| 242 | const waitUntilReady = (name: string) => |
| 243 | getByName(name).pipe( |
| 244 | Effect.filterOrFail( |
| 245 | (config): config is spanner.InstanceConfig => config !== undefined, |
| 246 | () => new InstanceConfigNotResolved({ resourceName: name }), |
| 247 | ), |
| 248 | Effect.filterOrFail( |
| 249 | (config) => isReady(config), |
| 250 | (config) => |
| 251 | new InstanceConfigNotReady({ |
| 252 | resourceName: name, |
| 253 | state: config.state ?? "STATE_UNSPECIFIED", |
| 254 | }), |
| 255 | ), |
| 256 | Effect.retry({ |
| 257 | while: (error) => |
| 258 | error._tag === "GCP.Spanner.InstanceConfigNotReady" || |
| 259 | error._tag === "GCP.Spanner.InstanceConfigNotResolved", |
| 260 | times: 10, |
| 261 | schedule: Schedule.spaced("8 seconds"), |
| 262 | }), |
| 263 | ); |
| 264 | |
| 265 | const waitUntilGone = (name: string) => |
| 266 | getByName(name).pipe( |
| 267 | Effect.flatMap((config) => |
| 268 | config === undefined |
| 269 | ? Effect.void |
| 270 | : Effect.fail(new InstanceConfigStillExists({ resourceName: name })), |
| 271 | ), |
| 272 | Effect.retry({ |
| 273 | while: (error) => error._tag === "GCP.Spanner.InstanceConfigStillExists", |
| 274 | times: 10, |
| 275 | schedule: Schedule.spaced("8 seconds"), |
| 276 | }), |
| 277 | ); |
| 278 | |
| 279 | const desiredReplicas = ( |
| 280 | project: string, |
| 281 | region: string, |
| 282 | news: InstanceConfigProps, |
| 283 | ): Effect.Effect< |
| 284 | ReplicaInfo[], |
| 285 | InstanceConfigNoOptionalReplicas | spanner.GetProjectsInstanceConfigsError, |
| 286 | spanner.GcpOpContext |
| 287 | > => |
| 288 | Effect.gen(function* () { |
| 289 | if (news.replicas !== undefined && news.replicas.length > 0) { |
| 290 | return news.replicas; |
| 291 | } |
| 292 | const baseName = configNameOf(project, news.baseConfig, region); |
| 293 | const base = yield* spanner.getProjectsInstanceConfigs({ name: baseName }); |
| 294 | const extra = (base.optionalReplicas ?? [])[0]; |
| 295 | if (extra === undefined) { |
| 296 | return yield* new InstanceConfigNoOptionalReplicas({ |
| 297 | baseConfig: baseName, |
| 298 | }); |
| 299 | } |
| 300 | return [...replicasOf(base.replicas), replicaOf(extra)]; |
| 301 | }); |
| 302 | |
| 303 | export const InstanceConfigProvider = () => |
| 304 | Provider.succeed(InstanceConfig, { |
| 305 | stables: ["name", "instanceConfigId", "project", "baseConfig", "configType"], |
| 306 | |
| 307 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 308 | if (!isResolved(news)) return undefined; |
| 309 | const env = yield* GcpEnvironment.current; |
| 310 | |
| 311 | const previousId = olds?.instanceConfigId ?? output?.instanceConfigId; |
| 312 | const nextId = news.instanceConfigId ?? previousId; |
| 313 | const previousBase = configIdOf(olds?.baseConfig ?? output?.baseConfig, env.region); |
| 314 | const nextBase = configIdOf(news.baseConfig ?? output?.baseConfig, env.region); |
| 315 | const replicasChanged = |
| 316 | news.replicas !== undefined && |
| 317 | olds?.replicas !== undefined && |
| 318 | replicaKey(news.replicas) !== replicaKey(olds.replicas); |
| 319 | |
| 320 | const idChanged = previousId !== undefined && nextId !== undefined && previousId !== nextId; |
| 321 | const replace = idChanged || previousBase !== nextBase || replicasChanged; |
| 322 | if (!replace) return undefined; |
| 323 | return { |
| 324 | action: "replace" as const, |
| 325 | deleteFirst: |
| 326 | previousBase !== nextBase || |
| 327 | replicasChanged || |
| 328 | (previousId !== undefined && nextId === previousId), |
| 329 | }; |
| 330 | }), |
| 331 | |
| 332 | read: Effect.fn(function* ({ id, olds, output }) { |
| 333 | const env = yield* GcpEnvironment.current; |
| 334 | const instanceConfigId = yield* toConfigId( |
| 335 | id, |
| 336 | olds?.instanceConfigId, |
| 337 | output?.instanceConfigId, |
| 338 | ); |
| 339 | const name = output?.name ?? instanceConfigName(env.project, instanceConfigId); |
| 340 | const existing = yield* getByName(name); |
| 341 | if (existing === undefined) return undefined; |
| 342 | const attrs = toAttrs(existing, env.project); |
| 343 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 344 | }), |
| 345 | |
| 346 | list: () => |
| 347 | Effect.gen(function* () { |
| 348 | const env = yield* GcpEnvironment.current; |
| 349 | return yield* spanner.listProjectsInstanceConfigs |
| 350 | .pages({ |
| 351 | parent: `projects/${env.project}`, |
| 352 | pageSize: 1000, |
| 353 | }) |
| 354 | .pipe( |
| 355 | Stream.flatMap((page) => Stream.fromIterable(page.instanceConfigs ?? [])), |
| 356 | Stream.filter( |
| 357 | (config) => |
| 358 | (config.configType ?? "") === "USER_MANAGED" && |
| 359 | Object.keys(config.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 360 | ), |
| 361 | Stream.map((config) => toAttrs(config, env.project)), |
| 362 | Stream.runCollect, |
| 363 | Effect.map((chunk) => Array.from(chunk)), |
| 364 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 365 | ); |
| 366 | }), |
| 367 | |
| 368 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 369 | const env = yield* GcpEnvironment.current; |
| 370 | const instanceConfigId = yield* toConfigId( |
| 371 | id, |
| 372 | news.instanceConfigId, |
| 373 | output?.instanceConfigId, |
| 374 | ); |
| 375 | const name = instanceConfigName(env.project, instanceConfigId); |
| 376 | const displayName = news.displayName?.trim() || instanceConfigId; |
| 377 | const baseConfig = configNameOf(env.project, news.baseConfig, env.region); |
| 378 | const desiredLabels = { |
| 379 | ...toLabels(news.labels), |
| 380 | ...(yield* createInternalLabels(id)), |
| 381 | }; |
| 382 | |
| 383 | let current = yield* getByName(output?.name ?? name); |
| 384 | |
| 385 | if (current === undefined) { |
| 386 | const replicas = yield* desiredReplicas(env.project, env.region, news); |
| 387 | const created = yield* spanner |
| 388 | .createProjectsInstanceConfigs({ |
| 389 | parent: `projects/${env.project}`, |
| 390 | body: { |
| 391 | instanceConfigId, |
| 392 | instanceConfig: { |
| 393 | name, |
| 394 | displayName, |
| 395 | baseConfig, |
| 396 | replicas, |
| 397 | labels: desiredLabels, |
| 398 | }, |
| 399 | }, |
| 400 | }) |
| 401 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 402 | if (created !== undefined) { |
| 403 | yield* waitForOperation(created, { alreadyExistsOk: true }); |
| 404 | } |
| 405 | current = yield* waitUntilExists(name); |
| 406 | } |
| 407 | |
| 408 | if (!isReady(current)) { |
| 409 | current = yield* waitUntilReady(name); |
| 410 | } |
| 411 | |
| 412 | const observedLabels = tagRecord(current.labels); |
| 413 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 414 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 415 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 416 | |
| 417 | if (labelsChanged || displayNameChanged) { |
| 418 | const fieldMask = [ |
| 419 | labelsChanged ? "labels" : undefined, |
| 420 | displayNameChanged ? "display_name" : undefined, |
| 421 | ].filter((field): field is string => field !== undefined); |
| 422 | const patched = yield* retryConcurrentChanges( |
| 423 | spanner.patchProjectsInstanceConfigs({ |
| 424 | name, |
| 425 | body: { |
| 426 | instanceConfig: { |
| 427 | name, |
| 428 | displayName, |
| 429 | labels: desiredLabels, |
| 430 | }, |
| 431 | updateMask: fieldMask.join(","), |
| 432 | }, |
| 433 | }), |
| 434 | ); |
| 435 | yield* waitForOperation(patched); |
| 436 | current = yield* waitUntilReady(name); |
| 437 | } |
| 438 | |
| 439 | return toAttrs(current, env.project); |
| 440 | }), |
| 441 | |
| 442 | delete: Effect.fn(function* ({ output }) { |
| 443 | yield* spanner.deleteProjectsInstanceConfigs({ name: output.name }).pipe( |
| 444 | Effect.catchTag("NotFound", () => Effect.void), |
| 445 | Effect.retry({ |
| 446 | while: (error) => error._tag === "Conflict", |
| 447 | times: 8, |
| 448 | schedule: Schedule.spaced("5 seconds"), |
| 449 | }), |
| 450 | ); |
| 451 | yield* waitUntilGone(output.name); |
| 452 | }), |
| 453 | }); |