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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! virtualization endpoints for clusters, vms, and interfaces.
//!
//! includes configuration rendering for virtual machines.
//!
//! 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 vms = client.virtualization().virtual_machines().list(None).await?;
//! println!("{}", vms.count);
//! # Ok(())
//! # }
//! ```
//!
//! config rendering:
//! ```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"))?;
//! // render a VM's configuration
//! let config = client.virtualization().render_vm_config(42).await?;
//! # Ok(())
//! # }
//! ```

use crate::Client;
use crate::error::Result;
use crate::resource::Resource;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// request for creating a cluster (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateClusterRequest {
    /// cluster name.
    pub name: String,
    /// cluster type id.
    pub r#type: i32,
    /// cluster 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>,
    /// tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// scope type string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_type: Option<String>,
    /// scope object id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_id: Option<i32>,
    /// 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>>,
}

/// request for updating a cluster (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateClusterRequest {
    /// updated cluster name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// updated cluster type id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<i32>,
    /// updated cluster 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 tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// updated scope type string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_type: Option<String>,
    /// updated scope object id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_id: Option<i32>,
    /// 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>>,
}

/// request for creating a virtual machine (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateVirtualMachineRequest {
    /// virtual machine name.
    pub name: String,
    /// status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// site id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub site: Option<i32>,
    /// cluster id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cluster: Option<i32>,
    /// device id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device: Option<i32>,
    /// serial number.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub serial: Option<String>,
    /// role id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<i32>,
    /// tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// platform id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platform: Option<i32>,
    /// primary IPv4 address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_ip4: Option<i32>,
    /// primary IPv6 address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_ip6: Option<i32>,
    /// vCPU count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vcpus: Option<f64>,
    /// memory in MB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory: Option<i32>,
    /// disk in GB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disk: Option<i32>,
    /// 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>,
    /// config template id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config_template: Option<i32>,
    /// local config context data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub local_context_data: Option<Value>,
    /// tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
}

/// request for updating a virtual machine (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateVirtualMachineRequest {
    /// updated virtual machine name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// updated status slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// updated site id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub site: Option<i32>,
    /// updated cluster id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cluster: Option<i32>,
    /// updated device id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device: Option<i32>,
    /// updated serial number.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub serial: Option<String>,
    /// updated role id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<i32>,
    /// updated tenant id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<i32>,
    /// updated platform id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platform: Option<i32>,
    /// updated primary IPv4 address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_ip4: Option<i32>,
    /// updated primary IPv6 address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_ip6: Option<i32>,
    /// updated vCPU count.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vcpus: Option<f64>,
    /// updated memory in MB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory: Option<i32>,
    /// updated disk in GB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disk: Option<i32>,
    /// 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 config template id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config_template: Option<i32>,
    /// updated local config context data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub local_context_data: Option<Value>,
    /// updated tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
}

/// request for creating a VM interface (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateVmInterfaceRequest {
    /// virtual machine id.
    pub virtual_machine: i32,
    /// interface name.
    pub name: String,
    /// enabled flag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// parent interface id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<i32>,
    /// bridge interface id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bridge: Option<i32>,
    /// mTU value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mtu: Option<i32>,
    /// primary MAC address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_mac_address: Option<i32>,
    /// description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// mode slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<String>,
    /// untagged VLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub untagged_vlan: Option<i32>,
    /// tagged VLAN IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tagged_vlans: Option<Vec<i32>>,
    /// q-in-Q SVLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qinq_svlan: Option<i32>,
    /// vLAN translation policy id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vlan_translation_policy: Option<i32>,
    /// vRF id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vrf: Option<i32>,
    /// tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
}

/// request for updating a VM interface (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateVmInterfaceRequest {
    /// updated virtual machine id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub virtual_machine: Option<i32>,
    /// updated interface name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// updated enabled flag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// updated parent interface id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<i32>,
    /// updated bridge interface id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bridge: Option<i32>,
    /// updated MTU value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mtu: Option<i32>,
    /// updated primary MAC address id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_mac_address: Option<i32>,
    /// updated description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// updated mode slug.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<String>,
    /// updated untagged VLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub untagged_vlan: Option<i32>,
    /// updated tagged VLAN IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tagged_vlans: Option<Vec<i32>>,
    /// updated Q-in-Q SVLAN id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qinq_svlan: Option<i32>,
    /// updated VLAN translation policy id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vlan_translation_policy: Option<i32>,
    /// updated VRF id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vrf: Option<i32>,
    /// updated tag IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<i32>>,
}

/// request for creating a virtual disk (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateVirtualDiskRequest {
    /// virtual machine id.
    pub virtual_machine: i32,
    /// disk name.
    pub name: String,
    /// disk size in GB.
    pub size: 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>>,
}

