The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | identityMappingStoreIdOf, |
| 13 | LIST_LOCATIONS, |
| 14 | locationParent, |
| 15 | normalizeLocation, |
| 16 | parseResourceName, |
| 17 | toPhysical, |
| 18 | } from "./internal.ts"; |
| 19 | import { waitForOperation } from "./operations.ts"; |
| 20 | |
| 21 | export type IdentityMappingStoreProps = { |
| 22 | /** |
| 23 | * Identity Mapping Store id (the last path segment). If omitted, a |
| 24 | * unique id is generated from the stack, stage, and logical id. |
| 25 | * Immutable — changing it replaces the store. Generated ids are |
| 26 | * prefixed with `alch` so `list` / nuke can find them. |
| 27 | */ |
| 28 | identityMappingStoreId?: string; |
| 29 | /** |
| 30 | * Location (`global`, `us`, `eu`). Immutable — changing it replaces |
| 31 | * the store. |
| 32 | * @default "global" |
| 33 | */ |
| 34 | location?: string; |
| 35 | /** |
| 36 | * Cloud KMS key that protects this store at creation time. Immutable. |
| 37 | */ |
| 38 | kmsKeyName?: string; |
| 39 | /** |
| 40 | * CMEK config resource name used to protect this store. |
| 41 | */ |
| 42 | cmekConfigName?: string; |
| 43 | /** |
| 44 | * When true, skip CMEK even if the project has a default CmekConfig. |
| 45 | * @default false |
| 46 | */ |
| 47 | disableCmek?: boolean; |
| 48 | }; |
| 49 | |
| 50 | export type IdentityMappingStore = Resource< |
| 51 | "GCP.DiscoveryEngine.IdentityMappingStore", |
| 52 | IdentityMappingStoreProps, |
| 53 | { |
| 54 | /** Full resource name `projects/{project}/locations/{location}/identityMappingStores/{id}`. */ |
| 55 | name: string; |
| 56 | /** Store id (last path segment). */ |
| 57 | identityMappingStoreId: string; |
| 58 | /** Project id. */ |
| 59 | project: string; |
| 60 | /** Location id. */ |
| 61 | location: string; |
| 62 | /** CMEK KMS key, if configured. */ |
| 63 | kmsKey: string | undefined; |
| 64 | /** CMEK config resource name, if configured. */ |
| 65 | cmekConfigName: string | undefined; |
| 66 | }, |
| 67 | never, |
| 68 | Providers |
| 69 | >; |
| 70 | |
| 71 | /** |
| 72 | * A Vertex AI Search Identity Mapping Store. |
| 73 | * |
| 74 | * Stores have no labels or display name, so generated ids are prefixed |
| 75 | * with `alch` for `list` / nuke. Location, id, and CMEK are identity — |
| 76 | * changing them replaces the store. There is nothing mutable to sync. |
| 77 | * |
| 78 | * ### Creating an Identity Mapping Store |
| 79 | * **Example:** Generated id |
| 80 | * ```typescript |
| 81 | * const store = yield* GCP.DiscoveryEngine.IdentityMappingStore("Users", { |
| 82 | * disableCmek: true, |
| 83 | * }); |
| 84 | * ``` |
| 85 | * |
| 86 | * **Example:** Explicit id and location |
| 87 | * ```typescript |
| 88 | * const store = yield* GCP.DiscoveryEngine.IdentityMappingStore("Users", { |
| 89 | * identityMappingStoreId: "app-users", |
| 90 | * location: "global", |
| 91 | * disableCmek: true, |
| 92 | * }); |
| 93 | * ``` |
| 94 | * |
| 95 | * @resource |
| 96 | * @category DiscoveryEngine |
| 97 | */ |
| 98 | export const IdentityMappingStore = Resource<IdentityMappingStore>( |
| 99 | "GCP.DiscoveryEngine.IdentityMappingStore", |
| 100 | ); |
| 101 | |
| 102 | export class IdentityMappingStoreNotResolved extends Data.TaggedError( |
| 103 | "GCP.DiscoveryEngine.IdentityMappingStoreNotResolved", |
| 104 | )<{ |
| 105 | resourceName: string; |
| 106 | }> {} |
| 107 | |
| 108 | const resourceName = (project: string, location: string, storeId: string) => |
| 109 | `${locationParent(project, location)}/identityMappingStores/${storeId}`; |
| 110 | |
| 111 | const toAttrs = ( |
| 112 | store: discoveryengine.GoogleCloudDiscoveryengineV1IdentityMappingStore, |
| 113 | project: string, |
| 114 | ) => { |
| 115 | const name = store.name ?? ""; |
| 116 | const parsed = parseResourceName(name, "identityMappingStores"); |
| 117 | return { |
| 118 | name, |
| 119 | identityMappingStoreId: parsed.id, |
| 120 | project: parsed.project || project, |
| 121 | location: parsed.location, |
| 122 | kmsKey: store.cmekConfig?.kmsKey ?? store.kmsKeyName, |
| 123 | cmekConfigName: store.cmekConfig?.name, |
| 124 | }; |
| 125 | }; |
| 126 | |
| 127 | const getByName = (name: string) => |
| 128 | name.length === 0 |
| 129 | ? Effect.undefined |
| 130 | : discoveryengine |
| 131 | .getProjectsLocationsIdentityMappingStores({ name }) |
| 132 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 133 | |
| 134 | const isAlchemyId = (storeId: string) => storeId.startsWith("alch"); |
| 135 | |
| 136 | const listStores = (project: string) => |
| 137 | Effect.forEach( |
| 138 | LIST_LOCATIONS, |
| 139 | (location) => |
| 140 | discoveryengine.listProjectsLocationsIdentityMappingStores |
| 141 | .pages({ |
| 142 | parent: locationParent(project, location), |
| 143 | pageSize: 100, |
| 144 | }) |
| 145 | .pipe( |
| 146 | Stream.flatMap((page) => Stream.fromIterable(page.identityMappingStores ?? [])), |
| 147 | Stream.filter((store) => isAlchemyId(lastId(store.name))), |
| 148 | Stream.runCollect, |
| 149 | Effect.map((chunk) => Array.from(chunk)), |
| 150 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 151 | ), |
| 152 | { concurrency: 2 }, |
| 153 | ).pipe(Effect.map((groups) => groups.flat())); |
| 154 | |
| 155 | const lastId = (name: string | undefined) => |
| 156 | name |
| 157 | ? (name |
| 158 | .split("/") |
| 159 | .filter((part) => part.length > 0) |
| 160 | .at(-1) ?? "") |
| 161 | : ""; |
| 162 | |
| 163 | export const IdentityMappingStoreProvider = () => |
| 164 | Provider.succeed(IdentityMappingStore, { |
| 165 | stables: ["name", "identityMappingStoreId", "project", "location", "kmsKey", "cmekConfigName"], |
| 166 | |
| 167 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 168 | if (!isResolved(news)) return undefined; |
| 169 | const previousId = olds?.identityMappingStoreId ?? output?.identityMappingStoreId; |
| 170 | const nextId = news.identityMappingStoreId ?? previousId; |
| 171 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 172 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 173 | const previousKey = olds?.kmsKeyName ?? output?.kmsKey ?? ""; |
| 174 | const nextKey = news.kmsKeyName ?? previousKey; |
| 175 | const replace = |
| 176 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 177 | previousLocation !== nextLocation || |
| 178 | previousKey !== nextKey; |
| 179 | if (!replace) return undefined; |
| 180 | return { |
| 181 | action: "replace" as const, |
| 182 | deleteFirst: |
| 183 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 184 | }; |
| 185 | }), |
| 186 | |
| 187 | read: Effect.fn(function* ({ id, olds, output }) { |
| 188 | const env = yield* GcpEnvironment.current; |
| 189 | const storeId = yield* toPhysical( |
| 190 | id, |
| 191 | olds?.identityMappingStoreId, |
| 192 | output?.identityMappingStoreId, |
| 193 | identityMappingStoreIdOf, |
| 194 | ); |
| 195 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 196 | const name = output?.name ?? resourceName(env.project, location, storeId); |
| 197 | const existing = yield* getByName(name); |
| 198 | if (existing === undefined) return undefined; |
| 199 | const attrs = toAttrs(existing, env.project); |
| 200 | return isAlchemyId(attrs.identityMappingStoreId) ? attrs : Unowned(attrs); |
| 201 | }), |
| 202 | |
| 203 | list: () => |
| 204 | Effect.gen(function* () { |
| 205 | const env = yield* GcpEnvironment.current; |
| 206 | const stores = yield* listStores(env.project); |
| 207 | return stores.map((store) => toAttrs(store, env.project)); |
| 208 | }), |
| 209 | |
| 210 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 211 | const env = yield* GcpEnvironment.current; |
| 212 | const location = normalizeLocation(news.location ?? output?.location); |
| 213 | const storeId = yield* toPhysical( |
| 214 | id, |
| 215 | news.identityMappingStoreId, |
| 216 | output?.identityMappingStoreId, |
| 217 | identityMappingStoreIdOf, |
| 218 | ); |
| 219 | const name = resourceName(env.project, location, storeId); |
| 220 | |
| 221 | let current = yield* getByName(output?.name ?? name); |
| 222 | |
| 223 | if (current === undefined) { |
| 224 | const created = yield* discoveryengine |
| 225 | .createProjectsLocationsIdentityMappingStores({ |
| 226 | parent: locationParent(env.project, location), |
| 227 | identityMappingStoreId: storeId, |
| 228 | cmekConfigName: news.cmekConfigName, |
| 229 | disableCmek: news.disableCmek === true ? true : undefined, |
| 230 | body: { |
| 231 | name, |
| 232 | kmsKeyName: news.kmsKeyName, |
| 233 | }, |
| 234 | }) |
| 235 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 236 | current = created ?? undefined; |
| 237 | } |
| 238 | |
| 239 | if (current === undefined) { |
| 240 | return yield* new IdentityMappingStoreNotResolved({ resourceName: name }); |
| 241 | } |
| 242 | |
| 243 | return toAttrs(current, env.project); |
| 244 | }), |
| 245 | |
| 246 | delete: Effect.fn(function* ({ output }) { |
| 247 | const existing = yield* getByName(output.name); |
| 248 | if (existing === undefined) return; |
| 249 | const operation = yield* discoveryengine |
| 250 | .deleteProjectsLocationsIdentityMappingStores({ name: output.name }) |
| 251 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 252 | if (operation !== undefined) { |
| 253 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 254 | } |
| 255 | }), |
| 256 | }); |