The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as networkmanagement from "@distilled.cloud/gcp/networkmanagement_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 { alchemyLabelKeys, createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | collectPages, |
| 13 | normalizeLocation, |
| 14 | parentOf, |
| 15 | parseName, |
| 16 | resourceName as qualifiedName, |
| 17 | rfc1035, |
| 18 | toPhysicalId, |
| 19 | waitForOperation, |
| 20 | waitUntilGone, |
| 21 | waitUntilPresent, |
| 22 | waitUntilReady, |
| 23 | } from "./internal.ts"; |
| 24 | |
| 25 | const COLLECTION = "networkMonitoringProviders"; |
| 26 | const DEFAULT_PROVIDER_TYPE = |
| 27 | "EXTERNAL" satisfies networkmanagement.NetworkMonitoringProviderProviderTypeEnum; |
| 28 | |
| 29 | export type NetworkMonitoringProviderType = |
| 30 | | networkmanagement.NetworkMonitoringProviderProviderTypeEnum |
| 31 | | (string & {}); |
| 32 | |
| 33 | export type NetworkMonitoringProviderProps = { |
| 34 | /** |
| 35 | * Provider id (the `{network_monitoring_provider}` segment of |
| 36 | * `projects/{project}/locations/{location}/networkMonitoringProviders/{network_monitoring_provider}`). |
| 37 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 38 | * it replaces the provider. |
| 39 | */ |
| 40 | networkMonitoringProviderId?: string; |
| 41 | /** |
| 42 | * Location. Network monitoring providers are only served from `global` |
| 43 | * (regional names are rejected as malformed). Immutable — changing it |
| 44 | * replaces the provider. |
| 45 | * @default "global" |
| 46 | */ |
| 47 | location?: string; |
| 48 | /** |
| 49 | * Provider type. The API currently supports `EXTERNAL`. Immutable — |
| 50 | * changing it replaces the provider. |
| 51 | * @default "EXTERNAL" |
| 52 | */ |
| 53 | providerType?: NetworkMonitoringProviderType; |
| 54 | }; |
| 55 | |
| 56 | export type NetworkMonitoringProvider = Resource< |
| 57 | "GCP.NetworkManagement.NetworkMonitoringProvider", |
| 58 | NetworkMonitoringProviderProps, |
| 59 | { |
| 60 | /** Full resource name `projects/{project}/locations/{location}/networkMonitoringProviders/{network_monitoring_provider}`. */ |
| 61 | name: string; |
| 62 | /** Provider id (last path segment). */ |
| 63 | networkMonitoringProviderId: string; |
| 64 | /** Project id. */ |
| 65 | project: string; |
| 66 | /** Location id. */ |
| 67 | location: string; |
| 68 | /** Provider type (`EXTERNAL`). */ |
| 69 | providerType: string | undefined; |
| 70 | /** Link to the provider UI. */ |
| 71 | providerUri: string | undefined; |
| 72 | /** Provider state (`ACTIVATING`, `ACTIVE`, …). */ |
| 73 | state: string | undefined; |
| 74 | /** Server-reported error messages. */ |
| 75 | errors: string[]; |
| 76 | /** RFC3339 creation timestamp. */ |
| 77 | createTime: string | undefined; |
| 78 | /** RFC3339 last-update timestamp. */ |
| 79 | updateTime: string | undefined; |
| 80 | }, |
| 81 | never, |
| 82 | Providers |
| 83 | >; |
| 84 | |
| 85 | /** |
| 86 | * A Network Intelligence Center Network Monitoring Provider. |
| 87 | * |
| 88 | * The API has no labels, description, or patch method. Identity is the |
| 89 | * id, location, and provider type; changing any of them replaces the |
| 90 | * provider. Alchemy stamps ownership into the generated id so `list` / |
| 91 | * nuke can find leaked rows. |
| 92 | * |
| 93 | * ### Creating a NetworkMonitoringProvider |
| 94 | * **Example:** External provider |
| 95 | * ```typescript |
| 96 | * const provider = yield* GCP.NetworkManagement.NetworkMonitoringProvider( |
| 97 | * "AppNeta", |
| 98 | * { providerType: "EXTERNAL" }, |
| 99 | * ); |
| 100 | * ``` |
| 101 | * |
| 102 | * **Example:** Named provider |
| 103 | * ```typescript |
| 104 | * const provider = yield* GCP.NetworkManagement.NetworkMonitoringProvider( |
| 105 | * "AppNeta", |
| 106 | * { |
| 107 | * networkMonitoringProviderId: "app-neta", |
| 108 | * location: "global", |
| 109 | * providerType: "EXTERNAL", |
| 110 | * }, |
| 111 | * ); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | * @category NetworkManagement |
| 116 | */ |
| 117 | export const NetworkMonitoringProvider = Resource<NetworkMonitoringProvider>( |
| 118 | "GCP.NetworkManagement.NetworkMonitoringProvider", |
| 119 | ); |
| 120 | |
| 121 | const resourceName = (project: string, location: string, networkMonitoringProviderId: string) => |
| 122 | qualifiedName(project, location, COLLECTION, networkMonitoringProviderId); |
| 123 | |
| 124 | const typeOf = (value: string | undefined) => (value ?? DEFAULT_PROVIDER_TYPE).toUpperCase(); |
| 125 | |
| 126 | const ownedPrefix = (labels: Record<string, string>) => { |
| 127 | const id = (labels[alchemyLabelKeys.id] ?? "x") |
| 128 | .toLowerCase() |
| 129 | .replace(/[^a-z0-9-]/g, "") |
| 130 | .slice(0, 8); |
| 131 | return `alch${id.length > 0 ? id : "x"}`; |
| 132 | }; |
| 133 | |
| 134 | const GLOBAL_LOCATION = "global"; |
| 135 | |
| 136 | const isOwnedId = (id: string) => id.toLowerCase().startsWith("alch"); |
| 137 | |
| 138 | const toOwnedId = (id: string, explicit: string | undefined, existing: string | undefined) => |
| 139 | Effect.gen(function* () { |
| 140 | if (explicit !== undefined) { |
| 141 | return rfc1035(explicit, "nmp"); |
| 142 | } |
| 143 | if (existing !== undefined) return existing; |
| 144 | const labels = yield* createInternalLabels(id); |
| 145 | const generated = yield* toPhysicalId(id, undefined, undefined, "nmp"); |
| 146 | const prefix = ownedPrefix(labels); |
| 147 | if (generated.startsWith(prefix)) return generated; |
| 148 | return rfc1035(`${prefix}-${generated}`, "nmp"); |
| 149 | }); |
| 150 | |
| 151 | const toAttrs = ( |
| 152 | provider: networkmanagement.NetworkMonitoringProvider, |
| 153 | project: string, |
| 154 | region: string, |
| 155 | ) => { |
| 156 | const name = provider.name ?? ""; |
| 157 | const parsed = parseName(name, COLLECTION, region); |
| 158 | return { |
| 159 | name, |
| 160 | networkMonitoringProviderId: parsed.id, |
| 161 | project: parsed.project || project, |
| 162 | location: parsed.location || region, |
| 163 | providerType: provider.providerType, |
| 164 | providerUri: provider.providerUri, |
| 165 | state: provider.state, |
| 166 | errors: provider.errors ?? [], |
| 167 | createTime: provider.createTime, |
| 168 | updateTime: provider.updateTime, |
| 169 | }; |
| 170 | }; |
| 171 | |
| 172 | const getByName = (name: string) => |
| 173 | networkmanagement |
| 174 | .getProjectsLocationsNetworkMonitoringProviders({ name }) |
| 175 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 176 | |
| 177 | export const NetworkMonitoringProviderProvider = () => |
| 178 | Provider.succeed(NetworkMonitoringProvider, { |
| 179 | stables: [ |
| 180 | "name", |
| 181 | "networkMonitoringProviderId", |
| 182 | "project", |
| 183 | "location", |
| 184 | "providerType", |
| 185 | "createTime", |
| 186 | ], |
| 187 | |
| 188 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 189 | if (!isResolved(news)) return undefined; |
| 190 | const previousId = olds?.networkMonitoringProviderId ?? output?.networkMonitoringProviderId; |
| 191 | const nextId = news.networkMonitoringProviderId |
| 192 | ? rfc1035(news.networkMonitoringProviderId, "nmp") |
| 193 | : previousId; |
| 194 | const previousLocation = normalizeLocation( |
| 195 | olds?.location ?? output?.location, |
| 196 | GLOBAL_LOCATION, |
| 197 | ); |
| 198 | const nextLocation = normalizeLocation( |
| 199 | news.location ?? olds?.location ?? output?.location, |
| 200 | GLOBAL_LOCATION, |
| 201 | ); |
| 202 | const previousType = typeOf(olds?.providerType ?? output?.providerType); |
| 203 | const nextType = typeOf(news.providerType ?? olds?.providerType ?? output?.providerType); |
| 204 | if ( |
| 205 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 206 | previousLocation !== nextLocation || |
| 207 | previousType !== nextType |
| 208 | ) { |
| 209 | return { action: "replace" as const }; |
| 210 | } |
| 211 | return undefined; |
| 212 | }), |
| 213 | |
| 214 | read: Effect.fn(function* ({ id, olds, output }) { |
| 215 | const env = yield* GcpEnvironment.current; |
| 216 | const networkMonitoringProviderId = yield* toOwnedId( |
| 217 | id, |
| 218 | olds?.networkMonitoringProviderId, |
| 219 | output?.networkMonitoringProviderId, |
| 220 | ); |
| 221 | const location = normalizeLocation(olds?.location ?? output?.location, GLOBAL_LOCATION); |
| 222 | const name = output?.name ?? resourceName(env.project, location, networkMonitoringProviderId); |
| 223 | const existing = yield* getByName(name); |
| 224 | if (existing === undefined) return undefined; |
| 225 | const attrs = toAttrs(existing, env.project, GLOBAL_LOCATION); |
| 226 | return isOwnedId(attrs.networkMonitoringProviderId) || |
| 227 | olds?.networkMonitoringProviderId !== undefined || |
| 228 | output?.networkMonitoringProviderId !== undefined |
| 229 | ? attrs |
| 230 | : Unowned(attrs); |
| 231 | }), |
| 232 | |
| 233 | list: () => |
| 234 | Effect.gen(function* () { |
| 235 | const env = yield* GcpEnvironment.current; |
| 236 | const listed: NetworkMonitoringProvider["Attributes"][] = []; |
| 237 | const seen = new Set<string>(); |
| 238 | const parents = [ |
| 239 | parentOf(env.project, "-"), |
| 240 | ...[GLOBAL_LOCATION].map((location) => parentOf(env.project, location)), |
| 241 | ]; |
| 242 | for (const parent of parents) { |
| 243 | const items = yield* collectPages( |
| 244 | networkmanagement.listProjectsLocationsNetworkMonitoringProviders.pages({ |
| 245 | parent, |
| 246 | pageSize: 1000, |
| 247 | }), |
| 248 | (page) => page.networkMonitoringProviders, |
| 249 | ); |
| 250 | for (const item of items) { |
| 251 | const attrs = toAttrs(item, env.project, GLOBAL_LOCATION); |
| 252 | if (!isOwnedId(attrs.networkMonitoringProviderId)) continue; |
| 253 | if (seen.has(attrs.name)) continue; |
| 254 | seen.add(attrs.name); |
| 255 | listed.push(attrs); |
| 256 | } |
| 257 | } |
| 258 | return listed; |
| 259 | }), |
| 260 | |
| 261 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 262 | const env = yield* GcpEnvironment.current; |
| 263 | const networkMonitoringProviderId = yield* toOwnedId( |
| 264 | id, |
| 265 | news.networkMonitoringProviderId, |
| 266 | output?.networkMonitoringProviderId, |
| 267 | ); |
| 268 | const location = normalizeLocation(news.location ?? output?.location, GLOBAL_LOCATION); |
| 269 | const name = resourceName(env.project, location, networkMonitoringProviderId); |
| 270 | const providerType = typeOf(news.providerType); |
| 271 | |
| 272 | let current = yield* getByName(name); |
| 273 | |
| 274 | if (current === undefined) { |
| 275 | const created = yield* networkmanagement |
| 276 | .createProjectsLocationsNetworkMonitoringProviders({ |
| 277 | parent: parentOf(env.project, location), |
| 278 | networkMonitoringProviderId, |
| 279 | body: { providerType }, |
| 280 | }) |
| 281 | .pipe( |
| 282 | Effect.retry({ |
| 283 | while: (error) => error._tag === "Conflict", |
| 284 | times: 5, |
| 285 | schedule: Schedule.spaced("2 seconds"), |
| 286 | }), |
| 287 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 288 | ); |
| 289 | if (created !== undefined) { |
| 290 | yield* waitForOperation(created); |
| 291 | } |
| 292 | current = yield* waitUntilPresent(getByName(name), name); |
| 293 | } |
| 294 | |
| 295 | current = yield* waitUntilReady(getByName(current.name ?? name), current.name ?? name); |
| 296 | |
| 297 | return toAttrs(current, env.project, GLOBAL_LOCATION); |
| 298 | }), |
| 299 | |
| 300 | delete: Effect.fn(function* ({ output }) { |
| 301 | const operation = yield* networkmanagement |
| 302 | .deleteProjectsLocationsNetworkMonitoringProviders({ |
| 303 | name: output.name, |
| 304 | }) |
| 305 | .pipe( |
| 306 | Effect.retry({ |
| 307 | while: (error) => error._tag === "Conflict", |
| 308 | times: 8, |
| 309 | schedule: Schedule.spaced("2 seconds"), |
| 310 | }), |
| 311 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 312 | ); |
| 313 | if (operation !== undefined) { |
| 314 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 315 | } |
| 316 | yield* waitUntilGone(getByName(output.name), output.name); |
| 317 | }), |
| 318 | }); |