The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as vmwareengine from "@distilled.cloud/gcp/vmwareengine_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 * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | DEFAULT_GLOBAL, |
| 12 | VmwareengineNotResolved, |
| 13 | canonicalizeLink, |
| 14 | changedFields, |
| 15 | collectPages, |
| 16 | createInternalLabels, |
| 17 | encodeOwnership, |
| 18 | expandName, |
| 19 | hasAlchemyLabels, |
| 20 | hasOwnershipMarker, |
| 21 | listAcrossLocations, |
| 22 | normalizeLocation, |
| 23 | parentOf, |
| 24 | parseName, |
| 25 | parseOwnership, |
| 26 | replaceOnIdentity, |
| 27 | rfc1035, |
| 28 | toPhysicalId, |
| 29 | waitForOperation, |
| 30 | waitUntilGone, |
| 31 | waitUntilPresent, |
| 32 | } from "./internal.ts"; |
| 33 | |
| 34 | const COLLECTION = "networkPolicies"; |
| 35 | const VEN_COLLECTION = "vmwareEngineNetworks"; |
| 36 | const DEFAULT_EDGE_CIDR = "192.168.100.0/26"; |
| 37 | |
| 38 | export type NetworkService = { |
| 39 | /** Whether the service is enabled. */ |
| 40 | enabled?: boolean; |
| 41 | /** Output-only service state. */ |
| 42 | state?: string; |
| 43 | }; |
| 44 | |
| 45 | export type NetworkPolicyProps = { |
| 46 | /** |
| 47 | * Network policy id (the `{networkPolicy}` segment of |
| 48 | * `projects/{project}/locations/{location}/networkPolicies/{networkPolicy}`). |
| 49 | * If omitted, a unique RFC1035 name is generated. Immutable. |
| 50 | */ |
| 51 | networkPolicyId?: string; |
| 52 | /** |
| 53 | * Region. Immutable — changing it replaces the policy. |
| 54 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 55 | * region, else `us-central1`) |
| 56 | */ |
| 57 | location?: string; |
| 58 | /** |
| 59 | * IP range in CIDR notation used for internet and external IP access. |
| 60 | * Must be an RFC 1918 `/26`. |
| 61 | * @default "192.168.100.0/26" |
| 62 | */ |
| 63 | edgeServicesCidr?: string; |
| 64 | /** |
| 65 | * VMware Engine network this policy governs |
| 66 | * (`projects/{project}/locations/{location}/vmwareEngineNetworks/{id}` |
| 67 | * or the network id). Immutable. |
| 68 | */ |
| 69 | vmwareEngineNetwork?: string; |
| 70 | /** |
| 71 | * Internet access for VMware workloads. |
| 72 | */ |
| 73 | internetAccess?: NetworkService; |
| 74 | /** |
| 75 | * External IP assignment. Can only be enabled when internet access is |
| 76 | * also enabled. |
| 77 | */ |
| 78 | externalIp?: NetworkService; |
| 79 | /** |
| 80 | * Human-readable description. Network policies have no labels field, so |
| 81 | * Alchemy stamps ownership into a `[alchemy …]` prefix and strips it |
| 82 | * from attributes. |
| 83 | */ |
| 84 | description?: string; |
| 85 | }; |
| 86 | |
| 87 | export type NetworkPolicy = Resource< |
| 88 | "GCP.VMwareEngine.NetworkPolicy", |
| 89 | NetworkPolicyProps, |
| 90 | { |
| 91 | /** Full resource name. */ |
| 92 | name: string; |
| 93 | /** Network policy id (last path segment). */ |
| 94 | networkPolicyId: string; |
| 95 | /** Project id. */ |
| 96 | project: string; |
| 97 | /** Location id. */ |
| 98 | location: string; |
| 99 | /** Edge services CIDR. */ |
| 100 | edgeServicesCidr: string | undefined; |
| 101 | /** Attached VMware Engine network. */ |
| 102 | vmwareEngineNetwork: string | undefined; |
| 103 | /** Canonical VMware Engine network name. */ |
| 104 | vmwareEngineNetworkCanonical: string | undefined; |
| 105 | /** Internet access service. */ |
| 106 | internetAccess: NetworkService | undefined; |
| 107 | /** External IP service. */ |
| 108 | externalIp: NetworkService | undefined; |
| 109 | /** User description with the Alchemy ownership prefix stripped. */ |
| 110 | description: string | undefined; |
| 111 | /** System-generated unique identifier. */ |
| 112 | uid: string | undefined; |
| 113 | /** RFC3339 creation timestamp. */ |
| 114 | createTime: string | undefined; |
| 115 | /** RFC3339 last-update timestamp. */ |
| 116 | updateTime: string | undefined; |
| 117 | }, |
| 118 | never, |
| 119 | Providers |
| 120 | >; |
| 121 | |
| 122 | /** |
| 123 | * A regional VMware Engine network policy that controls internet and |
| 124 | * external IP access for private clouds on a VMware Engine network. |
| 125 | * |
| 126 | * Network policies have no labels field, so Alchemy stamps ownership into |
| 127 | * the description for `list` / nuke. Changing `networkPolicyId`, |
| 128 | * `location`, or `vmwareEngineNetwork` replaces the policy. CIDR, |
| 129 | * internet access, external IP, and description update in place. |
| 130 | * |
| 131 | * ### Creating a NetworkPolicy |
| 132 | * **Example:** Disable internet and external IP |
| 133 | * ```typescript |
| 134 | * const policy = yield* GCP.VMwareEngine.NetworkPolicy("Edge", { |
| 135 | * vmwareEngineNetwork: ven.name, |
| 136 | * edgeServicesCidr: "192.168.100.0/26", |
| 137 | * internetAccess: { enabled: false }, |
| 138 | * externalIp: { enabled: false }, |
| 139 | * description: "edge policy", |
| 140 | * }); |
| 141 | * ``` |
| 142 | * |
| 143 | * @resource |
| 144 | * @category VMwareEngine |
| 145 | */ |
| 146 | export const NetworkPolicy = Resource<NetworkPolicy>("GCP.VMwareEngine.NetworkPolicy"); |
| 147 | |
| 148 | const resourceName = (project: string, location: string, networkPolicyId: string) => |
| 149 | `${parentOf(project, location)}/${COLLECTION}/${networkPolicyId}`; |
| 150 | |
| 151 | const serviceOf = ( |
| 152 | value: vmwareengine.NetworkService | NetworkService | undefined, |
| 153 | ): NetworkService | undefined => { |
| 154 | if (value === undefined) return undefined; |
| 155 | return { |
| 156 | enabled: value.enabled === true, |
| 157 | state: value.state, |
| 158 | }; |
| 159 | }; |
| 160 | |
| 161 | const toAttrs = (item: vmwareengine.NetworkPolicy, project: string, region: string) => { |
| 162 | const name = item.name ?? ""; |
| 163 | const parsed = parseName(name, COLLECTION, region); |
| 164 | const ownership = parseOwnership(item.description); |
| 165 | return { |
| 166 | name, |
| 167 | networkPolicyId: parsed.id, |
| 168 | project: parsed.project || project, |
| 169 | location: parsed.location, |
| 170 | edgeServicesCidr: item.edgeServicesCidr, |
| 171 | vmwareEngineNetwork: item.vmwareEngineNetwork, |
| 172 | vmwareEngineNetworkCanonical: item.vmwareEngineNetworkCanonical, |
| 173 | internetAccess: serviceOf(item.internetAccess), |
| 174 | externalIp: serviceOf(item.externalIp), |
| 175 | description: ownership.text, |
| 176 | uid: item.uid, |
| 177 | createTime: item.createTime, |
| 178 | updateTime: item.updateTime, |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | const getByName = (name: string) => |
| 183 | vmwareengine |
| 184 | .getProjectsLocationsNetworkPolicies({ name }) |
| 185 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 186 | |
| 187 | export const NetworkPolicyProvider = () => |
| 188 | Provider.succeed(NetworkPolicy, { |
| 189 | stables: ["name", "networkPolicyId", "project", "location", "uid", "createTime"], |
| 190 | |
| 191 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 192 | if (!isResolved(news)) return undefined; |
| 193 | const env = yield* GcpEnvironment.current; |
| 194 | const previousVen = canonicalizeLink( |
| 195 | olds?.vmwareEngineNetwork ?? output?.vmwareEngineNetwork, |
| 196 | ); |
| 197 | const nextVen = canonicalizeLink(news.vmwareEngineNetwork); |
| 198 | return replaceOnIdentity({ |
| 199 | previousId: olds?.networkPolicyId ?? output?.networkPolicyId, |
| 200 | nextId: news.networkPolicyId |
| 201 | ? rfc1035(news.networkPolicyId, "networkpolicy") |
| 202 | : (olds?.networkPolicyId ?? output?.networkPolicyId), |
| 203 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 204 | nextLocation: normalizeLocation( |
| 205 | news.location ?? olds?.location ?? output?.location, |
| 206 | env.region, |
| 207 | ), |
| 208 | extra: previousVen.length > 0 && nextVen.length > 0 && previousVen !== nextVen, |
| 209 | }); |
| 210 | }), |
| 211 | |
| 212 | read: Effect.fn(function* ({ id, olds, output }) { |
| 213 | const env = yield* GcpEnvironment.current; |
| 214 | const networkPolicyId = yield* toPhysicalId( |
| 215 | id, |
| 216 | olds?.networkPolicyId, |
| 217 | output?.networkPolicyId, |
| 218 | "networkpolicy", |
| 219 | ); |
| 220 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 221 | const name = output?.name ?? resourceName(env.project, location, networkPolicyId); |
| 222 | const existing = yield* getByName(name); |
| 223 | if (existing === undefined) return undefined; |
| 224 | const attrs = toAttrs(existing, env.project, env.region); |
| 225 | const { labels } = parseOwnership(existing.description); |
| 226 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 227 | }), |
| 228 | |
| 229 | list: () => |
| 230 | Effect.gen(function* () { |
| 231 | const env = yield* GcpEnvironment.current; |
| 232 | const items = yield* listAcrossLocations(env.project, env.region, (parent) => |
| 233 | collectPages( |
| 234 | vmwareengine.listProjectsLocationsNetworkPolicies.pages({ |
| 235 | parent, |
| 236 | pageSize: 1000, |
| 237 | }), |
| 238 | (page) => page.networkPolicies, |
| 239 | ), |
| 240 | ); |
| 241 | return items |
| 242 | .filter((item) => hasOwnershipMarker(item.description)) |
| 243 | .map((item) => toAttrs(item, env.project, env.region)); |
| 244 | }), |
| 245 | |
| 246 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 247 | const env = yield* GcpEnvironment.current; |
| 248 | const networkPolicyId = yield* toPhysicalId( |
| 249 | id, |
| 250 | news.networkPolicyId, |
| 251 | output?.networkPolicyId, |
| 252 | "networkpolicy", |
| 253 | ); |
| 254 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 255 | const name = resourceName(env.project, location, networkPolicyId); |
| 256 | const ownership = yield* createInternalLabels(id); |
| 257 | const desiredDescription = encodeOwnership(ownership, news.description); |
| 258 | const edgeServicesCidr = news.edgeServicesCidr ?? DEFAULT_EDGE_CIDR; |
| 259 | const vmwareEngineNetwork = news.vmwareEngineNetwork |
| 260 | ? expandName(news.vmwareEngineNetwork, env.project, DEFAULT_GLOBAL, VEN_COLLECTION) |
| 261 | : undefined; |
| 262 | const internetEnabled = news.internetAccess?.enabled === true; |
| 263 | const externalIpEnabled = news.externalIp?.enabled === true; |
| 264 | |
| 265 | let current = yield* getByName(output?.name ?? name); |
| 266 | |
| 267 | if (current === undefined) { |
| 268 | const created = yield* vmwareengine |
| 269 | .createProjectsLocationsNetworkPolicies({ |
| 270 | parent: parentOf(env.project, location), |
| 271 | networkPolicyId, |
| 272 | body: { |
| 273 | edgeServicesCidr, |
| 274 | vmwareEngineNetwork, |
| 275 | internetAccess: { enabled: internetEnabled }, |
| 276 | externalIp: { enabled: externalIpEnabled }, |
| 277 | description: desiredDescription, |
| 278 | }, |
| 279 | }) |
| 280 | .pipe( |
| 281 | Effect.retry({ |
| 282 | while: (error) => error._tag === "Conflict", |
| 283 | times: 5, |
| 284 | schedule: Schedule.spaced("2 seconds"), |
| 285 | }), |
| 286 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 287 | ); |
| 288 | if (created !== undefined) { |
| 289 | yield* waitForOperation(created); |
| 290 | } |
| 291 | current = yield* waitUntilPresent(getByName(name), name); |
| 292 | } |
| 293 | |
| 294 | if (current === undefined) { |
| 295 | return yield* new VmwareengineNotResolved({ resourceName: name }); |
| 296 | } |
| 297 | |
| 298 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 299 | const cidrChanged = (current.edgeServicesCidr ?? "") !== edgeServicesCidr; |
| 300 | const internetChanged = (current.internetAccess?.enabled === true) !== internetEnabled; |
| 301 | const externalIpChanged = (current.externalIp?.enabled === true) !== externalIpEnabled; |
| 302 | const updateMask = changedFields([ |
| 303 | ["description", descriptionChanged], |
| 304 | ["edgeServicesCidr", cidrChanged], |
| 305 | ["internetAccess.enabled", internetChanged], |
| 306 | ["externalIp.enabled", externalIpChanged], |
| 307 | ]); |
| 308 | |
| 309 | if (updateMask.length > 0) { |
| 310 | const operation = yield* vmwareengine.patchProjectsLocationsNetworkPolicies({ |
| 311 | name: current.name ?? name, |
| 312 | updateMask: updateMask.join(","), |
| 313 | body: { |
| 314 | name: current.name ?? name, |
| 315 | description: desiredDescription, |
| 316 | edgeServicesCidr, |
| 317 | internetAccess: { enabled: internetEnabled }, |
| 318 | externalIp: { enabled: externalIpEnabled }, |
| 319 | }, |
| 320 | }); |
| 321 | yield* waitForOperation(operation); |
| 322 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 323 | } |
| 324 | |
| 325 | return toAttrs(current, env.project, env.region); |
| 326 | }), |
| 327 | |
| 328 | delete: Effect.fn(function* ({ output }) { |
| 329 | const operation = yield* vmwareengine |
| 330 | .deleteProjectsLocationsNetworkPolicies({ name: output.name }) |
| 331 | .pipe( |
| 332 | Effect.retry({ |
| 333 | while: (error) => error._tag === "Conflict", |
| 334 | times: 8, |
| 335 | schedule: Schedule.spaced("2 seconds"), |
| 336 | }), |
| 337 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 338 | ); |
| 339 | if (operation !== undefined) { |
| 340 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 341 | } |
| 342 | yield* waitUntilGone(getByName(output.name), output.name); |
| 343 | }), |
| 344 | }); |