/// request for updating a virtual disk (id-based references).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateVirtualDiskRequest {
    /// updated virtual machine id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub virtual_machine: Option<i32>,
    /// updated disk name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// updated disk size in GB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: 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>>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ClientConfig;
    use httpmock::prelude::*;
    use proptest::prelude::*;
    use serde_json::{Value, json};

    fn mock_client(server: &MockServer) -> Client {
        let config = ClientConfig::new(server.base_url(), "test-token");
        Client::new(config).unwrap()
    }

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

    fn assert_optional_i32(value: &Value, key: &str, field: &Option<i32>) {
        match field {
            Some(v) => assert_eq!(value[key], serde_json::json!(*v)),
            None => assert_missing(value, key),
        }
    }

    fn assert_optional_string(value: &Value, key: &str, field: &Option<String>) {
        match field {
            Some(v) => assert_eq!(value[key], serde_json::json!(v)),
            None => assert_missing(value, key),
        }
    }

    #[test]
    fn serialize_cluster_requests() {
        let create = CreateClusterRequest {
            name: "cluster".to_string(),
            r#type: 1,
            group: None,
            status: Some("active".to_string()),
            tenant: None,
            scope_type: None,
            scope_id: None,
            description: None,
            comments: None,
            tags: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["name"], "cluster");
        assert_eq!(value["type"], 1);
        assert_eq!(value["status"], "active");
        assert_missing(&value, "group");

        let update = UpdateClusterRequest {
            name: None,
            r#type: None,
            group: Some(2),
            status: None,
            tenant: None,
            scope_type: None,
            scope_id: None,
            description: Some("Updated".to_string()),
            comments: None,
            tags: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["group"], 2);
        assert_eq!(value["description"], "Updated");
        assert_missing(&value, "name");
    }

    #[test]
    fn serialize_virtual_machine_requests() {
        let create = CreateVirtualMachineRequest {
            name: "vm".to_string(),
            status: Some("active".to_string()),
            site: None,
            cluster: Some(1),
            device: None,
            serial: None,
            role: None,
            tenant: None,
            platform: None,
            primary_ip4: None,
            primary_ip6: None,
            vcpus: Some(2.0),
            memory: Some(2048),
            disk: Some(40),
            description: None,
            comments: None,
            config_template: None,
            local_context_data: None,
            tags: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["name"], "vm");
        assert_eq!(value["status"], "active");
        assert_eq!(value["cluster"], 1);
        assert_eq!(value["vcpus"], 2.0);
        assert_eq!(value["memory"], 2048);
        assert_eq!(value["disk"], 40);
        assert_missing(&value, "site");

        let update = UpdateVirtualMachineRequest {
            name: None,
            status: None,
            site: None,
            cluster: None,
            device: None,
            serial: None,
            role: None,
            tenant: None,
            platform: None,
            primary_ip4: None,
            primary_ip6: None,
            vcpus: None,
            memory: None,
            disk: None,
            description: Some("Updated".to_string()),
            comments: None,
            config_template: None,
            local_context_data: None,
            tags: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["description"], "Updated");
        assert_missing(&value, "name");
    }

    #[test]
    fn serialize_vm_interface_requests() {
        let create = CreateVmInterfaceRequest {
            virtual_machine: 1,
            name: "eth0".to_string(),
            enabled: Some(true),
            parent: None,
            bridge: None,
            mtu: None,
            primary_mac_address: None,
            description: None,
            mode: None,
            untagged_vlan: None,
            tagged_vlans: None,
            qinq_svlan: None,
            vlan_translation_policy: None,
            vrf: None,
            tags: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["virtual_machine"], 1);
        assert_eq!(value["name"], "eth0");
        assert_eq!(value["enabled"], true);
        assert_missing(&value, "parent");

        let update = UpdateVmInterfaceRequest {
            virtual_machine: None,
            name: None,
            enabled: None,
            parent: None,
            bridge: None,
            mtu: Some(1500),
            primary_mac_address: None,
            description: Some("Updated".to_string()),
            mode: None,
            untagged_vlan: None,
            tagged_vlans: None,
            qinq_svlan: None,
            vlan_translation_policy: None,
            vrf: None,
            tags: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["mtu"], 1500);
        assert_eq!(value["description"], "Updated");
        assert_missing(&value, "name");
    }

    #[test]
    fn serialize_virtual_disk_requests() {
        let create = CreateVirtualDiskRequest {
            virtual_machine: 1,
            name: "root".to_string(),
            size: 100,
            description: None,
            tags: None,
        };
        let value = serde_json::to_value(&create).unwrap();
        assert_eq!(value["virtual_machine"], 1);
        assert_eq!(value["name"], "root");
        assert_eq!(value["size"], 100);
        assert_missing(&value, "description");

        let update = UpdateVirtualDiskRequest {
            virtual_machine: None,
            name: None,
            size: Some(200),
            description: None,
            tags: None,
        };
        let value = serde_json::to_value(&update).unwrap();
        assert_eq!(value["size"], 200);
        assert_missing(&value, "name");
    }

    proptest! {
        #[test]
        fn prop_virtual_machine_optional_fields(
            status in proptest::option::of("[a-z0-9-]{1,12}"),
            memory in proptest::option::of(0i32..65536),
        ) {
            let request = CreateVirtualMachineRequest {
                name: "vm".to_string(),
                status: status.clone(),
                site: None,
                cluster: None,
                device: None,
                serial: None,
                role: None,
                tenant: None,
                platform: None,
                primary_ip4: None,
                primary_ip6: None,
                vcpus: None,
                memory,
                disk: None,
                description: None,
                comments: None,
                config_template: None,
                local_context_data: None,
                tags: None,
            };
            let value = serde_json::to_value(&request).unwrap();
            assert_optional_string(&value, "status", &status);
            assert_optional_i32(&value, "memory", &memory);
        }
    }

    proptest! {
        #[test]
        fn prop_vm_interface_optional_fields(
            mtu in proptest::option::of(0i32..9000),
            mode in proptest::option::of("[a-z0-9-]{1,12}"),
        ) {
            let request = CreateVmInterfaceRequest {
                virtual_machine: 1,
                name: "eth0".to_string(),
                enabled: None,
                parent: None,
                bridge: None,
                mtu,
                primary_mac_address: None,
                description: None,
                mode: mode.clone(),
                untagged_vlan: None,
                tagged_vlans: None,
                qinq_svlan: None,
                vlan_translation_policy: None,
                vrf: None,
                tags: None,
            };
            let value = serde_json::to_value(&request).unwrap();
            assert_optional_i32(&value, "mtu", &mtu);
            assert_optional_string(&value, "mode", &mode);
        }
    }

    proptest! {
        #[test]
        fn prop_virtual_disk_optional_fields(
            description in proptest::option::of("[a-z0-9-]{0,16}"),
        ) {
            let request = CreateVirtualDiskRequest {
                virtual_machine: 1,
                name: "root".to_string(),
                size: 10,
                description: description.clone(),
                tags: None,
            };
            let value = serde_json::to_value(&request).unwrap();
            assert_optional_string(&value, "description", &description);
        }
    }

    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn render_vm_config_uses_expected_path() {
        let server = MockServer::start();
        let mock = server.mock(|when, then| {
            when.method(POST)
                .path("/api/virtualization/virtual-machines/42/render-config/");
            then.status(200).json_body(json!("hostname vm-01"));
        });

        let client = mock_client(&server);
        let result = client.virtualization().render_vm_config(42).await;
        assert!(result.is_ok());
        mock.assert();
    }
}

