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_ZONE, |
| 12 | VmwareengineNotResolved, |
| 13 | changedFields, |
| 14 | collectPages, |
| 15 | createInternalLabels, |
| 16 | expandName, |
| 17 | hasOwnershipMarker, |
| 18 | listAcrossLocations, |
| 19 | locationFromName, |
| 20 | normalizeLocation, |
| 21 | parseName, |
| 22 | replaceOnIdentity, |
| 23 | rfc1035, |
| 24 | toPhysicalId, |
| 25 | waitForOperation, |
| 26 | waitUntilGone, |
| 27 | waitUntilPresent, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "loggingServers"; |
| 31 | const PARENT_COLLECTION = "privateClouds"; |
| 32 | const DEFAULT_PORT = 514; |
| 33 | const DEFAULT_PROTOCOL = "UDP"; |
| 34 | const DEFAULT_SOURCE_TYPE = "ESXI"; |
| 35 | |
| 36 | export type LoggingServerProtocol = vmwareengine.LoggingServerProtocolEnum | (string & {}); |
| 37 | |
| 38 | export type LoggingServerSourceType = vmwareengine.LoggingServerSourceTypeEnum | (string & {}); |
| 39 | |
| 40 | export type PrivateCloudsLoggingServerProps = { |
| 41 | /** |
| 42 | * Parent PrivateCloud resource name |
| 43 | * (`projects/{project}/locations/{location}/privateClouds/{privateCloud}`) |
| 44 | * or the cloud id. Immutable — changing it replaces the logging server. |
| 45 | */ |
| 46 | privateCloud: string; |
| 47 | /** |
| 48 | * Logging server id (the `{loggingServer}` segment of |
| 49 | * `.../privateClouds/{privateCloud}/loggingServers/{loggingServer}`). |
| 50 | * If omitted, a unique RFC1035 name is generated. Immutable. |
| 51 | */ |
| 52 | loggingServerId?: string; |
| 53 | /** |
| 54 | * Location of the parent private cloud. Inferred from `privateCloud` |
| 55 | * when that value is a full resource name. Immutable. |
| 56 | * @default "us-central1-a" |
| 57 | */ |
| 58 | location?: string; |
| 59 | /** |
| 60 | * FQDN or IP address of the logging server. |
| 61 | */ |
| 62 | hostname: string; |
| 63 | /** |
| 64 | * Port the logging server listens on. |
| 65 | * @default 514 |
| 66 | */ |
| 67 | port?: number; |
| 68 | /** |
| 69 | * Protocol used to send logs (`UDP`, `TCP`, `TLS`, `SSL`, `RELP`). |
| 70 | * @default "UDP" |
| 71 | */ |
| 72 | protocol?: LoggingServerProtocol; |
| 73 | /** |
| 74 | * Component that produces logs (`ESXI` or `VCSA`). |
| 75 | * @default "ESXI" |
| 76 | */ |
| 77 | sourceType?: LoggingServerSourceType; |
| 78 | }; |
| 79 | |
| 80 | export type PrivateCloudsLoggingServer = Resource< |
| 81 | "GCP.VMwareEngine.PrivateCloudsLoggingServer", |
| 82 | PrivateCloudsLoggingServerProps, |
| 83 | { |
| 84 | /** Full resource name. */ |
| 85 | name: string; |
| 86 | /** Logging server id (last path segment). */ |
| 87 | loggingServerId: string; |
| 88 | /** Parent PrivateCloud resource name. */ |
| 89 | privateCloud: string; |
| 90 | /** Project id. */ |
| 91 | project: string; |
| 92 | /** Location id. */ |
| 93 | location: string; |
| 94 | /** FQDN or IP address. */ |
| 95 | hostname: string | undefined; |
| 96 | /** Listen port. */ |
| 97 | port: number | undefined; |
| 98 | /** Log transport protocol. */ |
| 99 | protocol: string | undefined; |
| 100 | /** Log source type. */ |
| 101 | sourceType: string | undefined; |
| 102 | /** System-generated unique identifier. */ |
| 103 | uid: string | undefined; |
| 104 | /** RFC3339 creation timestamp. */ |
| 105 | createTime: string | undefined; |
| 106 | /** RFC3339 last-update timestamp. */ |
| 107 | updateTime: string | undefined; |
| 108 | }, |
| 109 | never, |
| 110 | Providers |
| 111 | >; |
| 112 | |
| 113 | /** |
| 114 | * A syslog destination that receives vCenter or ESXi logs from a VMware |
| 115 | * Engine private cloud. |
| 116 | * |
| 117 | * Logging servers have no labels or description field, so Alchemy treats |
| 118 | * servers whose parent private cloud carries the `[alchemy …]` ownership |
| 119 | * marker as owned for `list` / nuke. Changing the parent cloud, server |
| 120 | * id, or location replaces the server. Hostname, port, protocol, and |
| 121 | * source type update in place. |
| 122 | * |
| 123 | * ### Creating a PrivateCloudsLoggingServer |
| 124 | * **Example:** Forward ESXi logs over UDP |
| 125 | * ```typescript |
| 126 | * const syslog = yield* GCP.VMwareEngine.PrivateCloudsLoggingServer( |
| 127 | * "Esxi", |
| 128 | * { |
| 129 | * privateCloud: cloud.name, |
| 130 | * hostname: "logs.example.com", |
| 131 | * port: 514, |
| 132 | * protocol: "UDP", |
| 133 | * sourceType: "ESXI", |
| 134 | * }, |
| 135 | * ); |
| 136 | * ``` |
| 137 | * |
| 138 | * @resource |
| 139 | * @category VMwareEngine |
| 140 | */ |
| 141 | export const PrivateCloudsLoggingServer = Resource<PrivateCloudsLoggingServer>( |
| 142 | "GCP.VMwareEngine.PrivateCloudsLoggingServer", |
| 143 | ); |
| 144 | |
| 145 | const parentCloudName = (project: string, location: string, privateCloud: string) => |
| 146 | expandName(privateCloud, project, location, PARENT_COLLECTION); |
| 147 | |
| 148 | const resourceNameOf = (parent: string, loggingServerId: string) => |
| 149 | `${parent}/${COLLECTION}/${loggingServerId}`; |
| 150 | |
| 151 | const toAttrs = (item: vmwareengine.LoggingServer, project: string) => { |
| 152 | const name = item.name ?? ""; |
| 153 | const parsed = parseName(name, COLLECTION, DEFAULT_ZONE); |
| 154 | return { |
| 155 | name, |
| 156 | loggingServerId: parsed.id, |
| 157 | privateCloud: parsed.parent, |
| 158 | project: parsed.project || project, |
| 159 | location: parsed.location, |
| 160 | hostname: item.hostname, |
| 161 | port: item.port, |
| 162 | protocol: item.protocol, |
| 163 | sourceType: item.sourceType, |
| 164 | uid: item.uid, |
| 165 | createTime: item.createTime, |
| 166 | updateTime: item.updateTime, |
| 167 | }; |
| 168 | }; |
| 169 | |
| 170 | const getByName = (name: string) => |
| 171 | vmwareengine |
| 172 | .getProjectsLocationsPrivateCloudsLoggingServers({ name }) |
| 173 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 174 | |
| 175 | const getParentCloud = (name: string) => |
| 176 | vmwareengine |
| 177 | .getProjectsLocationsPrivateClouds({ name }) |
| 178 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 179 | |
| 180 | export const PrivateCloudsLoggingServerProvider = () => |
| 181 | Provider.succeed(PrivateCloudsLoggingServer, { |
| 182 | stables: [ |
| 183 | "name", |
| 184 | "loggingServerId", |
| 185 | "privateCloud", |
| 186 | "project", |
| 187 | "location", |
| 188 | "uid", |
| 189 | "createTime", |
| 190 | ], |
| 191 | |
| 192 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 193 | if (!isResolved(news)) return undefined; |
| 194 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 195 | return replaceOnIdentity({ |
| 196 | previousId: olds?.loggingServerId ?? output?.loggingServerId, |
| 197 | nextId: news.loggingServerId |
| 198 | ? rfc1035(news.loggingServerId, "loggingserver") |
| 199 | : (olds?.loggingServerId ?? output?.loggingServerId), |
| 200 | previousLocation, |
| 201 | nextLocation: normalizeLocation( |
| 202 | news.location ?? locationFromName(news.privateCloud, previousLocation), |
| 203 | DEFAULT_ZONE, |
| 204 | ), |
| 205 | previousParent: olds?.privateCloud ?? output?.privateCloud, |
| 206 | nextParent: news.privateCloud, |
| 207 | }); |
| 208 | }), |
| 209 | |
| 210 | read: Effect.fn(function* ({ id, olds, output }) { |
| 211 | const env = yield* GcpEnvironment.current; |
| 212 | const location = normalizeLocation( |
| 213 | olds?.location ?? |
| 214 | output?.location ?? |
| 215 | (olds?.privateCloud ? locationFromName(olds.privateCloud, DEFAULT_ZONE) : undefined), |
| 216 | DEFAULT_ZONE, |
| 217 | ); |
| 218 | const parent = parentCloudName( |
| 219 | env.project, |
| 220 | location, |
| 221 | olds?.privateCloud ?? output?.privateCloud ?? "", |
| 222 | ); |
| 223 | const loggingServerId = yield* toPhysicalId( |
| 224 | id, |
| 225 | olds?.loggingServerId, |
| 226 | output?.loggingServerId, |
| 227 | "loggingserver", |
| 228 | ); |
| 229 | const name = output?.name ?? resourceNameOf(parent, loggingServerId); |
| 230 | const existing = yield* getByName(name); |
| 231 | if (existing === undefined) return undefined; |
| 232 | const attrs = toAttrs(existing, env.project); |
| 233 | if (output?.name === (existing.name ?? name)) return attrs; |
| 234 | const parentCloud = yield* getParentCloud(attrs.privateCloud); |
| 235 | return hasOwnershipMarker(parentCloud?.description) ? attrs : Unowned(attrs); |
| 236 | }), |
| 237 | |
| 238 | list: () => |
| 239 | Effect.gen(function* () { |
| 240 | const env = yield* GcpEnvironment.current; |
| 241 | const clouds = yield* listAcrossLocations(env.project, env.region, (parent) => |
| 242 | collectPages( |
| 243 | vmwareengine.listProjectsLocationsPrivateClouds.pages({ |
| 244 | parent, |
| 245 | pageSize: 1000, |
| 246 | }), |
| 247 | (page) => page.privateClouds, |
| 248 | ), |
| 249 | ); |
| 250 | const ownedClouds = clouds.filter((cloud) => hasOwnershipMarker(cloud.description)); |
| 251 | const nested = yield* Effect.forEach( |
| 252 | ownedClouds.filter((cloud) => (cloud.name ?? "").length > 0), |
| 253 | (cloud) => |
| 254 | collectPages( |
| 255 | vmwareengine.listProjectsLocationsPrivateCloudsLoggingServers.pages({ |
| 256 | parent: cloud.name ?? "", |
| 257 | pageSize: 1000, |
| 258 | }), |
| 259 | (page) => page.loggingServers, |
| 260 | ), |
| 261 | { concurrency: 4 }, |
| 262 | ); |
| 263 | return nested.flat().map((item) => toAttrs(item, env.project)); |
| 264 | }), |
| 265 | |
| 266 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const location = normalizeLocation( |
| 269 | news.location ?? output?.location ?? locationFromName(news.privateCloud, DEFAULT_ZONE), |
| 270 | DEFAULT_ZONE, |
| 271 | ); |
| 272 | const parent = parentCloudName(env.project, location, news.privateCloud); |
| 273 | const loggingServerId = yield* toPhysicalId( |
| 274 | id, |
| 275 | news.loggingServerId, |
| 276 | output?.loggingServerId, |
| 277 | "loggingserver", |
| 278 | ); |
| 279 | const name = resourceNameOf(parent, loggingServerId); |
| 280 | yield* createInternalLabels(id); |
| 281 | const port = news.port ?? DEFAULT_PORT; |
| 282 | const protocol = news.protocol ?? DEFAULT_PROTOCOL; |
| 283 | const sourceType = news.sourceType ?? DEFAULT_SOURCE_TYPE; |
| 284 | |
| 285 | let current = yield* getByName(output?.name ?? name); |
| 286 | |
| 287 | if (current === undefined) { |
| 288 | const created = yield* vmwareengine |
| 289 | .createProjectsLocationsPrivateCloudsLoggingServers({ |
| 290 | parent, |
| 291 | loggingServerId, |
| 292 | body: { |
| 293 | hostname: news.hostname, |
| 294 | port, |
| 295 | protocol, |
| 296 | sourceType, |
| 297 | }, |
| 298 | }) |
| 299 | .pipe( |
| 300 | Effect.retry({ |
| 301 | while: (error) => error._tag === "Conflict", |
| 302 | times: 5, |
| 303 | schedule: Schedule.spaced("2 seconds"), |
| 304 | }), |
| 305 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 306 | ); |
| 307 | if (created !== undefined) { |
| 308 | yield* waitForOperation(created); |
| 309 | } |
| 310 | current = yield* waitUntilPresent(getByName(name), name); |
| 311 | } |
| 312 | |
| 313 | if (current === undefined) { |
| 314 | return yield* new VmwareengineNotResolved({ resourceName: name }); |
| 315 | } |
| 316 | |
| 317 | const hostnameChanged = (current.hostname ?? "") !== news.hostname; |
| 318 | const portChanged = (current.port ?? 0) !== port; |
| 319 | const protocolChanged = (current.protocol ?? "") !== protocol; |
| 320 | const sourceChanged = (current.sourceType ?? "") !== sourceType; |
| 321 | const updateMask = changedFields([ |
| 322 | ["hostname", hostnameChanged], |
| 323 | ["port", portChanged], |
| 324 | ["protocol", protocolChanged], |
| 325 | ["sourceType", sourceChanged], |
| 326 | ]); |
| 327 | |
| 328 | if (updateMask.length > 0) { |
| 329 | const operation = yield* vmwareengine.patchProjectsLocationsPrivateCloudsLoggingServers({ |
| 330 | name: current.name ?? name, |
| 331 | updateMask: updateMask.join(","), |
| 332 | body: { |
| 333 | name: current.name ?? name, |
| 334 | hostname: news.hostname, |
| 335 | port, |
| 336 | protocol, |
| 337 | sourceType, |
| 338 | }, |
| 339 | }); |
| 340 | yield* waitForOperation(operation); |
| 341 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 342 | } |
| 343 | |
| 344 | return toAttrs(current, env.project); |
| 345 | }), |
| 346 | |
| 347 | delete: Effect.fn(function* ({ output }) { |
| 348 | const operation = yield* vmwareengine |
| 349 | .deleteProjectsLocationsPrivateCloudsLoggingServers({ |
| 350 | name: output.name, |
| 351 | }) |
| 352 | .pipe( |
| 353 | Effect.retry({ |
| 354 | while: (error) => error._tag === "Conflict", |
| 355 | times: 8, |
| 356 | schedule: Schedule.spaced("2 seconds"), |
| 357 | }), |
| 358 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 359 | ); |
| 360 | if (operation !== undefined) { |
| 361 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 362 | } |
| 363 | yield* waitUntilGone(getByName(output.name), output.name); |
| 364 | }), |
| 365 | }); |