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 Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DEFAULT_ZONE, |
| 14 | canonicalizeLink, |
| 15 | changedFields, |
| 16 | collectPages, |
| 17 | hasAlchemyLabelKeys, |
| 18 | linkKey, |
| 19 | normalizeLocation, |
| 20 | parentOf, |
| 21 | parseName, |
| 22 | rfc1035, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitForOperation, |
| 26 | waitUntilGone, |
| 27 | waitUntilReady, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "interceptDeployments"; |
| 31 | |
| 32 | export type InterceptDeploymentState = networksecurity.InterceptDeploymentStateEnum | (string & {}); |
| 33 | |
| 34 | export type InterceptDeploymentProps = { |
| 35 | /** |
| 36 | * Deployment id (the `{interceptDeployment}` segment of |
| 37 | * `projects/{project}/locations/{location}/interceptDeployments/{interceptDeployment}`). |
| 38 | * If omitted, a unique name is generated from the stack, stage, and |
| 39 | * logical id. Immutable — changing it replaces the deployment. |
| 40 | */ |
| 41 | interceptDeploymentId?: string; |
| 42 | /** |
| 43 | * Zone of the deployment (`us-central1-a`, …). Immutable — changing |
| 44 | * it replaces the deployment. `US-CENTRAL1-A` is accepted and |
| 45 | * normalized to `us-central1-a`. |
| 46 | * @default "us-central1-a" |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * Parent InterceptDeploymentGroup resource name |
| 51 | * (`projects/{project}/locations/global/interceptDeploymentGroups/{interceptDeploymentGroup}`). |
| 52 | * Immutable — changing it replaces the deployment. |
| 53 | */ |
| 54 | interceptDeploymentGroup: string; |
| 55 | /** |
| 56 | * Regional forwarding rule that fronts interceptors, e.g. |
| 57 | * `projects/{project}/regions/{region}/forwardingRules/{forwardingRule}`. |
| 58 | * Immutable — changing it replaces the deployment. |
| 59 | */ |
| 60 | forwardingRule: string; |
| 61 | /** |
| 62 | * Human-readable description. |
| 63 | */ |
| 64 | description?: string; |
| 65 | /** |
| 66 | * User labels. Alchemy ownership labels are merged in automatically. |
| 67 | */ |
| 68 | labels?: Record<string, string>; |
| 69 | }; |
| 70 | |
| 71 | export type InterceptDeployment = Resource< |
| 72 | "GCP.NetworkSecurity.InterceptDeployment", |
| 73 | InterceptDeploymentProps, |
| 74 | { |
| 75 | /** Full resource name `projects/{project}/locations/{location}/interceptDeployments/{interceptDeployment}`. */ |
| 76 | name: string; |
| 77 | /** Deployment id (last path segment). */ |
| 78 | interceptDeploymentId: string; |
| 79 | /** Project id. */ |
| 80 | project: string; |
| 81 | /** Zone id (`us-central1-a`). */ |
| 82 | location: string; |
| 83 | /** Parent InterceptDeploymentGroup resource name. */ |
| 84 | interceptDeploymentGroup: string | undefined; |
| 85 | /** Forwarding rule resource name. */ |
| 86 | forwardingRule: string | undefined; |
| 87 | /** User-provided description. */ |
| 88 | description: string | undefined; |
| 89 | /** User labels (Alchemy ownership labels stripped). */ |
| 90 | labels: Record<string, string>; |
| 91 | /** Server-reported lifecycle state (`ACTIVE`, `CREATING`, …). */ |
| 92 | state: string | undefined; |
| 93 | /** Whether reconciling is in progress. */ |
| 94 | reconciling: boolean; |
| 95 | /** RFC3339 creation timestamp. */ |
| 96 | createTime: string | undefined; |
| 97 | /** RFC3339 last-update timestamp. */ |
| 98 | updateTime: string | undefined; |
| 99 | }, |
| 100 | never, |
| 101 | Providers |
| 102 | >; |
| 103 | |
| 104 | /** |
| 105 | * A zonal intercept deployment — a GENEVE backend (typically an |
| 106 | * internal passthrough load balancer) that belongs to a global |
| 107 | * InterceptDeploymentGroup. |
| 108 | * |
| 109 | * Changing `interceptDeploymentId`, `location`, `interceptDeploymentGroup`, |
| 110 | * or `forwardingRule` replaces the deployment. Description and labels |
| 111 | * update in place. |
| 112 | * |
| 113 | * ### Creating an InterceptDeployment |
| 114 | * **Example:** Attach a forwarding rule to a group |
| 115 | * ```typescript |
| 116 | * const deployment = yield* GCP.NetworkSecurity.InterceptDeployment( |
| 117 | * "ZoneA", |
| 118 | * { |
| 119 | * location: "us-central1-a", |
| 120 | * interceptDeploymentGroup: group.name, |
| 121 | * forwardingRule: rule.selfLink, |
| 122 | * description: "us-central1-a interceptors", |
| 123 | * }, |
| 124 | * ); |
| 125 | * ``` |
| 126 | * |
| 127 | * @resource |
| 128 | * @category NetworkSecurity |
| 129 | */ |
| 130 | export const InterceptDeployment = Resource<InterceptDeployment>( |
| 131 | "GCP.NetworkSecurity.InterceptDeployment", |
| 132 | ); |
| 133 | |
| 134 | const resourceName = (project: string, location: string, interceptDeploymentId: string) => |
| 135 | `projects/${project}/locations/${location}/interceptDeployments/${interceptDeploymentId}`; |
| 136 | |
| 137 | const toAttrs = (deployment: networksecurity.InterceptDeployment, project: string) => { |
| 138 | const name = deployment.name ?? ""; |
| 139 | const parsed = parseName(name, COLLECTION, DEFAULT_ZONE); |
| 140 | return { |
| 141 | name, |
| 142 | interceptDeploymentId: parsed.id, |
| 143 | project: parsed.project || project, |
| 144 | location: parsed.location || DEFAULT_ZONE, |
| 145 | interceptDeploymentGroup: deployment.interceptDeploymentGroup |
| 146 | ? canonicalizeLink(deployment.interceptDeploymentGroup) |
| 147 | : undefined, |
| 148 | forwardingRule: deployment.forwardingRule |
| 149 | ? canonicalizeLink(deployment.forwardingRule) |
| 150 | : undefined, |
| 151 | description: deployment.description, |
| 152 | labels: userLabels(deployment.labels), |
| 153 | state: deployment.state, |
| 154 | reconciling: deployment.reconciling === true, |
| 155 | createTime: deployment.createTime, |
| 156 | updateTime: deployment.updateTime, |
| 157 | }; |
| 158 | }; |
| 159 | |
| 160 | const getByName = (name: string) => |
| 161 | networksecurity |
| 162 | .getProjectsLocationsInterceptDeployments({ name }) |
| 163 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 164 | |
| 165 | export const InterceptDeploymentProvider = () => |
| 166 | Provider.succeed(InterceptDeployment, { |
| 167 | stables: [ |
| 168 | "name", |
| 169 | "interceptDeploymentId", |
| 170 | "project", |
| 171 | "location", |
| 172 | "interceptDeploymentGroup", |
| 173 | "forwardingRule", |
| 174 | "createTime", |
| 175 | ], |
| 176 | |
| 177 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 178 | if (!isResolved(news)) return undefined; |
| 179 | const previousId = olds?.interceptDeploymentId ?? output?.interceptDeploymentId; |
| 180 | const nextId = news.interceptDeploymentId |
| 181 | ? rfc1035(news.interceptDeploymentId, "intercept-deployment") |
| 182 | : previousId; |
| 183 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 184 | const nextLocation = normalizeLocation( |
| 185 | news.location ?? olds?.location ?? output?.location, |
| 186 | DEFAULT_ZONE, |
| 187 | ); |
| 188 | const previousGroup = linkKey( |
| 189 | olds?.interceptDeploymentGroup ?? output?.interceptDeploymentGroup, |
| 190 | ); |
| 191 | const nextGroup = linkKey(news.interceptDeploymentGroup); |
| 192 | const previousRule = linkKey(olds?.forwardingRule ?? output?.forwardingRule); |
| 193 | const nextRule = linkKey(news.forwardingRule); |
| 194 | if ( |
| 195 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 196 | previousLocation !== nextLocation || |
| 197 | (previousGroup.length > 0 && previousGroup !== nextGroup) || |
| 198 | (previousRule.length > 0 && previousRule !== nextRule) |
| 199 | ) { |
| 200 | return { action: "replace" as const }; |
| 201 | } |
| 202 | return undefined; |
| 203 | }), |
| 204 | |
| 205 | read: Effect.fn(function* ({ id, olds, output }) { |
| 206 | const env = yield* GcpEnvironment.current; |
| 207 | const interceptDeploymentId = yield* toPhysicalId( |
| 208 | id, |
| 209 | olds?.interceptDeploymentId, |
| 210 | output?.interceptDeploymentId, |
| 211 | "intercept-deployment", |
| 212 | ); |
| 213 | const location = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 214 | const name = output?.name ?? resourceName(env.project, location, interceptDeploymentId); |
| 215 | const existing = yield* getByName(name); |
| 216 | if (existing === undefined) return undefined; |
| 217 | const attrs = toAttrs(existing, env.project); |
| 218 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 219 | }), |
| 220 | |
| 221 | list: () => |
| 222 | Effect.gen(function* () { |
| 223 | const env = yield* GcpEnvironment.current; |
| 224 | const items = yield* collectPages( |
| 225 | networksecurity.listProjectsLocationsInterceptDeployments.pages({ |
| 226 | parent: parentOf(env.project, "-"), |
| 227 | pageSize: 1000, |
| 228 | }), |
| 229 | (page) => page.interceptDeployments, |
| 230 | ); |
| 231 | return items |
| 232 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 233 | .map((item) => toAttrs(item, env.project)); |
| 234 | }), |
| 235 | |
| 236 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 237 | const env = yield* GcpEnvironment.current; |
| 238 | const interceptDeploymentId = yield* toPhysicalId( |
| 239 | id, |
| 240 | news.interceptDeploymentId, |
| 241 | output?.interceptDeploymentId, |
| 242 | "intercept-deployment", |
| 243 | ); |
| 244 | const location = normalizeLocation(news.location ?? output?.location, DEFAULT_ZONE); |
| 245 | const name = resourceName(env.project, location, interceptDeploymentId); |
| 246 | const desiredLabels = { |
| 247 | ...toLabels(news.labels), |
| 248 | ...(yield* createInternalLabels(id)), |
| 249 | }; |
| 250 | const interceptDeploymentGroup = canonicalizeLink(news.interceptDeploymentGroup); |
| 251 | const forwardingRule = canonicalizeLink(news.forwardingRule); |
| 252 | |
| 253 | let current = yield* getByName(name); |
| 254 | |
| 255 | if (current === undefined) { |
| 256 | const created = yield* networksecurity |
| 257 | .createProjectsLocationsInterceptDeployments({ |
| 258 | parent: parentOf(env.project, location), |
| 259 | interceptDeploymentId, |
| 260 | body: { |
| 261 | interceptDeploymentGroup, |
| 262 | forwardingRule, |
| 263 | description: news.description, |
| 264 | labels: desiredLabels, |
| 265 | }, |
| 266 | }) |
| 267 | .pipe( |
| 268 | Effect.retry({ |
| 269 | while: (error) => error._tag === "Conflict", |
| 270 | times: 5, |
| 271 | schedule: Schedule.spaced("2 seconds"), |
| 272 | }), |
| 273 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 274 | ); |
| 275 | if (created !== undefined) { |
| 276 | yield* waitForOperation(created); |
| 277 | } |
| 278 | current = yield* waitUntilReady(getByName(name), name); |
| 279 | } |
| 280 | |
| 281 | const observedLabels = tagRecord(current.labels); |
| 282 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 283 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 284 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 285 | const updateMask = changedFields([ |
| 286 | ["labels", labelsChanged], |
| 287 | ["description", descriptionChanged], |
| 288 | ]); |
| 289 | |
| 290 | if (updateMask.length > 0) { |
| 291 | const operation = yield* networksecurity.patchProjectsLocationsInterceptDeployments({ |
| 292 | name: current.name ?? name, |
| 293 | updateMask: updateMask.join(","), |
| 294 | body: { |
| 295 | name: current.name ?? name, |
| 296 | labels: desiredLabels, |
| 297 | description: news.description, |
| 298 | }, |
| 299 | }); |
| 300 | yield* waitForOperation(operation); |
| 301 | current = yield* waitUntilReady(getByName(current.name ?? name), current.name ?? name); |
| 302 | } |
| 303 | |
| 304 | return toAttrs(current, env.project); |
| 305 | }), |
| 306 | |
| 307 | delete: Effect.fn(function* ({ output }) { |
| 308 | const operation = yield* networksecurity |
| 309 | .deleteProjectsLocationsInterceptDeployments({ name: output.name }) |
| 310 | .pipe( |
| 311 | Effect.retry({ |
| 312 | while: (error) => error._tag === "Conflict", |
| 313 | times: 8, |
| 314 | schedule: Schedule.spaced("2 seconds"), |
| 315 | }), |
| 316 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 317 | ); |
| 318 | if (operation !== undefined) { |
| 319 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 320 | } |
| 321 | yield* waitUntilGone(getByName(output.name), output.name); |
| 322 | }), |
| 323 | }); |