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 compute from "@distilled.cloud/gcp/compute_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 Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | encodeDescription, |
| 15 | hasOwnershipMarker, |
| 16 | lastSegment, |
| 17 | parseDescription, |
| 18 | runGlobalOp, |
| 19 | toPhysicalName, |
| 20 | } from "./internal.ts"; |
| 21 | |
| 22 | export type PublicAdvertisedPrefixPdpScope = |
| 23 | | compute.PublicAdvertisedPrefixPdpScopeEnum |
| 24 | | (string & {}); |
| 25 | export type PublicAdvertisedPrefixIpv6AccessType = |
| 26 | | compute.PublicAdvertisedPrefixIpv6AccessTypeEnum |
| 27 | | (string & {}); |
| 28 | |
| 29 | export type PublicAdvertisedPrefixProps = { |
| 30 | /** |
| 31 | * Prefix name (RFC1035, 1-63 characters). If omitted, a unique name is |
| 32 | * generated from the stack, stage, and logical id. Changing it replaces |
| 33 | * the prefix. |
| 34 | */ |
| 35 | prefixName?: string; |
| 36 | /** |
| 37 | * The address range in CIDR format represented by this public advertised |
| 38 | * prefix. Immutable — changing it replaces the prefix. |
| 39 | */ |
| 40 | ipCidrRange: string; |
| 41 | /** |
| 42 | * Address used for reverse DNS verification. Immutable — changing it |
| 43 | * replaces the prefix. |
| 44 | */ |
| 45 | dnsVerificationIp?: string; |
| 46 | /** |
| 47 | * How child public delegated prefixes are scoped (`REGIONAL` or |
| 48 | * `GLOBAL`). Immutable — changing it replaces the prefix. |
| 49 | */ |
| 50 | pdpScope?: PublicAdvertisedPrefixPdpScope; |
| 51 | /** |
| 52 | * Internet access type for IPv6 prefixes (`EXTERNAL` or `INTERNAL`). |
| 53 | * Immutable — changing it replaces the prefix. |
| 54 | */ |
| 55 | ipv6AccessType?: PublicAdvertisedPrefixIpv6AccessType; |
| 56 | /** |
| 57 | * Optional description. Public advertised prefixes have no labels |
| 58 | * field, so Alchemy ownership is stored in a `[alchemy …]` prefix for |
| 59 | * `list` / nuke. |
| 60 | */ |
| 61 | description?: string; |
| 62 | }; |
| 63 | |
| 64 | export type PublicAdvertisedPrefix = Resource< |
| 65 | "GCP.Compute.PublicAdvertisedPrefix", |
| 66 | PublicAdvertisedPrefixProps, |
| 67 | { |
| 68 | /** Prefix name. */ |
| 69 | prefixName: string; |
| 70 | /** Project id. */ |
| 71 | project: string; |
| 72 | /** Advertised CIDR range. */ |
| 73 | ipCidrRange: string; |
| 74 | /** Reverse-DNS verification address. */ |
| 75 | dnsVerificationIp: string | undefined; |
| 76 | /** Child PDP scope. */ |
| 77 | pdpScope: string | undefined; |
| 78 | /** IPv6 access type. */ |
| 79 | ipv6AccessType: string | undefined; |
| 80 | /** User description with the Alchemy ownership prefix stripped. */ |
| 81 | description: string | undefined; |
| 82 | /** Provisioning status. */ |
| 83 | status: string | undefined; |
| 84 | /** Shared secret for reverse DNS verification. */ |
| 85 | sharedSecret: string | undefined; |
| 86 | /** Child public delegated prefixes. */ |
| 87 | publicDelegatedPrefixs: compute.PublicAdvertisedPrefixPublicDelegatedPrefix[]; |
| 88 | /** BYOIP API version. */ |
| 89 | byoipApiVersion: string | undefined; |
| 90 | /** Optimistic-locking fingerprint. */ |
| 91 | fingerprint: string | undefined; |
| 92 | /** Server-defined URL. */ |
| 93 | selfLink: string | undefined; |
| 94 | /** Server-assigned numeric id. */ |
| 95 | prefixId: string | undefined; |
| 96 | /** RFC3339 creation timestamp. */ |
| 97 | creationTimestamp: string | undefined; |
| 98 | /** Resource kind. */ |
| 99 | kind: string | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | >; |
| 104 | |
| 105 | /** |
| 106 | * A global public advertised prefix (BYOIP). |
| 107 | * |
| 108 | * A public advertised prefix is an aggregated IP prefix you bring to |
| 109 | * Google Cloud. Creating one requires a prefix you own and reverse-DNS |
| 110 | * verification. Name, CIDR, verification IP, and PDP scope are immutable. |
| 111 | * Description updates in place via `publicAdvertisedPrefixes.patch`. |
| 112 | * |
| 113 | * ### Creating a Public Advertised Prefix |
| 114 | * **Example:** Regional-scope IPv4 prefix |
| 115 | * ```typescript |
| 116 | * const prefix = yield* GCP.Compute.PublicAdvertisedPrefix("Byoip", { |
| 117 | * ipCidrRange: "203.0.113.0/24", |
| 118 | * dnsVerificationIp: "203.0.113.1", |
| 119 | * pdpScope: "REGIONAL", |
| 120 | * description: "lab prefix", |
| 121 | * }); |
| 122 | * ``` |
| 123 | * |
| 124 | * **Example:** Explicit name |
| 125 | * ```typescript |
| 126 | * const prefix = yield* GCP.Compute.PublicAdvertisedPrefix("Byoip", { |
| 127 | * prefixName: "lab-pap", |
| 128 | * ipCidrRange: "203.0.113.0/24", |
| 129 | * pdpScope: "REGIONAL", |
| 130 | * }); |
| 131 | * ``` |
| 132 | * |
| 133 | * @resource |
| 134 | * @category Compute |
| 135 | */ |
| 136 | export const PublicAdvertisedPrefix = Resource<PublicAdvertisedPrefix>( |
| 137 | "GCP.Compute.PublicAdvertisedPrefix", |
| 138 | ); |
| 139 | |
| 140 | export class PublicAdvertisedPrefixNotResolved extends Data.TaggedError( |
| 141 | "GCP.Compute.PublicAdvertisedPrefixNotResolved", |
| 142 | )<{ |
| 143 | prefixName: string; |
| 144 | }> {} |
| 145 | |
| 146 | const toAttrs = ( |
| 147 | prefix: compute.PublicAdvertisedPrefix, |
| 148 | project: string, |
| 149 | ): PublicAdvertisedPrefix["Attributes"] => { |
| 150 | const parsed = parseDescription(prefix.description); |
| 151 | return { |
| 152 | prefixName: prefix.name ?? lastSegment(prefix.selfLink), |
| 153 | project, |
| 154 | ipCidrRange: prefix.ipCidrRange ?? "", |
| 155 | dnsVerificationIp: prefix.dnsVerificationIp, |
| 156 | pdpScope: prefix.pdpScope, |
| 157 | ipv6AccessType: prefix.ipv6AccessType, |
| 158 | description: parsed.description, |
| 159 | status: prefix.status, |
| 160 | sharedSecret: prefix.sharedSecret, |
| 161 | publicDelegatedPrefixs: prefix.publicDelegatedPrefixs ?? [], |
| 162 | byoipApiVersion: prefix.byoipApiVersion, |
| 163 | fingerprint: prefix.fingerprint, |
| 164 | selfLink: prefix.selfLink, |
| 165 | prefixId: prefix.id, |
| 166 | creationTimestamp: prefix.creationTimestamp, |
| 167 | kind: prefix.kind, |
| 168 | }; |
| 169 | }; |
| 170 | |
| 171 | const getByName = (project: string, publicAdvertisedPrefix: string) => |
| 172 | compute |
| 173 | .getPublicAdvertisedPrefixes({ project, publicAdvertisedPrefix }) |
| 174 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 175 | |
| 176 | const awaitResource = (project: string, prefixName: string) => |
| 177 | getByName(project, prefixName).pipe( |
| 178 | Effect.flatMap((prefix) => |
| 179 | prefix !== undefined |
| 180 | ? Effect.succeed(prefix) |
| 181 | : Effect.fail(new PublicAdvertisedPrefixNotResolved({ prefixName })), |
| 182 | ), |
| 183 | Effect.retry({ |
| 184 | while: (error) => error._tag === "GCP.Compute.PublicAdvertisedPrefixNotResolved", |
| 185 | times: 8, |
| 186 | schedule: Schedule.spaced("1 second"), |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | export const PublicAdvertisedPrefixProvider = () => |
| 191 | Provider.succeed(PublicAdvertisedPrefix, { |
| 192 | stables: ["prefixName", "project", "ipCidrRange", "prefixId", "selfLink", "creationTimestamp"], |
| 193 | |
| 194 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 195 | if (!isResolved(news)) return undefined; |
| 196 | const previousName = olds?.prefixName ?? output?.prefixName; |
| 197 | const nextName = news.prefixName ?? previousName; |
| 198 | const nameChanged = |
| 199 | previousName !== undefined && nextName !== undefined && previousName !== nextName; |
| 200 | const cidrChanged = |
| 201 | (olds?.ipCidrRange ?? output?.ipCidrRange) !== undefined && |
| 202 | news.ipCidrRange !== (olds?.ipCidrRange ?? output?.ipCidrRange); |
| 203 | const dnsChanged = |
| 204 | news.dnsVerificationIp !== undefined && |
| 205 | (olds?.dnsVerificationIp ?? output?.dnsVerificationIp) !== undefined && |
| 206 | news.dnsVerificationIp !== (olds?.dnsVerificationIp ?? output?.dnsVerificationIp); |
| 207 | const scopeChanged = |
| 208 | news.pdpScope !== undefined && |
| 209 | (olds?.pdpScope ?? output?.pdpScope) !== undefined && |
| 210 | news.pdpScope !== (olds?.pdpScope ?? output?.pdpScope); |
| 211 | if (nameChanged || cidrChanged || dnsChanged || scopeChanged) { |
| 212 | return { |
| 213 | action: "replace" as const, |
| 214 | deleteFirst: !nameChanged || nextName === undefined || nextName === previousName, |
| 215 | }; |
| 216 | } |
| 217 | return undefined; |
| 218 | }), |
| 219 | |
| 220 | read: Effect.fn(function* ({ id, olds, output }) { |
| 221 | const env = yield* GcpEnvironment.current; |
| 222 | const prefixName = yield* toPhysicalName(id, olds?.prefixName, output?.prefixName, "prefix"); |
| 223 | const existing = yield* getByName(env.project, prefixName); |
| 224 | if (existing === undefined) return undefined; |
| 225 | const attrs = toAttrs(existing, env.project); |
| 226 | const { labels } = parseDescription(existing.description); |
| 227 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 228 | }), |
| 229 | |
| 230 | list: () => |
| 231 | Effect.gen(function* () { |
| 232 | const env = yield* GcpEnvironment.current; |
| 233 | return yield* compute.listPublicAdvertisedPrefixes |
| 234 | .items({ |
| 235 | project: env.project, |
| 236 | maxResults: 500, |
| 237 | returnPartialSuccess: true, |
| 238 | }) |
| 239 | .pipe( |
| 240 | Stream.filter((item) => hasOwnershipMarker(item.description)), |
| 241 | Stream.map((item) => toAttrs(item, env.project)), |
| 242 | Stream.runCollect, |
| 243 | Effect.map((chunk) => Array.from(chunk)), |
| 244 | Effect.catchTag("NotFound", () => |
| 245 | Effect.succeed([] as PublicAdvertisedPrefix["Attributes"][]), |
| 246 | ), |
| 247 | ); |
| 248 | }), |
| 249 | |
| 250 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const prefixName = yield* toPhysicalName(id, news.prefixName, output?.prefixName, "prefix"); |
| 253 | const ownership = yield* createInternalLabels(id); |
| 254 | const desiredDescription = encodeDescription(ownership, news.description); |
| 255 | |
| 256 | let current = yield* getByName(env.project, prefixName); |
| 257 | |
| 258 | if (current === undefined) { |
| 259 | yield* runGlobalOp( |
| 260 | env.project, |
| 261 | compute.insertPublicAdvertisedPrefixes({ |
| 262 | project: env.project, |
| 263 | body: { |
| 264 | name: prefixName, |
| 265 | description: desiredDescription, |
| 266 | ipCidrRange: news.ipCidrRange, |
| 267 | dnsVerificationIp: news.dnsVerificationIp, |
| 268 | pdpScope: news.pdpScope, |
| 269 | ipv6AccessType: news.ipv6AccessType, |
| 270 | }, |
| 271 | }), |
| 272 | { ignoreAlreadyExists: true }, |
| 273 | ).pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 274 | current = yield* awaitResource(env.project, prefixName); |
| 275 | } |
| 276 | |
| 277 | if (current === undefined) { |
| 278 | return yield* new PublicAdvertisedPrefixNotResolved({ prefixName }); |
| 279 | } |
| 280 | |
| 281 | if ((current.description ?? "") !== desiredDescription) { |
| 282 | yield* runGlobalOp( |
| 283 | env.project, |
| 284 | compute.patchPublicAdvertisedPrefixes({ |
| 285 | project: env.project, |
| 286 | publicAdvertisedPrefix: prefixName, |
| 287 | body: { |
| 288 | description: desiredDescription, |
| 289 | fingerprint: current.fingerprint, |
| 290 | }, |
| 291 | }), |
| 292 | ); |
| 293 | current = (yield* getByName(env.project, prefixName)) ?? current; |
| 294 | } |
| 295 | |
| 296 | return toAttrs(current, env.project); |
| 297 | }), |
| 298 | |
| 299 | delete: Effect.fn(function* ({ output }) { |
| 300 | const env = yield* GcpEnvironment.current; |
| 301 | yield* runGlobalOp( |
| 302 | env.project, |
| 303 | compute.deletePublicAdvertisedPrefixes({ |
| 304 | project: env.project, |
| 305 | publicAdvertisedPrefix: output.prefixName, |
| 306 | }), |
| 307 | { ignoreNotFound: true }, |
| 308 | ).pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 309 | }), |
| 310 | }); |