netbox 0.3.3

ergonomic rust client for NetBox 4.x REST API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! wireless endpoints for lans, groups, and links.
//!
//! basic usage:
//! ```no_run
//! # use netbox::{Client, ClientConfig};
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
//! let lans = client.wireless().wireless_lans().list(None).await?;
//! println!("{}", lans.count);
//! # Ok(())
//! # }
//! ```

use crate::Client;
use crate::resource::Resource;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// request for creating a wireless LAN group.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWirelessLanGroupRequest {
    /// group name.
    pub name: String,
    /// group slug.
    pub slug: String,
    /// parent group id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<i32>,
    /// description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
    /// comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
}

/// request for updating a wireless LAN group.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateWirelessLanGroupRequest {
    /// updated group name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// updated group slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slug: Option<String>,
    /// updated parent group id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<i32>,
    /// updated description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// updated tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// updated custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
    /// updated comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
}

/// request for creating a wireless LAN.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWirelessLanRequest {
    /// sSID string.
    pub ssid: String,
    /// description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// group id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<i32>,
    /// status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// vLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vlan: Option<i32>,
    /// scope type string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_type: Option<String>,
    /// scope id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_id: Option<i32>,
    /// tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// auth type slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_type: Option<String>,
    /// auth cipher slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_cipher: Option<String>,
    /// auth PSK string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_psk: Option<String>,
    /// comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
    /// tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
}

/// request for updating a wireless LAN.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateWirelessLanRequest {
    /// updated SSID string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssid: Option<String>,
    /// updated description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// updated group id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<i32>,
    /// updated status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// updated VLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vlan: Option<i32>,
    /// updated scope type string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_type: Option<String>,
    /// updated scope id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_id: Option<i32>,
    /// updated tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// updated auth type slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_type: Option<String>,
    /// updated auth cipher slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_cipher: Option<String>,
    /// updated auth PSK string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_psk: Option<String>,
    /// updated comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
    /// updated tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// updated custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
}

/// request for creating a wireless link.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWirelessLinkRequest {
    /// interface A id.
    pub interface_a: i32,
    /// interface B id.
    pub interface_b: i32,
    /// sSID string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssid: Option<String>,
    /// status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// auth type slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_type: Option<String>,
    /// auth cipher slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_cipher: Option<String>,
    /// auth PSK string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_psk: Option<String>,
    /// link distance.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distance: Option<f64>,
    /// distance unit slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distance_unit: Option<String>,
    /// description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
    /// tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
}

/// request for updating a wireless link.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateWirelessLinkRequest {
    /// updated interface A id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interface_a: Option<i32>,
    /// updated interface B id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interface_b: Option<i32>,
    /// updated SSID string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssid: Option<String>,
    /// updated status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// updated tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// updated auth type slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_type: Option<String>,
    /// updated auth cipher slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_cipher: Option<String>,
    /// updated auth PSK string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_psk: Option<String>,
    /// updated link distance.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distance: Option<f64>,
    /// updated distance unit slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distance_unit: Option<String>,
    /// updated description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// updated comments text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<String>,
    /// updated tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
    /// updated custom fields payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<HashMap<String, Value>>,
}

/// wireless LAN group model.
pub type WirelessLanGroup = crate::models::WirelessLanGroup;
/// wireless LAN model.
pub type WirelessLan = crate::models::WirelessLan;
/// wireless link model.
pub type WirelessLink = crate::models::WirelessLink;

/// resource for wireless LAN groups.
pub type WirelessLanGroupsApi = Resource<crate::models::WirelessLanGroup>;
/// resource for wireless LANs.
pub type WirelessLansApi = Resource<crate::models::WirelessLan>;
/// resource for wireless links.
pub type WirelessLinksApi = Resource<crate::models::WirelessLink>;

/// api for wireless endpoints.
#[derive(Clone)]
pub struct WirelessApi {
    client: Client,
}

impl WirelessApi {
    pub(crate) fn new(client: Client) -> Self {
        Self { client }
    }

    /// returns the wireless LAN groups resource.
    pub fn wireless_lan_groups(&self) -> WirelessLanGroupsApi {
        Resource::new(self.client.clone(), "wireless/wireless-lan-groups/")
    }

    /// returns the wireless LANs resource.
    pub fn wireless_lans(&self) -> WirelessLansApi {
        Resource::new(self.client.clone(), "wireless/wireless-lans/")
    }

