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 mc from "@distilled.cloud/gcp/migrationcenter_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 * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | expandParent, |
| 15 | fieldMask, |
| 16 | hasAlchemyLabelMap, |
| 17 | locationParent, |
| 18 | normalizeLocation, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type DiscoveryClientState = mc.DiscoveryClientStateEnum | (string & {}); |
| 30 | |
| 31 | export type DiscoveryClientProps = { |
| 32 | /** |
| 33 | * Discovery client id (the `{discoveryClient}` segment of |
| 34 | * `projects/{project}/locations/{location}/discoveryClients/{discoveryClient}`). |
| 35 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 36 | * it replaces the client. |
| 37 | */ |
| 38 | discoveryClientId?: string; |
| 39 | /** |
| 40 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 41 | * client. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 42 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 43 | * region, else `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * Full name of the associated source, or the source id (combined with |
| 48 | * `location`). Immutable — changing it replaces the client. |
| 49 | */ |
| 50 | source: string; |
| 51 | /** |
| 52 | * Service account used by the discovery client |
| 53 | * (`user@project.iam.gserviceaccount.com` or |
| 54 | * `projects/{project}/serviceAccounts/{email}`). |
| 55 | */ |
| 56 | serviceAccount: string; |
| 57 | /** |
| 58 | * User-friendly display name. Maximum length is 63 characters. |
| 59 | */ |
| 60 | displayName?: string; |
| 61 | /** |
| 62 | * Free-text description. Maximum length is 1000 characters. |
| 63 | */ |
| 64 | description?: string; |
| 65 | /** |
| 66 | * Client time-to-live (input only), e.g. `"2592000s"`. |
| 67 | */ |
| 68 | ttl?: string; |
| 69 | /** |
| 70 | * Client expiration time in UTC. The backend stops accepting frames |
| 71 | * after this time. |
| 72 | */ |
| 73 | expireTime?: string; |
| 74 | /** |
| 75 | * User labels. Alchemy ownership labels are merged in automatically. |
| 76 | */ |
| 77 | labels?: Record<string, string>; |
| 78 | }; |
| 79 | |
| 80 | export type DiscoveryClient = Resource< |
| 81 | "GCP.MigrationCenter.DiscoveryClient", |
| 82 | DiscoveryClientProps, |
| 83 | { |
| 84 | /** Full resource name. */ |
| 85 | name: string; |
| 86 | /** Discovery client id (last path segment). */ |
| 87 | discoveryClientId: string; |
| 88 | /** Project id. */ |
| 89 | project: string; |
| 90 | /** Location id. */ |
| 91 | location: string; |
| 92 | /** Associated source resource name. */ |
| 93 | source: string | undefined; |
| 94 | /** Service account used by the client. */ |
| 95 | serviceAccount: string | undefined; |
| 96 | /** User-friendly display name. */ |
| 97 | displayName: string | undefined; |
| 98 | /** Free-text description. */ |
| 99 | description: string | undefined; |
| 100 | /** Expiration timestamp. */ |
| 101 | expireTime: string | undefined; |
| 102 | /** User labels (Alchemy ownership labels stripped). */ |
| 103 | labels: Record<string, string>; |
| 104 | /** Server-reported state. */ |
| 105 | state: string | undefined; |
| 106 | /** Client version from the last heartbeat. */ |
| 107 | version: string | undefined; |
| 108 | /** Signals endpoint. */ |
| 109 | signalsEndpoint: string | undefined; |
| 110 | /** Last heartbeat timestamp. */ |
| 111 | heartbeatTime: string | undefined; |
| 112 | /** RFC3339 creation timestamp. */ |
| 113 | createTime: string | undefined; |
| 114 | /** RFC3339 last-update timestamp. */ |
| 115 | updateTime: string | undefined; |
| 116 | }, |
| 117 | never, |
| 118 | Providers |
| 119 | >; |
| 120 | |
| 121 | /** |
| 122 | * A Migration Center discovery client that reports on-prem asset frames |
| 123 | * for a source. |
| 124 | * |
| 125 | * `discoveryClientId`, `location`, and `source` are immutable. Display |
| 126 | * name, description, service account, expiration, and labels update in |
| 127 | * place. |
| 128 | * |
| 129 | * ### Creating a Discovery Client |
| 130 | * **Example:** Bind a scanner to a source |
| 131 | * ```typescript |
| 132 | * const source = yield* GCP.MigrationCenter.Source("Scanner", { |
| 133 | * type: "SOURCE_TYPE_DISCOVERY_CLIENT", |
| 134 | * }); |
| 135 | * const client = yield* GCP.MigrationCenter.DiscoveryClient("Agent", { |
| 136 | * source: source.name, |
| 137 | * serviceAccount: "scanner@my-project.iam.gserviceaccount.com", |
| 138 | * displayName: "on-prem-agent", |
| 139 | * }); |
| 140 | * ``` |
| 141 | * |
| 142 | * @resource |
| 143 | * @category MigrationCenter |
| 144 | */ |
| 145 | export const DiscoveryClient = Resource<DiscoveryClient>("GCP.MigrationCenter.DiscoveryClient"); |
| 146 | |
| 147 | const resourceName = (project: string, location: string, discoveryClientId: string) => |
| 148 | `${locationParent(project, location)}/discoveryClients/${discoveryClientId}`; |
| 149 | |
| 150 | const sourceOf = (value: string, project: string, location: string) => |
| 151 | expandParent(value, project, location, "sources"); |
| 152 | |
| 153 | const toAttrs = (item: mc.DiscoveryClient, project: string, region: string) => { |
| 154 | const name = item.name ?? ""; |
| 155 | const parsed = parseName(name, "discoveryClients", region); |
| 156 | return { |
| 157 | name, |
| 158 | discoveryClientId: parsed.id, |
| 159 | project: parsed.project || project, |
| 160 | location: parsed.location, |
| 161 | source: item.source, |
| 162 | serviceAccount: item.serviceAccount, |
| 163 | displayName: item.displayName, |
| 164 | description: item.description, |
| 165 | expireTime: item.expireTime, |
| 166 | labels: userLabels(item.labels), |
| 167 | state: item.state, |
| 168 | version: item.version, |
| 169 | signalsEndpoint: item.signalsEndpoint, |
| 170 | heartbeatTime: item.heartbeatTime, |
| 171 | createTime: item.createTime, |
| 172 | updateTime: item.updateTime, |
| 173 | }; |
| 174 | }; |
| 175 | |
| 176 | const getByName = (name: string) => |
| 177 | name.length === 0 |
| 178 | ? Effect.undefined |
| 179 | : mc |
| 180 | .getProjectsLocationsDiscoveryClients({ name }) |
| 181 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 182 | |
| 183 | const listOwned = (project: string, region: string) => |
| 184 | mc.listProjectsLocationsDiscoveryClients |
| 185 | .pages({ |
| 186 | parent: `projects/${project}/locations/-`, |
| 187 | pageSize: 1000, |
| 188 | }) |
| 189 | .pipe( |
| 190 | Stream.flatMap((page) => Stream.fromIterable(page.discoveryClients ?? [])), |
| 191 | Stream.filter((item) => hasAlchemyLabelMap(item.labels)), |
| 192 | Stream.runCollect, |
| 193 | Effect.map((chunk) => Array.from(chunk)), |
| 194 | Effect.catchTag("NotFound", () => |
| 195 | mc.listProjectsLocationsDiscoveryClients |
| 196 | .pages({ |
| 197 | parent: locationParent(project, region), |
| 198 | pageSize: 1000, |
| 199 | }) |
| 200 | .pipe( |
| 201 | Stream.flatMap((page) => Stream.fromIterable(page.discoveryClients ?? [])), |
| 202 | Stream.filter((item) => hasAlchemyLabelMap(item.labels)), |
| 203 | Stream.runCollect, |
| 204 | Effect.map((chunk) => Array.from(chunk)), |
| 205 | Effect.catchTag("NotFound", () => Effect.succeed([] as mc.DiscoveryClient[])), |
| 206 | ), |
| 207 | ), |
| 208 | ); |
| 209 | |
| 210 | export const DiscoveryClientProvider = () => |
| 211 | Provider.succeed(DiscoveryClient, { |
| 212 | stables: ["name", "discoveryClientId", "project", "location", "createTime"], |
| 213 | |
| 214 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 215 | if (!isResolved(news)) return undefined; |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const previousSource = olds?.source ?? output?.source; |
| 218 | const nextSource = news.source; |
| 219 | const sourceChanged = |
| 220 | previousSource !== undefined && |
| 221 | nextSource !== undefined && |
| 222 | previousSource !== nextSource && |
| 223 | !previousSource.endsWith(`/${nextSource}`) && |
| 224 | !nextSource.endsWith(`/${previousSource}`); |
| 225 | return replaceOnIdentity({ |
| 226 | previousId: olds?.discoveryClientId ?? output?.discoveryClientId, |
| 227 | nextId: news.discoveryClientId ?? olds?.discoveryClientId ?? output?.discoveryClientId, |
| 228 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 229 | nextLocation: normalizeLocation( |
| 230 | news.location ?? olds?.location ?? output?.location, |
| 231 | env.region, |
| 232 | ), |
| 233 | extra: sourceChanged, |
| 234 | }); |
| 235 | }), |
| 236 | |
| 237 | read: Effect.fn(function* ({ id, olds, output }) { |
| 238 | const env = yield* GcpEnvironment.current; |
| 239 | const discoveryClientId = yield* toPhysicalId( |
| 240 | id, |
| 241 | olds?.discoveryClientId, |
| 242 | output?.discoveryClientId, |
| 243 | "discclient", |
| 244 | ); |
| 245 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 246 | const name = output?.name ?? resourceName(env.project, location, discoveryClientId); |
| 247 | const existing = yield* getByName(name); |
| 248 | if (existing === undefined) return undefined; |
| 249 | const attrs = toAttrs(existing, env.project, env.region); |
| 250 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 251 | }), |
| 252 | |
| 253 | list: () => |
| 254 | Effect.gen(function* () { |
| 255 | const env = yield* GcpEnvironment.current; |
| 256 | const items = yield* listOwned(env.project, env.region); |
| 257 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 258 | }), |
| 259 | |
| 260 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 261 | const env = yield* GcpEnvironment.current; |
| 262 | const discoveryClientId = yield* toPhysicalId( |
| 263 | id, |
| 264 | news.discoveryClientId, |
| 265 | output?.discoveryClientId, |
| 266 | "discclient", |
| 267 | ); |
| 268 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 269 | const name = resourceName(env.project, location, discoveryClientId); |
| 270 | const source = sourceOf(news.source, env.project, location); |
| 271 | const desiredLabels = { |
| 272 | ...toLabels(news.labels), |
| 273 | ...(yield* createInternalLabels(id)), |
| 274 | }; |
| 275 | const displayName = news.displayName ?? discoveryClientId; |
| 276 | |
| 277 | let current = yield* getByName(output?.name ?? name); |
| 278 | |
| 279 | if (current === undefined) { |
| 280 | const created = yield* mc |
| 281 | .createProjectsLocationsDiscoveryClients({ |
| 282 | parent: locationParent(env.project, location), |
| 283 | discoveryClientId, |
| 284 | body: { |
| 285 | source, |
| 286 | serviceAccount: news.serviceAccount, |
| 287 | displayName, |
| 288 | description: news.description, |
| 289 | ttl: news.ttl, |
| 290 | expireTime: news.expireTime, |
| 291 | labels: desiredLabels, |
| 292 | }, |
| 293 | }) |
| 294 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 295 | if (created !== undefined) { |
| 296 | yield* waitForOperation(created); |
| 297 | } |
| 298 | current = yield* waitUntilExists(getByName(name), name); |
| 299 | } |
| 300 | |
| 301 | if (current === undefined) { |
| 302 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 303 | } |
| 304 | |
| 305 | const observedLabels = tagRecord(current.labels); |
| 306 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 307 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 308 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 309 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 310 | const serviceAccountChanged = (current.serviceAccount ?? "") !== news.serviceAccount; |
| 311 | const expireTimeChanged = (current.expireTime ?? "") !== (news.expireTime ?? ""); |
| 312 | const mask = fieldMask([ |
| 313 | labelsChanged && "labels", |
| 314 | displayNameChanged && "displayName", |
| 315 | descriptionChanged && "description", |
| 316 | serviceAccountChanged && "serviceAccount", |
| 317 | expireTimeChanged && "expireTime", |
| 318 | ]); |
| 319 | |
| 320 | if (mask.length > 0) { |
| 321 | const operation = yield* mc.patchProjectsLocationsDiscoveryClients({ |
| 322 | name: current.name ?? name, |
| 323 | updateMask: mask, |
| 324 | body: { |
| 325 | name: current.name ?? name, |
| 326 | displayName, |
| 327 | description: news.description, |
| 328 | serviceAccount: news.serviceAccount, |
| 329 | expireTime: news.expireTime, |
| 330 | labels: desiredLabels, |
| 331 | }, |
| 332 | }); |
| 333 | yield* waitForOperation(operation); |
| 334 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 335 | } |
| 336 | |
| 337 | return toAttrs(current, env.project, env.region); |
| 338 | }), |
| 339 | |
| 340 | delete: Effect.fn(function* ({ output }) { |
| 341 | const operation = yield* mc |
| 342 | .deleteProjectsLocationsDiscoveryClients({ name: output.name }) |
| 343 | .pipe( |
| 344 | Effect.retry({ |
| 345 | while: (error) => error._tag === "Conflict", |
| 346 | times: 8, |
| 347 | schedule: Schedule.spaced("2 seconds"), |
| 348 | }), |
| 349 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 350 | ); |
| 351 | if (operation !== undefined) { |
| 352 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 353 | } |
| 354 | yield* waitUntilGone(getByName(output.name), output.name); |
| 355 | }), |
| 356 | }); |