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