The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as saasservicemgmt from "@distilled.cloud/gcp/saasservicemgmt_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 | ResourceNotResolved, |
| 14 | collectPages, |
| 15 | expandName, |
| 16 | fieldMask, |
| 17 | fingerprint, |
| 18 | hasAlchemyLabelKeys, |
| 19 | lastSegment, |
| 20 | normalizeLocation, |
| 21 | parentOf, |
| 22 | parseName, |
| 23 | replaceOnIdentity, |
| 24 | resourceName, |
| 25 | sameRef, |
| 26 | toPhysicalId, |
| 27 | userAnnotations, |
| 28 | userLabels, |
| 29 | waitUntilGone, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | const COLLECTION = "tenants"; |
| 33 | |
| 34 | export type TenantProps = { |
| 35 | /** |
| 36 | * Tenant id (the `{tenant}` segment of |
| 37 | * `projects/{project}/locations/{location}/tenants/{tenant}`). If |
| 38 | * omitted, a unique RFC1035 name is generated. Immutable — changing |
| 39 | * it replaces the tenant. |
| 40 | */ |
| 41 | tenantId?: string; |
| 42 | /** |
| 43 | * Region of the tenant (`us-central1`, …). Immutable — changing it |
| 44 | * replaces the tenant. `US-CENTRAL1` is accepted and normalized to |
| 45 | * `us-central1`. |
| 46 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * SaaS this tenant belongs to. Accepts a SaaS id or a full resource |
| 51 | * name. Immutable — changing it replaces the tenant. |
| 52 | */ |
| 53 | saas: string; |
| 54 | /** |
| 55 | * Consumer resource this tenant represents, e.g. a Cloud resource |
| 56 | * handed to the customer. Immutable — changing it replaces the |
| 57 | * tenant. |
| 58 | */ |
| 59 | consumerResource?: string; |
| 60 | /** |
| 61 | * User labels. Alchemy ownership labels are merged in automatically. |
| 62 | */ |
| 63 | labels?: Record<string, string>; |
| 64 | /** |
| 65 | * Unstructured annotations preserved across updates. |
| 66 | */ |
| 67 | annotations?: Record<string, string>; |
| 68 | }; |
| 69 | |
| 70 | export type Tenant = Resource< |
| 71 | "GCP.SaasServiceManagement.Tenant", |
| 72 | TenantProps, |
| 73 | { |
| 74 | /** Full resource name `projects/{project}/locations/{location}/tenants/{tenant}`. */ |
| 75 | name: string; |
| 76 | /** Tenant id (last path segment). */ |
| 77 | tenantId: string; |
| 78 | /** Project id. */ |
| 79 | project: string; |
| 80 | /** Location id. */ |
| 81 | location: string; |
| 82 | /** SaaS resource name. */ |
| 83 | saas: string | undefined; |
| 84 | /** SaaS id (last path segment). */ |
| 85 | saasId: string | undefined; |
| 86 | /** Consumer resource this tenant represents. */ |
| 87 | consumerResource: string | undefined; |
| 88 | /** User labels (Alchemy ownership labels stripped). */ |
| 89 | labels: Record<string, string>; |
| 90 | /** User annotations. */ |
| 91 | annotations: Record<string, string>; |
| 92 | /** Server UUID. */ |
| 93 | uid: string | undefined; |
| 94 | /** Server etag. */ |
| 95 | etag: string | undefined; |
| 96 | /** RFC3339 creation timestamp. */ |
| 97 | createTime: string | undefined; |
| 98 | /** RFC3339 last-update timestamp. */ |
| 99 | updateTime: string | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | >; |
| 104 | |
| 105 | /** |
| 106 | * An App Lifecycle Manager tenant — the producer-side instance of a |
| 107 | * SaaS created for one consumer. |
| 108 | * |
| 109 | * `tenantId`, `location`, `saas`, and `consumerResource` replace the |
| 110 | * tenant. Labels and annotations update in place. |
| 111 | * |
| 112 | * ### Creating a Tenant |
| 113 | * **Example:** Tenant of a SaaS |
| 114 | * ```typescript |
| 115 | * const product = yield* GCP.SaasServiceManagement.Saa("Inventory", { |
| 116 | * locations: [{ name: "us-central1" }], |
| 117 | * }); |
| 118 | * const tenant = yield* GCP.SaasServiceManagement.Tenant("Acme", { |
| 119 | * saas: product.name, |
| 120 | * labels: { env: "prod" }, |
| 121 | * }); |
| 122 | * ``` |
| 123 | * |
| 124 | * ### Updating a Tenant |
| 125 | * **Example:** Relabel |
| 126 | * ```typescript |
| 127 | * const tenant = yield* GCP.SaasServiceManagement.Tenant("Acme", { |
| 128 | * tenantId: "acme", |
| 129 | * saas: product.name, |
| 130 | * labels: { env: "prod", customer: "acme" }, |
| 131 | * }); |
| 132 | * ``` |
| 133 | * |
| 134 | * @resource |
| 135 | * @category SaasServiceManagement |
| 136 | */ |
| 137 | export const Tenant = Resource<Tenant>("GCP.SaasServiceManagement.Tenant"); |
| 138 | |
| 139 | const toAttrs = (item: saasservicemgmt.Tenant, project: string) => { |
| 140 | const name = item.name ?? ""; |
| 141 | const parsed = parseName(name, COLLECTION); |
| 142 | return { |
| 143 | name, |
| 144 | tenantId: parsed.id, |
| 145 | project: parsed.project || project, |
| 146 | location: parsed.location, |
| 147 | saas: item.saas, |
| 148 | saasId: item.saas ? lastSegment(item.saas) : undefined, |
| 149 | consumerResource: item.consumerResource, |
| 150 | labels: userLabels(item.labels), |
| 151 | annotations: userAnnotations(item.annotations), |
| 152 | uid: item.uid, |
| 153 | etag: item.etag, |
| 154 | createTime: item.createTime, |
| 155 | updateTime: item.updateTime, |
| 156 | }; |
| 157 | }; |
| 158 | |
| 159 | const getByName = (name: string) => |
| 160 | name.length === 0 |
| 161 | ? Effect.undefined |
| 162 | : saasservicemgmt |
| 163 | .getProjectsLocationsTenants({ name }) |
| 164 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 165 | |
| 166 | const listOwned = (project: string, location: string) => |
| 167 | collectPages( |
| 168 | saasservicemgmt.listProjectsLocationsTenants.pages({ |
| 169 | parent: parentOf(project, "-"), |
| 170 | pageSize: 1000, |
| 171 | }), |
| 172 | (page) => page.tenants, |
| 173 | ).pipe( |
| 174 | Effect.map((items) => items.filter((item) => hasAlchemyLabelKeys(item.labels))), |
| 175 | Effect.flatMap((items) => |
| 176 | items.length > 0 |
| 177 | ? Effect.succeed(items) |
| 178 | : collectPages( |
| 179 | saasservicemgmt.listProjectsLocationsTenants.pages({ |
| 180 | parent: parentOf(project, location), |
| 181 | pageSize: 1000, |
| 182 | }), |
| 183 | (page) => page.tenants, |
| 184 | ).pipe( |
| 185 | Effect.map((fallback) => fallback.filter((item) => hasAlchemyLabelKeys(item.labels))), |
| 186 | ), |
| 187 | ), |
| 188 | ); |
| 189 | |
| 190 | export const TenantProvider = () => |
| 191 | Provider.succeed(Tenant, { |
| 192 | stables: ["name", "tenantId", "project", "location", "saasId", "uid", "createTime"], |
| 193 | |
| 194 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 195 | if (!isResolved(news)) return undefined; |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | return replaceOnIdentity({ |
| 198 | previousId: olds?.tenantId ?? output?.tenantId, |
| 199 | nextId: news.tenantId ?? olds?.tenantId ?? output?.tenantId, |
| 200 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 201 | nextLocation: normalizeLocation( |
| 202 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 203 | ), |
| 204 | extra: |
| 205 | !sameRef(olds?.saas ?? output?.saas, news.saas) || |
| 206 | (olds?.consumerResource ?? output?.consumerResource ?? "") !== |
| 207 | (news.consumerResource ?? olds?.consumerResource ?? output?.consumerResource ?? ""), |
| 208 | }); |
| 209 | }), |
| 210 | |
| 211 | read: Effect.fn(function* ({ id, olds, output }) { |
| 212 | const env = yield* GcpEnvironment.current; |
| 213 | const tenantId = yield* toPhysicalId(id, olds?.tenantId, output?.tenantId, "tnt"); |
| 214 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 215 | const name = output?.name ?? resourceName(env.project, location, COLLECTION, tenantId); |
| 216 | const existing = yield* getByName(name); |
| 217 | if (existing === undefined) return undefined; |
| 218 | const attrs = toAttrs(existing, env.project); |
| 219 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 220 | }), |
| 221 | |
| 222 | list: () => |
| 223 | Effect.gen(function* () { |
| 224 | const env = yield* GcpEnvironment.current; |
| 225 | const items = yield* listOwned(env.project, env.region); |
| 226 | return items.map((item) => toAttrs(item, env.project)); |
| 227 | }), |
| 228 | |
| 229 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 230 | const env = yield* GcpEnvironment.current; |
| 231 | const tenantId = yield* toPhysicalId(id, news.tenantId, output?.tenantId, "tnt"); |
| 232 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 233 | const name = resourceName(env.project, location, COLLECTION, tenantId); |
| 234 | const desiredLabels = { |
| 235 | ...toLabels(news.labels), |
| 236 | ...(yield* createInternalLabels(id)), |
| 237 | }; |
| 238 | const saas = expandName(news.saas, env.project, location, "saas"); |
| 239 | const annotations = news.annotations; |
| 240 | |
| 241 | let current = yield* getByName(output?.name ?? name); |
| 242 | |
| 243 | if (current === undefined) { |
| 244 | const created = yield* saasservicemgmt |
| 245 | .createProjectsLocationsTenants({ |
| 246 | parent: parentOf(env.project, location), |
| 247 | tenantId, |
| 248 | body: { |
| 249 | saas, |
| 250 | consumerResource: news.consumerResource, |
| 251 | labels: desiredLabels, |
| 252 | annotations, |
| 253 | }, |
| 254 | }) |
| 255 | .pipe( |
| 256 | // A Saas created seconds earlier is unreadable here until it |
| 257 | // propagates. |
| 258 | Effect.retry({ |
| 259 | while: (error) => error._tag === "SaasNotFound", |
| 260 | schedule: Schedule.spaced("2 seconds"), |
| 261 | times: 15, |
| 262 | }), |
| 263 | Effect.catchTag("Conflict", () => getByName(name)), |
| 264 | ); |
| 265 | current = created ?? undefined; |
| 266 | } |
| 267 | |
| 268 | if (current === undefined) { |
| 269 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 270 | } |
| 271 | |
| 272 | const observedLabels = tagRecord(current.labels); |
| 273 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 274 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 275 | const annotationsChanged = |
| 276 | annotations !== undefined && |
| 277 | fingerprint(userAnnotations(current.annotations)) !== fingerprint(annotations); |
| 278 | const mask = fieldMask([labelsChanged && "labels", annotationsChanged && "annotations"]); |
| 279 | |
| 280 | if (mask.length > 0) { |
| 281 | current = yield* saasservicemgmt.patchProjectsLocationsTenants({ |
| 282 | name: current.name ?? name, |
| 283 | updateMask: mask, |
| 284 | body: { |
| 285 | name: current.name ?? name, |
| 286 | etag: current.etag, |
| 287 | labels: desiredLabels, |
| 288 | annotations, |
| 289 | }, |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | return toAttrs(current, env.project); |
| 294 | }), |
| 295 | |
| 296 | delete: Effect.fn(function* ({ output }) { |
| 297 | yield* saasservicemgmt.deleteProjectsLocationsTenants({ name: output.name }).pipe( |
| 298 | Effect.retry({ |
| 299 | while: (error) => error._tag === "Conflict", |
| 300 | times: 8, |
| 301 | schedule: Schedule.spaced("2 seconds"), |
| 302 | }), |
| 303 | Effect.catchTag("NotFound", () => Effect.void), |
| 304 | ); |
| 305 | yield* waitUntilGone(getByName(output.name), output.name); |
| 306 | }), |
| 307 | }); |