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, 04:45 UTC
| 1 | import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_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, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | NetworkConnectivityNotResolved, |
| 14 | canonicalizeLink, |
| 15 | collectPages, |
| 16 | hasAlchemyLabelKeys, |
| 17 | lastSegment, |
| 18 | normalizeLocation, |
| 19 | parentOf, |
| 20 | parseName, |
| 21 | rfc1035, |
| 22 | toNetworkResource, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitForOperation, |
| 26 | waitUntilGone, |
| 27 | waitUntilPresent, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "serviceConnectionTokens"; |
| 31 | |
| 32 | export type ServiceConnectionTokenProps = { |
| 33 | /** |
| 34 | * Token id (the `{service_connection_token}` segment). If omitted, a |
| 35 | * unique name is generated. Immutable — changing it replaces the |
| 36 | * token. |
| 37 | */ |
| 38 | serviceConnectionTokenId?: string; |
| 39 | /** |
| 40 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 41 | * token. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 42 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 43 | * region, else `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * VPC network associated with this token |
| 48 | * (`projects/{project}/global/networks/{network}` or a Compute |
| 49 | * self-link). Immutable — changing it replaces the token. |
| 50 | */ |
| 51 | network: string; |
| 52 | /** |
| 53 | * Human-readable description. Set at create; the API has no update. |
| 54 | */ |
| 55 | description?: string; |
| 56 | /** |
| 57 | * User labels. Alchemy ownership labels are merged in automatically. |
| 58 | */ |
| 59 | labels?: Record<string, string>; |
| 60 | }; |
| 61 | |
| 62 | export type ServiceConnectionToken = Resource< |
| 63 | "GCP.NetworkConnectivity.ServiceConnectionToken", |
| 64 | ServiceConnectionTokenProps, |
| 65 | { |
| 66 | /** Full resource name `projects/{project}/locations/{location}/serviceConnectionTokens/{id}`. */ |
| 67 | name: string; |
| 68 | /** Token id (last path segment). */ |
| 69 | serviceConnectionTokenId: string; |
| 70 | /** Project id. */ |
| 71 | project: string; |
| 72 | /** Location id. */ |
| 73 | location: string; |
| 74 | /** VPC network resource path. */ |
| 75 | network: string | undefined; |
| 76 | /** Automation-generated token value. */ |
| 77 | token: string | undefined; |
| 78 | /** RFC3339 expiry. */ |
| 79 | expireTime: string | undefined; |
| 80 | /** User-provided description. */ |
| 81 | description: string | undefined; |
| 82 | /** User labels (Alchemy ownership labels stripped). */ |
| 83 | labels: Record<string, string>; |
| 84 | /** Server etag. */ |
| 85 | etag: string | undefined; |
| 86 | /** RFC3339 creation timestamp. */ |
| 87 | createTime: string | undefined; |
| 88 | /** RFC3339 last-update timestamp. */ |
| 89 | updateTime: string | undefined; |
| 90 | }, |
| 91 | never, |
| 92 | Providers |
| 93 | >; |
| 94 | |
| 95 | /** |
| 96 | * A PSC Service Connection Token that authenticates a consumer to |
| 97 | * create connections in a producer Service Connection Map. |
| 98 | * |
| 99 | * Changing `serviceConnectionTokenId`, `location`, or `network` |
| 100 | * replaces the token. The API has no patch method. |
| 101 | * |
| 102 | * ### Creating a ServiceConnectionToken |
| 103 | * **Example:** Token for a consumer VPC |
| 104 | * ```typescript |
| 105 | * const network = yield* GCP.Compute.Network("AppVpc", { |
| 106 | * autoCreateSubnetworks: false, |
| 107 | * }); |
| 108 | * const token = yield* GCP.NetworkConnectivity.ServiceConnectionToken( |
| 109 | * "Consumer", |
| 110 | * { |
| 111 | * network: network.selfLink ?? network.networkName, |
| 112 | * description: "psc token", |
| 113 | * labels: { env: "prod" }, |
| 114 | * }, |
| 115 | * ); |
| 116 | * ``` |
| 117 | * |
| 118 | * @resource |
| 119 | * @category NetworkConnectivity |
| 120 | */ |
| 121 | export const ServiceConnectionToken = Resource<ServiceConnectionToken>( |
| 122 | "GCP.NetworkConnectivity.ServiceConnectionToken", |
| 123 | ); |
| 124 | |
| 125 | const resourceName = (project: string, location: string, serviceConnectionTokenId: string) => |
| 126 | `projects/${project}/locations/${location}/serviceConnectionTokens/${serviceConnectionTokenId}`; |
| 127 | |
| 128 | const toAttrs = ( |
| 129 | token: networkconnectivity.ServiceConnectionToken, |
| 130 | project: string, |
| 131 | region: string, |
| 132 | ) => { |
| 133 | const name = token.name ?? ""; |
| 134 | const parsed = parseName(name, COLLECTION, region); |
| 135 | return { |
| 136 | name, |
| 137 | serviceConnectionTokenId: parsed.id, |
| 138 | project: parsed.project || project, |
| 139 | location: parsed.location || region, |
| 140 | network: token.network, |
| 141 | token: token.token, |
| 142 | expireTime: token.expireTime, |
| 143 | description: token.description, |
| 144 | labels: userLabels(token.labels), |
| 145 | etag: token.etag, |
| 146 | createTime: token.createTime, |
| 147 | updateTime: token.updateTime, |
| 148 | }; |
| 149 | }; |
| 150 | |
| 151 | const getByName = (name: string) => |
| 152 | networkconnectivity |
| 153 | .getProjectsLocationsServiceConnectionTokens({ name }) |
| 154 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 155 | |
| 156 | export const ServiceConnectionTokenProvider = () => |
| 157 | Provider.succeed(ServiceConnectionToken, { |
| 158 | stables: ["name", "serviceConnectionTokenId", "project", "location", "network", "createTime"], |
| 159 | |
| 160 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 161 | if (!isResolved(news)) return undefined; |
| 162 | const env = yield* GcpEnvironment.current; |
| 163 | const previousId = olds?.serviceConnectionTokenId ?? output?.serviceConnectionTokenId; |
| 164 | const nextId = news.serviceConnectionTokenId |
| 165 | ? rfc1035(news.serviceConnectionTokenId, "service-connection-token") |
| 166 | : previousId; |
| 167 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 168 | const nextLocation = normalizeLocation( |
| 169 | news.location ?? olds?.location ?? output?.location, |
| 170 | env.region, |
| 171 | ); |
| 172 | const previousNetwork = lastSegment(canonicalizeLink(olds?.network ?? output?.network)); |
| 173 | const nextNetwork = lastSegment(canonicalizeLink(news.network)); |
| 174 | if ( |
| 175 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 176 | previousLocation !== nextLocation || |
| 177 | (previousNetwork.length > 0 && previousNetwork !== nextNetwork) |
| 178 | ) { |
| 179 | return { action: "replace" as const }; |
| 180 | } |
| 181 | return undefined; |
| 182 | }), |
| 183 | |
| 184 | read: Effect.fn(function* ({ id, olds, output }) { |
| 185 | const env = yield* GcpEnvironment.current; |
| 186 | const serviceConnectionTokenId = yield* toPhysicalId( |
| 187 | id, |
| 188 | olds?.serviceConnectionTokenId, |
| 189 | output?.serviceConnectionTokenId, |
| 190 | "service-connection-token", |
| 191 | ); |
| 192 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 193 | const name = output?.name ?? resourceName(env.project, location, serviceConnectionTokenId); |
| 194 | const existing = yield* getByName(name); |
| 195 | if (existing === undefined) return undefined; |
| 196 | const attrs = toAttrs(existing, env.project, env.region); |
| 197 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 198 | }), |
| 199 | |
| 200 | list: () => |
| 201 | Effect.gen(function* () { |
| 202 | const env = yield* GcpEnvironment.current; |
| 203 | const items = yield* collectPages( |
| 204 | networkconnectivity.listProjectsLocationsServiceConnectionTokens.pages({ |
| 205 | parent: parentOf(env.project, "-"), |
| 206 | pageSize: 1000, |
| 207 | }), |
| 208 | (page) => page.serviceConnectionTokens, |
| 209 | ); |
| 210 | return items |
| 211 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 212 | .map((item) => toAttrs(item, env.project, env.region)); |
| 213 | }), |
| 214 | |
| 215 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const serviceConnectionTokenId = yield* toPhysicalId( |
| 218 | id, |
| 219 | news.serviceConnectionTokenId, |
| 220 | output?.serviceConnectionTokenId, |
| 221 | "service-connection-token", |
| 222 | ); |
| 223 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 224 | const name = resourceName(env.project, location, serviceConnectionTokenId); |
| 225 | const desiredLabels = { |
| 226 | ...toLabels(news.labels), |
| 227 | ...(yield* createInternalLabels(id)), |
| 228 | }; |
| 229 | const network = toNetworkResource(env.project, news.network); |
| 230 | |
| 231 | let current = yield* getByName(name); |
| 232 | |
| 233 | if (current === undefined) { |
| 234 | const created = yield* networkconnectivity |
| 235 | .createProjectsLocationsServiceConnectionTokens({ |
| 236 | parent: parentOf(env.project, location), |
| 237 | serviceConnectionTokenId, |
| 238 | body: { |
| 239 | network, |
| 240 | description: news.description, |
| 241 | labels: desiredLabels, |
| 242 | }, |
| 243 | }) |
| 244 | .pipe( |
| 245 | Effect.retry({ |
| 246 | while: (error) => error._tag === "Conflict", |
| 247 | times: 5, |
| 248 | schedule: Schedule.spaced("2 seconds"), |
| 249 | }), |
| 250 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 251 | ); |
| 252 | if (created !== undefined) { |
| 253 | yield* waitForOperation(created); |
| 254 | } |
| 255 | current = yield* waitUntilPresent(getByName(name), name); |
| 256 | } |
| 257 | |
| 258 | if (current === undefined) { |
| 259 | return yield* new NetworkConnectivityNotResolved({ resourceName: name }); |
| 260 | } |
| 261 | |
| 262 | return toAttrs(current, env.project, env.region); |
| 263 | }), |
| 264 | |
| 265 | delete: Effect.fn(function* ({ output }) { |
| 266 | const operation = yield* networkconnectivity |
| 267 | .deleteProjectsLocationsServiceConnectionTokens({ |
| 268 | name: output.name, |
| 269 | etag: output.etag, |
| 270 | }) |
| 271 | .pipe( |
| 272 | Effect.retry({ |
| 273 | while: (error) => error._tag === "Conflict", |
| 274 | times: 8, |
| 275 | schedule: Schedule.spaced("2 seconds"), |
| 276 | }), |
| 277 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 278 | ); |
| 279 | if (operation !== undefined) { |
| 280 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 281 | } |
| 282 | yield* waitUntilGone(getByName(output.name), output.name); |
| 283 | }), |
| 284 | }); |