/// cluster group model.
pub type ClusterGroup = crate::models::ClusterGroup;
/// cluster type model.
pub type ClusterType = crate::models::ClusterType;
/// cluster model.
pub type Cluster = crate::models::Cluster;
/// vM interface model.
pub type VmInterface = crate::models::VmInterface;
/// virtual disk model.
pub type VirtualDisk = crate::models::VirtualDisk;
/// virtual machine model with config context.
pub type VirtualMachine = crate::models::VirtualMachineWithConfigContext;

/// resource for cluster groups.
pub type ClusterGroupsApi = Resource<crate::models::ClusterGroup>;
/// resource for cluster types.
pub type ClusterTypesApi = Resource<crate::models::ClusterType>;
/// resource for clusters.
pub type ClustersApi = Resource<crate::models::Cluster>;
/// resource for VM interfaces.
pub type VmInterfacesApi = Resource<crate::models::VmInterface>;
/// resource for virtual disks.
pub type VirtualDisksApi = Resource<crate::models::VirtualDisk>;
/// resource for virtual machines.
pub type VirtualMachinesApi = Resource<crate::models::VirtualMachineWithConfigContext>;

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

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

    /// returns the cluster groups resource.
    pub fn cluster_groups(&self) -> ClusterGroupsApi {
        Resource::new(self.client.clone(), "virtualization/cluster-groups/")
    }

    /// returns the cluster types resource.
    pub fn cluster_types(&self) -> ClusterTypesApi {
        Resource::new(self.client.clone(), "virtualization/cluster-types/")
    }

    /// returns the clusters resource.
    pub fn clusters(&self) -> ClustersApi {
        Resource::new(self.client.clone(), "virtualization/clusters/")
    }

    /// returns the VM interfaces resource.
    pub fn interfaces(&self) -> VmInterfacesApi {
        Resource::new(self.client.clone(), "virtualization/interfaces/")
    }

    /// returns the virtual disks resource.
    pub fn virtual_disks(&self) -> VirtualDisksApi {
        Resource::new(self.client.clone(), "virtualization/virtual-disks/")
    }

    /// returns the virtual machines resource.
    pub fn virtual_machines(&self) -> VirtualMachinesApi {
        Resource::new(self.client.clone(), "virtualization/virtual-machines/")
    }

    /// render a virtual machine's configuration.
    pub async fn render_vm_config(&self, id: u64) -> Result<String> {
        self.client
            .post(
                &format!("virtualization/virtual-machines/{}/render-config/", id),
                &(),
            )
            .await
    }
}