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 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 | waitUntilPresent, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "firewallEndpointAssociations"; |
| 31 | |
| 32 | export type FirewallEndpointAssociationState = |
| 33 | | networksecurity.FirewallEndpointAssociationStateEnum |
| 34 | | (string & {}); |
| 35 | |
| 36 | export type FirewallEndpointAssociationProps = { |
| 37 | /** |
| 38 | * Association id (the `{firewallEndpointAssociation}` segment of |
| 39 | * `projects/{project}/locations/{location}/firewallEndpointAssociations/{firewallEndpointAssociation}`). |
| 40 | * If omitted, a unique name is generated from the stack, stage, and |
| 41 | * logical id. Immutable — changing it replaces the association. |
| 42 | */ |
| 43 | firewallEndpointAssociationId?: string; |
| 44 | /** |
| 45 | * Zone of the association (`us-central1-a`, …). Immutable — changing |
| 46 | * it replaces the association. `US-CENTRAL1-A` is accepted and |
| 47 | * normalized to `us-central1-a`. |
| 48 | * @default "us-central1-a" |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * FirewallEndpoint resource name |
| 53 | * (`projects/{project}/locations/{location}/firewallEndpoints/{firewallEndpoint}`). |
| 54 | * Immutable — changing it replaces the association. |
| 55 | */ |
| 56 | firewallEndpoint: string; |
| 57 | /** |
| 58 | * VPC network resource name |
| 59 | * (`projects/{project}/global/networks/{network}`). Immutable — |
| 60 | * changing it replaces the association. |
| 61 | */ |
| 62 | network: string; |
| 63 | /** |
| 64 | * Optional TlsInspectionPolicy resource name attached to this |
| 65 | * association. |
| 66 | */ |
| 67 | tlsInspectionPolicy?: string; |
| 68 | /** |
| 69 | * When true, the association exists but does not intercept traffic. |
| 70 | * @default false |
| 71 | */ |
| 72 | disabled?: boolean; |
| 73 | /** |
| 74 | * User labels. Alchemy ownership labels are merged in automatically. |
| 75 | */ |
| 76 | labels?: Record<string, string>; |
| 77 | }; |
| 78 | |
| 79 | export type FirewallEndpointAssociation = Resource< |
| 80 | "GCP.NetworkSecurity.FirewallEndpointAssociation", |
| 81 | FirewallEndpointAssociationProps, |
| 82 | { |
| 83 | /** Full resource name `projects/{project}/locations/{location}/firewallEndpointAssociations/{firewallEndpointAssociation}`. */ |
| 84 | name: string; |
| 85 | /** Association id (last path segment). */ |
| 86 | firewallEndpointAssociationId: string; |
| 87 | /** Project id. */ |
| 88 | project: string; |
| 89 | /** Zone id (`us-central1-a`). */ |
| 90 | location: string; |
| 91 | /** Associated FirewallEndpoint resource name. */ |
| 92 | firewallEndpoint: string | undefined; |
| 93 | /** Associated VPC network resource name. */ |
| 94 | network: string | undefined; |
| 95 | /** Attached TlsInspectionPolicy resource name, if any. */ |
| 96 | tlsInspectionPolicy: string | undefined; |
| 97 | /** Whether the association is disabled. */ |
| 98 | disabled: boolean; |
| 99 | /** User labels (Alchemy ownership labels stripped). */ |
| 100 | labels: Record<string, string>; |
| 101 | /** Server-reported lifecycle state (`ACTIVE`, `CREATING`, …). */ |
| 102 | state: string | undefined; |
| 103 | /** Whether reconciling is in progress. */ |
| 104 | reconciling: boolean; |
| 105 | /** RFC3339 creation timestamp. */ |
| 106 | createTime: string | undefined; |
| 107 | /** RFC3339 last-update timestamp. */ |
| 108 | updateTime: string | undefined; |
| 109 | }, |
| 110 | never, |
| 111 | Providers |
| 112 | >; |
| 113 | |
| 114 | /** |
| 115 | * Associates a Cloud NGFW FirewallEndpoint with a VPC network so |
| 116 | * matching traffic is intercepted for inspection. |
| 117 | * |
| 118 | * Changing `firewallEndpointAssociationId`, `location`, `network`, or |
| 119 | * `firewallEndpoint` replaces the association. Labels, `disabled`, and |
| 120 | * `tlsInspectionPolicy` update in place. |
| 121 | * |
| 122 | * ### Creating a FirewallEndpointAssociation |
| 123 | * **Example:** Attach an endpoint to a VPC |
| 124 | * ```typescript |
| 125 | * const association = yield* GCP.NetworkSecurity.FirewallEndpointAssociation( |
| 126 | * "Inspect", |
| 127 | * { |
| 128 | * location: "us-central1-a", |
| 129 | * firewallEndpoint: endpoint.name, |
| 130 | * network: `projects/${vpc.project}/global/networks/${vpc.networkName}`, |
| 131 | * }, |
| 132 | * ); |
| 133 | * ``` |
| 134 | * |
| 135 | * **Example:** Disable interception |
| 136 | * ```typescript |
| 137 | * const association = yield* GCP.NetworkSecurity.FirewallEndpointAssociation( |
| 138 | * "Inspect", |
| 139 | * { |
| 140 | * location: "us-central1-a", |
| 141 | * firewallEndpoint: endpoint.name, |
| 142 | * network: `projects/${vpc.project}/global/networks/${vpc.networkName}`, |
| 143 | * disabled: true, |
| 144 | * }, |
| 145 | * ); |
| 146 | * ``` |
| 147 | * |
| 148 | * @resource |
| 149 | * @category NetworkSecurity |
| 150 | */ |
| 151 | export const FirewallEndpointAssociation = Resource<FirewallEndpointAssociation>( |
| 152 | "GCP.NetworkSecurity.FirewallEndpointAssociation", |
| 153 | ); |
| 154 | |
| 155 | const resourceName = (project: string, location: string, firewallEndpointAssociationId: string) => |
| 156 | `projects/${project}/locations/${location}/firewallEndpointAssociations/${firewallEndpointAssociationId}`; |
| 157 | |
| 158 | const toAttrs = (association: networksecurity.FirewallEndpointAssociation, project: string) => { |
| 159 | const name = association.name ?? ""; |
| 160 | const parsed = parseName(name, COLLECTION, DEFAULT_ZONE); |
| 161 | return { |
| 162 | name, |
| 163 | firewallEndpointAssociationId: parsed.id, |
| 164 | project: parsed.project || project, |
| 165 | location: parsed.location || DEFAULT_ZONE, |
| 166 | firewallEndpoint: association.firewallEndpoint |
| 167 | ? canonicalizeLink(association.firewallEndpoint) |
| 168 | : undefined, |
| 169 | network: association.network ? canonicalizeLink(association.network) : undefined, |
| 170 | tlsInspectionPolicy: association.tlsInspectionPolicy |
| 171 | ? canonicalizeLink(association.tlsInspectionPolicy) |
| 172 | : undefined, |
| 173 | disabled: association.disabled === true, |
| 174 | labels: userLabels(association.labels), |
| 175 | state: association.state, |
| 176 | reconciling: association.reconciling === true, |
| 177 | createTime: association.createTime, |
| 178 | updateTime: association.updateTime, |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | const getByName = (name: string) => |
| 183 | networksecurity |
| 184 | .getProjectsLocationsFirewallEndpointAssociations({ name }) |
| 185 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 186 | |
| 187 | export const FirewallEndpointAssociationProvider = () => |
| 188 | Provider.succeed(FirewallEndpointAssociation, { |
| 189 | stables: [ |
| 190 | "name", |
| 191 | "firewallEndpointAssociationId", |
| 192 | "project", |
| 193 | "location", |
| 194 | "firewallEndpoint", |
| 195 | "network", |
| 196 | "createTime", |
| 197 | ], |
| 198 | |
| 199 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 200 | if (!isResolved(news)) return undefined; |
| 201 | const previousId = |
| 202 | olds?.firewallEndpointAssociationId ?? output?.firewallEndpointAssociationId; |
| 203 | const nextId = news.firewallEndpointAssociationId |
| 204 | ? rfc1035(news.firewallEndpointAssociationId, "firewall-endpoint-association") |
| 205 | : previousId; |
| 206 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 207 | const nextLocation = normalizeLocation( |
| 208 | news.location ?? olds?.location ?? output?.location, |
| 209 | DEFAULT_ZONE, |
| 210 | ); |
| 211 | const previousEndpoint = linkKey(olds?.firewallEndpoint ?? output?.firewallEndpoint); |
| 212 | const nextEndpoint = linkKey(news.firewallEndpoint); |
| 213 | const previousNetwork = linkKey(olds?.network ?? output?.network); |
| 214 | const nextNetwork = linkKey(news.network); |
| 215 | if ( |
| 216 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 217 | previousLocation !== nextLocation || |
| 218 | (previousEndpoint.length > 0 && previousEndpoint !== nextEndpoint) || |
| 219 | (previousNetwork.length > 0 && previousNetwork !== nextNetwork) |
| 220 | ) { |
| 221 | return { action: "replace" as const }; |
| 222 | } |
| 223 | return undefined; |
| 224 | }), |
| 225 | |
| 226 | read: Effect.fn(function* ({ id, olds, output }) { |
| 227 | const env = yield* GcpEnvironment.current; |
| 228 | const firewallEndpointAssociationId = yield* toPhysicalId( |
| 229 | id, |
| 230 | olds?.firewallEndpointAssociationId, |
| 231 | output?.firewallEndpointAssociationId, |
| 232 | "firewall-endpoint-association", |
| 233 | ); |
| 234 | const location = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 235 | const name = |
| 236 | output?.name ?? resourceName(env.project, location, firewallEndpointAssociationId); |
| 237 | const existing = yield* getByName(name); |
| 238 | if (existing === undefined) return undefined; |
| 239 | const attrs = toAttrs(existing, env.project); |
| 240 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 241 | }), |
| 242 | |
| 243 | list: () => |
| 244 | Effect.gen(function* () { |
| 245 | const env = yield* GcpEnvironment.current; |
| 246 | const items = yield* collectPages( |
| 247 | networksecurity.listProjectsLocationsFirewallEndpointAssociations.pages({ |
| 248 | parent: parentOf(env.project, "-"), |
| 249 | pageSize: 1000, |
| 250 | }), |
| 251 | (page) => page.firewallEndpointAssociations, |
| 252 | ); |
| 253 | return items |
| 254 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 255 | .map((item) => toAttrs(item, env.project)); |
| 256 | }), |
| 257 | |
| 258 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 259 | const env = yield* GcpEnvironment.current; |
| 260 | const firewallEndpointAssociationId = yield* toPhysicalId( |
| 261 | id, |
| 262 | news.firewallEndpointAssociationId, |
| 263 | output?.firewallEndpointAssociationId, |
| 264 | "firewall-endpoint-association", |
| 265 | ); |
| 266 | const location = normalizeLocation(news.location ?? output?.location, DEFAULT_ZONE); |
| 267 | const name = resourceName(env.project, location, firewallEndpointAssociationId); |
| 268 | const desiredLabels = { |
| 269 | ...toLabels(news.labels), |
| 270 | ...(yield* createInternalLabels(id)), |
| 271 | }; |
| 272 | const firewallEndpoint = canonicalizeLink(news.firewallEndpoint); |
| 273 | const network = canonicalizeLink(news.network); |
| 274 | const tlsInspectionPolicy = news.tlsInspectionPolicy |
| 275 | ? canonicalizeLink(news.tlsInspectionPolicy) |
| 276 | : undefined; |
| 277 | const disabled = news.disabled === true; |
| 278 | |
| 279 | let current = yield* getByName(name); |
| 280 | |
| 281 | if (current === undefined) { |
| 282 | const created = yield* networksecurity |
| 283 | .createProjectsLocationsFirewallEndpointAssociations({ |
| 284 | parent: parentOf(env.project, location), |
| 285 | firewallEndpointAssociationId, |
| 286 | body: { |
| 287 | firewallEndpoint, |
| 288 | network, |
| 289 | tlsInspectionPolicy, |
| 290 | disabled, |
| 291 | labels: desiredLabels, |
| 292 | }, |
| 293 | }) |
| 294 | .pipe( |
| 295 | Effect.retry({ |
| 296 | while: (error) => error._tag === "Conflict", |
| 297 | times: 5, |
| 298 | schedule: Schedule.spaced("2 seconds"), |
| 299 | }), |
| 300 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 301 | ); |
| 302 | if (created !== undefined) { |
| 303 | yield* waitForOperation(created); |
| 304 | } |
| 305 | current = yield* waitUntilPresent(getByName(name), name); |
| 306 | } |
| 307 | |
| 308 | const observedLabels = tagRecord(current.labels); |
| 309 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 310 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 311 | const disabledChanged = (current.disabled === true) !== disabled; |
| 312 | const tlsChanged = |
| 313 | canonicalizeLink(current.tlsInspectionPolicy) !== (tlsInspectionPolicy ?? ""); |
| 314 | const updateMask = changedFields([ |
| 315 | ["labels", labelsChanged], |
| 316 | ["disabled", disabledChanged], |
| 317 | ["tlsInspectionPolicy", tlsChanged], |
| 318 | ]); |
| 319 | |
| 320 | if (updateMask.length > 0) { |
| 321 | const operation = yield* networksecurity.patchProjectsLocationsFirewallEndpointAssociations( |
| 322 | { |
| 323 | name: current.name ?? name, |
| 324 | updateMask: updateMask.join(","), |
| 325 | body: { |
| 326 | name: current.name ?? name, |
| 327 | labels: desiredLabels, |
| 328 | disabled, |
| 329 | tlsInspectionPolicy, |
| 330 | }, |
| 331 | }, |
| 332 | ); |
| 333 | yield* waitForOperation(operation); |
| 334 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 335 | } |
| 336 | |
| 337 | return toAttrs(current, env.project); |
| 338 | }), |
| 339 | |
| 340 | delete: Effect.fn(function* ({ output }) { |
| 341 | const operation = yield* networksecurity |
| 342 | .deleteProjectsLocationsFirewallEndpointAssociations({ |
| 343 | name: output.name, |
| 344 | }) |
| 345 | .pipe( |
| 346 | Effect.retry({ |
| 347 | while: (error) => error._tag === "Conflict", |
| 348 | times: 8, |
| 349 | schedule: Schedule.spaced("2 seconds"), |
| 350 | }), |
| 351 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 352 | ); |
| 353 | if (operation !== undefined) { |
| 354 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 355 | } |
| 356 | yield* waitUntilGone(getByName(output.name), output.name); |
| 357 | }), |
| 358 | }); |