Skip to main content

unifly_api/command/
mod.rs

1// ── Command API ──
2//
3// All write operations flow through a unified `Command` enum.
4// The controller routes each variant to the appropriate API backend
5// (Integration API preferred, Legacy API for legacy-only operations).
6
7pub mod requests;
8
9use crate::core_error::CoreError;
10use crate::model::{
11    AclRule, Client, Device, DnsPolicy, EntityId, FirewallPolicy, FirewallZone, MacAddress,
12    Network, TrafficMatchingList, Voucher, WifiBroadcast,
13};
14
15pub use requests::{
16    CreateAclRuleRequest, CreateDnsPolicyRequest, CreateFirewallPolicyRequest,
17    CreateFirewallZoneRequest, CreateNetworkRequest, CreateTrafficMatchingListRequest,
18    CreateVouchersRequest, CreateWifiBroadcastRequest, TrafficFilterSpec, UpdateAclRuleRequest,
19    UpdateDnsPolicyRequest, UpdateFirewallPolicyRequest, UpdateFirewallZoneRequest,
20    UpdateNetworkRequest, UpdateTrafficMatchingListRequest, UpdateWifiBroadcastRequest,
21};
22
23/// A command envelope sent through the command channel.
24/// Contains the command and a oneshot response channel.
25pub(crate) struct CommandEnvelope {
26    pub command: Command,
27    pub response_tx: tokio::sync::oneshot::Sender<Result<CommandResult, CoreError>>,
28}
29
30/// All possible write operations against a UniFi controller.
31#[derive(Debug, Clone)]
32pub enum Command {
33    // ── Device operations ────────────────────────────────────────────
34    AdoptDevice {
35        mac: MacAddress,
36        ignore_device_limit: bool,
37    },
38    RemoveDevice {
39        id: EntityId,
40    },
41    RestartDevice {
42        id: EntityId,
43    },
44    LocateDevice {
45        mac: MacAddress,
46        enable: bool,
47    },
48    UpgradeDevice {
49        mac: MacAddress,
50        firmware_url: Option<String>,
51    },
52    ProvisionDevice {
53        mac: MacAddress,
54    },
55    SpeedtestDevice,
56    PowerCyclePort {
57        device_id: EntityId,
58        port_idx: u32,
59    },
60
61    // ── Client operations ────────────────────────────────────────────
62    BlockClient {
63        mac: MacAddress,
64    },
65    UnblockClient {
66        mac: MacAddress,
67    },
68    KickClient {
69        mac: MacAddress,
70    },
71    ForgetClient {
72        mac: MacAddress,
73    },
74    AuthorizeGuest {
75        client_id: EntityId,
76        time_limit_minutes: Option<u32>,
77        data_limit_mb: Option<u64>,
78        rx_rate_kbps: Option<u64>,
79        tx_rate_kbps: Option<u64>,
80    },
81    UnauthorizeGuest {
82        client_id: EntityId,
83    },
84    SetClientFixedIp {
85        mac: MacAddress,
86        ip: std::net::Ipv4Addr,
87        network_id: EntityId,
88    },
89    RemoveClientFixedIp {
90        mac: MacAddress,
91    },
92
93    // ── Network CRUD ─────────────────────────────────────────────────
94    CreateNetwork(CreateNetworkRequest),
95    UpdateNetwork {
96        id: EntityId,
97        update: UpdateNetworkRequest,
98    },
99    DeleteNetwork {
100        id: EntityId,
101        force: bool,
102    },
103
104    // ── WiFi CRUD ────────────────────────────────────────────────────
105    CreateWifiBroadcast(CreateWifiBroadcastRequest),
106    UpdateWifiBroadcast {
107        id: EntityId,
108        update: UpdateWifiBroadcastRequest,
109    },
110    DeleteWifiBroadcast {
111        id: EntityId,
112        force: bool,
113    },
114
115    // ── Firewall ─────────────────────────────────────────────────────
116    CreateFirewallPolicy(CreateFirewallPolicyRequest),
117    UpdateFirewallPolicy {
118        id: EntityId,
119        update: UpdateFirewallPolicyRequest,
120    },
121    DeleteFirewallPolicy {
122        id: EntityId,
123    },
124    PatchFirewallPolicy {
125        id: EntityId,
126        enabled: Option<bool>,
127        logging: Option<bool>,
128    },
129    ReorderFirewallPolicies {
130        zone_pair: (EntityId, EntityId),
131        ordered_ids: Vec<EntityId>,
132    },
133    CreateFirewallZone(CreateFirewallZoneRequest),
134    UpdateFirewallZone {
135        id: EntityId,
136        update: UpdateFirewallZoneRequest,
137    },
138    DeleteFirewallZone {
139        id: EntityId,
140    },
141
142    // ── ACL ──────────────────────────────────────────────────────────
143    CreateAclRule(CreateAclRuleRequest),
144    UpdateAclRule {
145        id: EntityId,
146        update: UpdateAclRuleRequest,
147    },
148    DeleteAclRule {
149        id: EntityId,
150    },
151    ReorderAclRules {
152        ordered_ids: Vec<EntityId>,
153    },
154
155    // ── DNS ──────────────────────────────────────────────────────────
156    CreateDnsPolicy(CreateDnsPolicyRequest),
157    UpdateDnsPolicy {
158        id: EntityId,
159        update: UpdateDnsPolicyRequest,
160    },
161    DeleteDnsPolicy {
162        id: EntityId,
163    },
164
165    // ── Traffic matching lists ───────────────────────────────────────
166    CreateTrafficMatchingList(CreateTrafficMatchingListRequest),
167    UpdateTrafficMatchingList {
168        id: EntityId,
169        update: UpdateTrafficMatchingListRequest,
170    },
171    DeleteTrafficMatchingList {
172        id: EntityId,
173    },
174
175    // ── Hotspot / Vouchers ───────────────────────────────────────────
176    CreateVouchers(CreateVouchersRequest),
177    DeleteVoucher {
178        id: EntityId,
179    },
180    PurgeVouchers {
181        filter: String,
182    },
183
184    // ── System (Legacy) ──────────────────────────────────────────────
185    ArchiveAlarm {
186        id: EntityId,
187    },
188    ArchiveAllAlarms,
189    CreateSite {
190        name: String,
191        description: String,
192    },
193    DeleteSite {
194        name: String,
195    },
196    CreateBackup,
197    DeleteBackup {
198        filename: String,
199    },
200    RebootController,
201    PoweroffController,
202    InviteAdmin {
203        name: String,
204        email: String,
205        role: String,
206    },
207    RevokeAdmin {
208        id: EntityId,
209    },
210    UpdateAdmin {
211        id: EntityId,
212        role: Option<String>,
213    },
214}
215
216/// Result of a command execution.
217#[derive(Debug)]
218pub enum CommandResult {
219    Ok,
220    Device(Device),
221    Client(Client),
222    Network(Network),
223    WifiBroadcast(WifiBroadcast),
224    FirewallPolicy(FirewallPolicy),
225    FirewallZone(FirewallZone),
226    AclRule(AclRule),
227    DnsPolicy(DnsPolicy),
228    Vouchers(Vec<Voucher>),
229    TrafficMatchingList(TrafficMatchingList),
230}