The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as firebasedataconnect from "@distilled.cloud/gcp/firebasedataconnect_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 | fieldMask, |
| 14 | fingerprint, |
| 15 | listAtLocation, |
| 16 | listLabeledPages, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | retryStaleEtag, |
| 23 | sameText, |
| 24 | stringMap, |
| 25 | toPhysicalId, |
| 26 | userLabels, |
| 27 | waitForDeleteOperation, |
| 28 | waitForOperation, |
| 29 | waitUntilExists, |
| 30 | waitUntilGone, |
| 31 | waitUntilReady, |
| 32 | } from "./internal.ts"; |
| 33 | |
| 34 | export type ServiceProps = { |
| 35 | /** |
| 36 | * Service id (the `{service}` segment of |
| 37 | * `projects/{project}/locations/{location}/services/{service}`). |
| 38 | * If omitted, a unique RFC1035 name is generated from the stack, |
| 39 | * stage, and logical id. Immutable — changing it replaces the |
| 40 | * service. Must start with a letter, contain only lowercase letters, |
| 41 | * digits, and hyphens, and be at most 63 characters. |
| 42 | */ |
| 43 | serviceId?: string; |
| 44 | /** |
| 45 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 46 | * replaces the service. `US-CENTRAL1` is accepted and normalized to |
| 47 | * `us-central1`. |
| 48 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * Human-readable name. 63 character limit. |
| 53 | */ |
| 54 | displayName?: string; |
| 55 | /** |
| 56 | * User annotations (preserved by external tools). |
| 57 | */ |
| 58 | annotations?: Record<string, string>; |
| 59 | /** |
| 60 | * User labels. Alchemy ownership labels are merged in automatically. |
| 61 | */ |
| 62 | labels?: Record<string, string>; |
| 63 | }; |
| 64 | |
| 65 | export type Service = Resource< |
| 66 | "GCP.FirebaseDataConnect.Service", |
| 67 | ServiceProps, |
| 68 | { |
| 69 | /** Full resource name. */ |
| 70 | name: string; |
| 71 | /** Service id (last path segment). */ |
| 72 | serviceId: string; |
| 73 | /** Project id. */ |
| 74 | project: string; |
| 75 | /** Location id. */ |
| 76 | location: string; |
| 77 | /** Human-readable name. */ |
| 78 | displayName: string | undefined; |
| 79 | /** True while Data Connect is applying an LRO. */ |
| 80 | reconciling: boolean; |
| 81 | /** User annotations. */ |
| 82 | annotations: Record<string, string>; |
| 83 | /** User labels (Alchemy ownership labels stripped). */ |
| 84 | labels: Record<string, string>; |
| 85 | /** Server-generated resource uid. */ |
| 86 | uid: string | undefined; |
| 87 | /** RFC3339 creation timestamp. */ |
| 88 | createTime: string | undefined; |
| 89 | /** RFC3339 last-update timestamp. */ |
| 90 | updateTime: string | undefined; |
| 91 | /** Server-computed etag. */ |
| 92 | etag: string | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | >; |
| 97 | |
| 98 | /** |
| 99 | * A Firebase Data Connect service — the parent of a GraphQL schema and |
| 100 | * connectors. |
| 101 | * |
| 102 | * Changing `serviceId` or `location` replaces the service. Display |
| 103 | * name, labels, and annotations update in place. |
| 104 | * |
| 105 | * ### Creating a Service |
| 106 | * **Example:** Generated name |
| 107 | * ```typescript |
| 108 | * const service = yield* GCP.FirebaseDataConnect.Service("App", { |
| 109 | * labels: { env: "test" }, |
| 110 | * }); |
| 111 | * ``` |
| 112 | * |
| 113 | * **Example:** Explicit id |
| 114 | * ```typescript |
| 115 | * const service = yield* GCP.FirebaseDataConnect.Service("App", { |
| 116 | * serviceId: "movies", |
| 117 | * displayName: "movie tracker", |
| 118 | * labels: { env: "prod" }, |
| 119 | * }); |
| 120 | * ``` |
| 121 | * |
| 122 | * ### Updating a Service |
| 123 | * **Example:** Display name and labels |
| 124 | * ```typescript |
| 125 | * const service = yield* GCP.FirebaseDataConnect.Service("App", { |
| 126 | * displayName: "movie tracker v2", |
| 127 | * labels: { env: "prod", team: "data" }, |
| 128 | * }); |
| 129 | * ``` |
| 130 | * |
| 131 | * @resource |
| 132 | * @category FirebaseDataConnect |
| 133 | */ |
| 134 | export const Service = Resource<Service>("GCP.FirebaseDataConnect.Service"); |
| 135 | |
| 136 | const resourceName = (project: string, location: string, serviceId: string) => |
| 137 | `projects/${project}/locations/${location}/services/${serviceId}`; |
| 138 | |
| 139 | const toAttrs = ( |
| 140 | item: firebasedataconnect.Service, |
| 141 | project: string, |
| 142 | region: string, |
| 143 | ): Service["Attributes"] => { |
| 144 | const name = item.name ?? ""; |
| 145 | const parsed = parseName(name, "services", region); |
| 146 | return { |
| 147 | name, |
| 148 | serviceId: parsed.id, |
| 149 | project: parsed.project || project, |
| 150 | location: parsed.location, |
| 151 | displayName: item.displayName, |
| 152 | reconciling: item.reconciling === true, |
| 153 | annotations: stringMap(item.annotations), |
| 154 | labels: userLabels(item.labels), |
| 155 | uid: item.uid, |
| 156 | createTime: item.createTime, |
| 157 | updateTime: item.updateTime, |
| 158 | etag: item.etag, |
| 159 | }; |
| 160 | }; |
| 161 | |
| 162 | const getByName = (name: string) => |
| 163 | firebasedataconnect |
| 164 | .getProjectsLocationsServices({ name }) |
| 165 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 166 | |
| 167 | const listOwned = (project: string) => |
| 168 | listAtLocation(project, (parent) => |
| 169 | listLabeledPages( |
| 170 | firebasedataconnect.listProjectsLocationsServices.pages({ |
| 171 | parent, |
| 172 | pageSize: 1000, |
| 173 | }), |
| 174 | (page) => page.services, |
| 175 | (item) => item.labels, |
| 176 | ), |
| 177 | ); |
| 178 | |
| 179 | export const ServiceProvider = () => |
| 180 | Provider.succeed(Service, { |
| 181 | stables: ["name", "serviceId", "project", "location", "uid", "createTime"], |
| 182 | |
| 183 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 184 | if (!isResolved(news)) return undefined; |
| 185 | const env = yield* GcpEnvironment.current; |
| 186 | return replaceOnIdentity({ |
| 187 | previousId: olds?.serviceId ?? output?.serviceId, |
| 188 | nextId: news.serviceId ?? olds?.serviceId ?? output?.serviceId, |
| 189 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 190 | nextLocation: normalizeLocation( |
| 191 | news.location ?? olds?.location ?? output?.location, |
| 192 | env.region, |
| 193 | ), |
| 194 | }); |
| 195 | }), |
| 196 | |
| 197 | read: Effect.fn(function* ({ id, olds, output }) { |
| 198 | const env = yield* GcpEnvironment.current; |
| 199 | const serviceId = yield* toPhysicalId(id, olds?.serviceId, output?.serviceId, "service"); |
| 200 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 201 | const name = output?.name ?? resourceName(env.project, location, serviceId); |
| 202 | const existing = yield* getByName(name); |
| 203 | if (existing === undefined) return undefined; |
| 204 | const attrs = toAttrs(existing, env.project, env.region); |
| 205 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 206 | }), |
| 207 | |
| 208 | list: () => |
| 209 | Effect.gen(function* () { |
| 210 | const env = yield* GcpEnvironment.current; |
| 211 | const items = yield* listOwned(env.project); |
| 212 | return items.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 serviceId = yield* toPhysicalId(id, news.serviceId, output?.serviceId, "service"); |
| 218 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 219 | const name = resourceName(env.project, location, serviceId); |
| 220 | const desiredLabels = { |
| 221 | ...toLabels(news.labels), |
| 222 | ...(yield* createInternalLabels(id)), |
| 223 | }; |
| 224 | const desiredAnnotations = news.annotations ?? {}; |
| 225 | |
| 226 | let current = yield* getByName(output?.name ?? name); |
| 227 | |
| 228 | if (current === undefined) { |
| 229 | const created = yield* firebasedataconnect |
| 230 | .createProjectsLocationsServices({ |
| 231 | parent: parentOf(env.project, location), |
| 232 | serviceId, |
| 233 | body: { |
| 234 | displayName: news.displayName, |
| 235 | annotations: desiredAnnotations, |
| 236 | labels: desiredLabels, |
| 237 | }, |
| 238 | }) |
| 239 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 240 | if (created !== undefined) { |
| 241 | yield* waitForOperation(created); |
| 242 | } |
| 243 | current = yield* waitUntilExists(getByName(name), name); |
| 244 | } |
| 245 | |
| 246 | if (current === undefined) { |
| 247 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 248 | } |
| 249 | |
| 250 | const observedLabels = tagRecord(current.labels); |
| 251 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 252 | const mask = fieldMask([ |
| 253 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 254 | fingerprint(stringMap(current.annotations)) !== fingerprint(desiredAnnotations) && |
| 255 | "annotations", |
| 256 | !sameText(current.displayName, news.displayName) && "displayName", |
| 257 | ]); |
| 258 | |
| 259 | if (mask.length > 0) { |
| 260 | const serviceName = current.name ?? name; |
| 261 | // Child schema/connector updates bump the service etag while this |
| 262 | // patch is in flight: re-read the etag on each attempt. |
| 263 | yield* retryStaleEtag( |
| 264 | Effect.gen(function* () { |
| 265 | const latest = yield* getByName(serviceName); |
| 266 | const operation = yield* firebasedataconnect.patchProjectsLocationsServices({ |
| 267 | name: serviceName, |
| 268 | updateMask: mask, |
| 269 | body: { |
| 270 | etag: latest?.etag, |
| 271 | displayName: news.displayName, |
| 272 | annotations: desiredAnnotations, |
| 273 | labels: desiredLabels, |
| 274 | }, |
| 275 | }); |
| 276 | yield* waitForOperation(operation); |
| 277 | }), |
| 278 | ); |
| 279 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 280 | } |
| 281 | |
| 282 | current = yield* waitUntilReady(getByName(current.name ?? name), current.name ?? name); |
| 283 | return toAttrs(current, env.project, env.region); |
| 284 | }), |
| 285 | |
| 286 | delete: Effect.fn(function* ({ output }) { |
| 287 | const operation = yield* firebasedataconnect |
| 288 | .deleteProjectsLocationsServices({ |
| 289 | name: output.name, |
| 290 | force: true, |
| 291 | }) |
| 292 | .pipe( |
| 293 | Effect.retry({ |
| 294 | while: (error) => error._tag === "Conflict", |
| 295 | times: 8, |
| 296 | schedule: Schedule.spaced("2 seconds"), |
| 297 | }), |
| 298 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 299 | ); |
| 300 | if (operation !== undefined) { |
| 301 | yield* waitForDeleteOperation(operation); |
| 302 | } |
| 303 | yield* waitUntilGone(getByName(output.name), output.name); |
| 304 | }), |
| 305 | }); |