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 backupdr from "@distilled.cloud/gcp/backupdr_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, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | fingerprint, |
| 14 | listAtLocation, |
| 15 | listLabeledPages, |
| 16 | networkName, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | waitUntilReady, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type NetworkConfig = { |
| 31 | /** |
| 32 | * VPC network the management server is connected to. Full name |
| 33 | * `projects/{project}/global/networks/{network}` or a network id. |
| 34 | */ |
| 35 | network?: string; |
| 36 | /** |
| 37 | * Peering mode. Only `PRIVATE_SERVICE_ACCESS` is supported. |
| 38 | */ |
| 39 | peeringMode?: backupdr.NetworkConfigPeeringModeEnum | (string & {}); |
| 40 | }; |
| 41 | |
| 42 | export type ManagementURI = { |
| 43 | /** AGM/RD Web UI URL. */ |
| 44 | webUi?: string; |
| 45 | /** AGM/RD API URL. */ |
| 46 | api?: string; |
| 47 | }; |
| 48 | |
| 49 | export type WorkforceIdentityBasedManagementURI = { |
| 50 | /** Third-party management URI. */ |
| 51 | thirdPartyManagementUri?: string; |
| 52 | /** First-party management URI. */ |
| 53 | firstPartyManagementUri?: string; |
| 54 | }; |
| 55 | |
| 56 | export type WorkforceIdentityBasedOAuth2ClientID = { |
| 57 | /** First-party OAuth client id. */ |
| 58 | firstPartyOauth2ClientId?: string; |
| 59 | /** Third-party OAuth client id. */ |
| 60 | thirdPartyOauth2ClientId?: string; |
| 61 | }; |
| 62 | |
| 63 | export type ManagementServerProps = { |
| 64 | /** |
| 65 | * Management server id (the `{managementServer}` segment of |
| 66 | * `projects/{project}/locations/{location}/managementServers/{managementServer}`). |
| 67 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 68 | * and logical id. Immutable — changing it replaces the server. |
| 69 | */ |
| 70 | managementServerId?: string; |
| 71 | /** |
| 72 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 73 | * replaces the server. `US-CENTRAL1` is accepted and normalized to |
| 74 | * `us-central1`. |
| 75 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 76 | */ |
| 77 | location?: string; |
| 78 | /** |
| 79 | * Instance type. Immutable — changing it replaces the server. |
| 80 | * @default "BACKUP_RESTORE" |
| 81 | */ |
| 82 | type?: backupdr.ManagementServerTypeEnum | (string & {}); |
| 83 | /** |
| 84 | * VPC networks the server is connected to. Only a single network is |
| 85 | * supported. Optional when created without Private Service Access. |
| 86 | * Immutable — changing it replaces the server. |
| 87 | */ |
| 88 | networks?: NetworkConfig[]; |
| 89 | /** |
| 90 | * Human-readable description (2048 characters or less). Set at create; |
| 91 | * the API has no patch method. |
| 92 | */ |
| 93 | description?: string; |
| 94 | /** |
| 95 | * User labels. Alchemy ownership labels are merged in automatically. |
| 96 | * Set at create; the API has no patch method. |
| 97 | */ |
| 98 | labels?: Record<string, string>; |
| 99 | }; |
| 100 | |
| 101 | export type ManagementServer = Resource< |
| 102 | "GCP.BackupDR.ManagementServer", |
| 103 | ManagementServerProps, |
| 104 | { |
| 105 | /** Full resource name. */ |
| 106 | name: string; |
| 107 | /** Management server id (last path segment). */ |
| 108 | managementServerId: string; |
| 109 | /** Project id. */ |
| 110 | project: string; |
| 111 | /** Location id. */ |
| 112 | location: string; |
| 113 | /** Instance type. */ |
| 114 | type: string | undefined; |
| 115 | /** Connected VPC networks. */ |
| 116 | networks: NetworkConfig[]; |
| 117 | /** Human-readable description. */ |
| 118 | description: string | undefined; |
| 119 | /** User labels (Alchemy ownership labels stripped). */ |
| 120 | labels: Record<string, string>; |
| 121 | /** OAuth 2.0 client id for the management API. */ |
| 122 | oauth2ClientId: string | undefined; |
| 123 | /** Management URIs. */ |
| 124 | managementUri: ManagementURI | undefined; |
| 125 | /** Workforce-identity management URIs. */ |
| 126 | workforceIdentityBasedManagementUri: WorkforceIdentityBasedManagementURI | undefined; |
| 127 | /** Workforce-identity OAuth client ids. */ |
| 128 | workforceIdentityBasedOauth2ClientId: WorkforceIdentityBasedOAuth2ClientID | undefined; |
| 129 | /** BA proxy URIs. */ |
| 130 | baProxyUri: string[]; |
| 131 | /** Server-reported state. */ |
| 132 | state: string | undefined; |
| 133 | /** Assured Workloads PZI. */ |
| 134 | satisfiesPzi: boolean | undefined; |
| 135 | /** Assured Workloads PZS. */ |
| 136 | satisfiesPzs: boolean | undefined; |
| 137 | /** RFC3339 creation timestamp. */ |
| 138 | createTime: string | undefined; |
| 139 | /** RFC3339 last-update timestamp. */ |
| 140 | updateTime: string | undefined; |
| 141 | }, |
| 142 | never, |
| 143 | Providers |
| 144 | >; |
| 145 | |
| 146 | /** |
| 147 | * A Backup and DR management console (ManagementServer). Provisioning is |
| 148 | * slow (tens of minutes) and typically limited to one console per region. |
| 149 | * |
| 150 | * There is no patch API — labels, description, type, and networks are |
| 151 | * set at create. Changing `managementServerId`, `location`, `type`, or |
| 152 | * `networks` replaces the server. |
| 153 | * |
| 154 | * ### Creating a Management Server |
| 155 | * **Example:** Generated name, default network |
| 156 | * ```typescript |
| 157 | * const server = yield* GCP.BackupDR.ManagementServer("Console", { |
| 158 | * networks: [{ network: "default", peeringMode: "PRIVATE_SERVICE_ACCESS" }], |
| 159 | * }); |
| 160 | * ``` |
| 161 | * |
| 162 | * **Example:** Explicit id |
| 163 | * ```typescript |
| 164 | * const server = yield* GCP.BackupDR.ManagementServer("Console", { |
| 165 | * managementServerId: "backup-console", |
| 166 | * type: "BACKUP_RESTORE", |
| 167 | * description: "primary management console", |
| 168 | * labels: { env: "prod" }, |
| 169 | * }); |
| 170 | * ``` |
| 171 | * |
| 172 | * @resource |
| 173 | * @category BackupDR |
| 174 | */ |
| 175 | export const ManagementServer = Resource<ManagementServer>("GCP.BackupDR.ManagementServer"); |
| 176 | |
| 177 | const resourceName = (project: string, location: string, managementServerId: string) => |
| 178 | `projects/${project}/locations/${location}/managementServers/${managementServerId}`; |
| 179 | |
| 180 | const toNetworks = ( |
| 181 | project: string, |
| 182 | networks: readonly NetworkConfig[] | undefined, |
| 183 | ): NetworkConfig[] | undefined => { |
| 184 | if (networks === undefined) return undefined; |
| 185 | return networks.map((network) => ({ |
| 186 | network: networkName(project, network.network), |
| 187 | peeringMode: network.peeringMode, |
| 188 | })); |
| 189 | }; |
| 190 | |
| 191 | const fromNetworks = (networks: readonly backupdr.NetworkConfig[] | undefined): NetworkConfig[] => |
| 192 | (networks ?? []).map((network) => ({ |
| 193 | network: network.network, |
| 194 | peeringMode: network.peeringMode, |
| 195 | })); |
| 196 | |
| 197 | const toAttrs = (item: backupdr.ManagementServer, project: string, region: string) => { |
| 198 | const name = item.name ?? ""; |
| 199 | const parsed = parseName(name, "managementServers", region); |
| 200 | return { |
| 201 | name, |
| 202 | managementServerId: parsed.id, |
| 203 | project: parsed.project || project, |
| 204 | location: parsed.location, |
| 205 | type: item.type, |
| 206 | networks: fromNetworks(item.networks), |
| 207 | description: item.description, |
| 208 | labels: userLabels(item.labels), |
| 209 | oauth2ClientId: item.oauth2ClientId, |
| 210 | managementUri: item.managementUri, |
| 211 | workforceIdentityBasedManagementUri: item.workforceIdentityBasedManagementUri, |
| 212 | workforceIdentityBasedOauth2ClientId: item.workforceIdentityBasedOauth2ClientId, |
| 213 | baProxyUri: item.baProxyUri ?? [], |
| 214 | state: item.state, |
| 215 | satisfiesPzi: item.satisfiesPzi, |
| 216 | satisfiesPzs: item.satisfiesPzs, |
| 217 | createTime: item.createTime, |
| 218 | updateTime: item.updateTime, |
| 219 | }; |
| 220 | }; |
| 221 | |
| 222 | const getByName = (name: string) => |
| 223 | backupdr |
| 224 | .getProjectsLocationsManagementServers({ name }) |
| 225 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 226 | |
| 227 | const listOwned = (project: string, region: string) => |
| 228 | listAtLocation(project, region, (parent) => |
| 229 | listLabeledPages( |
| 230 | backupdr.listProjectsLocationsManagementServers.pages({ |
| 231 | parent, |
| 232 | pageSize: 1000, |
| 233 | }), |
| 234 | (page) => page.managementServers, |
| 235 | (item) => item.labels, |
| 236 | ), |
| 237 | ); |
| 238 | |
| 239 | export const ManagementServerProvider = () => |
| 240 | Provider.succeed(ManagementServer, { |
| 241 | stables: ["name", "managementServerId", "project", "location", "createTime"], |
| 242 | |
| 243 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 244 | if (!isResolved(news)) return undefined; |
| 245 | const env = yield* GcpEnvironment.current; |
| 246 | const previousType = olds?.type ?? output?.type; |
| 247 | const previousNetworks = fingerprint(olds?.networks ?? output?.networks); |
| 248 | const nextNetworks = |
| 249 | news.networks === undefined ? previousNetworks : fingerprint(news.networks); |
| 250 | return replaceOnIdentity({ |
| 251 | previousId: olds?.managementServerId ?? output?.managementServerId, |
| 252 | nextId: news.managementServerId ?? olds?.managementServerId ?? output?.managementServerId, |
| 253 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 254 | nextLocation: normalizeLocation( |
| 255 | news.location ?? olds?.location ?? output?.location, |
| 256 | env.region, |
| 257 | ), |
| 258 | extra: |
| 259 | (previousType !== undefined && news.type !== undefined && previousType !== news.type) || |
| 260 | previousNetworks !== nextNetworks, |
| 261 | }); |
| 262 | }), |
| 263 | |
| 264 | read: Effect.fn(function* ({ id, olds, output }) { |
| 265 | const env = yield* GcpEnvironment.current; |
| 266 | const managementServerId = yield* toPhysicalId( |
| 267 | id, |
| 268 | olds?.managementServerId, |
| 269 | output?.managementServerId, |
| 270 | "mgmtserver", |
| 271 | ); |
| 272 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 273 | const name = output?.name ?? resourceName(env.project, location, managementServerId); |
| 274 | const existing = yield* getByName(name); |
| 275 | if (existing === undefined) return undefined; |
| 276 | const attrs = toAttrs(existing, env.project, env.region); |
| 277 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 278 | }), |
| 279 | |
| 280 | list: () => |
| 281 | Effect.gen(function* () { |
| 282 | const env = yield* GcpEnvironment.current; |
| 283 | const items = yield* listOwned(env.project, env.region); |
| 284 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 285 | }), |
| 286 | |
| 287 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 288 | const env = yield* GcpEnvironment.current; |
| 289 | const managementServerId = yield* toPhysicalId( |
| 290 | id, |
| 291 | news.managementServerId, |
| 292 | output?.managementServerId, |
| 293 | "mgmtserver", |
| 294 | ); |
| 295 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 296 | const name = resourceName(env.project, location, managementServerId); |
| 297 | const desiredLabels = { |
| 298 | ...toLabels(news.labels), |
| 299 | ...(yield* createInternalLabels(id)), |
| 300 | }; |
| 301 | const networks = toNetworks(env.project, news.networks); |
| 302 | |
| 303 | let current = yield* getByName(output?.name ?? name); |
| 304 | |
| 305 | if (current === undefined) { |
| 306 | const created = yield* backupdr |
| 307 | .createProjectsLocationsManagementServers({ |
| 308 | parent: parentOf(env.project, location), |
| 309 | managementServerId, |
| 310 | body: { |
| 311 | type: news.type, |
| 312 | networks, |
| 313 | description: news.description, |
| 314 | labels: desiredLabels, |
| 315 | }, |
| 316 | }) |
| 317 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 318 | if (created !== undefined) { |
| 319 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 320 | yield* waitForOperation(created).pipe( |
| 321 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 322 | error.code === 6 ? Effect.void : Effect.fail(error), |
| 323 | ), |
| 324 | ); |
| 325 | } |
| 326 | current = yield* waitUntilExists(getByName(name), name); |
| 327 | } |
| 328 | |
| 329 | if (current === undefined) { |
| 330 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 331 | } |
| 332 | |
| 333 | current = yield* waitUntilReady( |
| 334 | getByName(current.name ?? name), |
| 335 | current.name ?? name, |
| 336 | (item) => item.state, |
| 337 | ); |
| 338 | |
| 339 | return toAttrs(current, env.project, env.region); |
| 340 | }), |
| 341 | |
| 342 | delete: Effect.fn(function* ({ output }) { |
| 343 | const operation = yield* backupdr |
| 344 | .deleteProjectsLocationsManagementServers({ name: output.name }) |
| 345 | .pipe( |
| 346 | Effect.retry({ |
| 347 | while: (error) => error._tag === "Conflict", |
| 348 | times: 8, |
| 349 | schedule: Schedule.spaced("2 seconds"), |
| 350 | }), |
| 351 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 352 | ); |
| 353 | if (operation !== undefined) { |
| 354 | // NOT_FOUND (5): already gone. |
| 355 | yield* waitForOperation(operation).pipe( |
| 356 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 357 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 358 | ), |
| 359 | ); |
| 360 | } |
| 361 | yield* waitUntilGone(getByName(output.name), output.name); |
| 362 | }), |
| 363 | }); |