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, 04:45 UTC
| 1 | import * as integrations from "@distilled.cloud/gcp/integrations_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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { findSfdcInstanceByDescription, listSfdcInstances } from "./internal.ts"; |
| 12 | import { |
| 13 | encodeOwnership, |
| 14 | hasOwnershipMarker, |
| 15 | isDeleted, |
| 16 | lastSegment, |
| 17 | locationOf, |
| 18 | locationParent, |
| 19 | normalizeLocation, |
| 20 | ownedByAlchemy, |
| 21 | parseOwnership, |
| 22 | withProjectId, |
| 23 | replaceOnIdentity, |
| 24 | sameText, |
| 25 | toResourceId, |
| 26 | updateMaskOf, |
| 27 | } from "./ownership.ts"; |
| 28 | |
| 29 | export type SfdcInstanceProps = { |
| 30 | /** |
| 31 | * Instance id (the `{sfdcInstance}` segment of |
| 32 | * `projects/{project}/locations/{location}/sfdcInstances/{sfdcInstance}`). |
| 33 | * Server-assigned on create when omitted. Immutable — changing it |
| 34 | * replaces the instance. |
| 35 | */ |
| 36 | sfdcInstanceId?: string; |
| 37 | /** |
| 38 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 39 | * instance. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 40 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 41 | * region, else `us-central1`) |
| 42 | */ |
| 43 | location?: string; |
| 44 | /** |
| 45 | * User-selected unique alias for the instance. |
| 46 | */ |
| 47 | displayName?: string; |
| 48 | /** |
| 49 | * Human-readable description. SFDC instances have no labels field, so |
| 50 | * Alchemy stamps ownership into a `[alchemy …]` prefix and strips it |
| 51 | * from attributes. |
| 52 | */ |
| 53 | description?: string; |
| 54 | /** |
| 55 | * Salesforce org id (`00D…`). Stored on the instance; Application |
| 56 | * Integration uses it when opening channels. |
| 57 | */ |
| 58 | sfdcOrgId?: string; |
| 59 | /** |
| 60 | * AuthConfig resource names that can be tried to open a channel to |
| 61 | * Salesforce. |
| 62 | */ |
| 63 | authConfigId?: string[]; |
| 64 | /** |
| 65 | * URL used for API calls after authentication (login authority lives |
| 66 | * on the referenced AuthConfig). |
| 67 | */ |
| 68 | serviceAuthority?: string; |
| 69 | }; |
| 70 | |
| 71 | export type SfdcInstance = Resource< |
| 72 | "GCP.Integrations.SfdcInstance", |
| 73 | SfdcInstanceProps, |
| 74 | { |
| 75 | /** Full resource name `projects/{project}/locations/{location}/sfdcInstances/{id}`. */ |
| 76 | name: string; |
| 77 | /** Instance id (last path segment). */ |
| 78 | sfdcInstanceId: string; |
| 79 | /** Location id. */ |
| 80 | location: string; |
| 81 | /** Project id. */ |
| 82 | project: string; |
| 83 | /** User-selected alias. */ |
| 84 | displayName: string | undefined; |
| 85 | /** User description with the Alchemy ownership prefix stripped. */ |
| 86 | description: string | undefined; |
| 87 | /** Salesforce org id. */ |
| 88 | sfdcOrgId: string | undefined; |
| 89 | /** AuthConfig resource names. */ |
| 90 | authConfigId: string[]; |
| 91 | /** Post-auth API authority URL. */ |
| 92 | serviceAuthority: string | undefined; |
| 93 | /** RFC3339 creation timestamp. */ |
| 94 | createTime: string | undefined; |
| 95 | /** RFC3339 last-update timestamp. */ |
| 96 | updateTime: string | undefined; |
| 97 | }, |
| 98 | never, |
| 99 | Providers |
| 100 | >; |
| 101 | |
| 102 | /** |
| 103 | * A Salesforce instance record in Application Integration. Holds org |
| 104 | * identity and the AuthConfigs used to open SFDC channels. |
| 105 | * |
| 106 | * Instances have no labels field, so Alchemy stamps ownership into the |
| 107 | * description for `list` / nuke. Location and id are identity — changing |
| 108 | * them replaces the instance. Display name, description, org id, auth |
| 109 | * configs, and service authority update in place. |
| 110 | * |
| 111 | * ### Creating an SFDC Instance |
| 112 | * **Example:** Org alias in us-central1 |
| 113 | * ```typescript |
| 114 | * const instance = yield* GCP.Integrations.SfdcInstance("Salesforce", { |
| 115 | * displayName: "prod-org", |
| 116 | * description: "production salesforce", |
| 117 | * sfdcOrgId: "00Dxx0000000001", |
| 118 | * }); |
| 119 | * ``` |
| 120 | * |
| 121 | * ### Updating an SFDC Instance |
| 122 | * Re-declare the same logical id with changed props; the engine keeps the |
| 123 | * physical resource and updates it in place. |
| 124 | * |
| 125 | * **Example:** Change alias and org id |
| 126 | * ```typescript |
| 127 | * const instance = yield* GCP.Integrations.SfdcInstance("Salesforce", { |
| 128 | * displayName: "prod-org-v2", |
| 129 | * description: "production salesforce v2", |
| 130 | * sfdcOrgId: "00Dxx0000000002", |
| 131 | * }); |
| 132 | * ``` |
| 133 | * |
| 134 | * @resource |
| 135 | * @category Integrations |
| 136 | */ |
| 137 | export const SfdcInstance = Resource<SfdcInstance>("GCP.Integrations.SfdcInstance"); |
| 138 | |
| 139 | export class SfdcInstanceNotResolved extends Data.TaggedError( |
| 140 | "GCP.Integrations.SfdcInstanceNotResolved", |
| 141 | )<{ |
| 142 | resourceName: string; |
| 143 | }> {} |
| 144 | |
| 145 | const resourceName = (project: string, location: string, sfdcInstanceId: string) => |
| 146 | `${locationParent(project, location)}/sfdcInstances/${sfdcInstanceId}`; |
| 147 | |
| 148 | const toAttrs = ( |
| 149 | instance: integrations.GoogleCloudIntegrationsV1alphaSfdcInstance, |
| 150 | project: string, |
| 151 | region: string, |
| 152 | ) => { |
| 153 | const name = withProjectId(instance.name ?? "", project); |
| 154 | return { |
| 155 | name, |
| 156 | sfdcInstanceId: lastSegment(name), |
| 157 | location: locationOf(name, region), |
| 158 | project, |
| 159 | displayName: instance.displayName, |
| 160 | description: parseOwnership(instance.description).text, |
| 161 | sfdcOrgId: instance.sfdcOrgId, |
| 162 | authConfigId: [...(instance.authConfigId ?? [])], |
| 163 | serviceAuthority: instance.serviceAuthority, |
| 164 | createTime: instance.createTime, |
| 165 | updateTime: instance.updateTime, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const getByName = (name: string) => |
| 170 | name.length === 0 |
| 171 | ? Effect.undefined |
| 172 | : integrations.getProjectsLocationsSfdcInstances({ name }).pipe( |
| 173 | Effect.map((instance) => (isDeleted(instance) ? undefined : instance)), |
| 174 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 175 | ); |
| 176 | |
| 177 | export const SfdcInstanceProvider = () => |
| 178 | Provider.succeed(SfdcInstance, { |
| 179 | stables: ["name", "sfdcInstanceId", "location", "project", "createTime"], |
| 180 | |
| 181 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 182 | if (!isResolved(news)) return undefined; |
| 183 | const env = yield* GcpEnvironment.current; |
| 184 | const previousId = olds?.sfdcInstanceId ?? output?.sfdcInstanceId; |
| 185 | const idChanged = |
| 186 | previousId !== undefined && |
| 187 | news.sfdcInstanceId !== undefined && |
| 188 | news.sfdcInstanceId !== previousId; |
| 189 | const previousLocation = olds?.location ?? output?.location; |
| 190 | const locationChanged = |
| 191 | previousLocation !== undefined && |
| 192 | news.location !== undefined && |
| 193 | normalizeLocation(news.location, env.region) !== |
| 194 | normalizeLocation(previousLocation, env.region); |
| 195 | return replaceOnIdentity(idChanged || locationChanged); |
| 196 | }), |
| 197 | |
| 198 | read: Effect.fn(function* ({ id, olds, output }) { |
| 199 | const env = yield* GcpEnvironment.current; |
| 200 | const sfdcInstanceId = yield* toResourceId(id, olds?.sfdcInstanceId, output?.sfdcInstanceId); |
| 201 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 202 | const name = output?.name ?? resourceName(env.project, location, sfdcInstanceId); |
| 203 | let existing = yield* getByName(name); |
| 204 | if (existing === undefined && output?.name === undefined) { |
| 205 | const ownership = yield* createInternalLabels(id); |
| 206 | existing = yield* findSfdcInstanceByDescription( |
| 207 | locationParent(env.project, location), |
| 208 | encodeOwnership(ownership, olds?.description), |
| 209 | ); |
| 210 | } |
| 211 | if (existing === undefined) return undefined; |
| 212 | const attrs = toAttrs(existing, env.project, env.region); |
| 213 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 214 | }), |
| 215 | |
| 216 | list: () => |
| 217 | Effect.gen(function* () { |
| 218 | const env = yield* GcpEnvironment.current; |
| 219 | const items = yield* listSfdcInstances(locationParent(env.project, env.region)); |
| 220 | return items |
| 221 | .filter((instance) => hasOwnershipMarker(instance.description)) |
| 222 | .map((instance) => toAttrs(instance, env.project, env.region)); |
| 223 | }), |
| 224 | |
| 225 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 226 | const env = yield* GcpEnvironment.current; |
| 227 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 228 | const parent = locationParent(env.project, location); |
| 229 | const sfdcInstanceId = yield* toResourceId(id, news.sfdcInstanceId, output?.sfdcInstanceId); |
| 230 | const name = output?.name ?? resourceName(env.project, location, sfdcInstanceId); |
| 231 | const ownership = yield* createInternalLabels(id); |
| 232 | const description = encodeOwnership(ownership, news.description); |
| 233 | const displayName = news.displayName ?? sfdcInstanceId; |
| 234 | const authConfigId = news.authConfigId; |
| 235 | const body: integrations.GoogleCloudIntegrationsV1alphaSfdcInstance = { |
| 236 | displayName, |
| 237 | description, |
| 238 | sfdcOrgId: news.sfdcOrgId, |
| 239 | authConfigId, |
| 240 | serviceAuthority: news.serviceAuthority, |
| 241 | }; |
| 242 | |
| 243 | let current = yield* getByName(output?.name ?? name); |
| 244 | if (current === undefined) { |
| 245 | current = yield* findSfdcInstanceByDescription(parent, description); |
| 246 | } |
| 247 | |
| 248 | if (current === undefined) { |
| 249 | const created = yield* integrations |
| 250 | .createProjectsLocationsSfdcInstances({ |
| 251 | parent, |
| 252 | body, |
| 253 | }) |
| 254 | .pipe( |
| 255 | Effect.catchTag("Conflict", () => findSfdcInstanceByDescription(parent, description)), |
| 256 | ); |
| 257 | current = created ?? undefined; |
| 258 | } |
| 259 | |
| 260 | if (current === undefined) { |
| 261 | return yield* new SfdcInstanceNotResolved({ resourceName: name }); |
| 262 | } |
| 263 | |
| 264 | const currentName = withProjectId(current.name ?? name, env.project); |
| 265 | const displayChanged = !sameText(current.displayName, displayName); |
| 266 | const descriptionChanged = (current.description ?? "") !== description; |
| 267 | const orgChanged = !sameText(current.sfdcOrgId, news.sfdcOrgId); |
| 268 | const authChanged = !sameStringList(current.authConfigId, authConfigId); |
| 269 | const authorityChanged = !sameText(current.serviceAuthority, news.serviceAuthority); |
| 270 | |
| 271 | if (displayChanged || descriptionChanged || orgChanged || authChanged || authorityChanged) { |
| 272 | current = yield* integrations.patchProjectsLocationsSfdcInstances({ |
| 273 | name: currentName, |
| 274 | updateMask: updateMaskOf( |
| 275 | displayChanged ? "displayName" : undefined, |
| 276 | descriptionChanged ? "description" : undefined, |
| 277 | orgChanged ? "sfdcOrgId" : undefined, |
| 278 | authChanged ? "authConfigId" : undefined, |
| 279 | authorityChanged ? "serviceAuthority" : undefined, |
| 280 | ), |
| 281 | body: { ...body, name: currentName }, |
| 282 | }); |
| 283 | } |
| 284 | |
| 285 | return toAttrs(current, env.project, env.region); |
| 286 | }), |
| 287 | |
| 288 | delete: Effect.fn(function* ({ output }) { |
| 289 | yield* integrations |
| 290 | .deleteProjectsLocationsSfdcInstances({ name: output.name }) |
| 291 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 292 | }), |
| 293 | }); |
| 294 | |
| 295 | const sameStringList = ( |
| 296 | left: readonly string[] | undefined, |
| 297 | right: readonly string[] | undefined, |
| 298 | ) => JSON.stringify([...(left ?? [])]) === JSON.stringify([...(right ?? [])]); |