@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 170 lines 6.08 KB
Raw
1 import * as cni from "@distilled.cloud/cloudflare/network-interconnects";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import * as Predicate from "effect/Predicate";
6 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
7 import type { Providers } from "../Providers.ts";
8
9 const TypeId = "Cloudflare.NetworkInterconnects.Settings" as const;
10 type TypeId = typeof TypeId;
11
12 export interface NetworkInterconnectSettingsProps {
13 /**
14 * The default ASN (Autonomous System Number) used for new CNI BGP
15 * sessions on this account when a CNI does not specify its own
16 * `customerAsn`.
17 *
18 * Mutable — updated in place via `PUT /accounts/{account_id}/cni/settings`.
19 */
20 defaultAsn: number;
21 }
22
23 export interface NetworkInterconnectSettingsAttributes {
24 /** Account the CNI settings belong to. */
25 accountId: string;
26 /** The currently configured default ASN. */
27 defaultAsn: number;
28 /**
29 * The default ASN observed before Alchemy first managed this
30 * singleton. Restored on destroy, so deleting the resource puts the
31 * account back the way it was found.
32 */
33 initialDefaultAsn: number;
34 }
35
36 export type NetworkInterconnectSettings = Resource<
37 TypeId,
38 NetworkInterconnectSettingsProps,
39 NetworkInterconnectSettingsAttributes,
40 never,
41 Providers
42 >;
43
44 /**
45 * Account-level settings for Cloudflare Network Interconnect (CNI v2) —
46 * currently the default ASN applied to new CNI BGP configurations
47 * (`/accounts/{account_id}/cni/settings`).
48 *
49 * The settings object is an **account singleton** — it always exists and
50 * can never be created or deleted. Reconcile PUTs the desired value when
51 * the observed value differs; destroy restores the value the setting had
52 * before Alchemy first managed it (captured as `initialDefaultAsn`).
53 *
54 * CNI is an enterprise feature — on accounts without the Network
55 * Interconnect entitlement the endpoint fails with the typed `Forbidden`
56 * error.
57 * ### Managing the default ASN
58 * **Example:** Pin the account's default ASN
59 * ```typescript
60 * yield* Cloudflare.NetworkInterconnects.NetworkInterconnectSettings("CniSettings", {
61 * defaultAsn: 65000,
62 * });
63 * ```
64 *
65 * **Example:** Use a private 32-bit ASN
66 * ```typescript
67 * yield* Cloudflare.NetworkInterconnects.NetworkInterconnectSettings("CniSettings", {
68 * defaultAsn: 4200000001,
69 * });
70 * ```
71 *
72 * @see https://developers.cloudflare.com/network-interconnect/
73 *
74 * @resource
75 * @product Network Interconnects
76 * @category Network
77 */
78 export const NetworkInterconnectSettings = Resource<NetworkInterconnectSettings>(TypeId);
79
80 /**
81 * Returns true if the given value is a NetworkInterconnectSettings resource.
82 */
83 export const isNetworkInterconnectSettings = (
84 value: unknown,
85 ): value is NetworkInterconnectSettings =>
86 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
87
88 export const NetworkInterconnectSettingsProvider = () =>
89 Provider.succeed(NetworkInterconnectSettings, {
90 nuke: { singleton: true },
91 stables: ["accountId", "initialDefaultAsn"],
92
93 // Account singleton — there is no enumeration API, just the single
94 // `/accounts/{account_id}/cni/settings` object. Read it and return a
95 // one-element array (mirroring `read` with no prior output, so
96 // `initialDefaultAsn` is the observed value). CNI is an enterprise
97 // feature: accounts without the entitlement reject the route with the
98 // typed `Forbidden` error — treat that as "unset" and return `[]`.
99 list: Effect.fn(function* () {
100 const { accountId } = yield* yield* CloudflareEnvironment;
101 const observed = yield* cni
102 .getSetting({ accountId })
103 .pipe(Effect.catchTag("Forbidden", () => Effect.undefined));
104 if (observed === undefined) return [];
105 return [
106 {
107 accountId,
108 defaultAsn: observed.defaultAsn,
109 initialDefaultAsn: observed.defaultAsn,
110 },
111 ];
112 }),
113
114 read: Effect.fn(function* ({ output }) {
115 const { accountId } = yield* yield* CloudflareEnvironment;
116 // The settings singleton always exists with a Cloudflare default —
117 // there is nothing to "own", so a cold read adopts freely (never
118 // `Unowned`). The observed value at adoption time becomes the
119 // `initialDefaultAsn` restored on destroy.
120 const observed = yield* cni.getSetting({ accountId });
121 const initialDefaultAsn =
122 output !== undefined ? output.initialDefaultAsn : observed.defaultAsn;
123 return {
124 accountId,
125 defaultAsn: observed.defaultAsn,
126 initialDefaultAsn,
127 };
128 }),
129
130 reconcile: Effect.fn(function* ({ news, output }) {
131 const { accountId } = yield* yield* CloudflareEnvironment;
132
133 // 1. Observe — the singleton always exists; read its live value.
134 const observed = yield* cni.getSetting({ accountId });
135
136 // 2. Capture — the pre-management value, restored on destroy.
137 // `output` (including an adoption read) already carries it;
138 // otherwise this is our first touch and the observed value is
139 // the account's original.
140 const initialDefaultAsn =
141 output !== undefined ? output.initialDefaultAsn : observed.defaultAsn;
142
143 // 3. Sync — PUT only when the observed value differs.
144 if (observed.defaultAsn === news.defaultAsn) {
145 return {
146 accountId,
147 defaultAsn: observed.defaultAsn,
148 initialDefaultAsn,
149 };
150 }
151 const updated = yield* cni.putSetting({
152 accountId,
153 defaultAsn: news.defaultAsn,
154 });
155 return {
156 accountId,
157 defaultAsn: updated.defaultAsn,
158 initialDefaultAsn,
159 };
160 }),
161
162 delete: Effect.fn(function* ({ output }) {
163 const { accountId, initialDefaultAsn } = output;
164 // Observe — restore the pre-management value; skip the call when
165 // it already matches (idempotent re-delete after a crashed run).
166 const observed = yield* cni.getSetting({ accountId });
167 if (observed.defaultAsn === initialDefaultAsn) return;
168 yield* cni.putSetting({ accountId, defaultAsn: initialDefaultAsn });
169 }),
170 });