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 networksecurity from "@distilled.cloud/gcp/networksecurity_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 Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | DEFAULT_LOCATION, |
| 16 | lastSegment, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseResourceName, |
| 20 | resourceName, |
| 21 | toId, |
| 22 | toResourcePath, |
| 23 | userLabels, |
| 24 | } from "./names.ts"; |
| 25 | import { waitForOperation } from "./operations.ts"; |
| 26 | |
| 27 | const COLLECTION = "mirroringEndpointGroups"; |
| 28 | const DEFAULT_TYPE = "DIRECT" satisfies networksecurity.MirroringEndpointGroupTypeEnum; |
| 29 | |
| 30 | export type MirroringEndpointGroupType = |
| 31 | | networksecurity.MirroringEndpointGroupTypeEnum |
| 32 | | (string & {}); |
| 33 | |
| 34 | export type MirroringEndpointGroupAssociationDetail = { |
| 35 | /** Connected association resource name. */ |
| 36 | name: string | undefined; |
| 37 | /** Associated VPC network. */ |
| 38 | network: string | undefined; |
| 39 | /** Most recent known association state. */ |
| 40 | state: string | undefined; |
| 41 | }; |
| 42 | |
| 43 | export type MirroringEndpointGroupConnectedDeploymentGroup = { |
| 44 | /** Connected deployment group resource name. */ |
| 45 | name: string | undefined; |
| 46 | /** Locations where the deployment group is present. */ |
| 47 | locations: Array<{ |
| 48 | location: string | undefined; |
| 49 | state: string | undefined; |
| 50 | }>; |
| 51 | }; |
| 52 | |
| 53 | export type MirroringEndpointGroupProps = { |
| 54 | /** |
| 55 | * Endpoint group id (the `{mirroringEndpointGroup}` segment of |
| 56 | * `projects/{project}/locations/{location}/mirroringEndpointGroups/{mirroringEndpointGroup}`). |
| 57 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 58 | * and logical id. Immutable — changing it replaces the group. |
| 59 | */ |
| 60 | mirroringEndpointGroupId?: string; |
| 61 | /** |
| 62 | * Location of the group. Endpoint groups are global — always |
| 63 | * `"global"`. Immutable — changing it replaces the group. `GLOBAL` is |
| 64 | * accepted and normalized to `global`. |
| 65 | * @default "global" |
| 66 | */ |
| 67 | location?: string; |
| 68 | /** |
| 69 | * Deployment group this DIRECT endpoint group is connected to, as a |
| 70 | * full resource name. Immutable — changing it replaces the group. |
| 71 | */ |
| 72 | mirroringDeploymentGroup: string; |
| 73 | /** |
| 74 | * Endpoint group type. If omitted, defaults to `DIRECT`. Immutable — |
| 75 | * changing it replaces the group. |
| 76 | * @default "DIRECT" |
| 77 | */ |
| 78 | type?: MirroringEndpointGroupType; |
| 79 | /** |
| 80 | * Human-readable description of the endpoint group. |
| 81 | */ |
| 82 | description?: string; |
| 83 | /** |
| 84 | * User labels. Alchemy ownership labels are merged in automatically. |
| 85 | */ |
| 86 | labels?: Record<string, string>; |
| 87 | }; |
| 88 | |
| 89 | export type MirroringEndpointGroup = Resource< |
| 90 | "GCP.NetworkSecurity.MirroringEndpointGroup", |
| 91 | MirroringEndpointGroupProps, |
| 92 | { |
| 93 | /** Full resource name. */ |
| 94 | name: string; |
| 95 | /** Endpoint group id (last path segment). */ |
| 96 | mirroringEndpointGroupId: string; |
| 97 | /** Project id. */ |
| 98 | project: string; |
| 99 | /** Location id. Always `"global"`. */ |
| 100 | location: string; |
| 101 | /** Connected deployment group resource name. */ |
| 102 | mirroringDeploymentGroup: string | undefined; |
| 103 | /** Endpoint group type (`DIRECT`, …). */ |
| 104 | type: string | undefined; |
| 105 | /** User-provided description. */ |
| 106 | description: string | undefined; |
| 107 | /** User labels (Alchemy ownership labels stripped). */ |
| 108 | labels: Record<string, string>; |
| 109 | /** Server-reported lifecycle state (`ACTIVE`, `CREATING`, …). */ |
| 110 | state: string | undefined; |
| 111 | /** Whether the API is still reconciling intended vs actual state. */ |
| 112 | reconciling: boolean; |
| 113 | /** Associations attached to this group. */ |
| 114 | associations: MirroringEndpointGroupAssociationDetail[]; |
| 115 | /** Connected deployment groups. */ |
| 116 | connectedDeploymentGroups: MirroringEndpointGroupConnectedDeploymentGroup[]; |
| 117 | /** RFC3339 creation timestamp. */ |
| 118 | createTime: string | undefined; |
| 119 | /** RFC3339 last-update timestamp. */ |
| 120 | updateTime: string | undefined; |
| 121 | }, |
| 122 | never, |
| 123 | Providers |
| 124 | >; |
| 125 | |
| 126 | /** |
| 127 | * A Network Security Integration mirroring endpoint group — the consumer |
| 128 | * frontend for a mirroring deployment group. |
| 129 | * |
| 130 | * Changing `mirroringEndpointGroupId`, `location`, `type`, or |
| 131 | * `mirroringDeploymentGroup` replaces the group. Description and labels |
| 132 | * update in place. |
| 133 | * |
| 134 | * ### Creating an Endpoint Group |
| 135 | * **Example:** Generated name |
| 136 | * ```typescript |
| 137 | * const endpoints = yield* GCP.NetworkSecurity.MirroringEndpointGroup("Front", { |
| 138 | * mirroringDeploymentGroup: group.name, |
| 139 | * }); |
| 140 | * ``` |
| 141 | * |
| 142 | * **Example:** Named group with labels |
| 143 | * ```typescript |
| 144 | * const endpoints = yield* GCP.NetworkSecurity.MirroringEndpointGroup("Front", { |
| 145 | * mirroringEndpointGroupId: "app-mirroring-eg", |
| 146 | * mirroringDeploymentGroup: group.name, |
| 147 | * description: "prod mirroring frontend", |
| 148 | * labels: { env: "prod" }, |
| 149 | * }); |
| 150 | * ``` |
| 151 | * |
| 152 | * ### Updating an Endpoint Group |
| 153 | * **Example:** Description and labels |
| 154 | * ```typescript |
| 155 | * const endpoints = yield* GCP.NetworkSecurity.MirroringEndpointGroup("Front", { |
| 156 | * mirroringEndpointGroupId: "app-mirroring-eg", |
| 157 | * mirroringDeploymentGroup: group.name, |
| 158 | * description: "prod mirroring frontend v2", |
| 159 | * labels: { env: "prod", role: "nsi" }, |
| 160 | * }); |
| 161 | * ``` |
| 162 | * |
| 163 | * @resource |
| 164 | * @category NetworkSecurity |
| 165 | */ |
| 166 | export const MirroringEndpointGroup = Resource<MirroringEndpointGroup>( |
| 167 | "GCP.NetworkSecurity.MirroringEndpointGroup", |
| 168 | ); |
| 169 | |
| 170 | export class MirroringEndpointGroupNotResolved extends Data.TaggedError( |
| 171 | "GCP.NetworkSecurity.MirroringEndpointGroupNotResolved", |
| 172 | )<{ |
| 173 | resourceName: string; |
| 174 | }> {} |
| 175 | |
| 176 | export class MirroringEndpointGroupFailed extends Data.TaggedError( |
| 177 | "GCP.NetworkSecurity.MirroringEndpointGroupFailed", |
| 178 | )<{ |
| 179 | resourceName: string; |
| 180 | state: string | undefined; |
| 181 | }> {} |
| 182 | |
| 183 | export class MirroringEndpointGroupStillExists extends Data.TaggedError( |
| 184 | "GCP.NetworkSecurity.MirroringEndpointGroupStillExists", |
| 185 | )<{ |
| 186 | resourceName: string; |
| 187 | }> {} |
| 188 | |
| 189 | const isPendingState = (state: string | undefined) => |
| 190 | state === "CREATING" || state === "DELETING" || state === "STATE_UNSPECIFIED"; |
| 191 | |
| 192 | const toAssociations = ( |
| 193 | associations: networksecurity.MirroringEndpointGroupAssociationDetailsList | undefined, |
| 194 | ): MirroringEndpointGroupAssociationDetail[] => |
| 195 | (associations ?? []).map((item) => ({ |
| 196 | name: item.name, |
| 197 | network: item.network, |
| 198 | state: item.state, |
| 199 | })); |
| 200 | |
| 201 | const toConnected = ( |
| 202 | groups: networksecurity.MirroringEndpointGroupConnectedDeploymentGroupList | undefined, |
| 203 | ): MirroringEndpointGroupConnectedDeploymentGroup[] => |
| 204 | (groups ?? []).map((item) => ({ |
| 205 | name: item.name, |
| 206 | locations: (item.locations ?? []).map((location) => ({ |
| 207 | location: location.location, |
| 208 | state: location.state, |
| 209 | })), |
| 210 | })); |
| 211 | |
| 212 | const toAttrs = (group: networksecurity.MirroringEndpointGroup, project: string) => { |
| 213 | const name = group.name ?? ""; |
| 214 | const parsed = parseResourceName(name, COLLECTION); |
| 215 | return { |
| 216 | name, |
| 217 | mirroringEndpointGroupId: parsed.id, |
| 218 | project: parsed.project || project, |
| 219 | location: parsed.location || DEFAULT_LOCATION, |
| 220 | mirroringDeploymentGroup: group.mirroringDeploymentGroup, |
| 221 | type: group.type, |
| 222 | description: group.description, |
| 223 | labels: userLabels(group.labels), |
| 224 | state: group.state, |
| 225 | reconciling: group.reconciling === true, |
| 226 | associations: toAssociations(group.associations), |
| 227 | connectedDeploymentGroups: toConnected(group.connectedDeploymentGroups), |
| 228 | createTime: group.createTime, |
| 229 | updateTime: group.updateTime, |
| 230 | }; |
| 231 | }; |
| 232 | |
| 233 | const getByName = (name: string) => |
| 234 | networksecurity |
| 235 | .getProjectsLocationsMirroringEndpointGroups({ name }) |
| 236 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 237 | |
| 238 | const waitUntilReady = (name: string) => |
| 239 | getByName(name).pipe( |
| 240 | Effect.filterOrFail( |
| 241 | (group): group is networksecurity.MirroringEndpointGroup => group !== undefined, |
| 242 | () => new MirroringEndpointGroupNotResolved({ resourceName: name }), |
| 243 | ), |
| 244 | Effect.filterOrFail( |
| 245 | (group) => group.state !== "DELETE_FAILED", |
| 246 | (group) => new MirroringEndpointGroupFailed({ resourceName: name, state: group.state }), |
| 247 | ), |
| 248 | Effect.filterOrFail( |
| 249 | (group) => !isPendingState(group.state), |
| 250 | () => new MirroringEndpointGroupNotResolved({ resourceName: name }), |
| 251 | ), |
| 252 | Effect.retry({ |
| 253 | while: (error) => error._tag === "GCP.NetworkSecurity.MirroringEndpointGroupNotResolved", |
| 254 | times: 10, |
| 255 | schedule: Schedule.spaced("2 seconds"), |
| 256 | }), |
| 257 | ); |
| 258 | |
| 259 | const waitUntilGone = (name: string) => |
| 260 | getByName(name).pipe( |
| 261 | Effect.flatMap((group) => |
| 262 | group === undefined |
| 263 | ? Effect.void |
| 264 | : Effect.fail(new MirroringEndpointGroupStillExists({ resourceName: name })), |
| 265 | ), |
| 266 | Effect.retry({ |
| 267 | while: (error) => error._tag === "GCP.NetworkSecurity.MirroringEndpointGroupStillExists", |
| 268 | times: 10, |
| 269 | schedule: Schedule.spaced("2 seconds"), |
| 270 | }), |
| 271 | ); |
| 272 | |
| 273 | const listOwned = (project: string) => |
| 274 | networksecurity.listProjectsLocationsMirroringEndpointGroups |
| 275 | .pages({ |
| 276 | parent: parentOf(project, "-"), |
| 277 | pageSize: 1000, |
| 278 | }) |
| 279 | .pipe( |
| 280 | Stream.flatMap((page) => Stream.fromIterable(page.mirroringEndpointGroups ?? [])), |
| 281 | Stream.filter((group) => |
| 282 | Object.keys(group.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 283 | ), |
| 284 | Stream.map((group) => toAttrs(group, project)), |
| 285 | Stream.runCollect, |
| 286 | Effect.map((chunk) => Array.from(chunk)), |
| 287 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 288 | ); |
| 289 | |
| 290 | export const MirroringEndpointGroupProvider = () => |
| 291 | Provider.succeed(MirroringEndpointGroup, { |
| 292 | stables: [ |
| 293 | "name", |
| 294 | "mirroringEndpointGroupId", |
| 295 | "project", |
| 296 | "location", |
| 297 | "mirroringDeploymentGroup", |
| 298 | "type", |
| 299 | "createTime", |
| 300 | ], |
| 301 | |
| 302 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 303 | if (!isResolved(news)) return undefined; |
| 304 | const previousId = olds?.mirroringEndpointGroupId ?? output?.mirroringEndpointGroupId; |
| 305 | const nextId = news.mirroringEndpointGroupId ?? previousId; |
| 306 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 307 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 308 | const previousGroup = lastSegment( |
| 309 | olds?.mirroringDeploymentGroup ?? output?.mirroringDeploymentGroup ?? "", |
| 310 | ); |
| 311 | const nextGroup = lastSegment(news.mirroringDeploymentGroup); |
| 312 | const previousType = (olds?.type ?? output?.type ?? DEFAULT_TYPE).toUpperCase(); |
| 313 | const nextType = (news.type ?? previousType).toUpperCase(); |
| 314 | const replace = |
| 315 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 316 | previousLocation !== nextLocation || |
| 317 | (previousGroup.length > 0 && previousGroup !== nextGroup) || |
| 318 | previousType !== nextType; |
| 319 | if (!replace) return undefined; |
| 320 | return { action: "replace" as const }; |
| 321 | }), |
| 322 | |
| 323 | read: Effect.fn(function* ({ id, olds, output }) { |
| 324 | const env = yield* GcpEnvironment.current; |
| 325 | const mirroringEndpointGroupId = yield* toId( |
| 326 | id, |
| 327 | olds?.mirroringEndpointGroupId, |
| 328 | output?.mirroringEndpointGroupId, |
| 329 | "meg", |
| 330 | ); |
| 331 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 332 | const name = |
| 333 | output?.name ?? resourceName(env.project, location, COLLECTION, mirroringEndpointGroupId); |
| 334 | const existing = yield* getByName(name); |
| 335 | if (existing === undefined) return undefined; |
| 336 | const attrs = toAttrs(existing, env.project); |
| 337 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 338 | }), |
| 339 | |
| 340 | list: () => |
| 341 | Effect.gen(function* () { |
| 342 | const env = yield* GcpEnvironment.current; |
| 343 | return yield* listOwned(env.project); |
| 344 | }), |
| 345 | |
| 346 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 347 | const env = yield* GcpEnvironment.current; |
| 348 | const mirroringEndpointGroupId = yield* toId( |
| 349 | id, |
| 350 | news.mirroringEndpointGroupId, |
| 351 | output?.mirroringEndpointGroupId, |
| 352 | "meg", |
| 353 | ); |
| 354 | const location = normalizeLocation(news.location ?? output?.location); |
| 355 | const name = resourceName(env.project, location, COLLECTION, mirroringEndpointGroupId); |
| 356 | const mirroringDeploymentGroup = toResourcePath(news.mirroringDeploymentGroup); |
| 357 | const type = news.type ?? DEFAULT_TYPE; |
| 358 | const desiredLabels = { |
| 359 | ...toLabels(news.labels), |
| 360 | ...(yield* createInternalLabels(id)), |
| 361 | }; |
| 362 | |
| 363 | let current = yield* getByName(name); |
| 364 | |
| 365 | if (current === undefined) { |
| 366 | const created = yield* networksecurity |
| 367 | .createProjectsLocationsMirroringEndpointGroups({ |
| 368 | parent: parentOf(env.project, location), |
| 369 | mirroringEndpointGroupId, |
| 370 | body: { |
| 371 | mirroringDeploymentGroup, |
| 372 | type, |
| 373 | description: news.description, |
| 374 | labels: desiredLabels, |
| 375 | }, |
| 376 | }) |
| 377 | .pipe( |
| 378 | Effect.retry({ |
| 379 | while: (error) => error._tag === "Conflict", |
| 380 | times: 5, |
| 381 | schedule: Schedule.spaced("2 seconds"), |
| 382 | }), |
| 383 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 384 | ); |
| 385 | if (created !== undefined) { |
| 386 | yield* waitForOperation(created); |
| 387 | } |
| 388 | current = yield* waitUntilReady(name); |
| 389 | } |
| 390 | |
| 391 | if (current === undefined) { |
| 392 | return yield* new MirroringEndpointGroupNotResolved({ resourceName: name }); |
| 393 | } |
| 394 | |
| 395 | const observedLabels = tagRecord(current.labels); |
| 396 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 397 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 398 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 399 | |
| 400 | if (labelsChanged || descriptionChanged) { |
| 401 | const updateMask = [ |
| 402 | labelsChanged ? "labels" : undefined, |
| 403 | descriptionChanged ? "description" : undefined, |
| 404 | ].filter((field): field is string => field !== undefined); |
| 405 | const operation = yield* networksecurity.patchProjectsLocationsMirroringEndpointGroups({ |
| 406 | name: current.name ?? name, |
| 407 | updateMask: updateMask.join(","), |
| 408 | body: { |
| 409 | name: current.name ?? name, |
| 410 | labels: desiredLabels, |
| 411 | description: news.description, |
| 412 | }, |
| 413 | }); |
| 414 | yield* waitForOperation(operation); |
| 415 | current = yield* waitUntilReady(current.name ?? name); |
| 416 | } |
| 417 | |
| 418 | return toAttrs(current, env.project); |
| 419 | }), |
| 420 | |
| 421 | delete: Effect.fn(function* ({ output }) { |
| 422 | const operation = yield* networksecurity |
| 423 | .deleteProjectsLocationsMirroringEndpointGroups({ |
| 424 | name: output.name, |
| 425 | }) |
| 426 | .pipe( |
| 427 | Effect.retry({ |
| 428 | while: (error) => error._tag === "Conflict", |
| 429 | times: 8, |
| 430 | schedule: Schedule.spaced("2 seconds"), |
| 431 | }), |
| 432 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 433 | ); |
| 434 | if (operation !== undefined) { |
| 435 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 436 | } |
| 437 | yield* waitUntilGone(output.name); |
| 438 | }), |
| 439 | }); |