The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Predicate from "effect/Predicate"; |
| 7 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { listAllZones } from "../Zone/lookup.ts"; |
| 10 | |
| 11 | const TypeId = "Cloudflare.DNS.ZoneTransferIncoming" as const; |
| 12 | type TypeId = typeof TypeId; |
| 13 | |
| 14 | export interface ZoneTransferIncomingProps { |
| 15 | /** |
| 16 | * Secondary zone whose incoming transfer configuration is managed. |
| 17 | * Stable — the configuration is a per-zone singleton, so changing the |
| 18 | * zone triggers a replacement. |
| 19 | */ |
| 20 | zoneId: string; |
| 21 | /** |
| 22 | * Zone name (e.g. `example.com.`). |
| 23 | * |
| 24 | * Mutable — updated in place (PUT). |
| 25 | */ |
| 26 | name: string; |
| 27 | /** |
| 28 | * Peers (by id) Cloudflare transfers the zone in from. Reference |
| 29 | * {@link ZoneTransferPeer} resources via their `peerId` attribute. |
| 30 | * |
| 31 | * Mutable — updated in place (PUT). |
| 32 | */ |
| 33 | peers: string[]; |
| 34 | /** |
| 35 | * How often (seconds) the secondary zone auto-refreshes regardless of |
| 36 | * DNS NOTIFY. |
| 37 | * |
| 38 | * Mutable — updated in place (PUT). |
| 39 | */ |
| 40 | autoRefreshSeconds: number; |
| 41 | } |
| 42 | |
| 43 | export interface ZoneTransferIncomingAttributes { |
| 44 | /** Zone whose incoming transfer configuration is managed. */ |
| 45 | zoneId: string; |
| 46 | /** Identifier of the configuration (mirrors the zone id). */ |
| 47 | id: string | undefined; |
| 48 | /** Zone name. */ |
| 49 | name: string | undefined; |
| 50 | /** Peer ids the zone transfers in from. */ |
| 51 | peers: string[]; |
| 52 | /** Auto-refresh interval in seconds. */ |
| 53 | autoRefreshSeconds: number | undefined; |
| 54 | /** SOA serial of the most recent transfer. */ |
| 55 | soaSerial: number | undefined; |
| 56 | /** When the zone was last checked. */ |
| 57 | checkedTime: string | undefined; |
| 58 | /** When the configuration was created. */ |
| 59 | createdTime: string | undefined; |
| 60 | /** When the configuration was last modified. */ |
| 61 | modifiedTime: string | undefined; |
| 62 | } |
| 63 | |
| 64 | export type ZoneTransferIncoming = Resource< |
| 65 | TypeId, |
| 66 | ZoneTransferIncomingProps, |
| 67 | ZoneTransferIncomingAttributes, |
| 68 | never, |
| 69 | Providers |
| 70 | >; |
| 71 | |
| 72 | /** |
| 73 | * The incoming zone-transfer configuration of a secondary zone |
| 74 | * (`/zones/{zone_id}/secondary_dns/incoming`) — links the zone to the |
| 75 | * {@link ZoneTransferPeer | peers} Cloudflare transfers it in from and |
| 76 | * sets the auto-refresh interval. |
| 77 | * |
| 78 | * Requires the Secondary DNS (zone transfer) entitlement, and the zone |
| 79 | * must be created with `type: "secondary"`. The configuration is a |
| 80 | * per-zone singleton: `zoneId` is the identity (replacement on change), |
| 81 | * everything else is mutable in place. |
| 82 | * ### Configuring incoming transfers |
| 83 | * **Example:** Transfer a secondary zone in from a primary |
| 84 | * ```typescript |
| 85 | * const peer = yield* Cloudflare.DNS.ZoneTransferPeer("Primary", { |
| 86 | * ip: "192.0.2.53", |
| 87 | * port: 53, |
| 88 | * }); |
| 89 | * yield* Cloudflare.DNS.ZoneTransferIncoming("Incoming", { |
| 90 | * zoneId: zone.zoneId, |
| 91 | * name: "example.com.", |
| 92 | * peers: [peer.peerId], |
| 93 | * autoRefreshSeconds: 86400, |
| 94 | * }); |
| 95 | * ``` |
| 96 | * |
| 97 | * @see https://developers.cloudflare.com/dns/zone-setups/zone-transfers/setup/ |
| 98 | * |
| 99 | * @resource |
| 100 | * @product DNS |
| 101 | * @category Domains & DNS |
| 102 | */ |
| 103 | export const ZoneTransferIncoming = Resource<ZoneTransferIncoming>(TypeId, { |
| 104 | aliases: ["Cloudflare.Dns.ZoneTransferIncoming"], |
| 105 | }); |
| 106 | |
| 107 | /** |
| 108 | * Returns true if the given value is a ZoneTransferIncoming resource. |
| 109 | */ |
| 110 | export const isZoneTransferIncoming = (value: unknown): value is ZoneTransferIncoming => |
| 111 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 112 | |
| 113 | export const ZoneTransferIncomingProvider = () => |
| 114 | Provider.succeed(ZoneTransferIncoming, { |
| 115 | stables: ["zoneId", "id", "createdTime"], |
| 116 | |
| 117 | list: Effect.fn(function* () { |
| 118 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 119 | // No account-wide API for this per-zone singleton — enumerate |
| 120 | // every zone and read its incoming transfer config. Only |
| 121 | // secondary zones with a configured transfer have one, so skip |
| 122 | // the rest (`IncomingZoneTransferNotFound`). |
| 123 | const allZones = yield* listAllZones(accountId); |
| 124 | const rows = yield* Effect.forEach( |
| 125 | allZones.map((zone) => zone.id), |
| 126 | (zoneId) => |
| 127 | getIncoming(zoneId).pipe( |
| 128 | Effect.map((observed) => |
| 129 | observed === undefined ? undefined : toAttributes(observed, zoneId), |
| 130 | ), |
| 131 | ), |
| 132 | { concurrency: 10 }, |
| 133 | ); |
| 134 | return rows.filter((row): row is ZoneTransferIncomingAttributes => row !== undefined); |
| 135 | }), |
| 136 | |
| 137 | diff: Effect.fn(function* ({ olds = {}, news, output }) { |
| 138 | const o = olds as ZoneTransferIncomingProps; |
| 139 | const n = news as ZoneTransferIncomingProps; |
| 140 | // zoneId is the resource's identity (per-zone singleton). |
| 141 | // Input<string> — compare only once concrete. |
| 142 | const oldZoneId = output?.zoneId ?? (typeof o.zoneId === "string" ? o.zoneId : undefined); |
| 143 | if (oldZoneId !== undefined && typeof n.zoneId === "string" && oldZoneId !== n.zoneId) { |
| 144 | return { action: "replace" } as const; |
| 145 | } |
| 146 | return undefined; |
| 147 | }), |
| 148 | |
| 149 | read: Effect.fn(function* ({ output, olds }) { |
| 150 | const zoneId = output?.zoneId ?? (typeof olds?.zoneId === "string" ? olds.zoneId : undefined); |
| 151 | if (!zoneId) return undefined; |
| 152 | const observed = yield* getIncoming(zoneId); |
| 153 | if (observed === undefined) return undefined; |
| 154 | const attrs = toAttributes(observed, zoneId); |
| 155 | // The configuration carries no ownership markers. With no prior |
| 156 | // state, gate takeover of an existing configuration behind |
| 157 | // adoption. |
| 158 | return output === undefined ? Unowned(attrs) : attrs; |
| 159 | }), |
| 160 | |
| 161 | reconcile: Effect.fn(function* ({ news }) { |
| 162 | // Inputs are resolved to concrete values by Plan. |
| 163 | const zoneId = news.zoneId as string; |
| 164 | const peers = news.peers as string[]; |
| 165 | |
| 166 | // Observe — singleton keyed by the zone itself. |
| 167 | const observed = yield* getIncoming(zoneId); |
| 168 | |
| 169 | if (!observed) { |
| 170 | // Ensure — first-time link of the zone to its peers. |
| 171 | const created = yield* dns.createZoneTransferIncoming({ |
| 172 | zoneId, |
| 173 | name: news.name, |
| 174 | peers, |
| 175 | autoRefreshSeconds: news.autoRefreshSeconds, |
| 176 | }); |
| 177 | return toAttributes(created, zoneId); |
| 178 | } |
| 179 | |
| 180 | // Sync — PUT with the full desired body; skip the call when the |
| 181 | // observed configuration already matches. |
| 182 | const dirty = |
| 183 | undef(observed.name) !== news.name || |
| 184 | undef(observed.autoRefreshSeconds) !== news.autoRefreshSeconds || |
| 185 | !samePeers(observed.peers ?? [], peers); |
| 186 | if (!dirty) { |
| 187 | return toAttributes(observed, zoneId); |
| 188 | } |
| 189 | const updated = yield* dns.updateZoneTransferIncoming({ |
| 190 | zoneId, |
| 191 | name: news.name, |
| 192 | peers, |
| 193 | autoRefreshSeconds: news.autoRefreshSeconds, |
| 194 | }); |
| 195 | return toAttributes(updated, zoneId); |
| 196 | }), |
| 197 | |
| 198 | delete: Effect.fn(function* ({ output }) { |
| 199 | yield* dns |
| 200 | .deleteZoneTransferIncoming({ zoneId: output.zoneId }) |
| 201 | .pipe(Effect.catchTag("IncomingZoneTransferNotFound", () => Effect.void)); |
| 202 | }), |
| 203 | }); |
| 204 | |
| 205 | type ObservedIncoming = |
| 206 | | dns.GetZoneTransferIncomingResponse |
| 207 | | dns.CreateZoneTransferIncomingResponse |
| 208 | | dns.UpdateZoneTransferIncomingResponse; |
| 209 | |
| 210 | const undef = <T>(v: T | null | undefined): T | undefined => (v == null ? undefined : v); |
| 211 | |
| 212 | /** Read the incoming configuration, mapping "not linked" to undefined. */ |
| 213 | const getIncoming = (zoneId: string) => |
| 214 | dns |
| 215 | .getZoneTransferIncoming({ zoneId }) |
| 216 | .pipe(Effect.catchTag("IncomingZoneTransferNotFound", () => Effect.undefined)); |
| 217 | |
| 218 | const samePeers = (observed: readonly string[], desired: readonly string[]) => |
| 219 | observed.length === desired.length && |
| 220 | [...observed].sort().join(",") === [...desired].sort().join(","); |
| 221 | |
| 222 | const toAttributes = ( |
| 223 | incoming: ObservedIncoming, |
| 224 | zoneId: string, |
| 225 | ): ZoneTransferIncomingAttributes => ({ |
| 226 | zoneId, |
| 227 | id: undef(incoming.id), |
| 228 | name: undef(incoming.name), |
| 229 | peers: [...(undef(incoming.peers) ?? [])], |
| 230 | autoRefreshSeconds: undef(incoming.autoRefreshSeconds), |
| 231 | soaSerial: undef(incoming.soaSerial), |
| 232 | checkedTime: undef(incoming.checkedTime), |
| 233 | createdTime: undef(incoming.createdTime), |
| 234 | modifiedTime: undef(incoming.modifiedTime), |
| 235 | }); |