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