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