Skip to main content

unifly_core/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::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, 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
85    // ── Network CRUD ─────────────────────────────────────────────────
86    CreateNetwork(CreateNetworkRequest),
87    UpdateNetwork {
88        id: EntityId,
89        update: UpdateNetworkRequest,
90    },
91    DeleteNetwork {
92        id: EntityId,
93        force: bool,
94    },
95
96    // ── WiFi CRUD ────────────────────────────────────────────────────
97    CreateWifiBroadcast(CreateWifiBroadcastRequest),
98    UpdateWifiBroadcast {
99        id: EntityId,
100        update: UpdateWifiBroadcastRequest,
101    },
102    DeleteWifiBroadcast {
103        id: EntityId,
104        force: bool,
105    },
106
107    // ── Firewall ─────────────────────────────────────────────────────
108    CreateFirewallPolicy(CreateFirewallPolicyRequest),
109    UpdateFirewallPolicy {
110        id: EntityId,
111        update: UpdateFirewallPolicyRequest,
112    },
113    DeleteFirewallPolicy {
114        id: EntityId,
115    },
116    PatchFirewallPolicy {
117        id: EntityId,
118        enabled: bool,
119    },
120    ReorderFirewallPolicies {
121        zone_pair: (EntityId, EntityId),
122        ordered_ids: Vec<EntityId>,
123    },
124    CreateFirewallZone(CreateFirewallZoneRequest),
125    UpdateFirewallZone {
126        id: EntityId,
127        update: UpdateFirewallZoneRequest,
128    },
129    DeleteFirewallZone {
130        id: EntityId,
131    },
132
133    // ── ACL ──────────────────────────────────────────────────────────
134    CreateAclRule(CreateAclRuleRequest),
135    UpdateAclRule {
136        id: EntityId,
137        update: UpdateAclRuleRequest,
138    },
139    DeleteAclRule {
140        id: EntityId,
141    },
142    ReorderAclRules {
143        ordered_ids: Vec<EntityId>,
144    },
145
146    // ── DNS ──────────────────────────────────────────────────────────
147    CreateDnsPolicy(CreateDnsPolicyRequest),
148    UpdateDnsPolicy {
149        id: EntityId,
150        update: UpdateDnsPolicyRequest,
151    },
152    DeleteDnsPolicy {
153        id: EntityId,
154    },
155
156    // ── Traffic matching lists ───────────────────────────────────────
157    CreateTrafficMatchingList(CreateTrafficMatchingListRequest),
158    UpdateTrafficMatchingList {
159        id: EntityId,
160        update: UpdateTrafficMatchingListRequest,
161    },
162    DeleteTrafficMatchingList {
163        id: EntityId,
164    },
165
166    // ── Hotspot / Vouchers ───────────────────────────────────────────
167    CreateVouchers(CreateVouchersRequest),
168    DeleteVoucher {
169        id: EntityId,
170    },
171    PurgeVouchers {
172        filter: String,
173    },
174
175    // ── System (Legacy) ──────────────────────────────────────────────
176    ArchiveAlarm {
177        id: EntityId,
178    },
179    ArchiveAllAlarms,
180    CreateSite {
181        name: String,
182        description: String,
183    },
184    DeleteSite {
185        name: String,
186    },
187    CreateBackup,
188    DeleteBackup {
189        filename: String,
190    },
191    RebootController,
192    PoweroffController,
193    InviteAdmin {
194        name: String,
195        email: String,
196        role: String,
197    },
198    RevokeAdmin {
199        id: EntityId,
200    },
201    UpdateAdmin {
202        id: EntityId,
203        role: Option<String>,
204    },
205}
206
207/// Result of a command execution.
208#[derive(Debug)]
209pub enum CommandResult {
210    Ok,
211    Device(Device),
212    Client(Client),
213    Network(Network),
214    WifiBroadcast(WifiBroadcast),
215    FirewallPolicy(FirewallPolicy),
216    FirewallZone(FirewallZone),
217    AclRule(AclRule),
218    DnsPolicy(DnsPolicy),
219    Vouchers(Vec<Voucher>),
220    TrafficMatchingList(TrafficMatchingList),
221}