    /// returns the wireless links resource.
    pub fn wireless_links(&self) -> WirelessLinksApi {
        Resource::new(self.client.clone(), "wireless/wireless-links/")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ClientConfig;
    use serde_json::json;

    fn test_client() -> Client {
        let config = ClientConfig::new("https://netbox.example.com", "token");
        Client::new(config).unwrap()
    }

    fn assert_path<T>(resource: Resource<T>, expected: &str)
    where
        T: serde::de::DeserializeOwned,
    {
        let paginator = resource.paginate(None).unwrap();
        assert_eq!(paginator.next_url(), Some(expected));
    }

    fn assert_missing(value: &serde_json::Value, key: &str) {
        assert!(value.get(key).is_none(), "expected {} to be omitted", key);
    }

    #[test]
    fn wireless_accessors_return_expected_paths() {
        let api = WirelessApi::new(test_client());

        assert_path(api.wireless_lan_groups(), "wireless/wireless-lan-groups/");
        assert_path(api.wireless_lans(), "wireless/wireless-lans/");
        assert_path(api.wireless_links(), "wireless/wireless-links/");
    }

    #[test]
    fn serialize_wireless_lan_group_requests() {
        let mut custom_fields = HashMap::new();
        custom_fields.insert("owner".to_string(), json!("netops"));
        let create = CreateWirelessLanGroupRequest {
            name: "campus".to_string(),
            slug: "campus".to_string(),
            parent: None,
            description: Some("Campus groups".to_string()),
            tags: Some(vec![1]),
            custom_fields: Some(custom_fields),
            comments: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["name"], "campus");
        assert_eq!(value["slug"], "campus");
        assert_eq!(value["description"], "Campus groups");
        assert_eq!(value["tags"], json!([1]));
        assert_eq!(value["custom_fields"]["owner"], "netops");
        assert_missing(&value, "parent");

        let update = UpdateWirelessLanGroupRequest {
            name: None,
            slug: None,
            parent: Some(2),
            description: None,
            tags: None,
            custom_fields: None,
            comments: Some("Updated".to_string()),
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["parent"], 2);
        assert_eq!(value["comments"], "Updated");
        assert_missing(&value, "name");
    }

    #[test]
    fn serialize_wireless_lan_requests() {
        let create = CreateWirelessLanRequest {
            ssid: "Guest".to_string(),
            description: None,
            group: Some(1),
            status: Some("active".to_string()),
            vlan: Some(100),
            scope_type: None,
            scope_id: None,
            tenant: None,
            auth_type: Some("wpa-personal".to_string()),
            auth_cipher: Some("aes".to_string()),
            auth_psk: None,
            comments: None,
            tags: Some(vec![2]),
            custom_fields: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["ssid"], "Guest");
        assert_eq!(value["group"], 1);
        assert_eq!(value["status"], "active");
        assert_eq!(value["vlan"], 100);
        assert_eq!(value["auth_type"], "wpa-personal");
        assert_eq!(value["auth_cipher"], "aes");
        assert_eq!(value["tags"], json!([2]));

        let update = UpdateWirelessLanRequest {
            ssid: None,
            description: Some("Updated".to_string()),
            group: None,
            status: None,
            vlan: None,
            scope_type: Some("dcim.site".to_string()),
            scope_id: Some(5),
            tenant: None,
            auth_type: None,
            auth_cipher: None,
            auth_psk: None,
            comments: None,
            tags: None,
            custom_fields: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["description"], "Updated");
        assert_eq!(value["scope_type"], "dcim.site");
        assert_eq!(value["scope_id"], 5);
        assert_missing(&value, "ssid");
    }

    #[test]
    fn serialize_wireless_link_requests() {
        let create = CreateWirelessLinkRequest {
            interface_a: 1,
            interface_b: 2,
            ssid: Some("Backhaul".to_string()),
            status: Some("connected".to_string()),
            tenant: None,
            auth_type: None,
            auth_cipher: None,
            auth_psk: None,
            distance: Some(0.5),
            distance_unit: Some("km".to_string()),
            description: None,
            comments: None,
            tags: Some(vec![7]),
            custom_fields: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["interface_a"], 1);
        assert_eq!(value["interface_b"], 2);
        assert_eq!(value["ssid"], "Backhaul");
        assert_eq!(value["status"], "connected");
        assert_eq!(value["distance"], 0.5);
        assert_eq!(value["distance_unit"], "km");
        assert_eq!(value["tags"], json!([7]));

        let update = UpdateWirelessLinkRequest {
            interface_a: None,
            interface_b: None,
            ssid: None,
            status: Some("planned".to_string()),
            tenant: Some(3),
            auth_type: None,
            auth_cipher: None,
            auth_psk: None,
            distance: None,
            distance_unit: None,
            description: Some("Planned link".to_string()),
            comments: None,
            tags: None,
            custom_fields: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["status"], "planned");
        assert_eq!(value["tenant"], 3);
        assert_eq!(value["description"], "Planned link");
        assert_missing(&value, "interface_a");
    }
}