nazara 0.1.0

A CLI application to create and update machines and VMs in NetBox.
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! # Publisher Module
//!
//! This module serves as a controller for publishing information to NetBox.
//! It handles conditionals, error handling and everything else outside of actually sending API
//! requests.
//!
//! It "steers" which endpoints are used when, which payload is created when and hands these
//! responsibilities off to the [`crate::publisher::translator`] and
//! [`crate::publisher::api_client`] modules respectively.

mod api_client;
pub mod translator;

use crate::configuration::parser::{CommonConfig, MachineConfig};
use crate::error::NazaraResult;
use crate::publisher::api_client::{
    create_mac_address, create_vm, create_vm_interface, patch_ip, search_mac_address, search_vm,
    search_vm_interface, search_vm_ip, update_vm, update_vm_interface,
};
use crate::publisher::translator::{
    compute_effective_name, information_to_device, information_to_existing_device,
    information_to_existing_vm, information_to_vm,
};
use crate::{
    Machine,
    collectors::network::NetworkInformation,
    configuration::parser::ConfigData,
    publisher::api_client::{
        create_device, create_interface, create_ip, search_device, search_interface, search_ip,
        update_device,
    },
};
use crate::{NazaraError, info, success};
pub use api_client::test_connection;
use api_client::update_interface;
use serde_json::Value;
use std::collections::HashMap;
use thanix_client::paths::{
    IpamIpAddressesRetrieveResponse, dcim_interfaces_partial_update,
    dcim_mac_addresses_partial_update, ipam_ip_addresses_retrieve,
    virtualization_interfaces_partial_update,
};
use thanix_client::types::{
    MACAddressRequest, PatchedMACAddressRequest, PatchedWritableDeviceWithConfigContextRequest,
    PatchedWritableIPAddressRequest, PatchedWritableInterfaceRequest,
    PatchedWritableVMInterfaceRequest, PatchedWritableVirtualMachineWithConfigContextRequest,
};
use thanix_client::{types::WritableIPAddressRequest, util::ThanixClient};

/// Register new device or virtual machine.
///
/// # Parameters
/// * `client: &ThanixClient` - A client instance.
/// * `machine: Machine` - The Machine to create.
/// * `config_data: ConfigData` - Nazara's configuration data.
///
/// # Returns
/// An empty `Ok()` or a [`NazaraError::NetBoxApiError`] depending on operation outcome.
pub fn register_machine(
    client: &ThanixClient,
    machine: Machine,
    config_data: ConfigData,
) -> NazaraResult<()> {
    println!("Starting registration process. This may take a while...");

    match &config_data.machine {
        MachineConfig::Device(config) => {
            println!("Registering device...");

            let payload = information_to_device(&machine, &config_data.common, config);
            let device_id = create_device(client, payload)?;

            // Create new interface object if no interface ID is given, or the given ID doesn not exist.
            for interface in &machine.network_information {
                let interface_id: i64 =
                    create_nwi(client, device_id, interface, &config_data.common)?;

                create_ips(client, interface, interface_id, false)?;
            }

            // Patch new device with primary IPs if they are set and not empty.
            if config_data
                .common
                .primary_ip4
                .as_ref()
                .map_or(false, |s| !s.is_empty())
                || config_data
                    .common
                    .primary_ip6
                    .as_ref()
                    .map_or(false, |s| !s.is_empty())
            {
                patch_device_primary_ips(client, &config_data, &machine, device_id)?;
            }

            success!("Registration processs completed!");
            return Ok(());
        }
        MachineConfig::VM(config) => {
            println!("Registering virtual machine");

            let payload = information_to_vm(&machine, &config_data.common, config);
            let vm_id = create_vm(client, payload)?;

            for interface in &machine.network_information {
                let interface_id = create_vm_nwi(client, vm_id, interface, &config_data.common)?;

                create_ips(client, interface, interface_id, true)?;
            }

            // Patch new device with primary IPs if they are set and not empty.
            if config_data
                .common
                .primary_ip4
                .as_ref()
                .map_or(false, |s| !s.is_empty())
                || config_data
                    .common
                    .primary_ip6
                    .as_ref()
                    .map_or(false, |s| !s.is_empty())
            {
                patch_vm_primary_ips(client, &config_data, &machine, vm_id)?;
            }

            success!("Registration process completed!");
            return Ok(());
        }
    }
}

/// Update device or virtual machine.
///
/// # Parameters
/// * `client: &ThanixClient` - A client instance.
/// * `machine: Machine` - The information about the machine to update.
/// * `config_data: ConfigData` - Nazara's configuration data.
/// * `machine_id: i64` - The ID of the device or VM to update handed over via CLI.
///
/// # Returns
/// An empty `Ok()` or a [`NazaraError::NetBoxApiError`] depending on operation outcome.
pub fn update_machine(
    client: &ThanixClient,
    machine: Machine,
    config_data: ConfigData,
    machine_id: i64,
) -> NazaraResult<()> {
    println!("Starting update process. This may take a while...");

    match &config_data.machine {
        MachineConfig::Device(config) => {
            let payload = information_to_existing_device(&machine, &config_data.common, config);
            let updated_id = update_device(client, payload, machine_id)?;

            let mut nwi_id;
            for interface in &machine.network_information {
                match search_interface(client, updated_id, &interface.name)? {
                    Some(interface_id) => {
                        nwi_id = update_nwi(
                            client,
                            updated_id,
                            interface,
                            &config_data.common,
                            &interface_id,
                        )?;
                    }
                    None => {
                        nwi_id = create_nwi(client, updated_id, interface, &config_data.common)?;
                    }
                }

                // If the collected interface reports an IP address, the address must exist in NetBox.
                // If it is not assigned an ID, claim it to this interface.
                // If it is assigned an ID, it *must* be our interface ID. If it is not, the data is bogus.
                // Updating interfaces that don't belong to us is undefined behavior because it may interfere with
                // external services that provide these IP addresses.

                let (ipv4, ipv6) = search_device_ips(client, interface, None)?;
                if let Some(ip) = interface.v4ip {
                    let ipv4 = ipv4.ok_or(NazaraError::NetBoxApiError(format!(
                        "IPv4 address \"{}\" was not registered in NetBox",
                        ip
                    )))?;

                    if let IpamIpAddressesRetrieveResponse::Http200(a) =
                        ipam_ip_addresses_retrieve(client, ipv4)?
                    {
                        if let Some(b) = a.assigned_object_id {
                            assert_eq!(b, nwi_id as u64);
                        } else {
                            patch_ip(
                                client,
                                PatchedWritableIPAddressRequest {
                                    status: Some("active".to_string()),
                                    assigned_object_type: Some(Some("dcim.interface".to_string())),
                                    assigned_object_id: Some(Some(nwi_id as u64)),
                                    ..Default::default()
                                },
                                ipv4,
                            )?;
                        }
                    }
                }

                if let Some(ip) = interface.v6ip {
                    let ipv6 = ipv6.ok_or(NazaraError::NetBoxApiError(format!(
                        "IPv6 address \"{}\" was not registered in NetBox",
                        ip
                    )))?;

                    if let IpamIpAddressesRetrieveResponse::Http200(a) =
                        ipam_ip_addresses_retrieve(client, ipv6)?
                    {
                        if let Some(b) = a.assigned_object_id {
                            assert_eq!(b, nwi_id as u64);
                        } else {
                            patch_ip(
                                client,
                                PatchedWritableIPAddressRequest {
                                    status: Some("active".to_string()),
                                    assigned_object_type: Some(Some("dcim.interface".to_string())),
                                    assigned_object_id: Some(Some(nwi_id as u64)),
                                    ..Default::default()
                                },
                                ipv6,
                            )?;
                        }
                    }
                }
            }

            // Patch primary IPs when one is specified in config.
            if config_data
                .common
                .primary_ip4
                .as_deref()
                .map_or(false, |s| !s.is_empty())
                || config_data
                    .common
                    .primary_ip6
                    .as_deref()
                    .map_or(false, |s| !s.is_empty())
            {
                patch_device_primary_ips(client, &config_data, &machine, updated_id)?;
            }

            success!("Device update process completed!");
            return Ok(());
        }
        MachineConfig::VM(config) => {
            let payload = information_to_existing_vm(&machine, &config_data.common, config);
            let updated_id = update_vm(client, payload, machine_id)?;
            let mut nwi_id;
            for interface in &machine.network_information {
                match search_vm_interface(client, updated_id, &interface.name)? {
                    Some(interface_id) => {
                        nwi_id = update_vm_nwi(
                            client,
                            updated_id,
                            interface,
                            &config_data.common,
                            &interface_id,
                        )?;
                    }
                    None => {
                        nwi_id = create_vm_nwi(client, updated_id, interface, &config_data.common)?;
                    }
                }

                // If the collected interface reports an IP address, the address must exist in NetBox.
                // If it is not assigned an ID, claim it to this interface.
                // If it is assigned an ID, it *must* be our interface ID. If it is not, the data is bogus.
                // Updating interfaces that don't belong to us is undefined behavior because it may interfere with
                // external services that provide these IP addresses.

                let (ipv4, ipv6) = search_vm_ips(client, interface, None)?;
                if let Some(ip) = interface.v4ip {
                    let ipv4 = ipv4.expect(&format!(
                        "IPv4 address \"{}\" was not registered in NetBox",
                        ip
                    ));

                    if let IpamIpAddressesRetrieveResponse::Http200(a) =
                        ipam_ip_addresses_retrieve(client, ipv4)?
                    {
                        if let Some(b) = a.assigned_object_id {
                            assert_eq!(b, nwi_id as u64);
                        } else {
                            patch_ip(
                                client,
                                PatchedWritableIPAddressRequest {
                                    status: Some("active".to_string()),
                                    assigned_object_type: Some(Some(
                                        "virtualization.vminterface".to_string(),
                                    )),
                                    assigned_object_id: Some(Some(nwi_id as u64)),
                                    ..Default::default()
                                },
                                ipv4,
                            )?;
                        }
                    }
                }

                if let Some(ip) = interface.v6ip {
                    let ipv6 = ipv6.expect(&format!(
                        "IPv6 address \"{}\" was not registered in NetBox",
                        ip
                    ));

                    if let IpamIpAddressesRetrieveResponse::Http200(a) =
                        ipam_ip_addresses_retrieve(client, ipv6)?
                    {
                        if let Some(b) = a.assigned_object_id {
                            assert_eq!(b, nwi_id as u64);
                        } else {
                            patch_ip(
                                client,
                                PatchedWritableIPAddressRequest {
                                    status: Some("active".to_string()),
                                    assigned_object_type: Some(Some(
                                        "virtualization.vminterface".to_string(),
                                    )),
                                    assigned_object_id: Some(Some(nwi_id as u64)),
                                    ..Default::default()
                                },
                                ipv6,
                            )?;
                        }
                    }
                }
            }

            // Patch primary IPs when one is specified in config.
            if config_data
                .common
                .primary_ip4
                .as_deref()
                .map_or(false, |s| !s.is_empty())
                || config_data
                    .common
                    .primary_ip6
                    .as_deref()
                    .map_or(false, |s| !s.is_empty())
            {
                patch_vm_primary_ips(client, &config_data, &machine, updated_id)?;
            }

            success!("VM update process completed!");
            return Ok(());
        }
    }
}

/// Register or update machine depending on search results.
///
/// This is Nazara's previous default behaviour, yet it may be unreliable and is therefore discouraged.
/// And will be completely deprecated in the future.
///
/// # Parameters
/// - `client`: A client instance.
/// - `machine`: Information about the host machine collected by the [`super::collectors`] module.
/// - `config_data`: Nazara's configuration.
///
/// # Returns
/// An empty `Ok()` or a [`NazaraError::NetBoxApiError`] depending on operation outcome.
pub fn auto_register_or_update_machine(
    client: &ThanixClient,
    machine: Machine,
    config_data: ConfigData,
) -> NazaraResult<()> {
    println!("Starting auto register/update process. This may take a while...");

    // Compute effective name once (includes hostname fallback or @ expansion)
    let search_name = compute_effective_name(
        &config_data.common.name,
        &machine.dmi_information.system_information.hostname,
    );

    // Determine whether we are dealing with a Device or VM and search for it
    match &config_data.machine {
        MachineConfig::Device(_) => {
            if let Some(device_id) = search_device(
                client,
                &search_name,
                &machine.dmi_information.system_information.serial,
            )? {
                info!(
                    "Device found in NetBoxentry with entry ID '{}', updating...",
                    device_id
                );
                update_machine(client, machine, config_data, device_id)?;
            } else {
                info!("Device not found in NetBox, registering new entry");
                register_machine(client, machine, config_data)?;
            }
        }
        MachineConfig::VM(_) => {
            if let Some(vm_id) = search_vm(
                client,
                &search_name,
                &machine.dmi_information.system_information.serial,
            )? {
                info!("VM found in NetBox with entry ID '{}', updating...", vm_id);
                update_machine(client, machine, config_data, vm_id)?;
            } else {
                info!("VM not found in NetBox, registering new entry");
                register_machine(client, machine, config_data)?;
            }
        }
    }

    success!("Auto register/update process completed successfully!");
    Ok(())
}

/// Create new Network Interface object in NetBox.
///
/// # Parameters
/// - `client`: The API client instance to use.
/// - `device_id`: The device this interface belongs to.
/// - `interface`: The interface to create.
/// - `config_data`: The configuration read from the config file.
///
/// # Returns
/// The ID of the newly created interface.
fn create_nwi(
    client: &ThanixClient,
    device_id: i64,
    interface: &NetworkInformation,
    common: &CommonConfig,
) -> NazaraResult<i64> {
    // Check if MAC exists before creating the interface, create the MAC if it doesn't exist
    let intf = interface
        .mac_addr
        .clone()
        .ok_or(NazaraError::NetBoxApiError(
            "Missing \"mac_addr\" field".into(),
        ))?;
    let mac = match search_mac_address(client, &intf)? {
        Some(x) => x,
        None => create_mac_address(
            client,
            MACAddressRequest {
                mac_address: intf.clone(),
                custom_fields: Some(HashMap::new()),
                ..Default::default()
            },
        )?,
    };

    let mut payload = translator::information_to_interface(common, interface, &device_id);

    payload.primary_mac_address = None;
    let intf_id = create_interface(client, payload)?;

    // Assign Intf to MAC
    let mut patch = PatchedMACAddressRequest::default();
    patch.mac_address = Some(intf);
    patch.assigned_object_id = Some(Some(intf_id as u64));
    patch.assigned_object_type = Some(Some("dcim.interface".to_string()));
    dcim_mac_addresses_partial_update(client, patch, mac)?;

    // Update Intf with primary MAC
    let mut intf_patch = PatchedWritableInterfaceRequest::default();
    intf_patch.primary_mac_address = Some(Some(Value::from(mac)));
    dcim_interfaces_partial_update(client, intf_patch, intf_id)?;

    Ok(intf_id)
}

fn create_vm_nwi(
    client: &ThanixClient,
    device_id: i64,
    interface: &NetworkInformation,
    common: &CommonConfig,
) -> NazaraResult<i64> {
    // Check if MAC exists before creating the interface, create the MAC if it doesn't exist
    let mac_addr = interface
        .mac_addr
        .clone()
        .ok_or(NazaraError::NetBoxApiError(
            "Missing \"mac_addr\" field".into(),
        ))?;

    let mac = match search_mac_address(client, &mac_addr)? {
        Some(x) => x,
        None => create_mac_address(
            client,
            MACAddressRequest {
                mac_address: mac_addr.clone(),
                custom_fields: Some(HashMap::new()),
                ..Default::default()
            },
        )?,
    };

    let mut payload = translator::information_to_vm_interface(common, interface, &device_id);

    payload.primary_mac_address = None;
    let intf_id = create_vm_interface(client, payload)?;

    // Assign Intf to MAC
    let mut patch = PatchedMACAddressRequest::default();
    patch.mac_address = Some(mac_addr);
    patch.assigned_object_id = Some(Some(intf_id as u64));
    patch.assigned_object_type = Some(Some("virtualization.vminterface".to_string()));
    dcim_mac_addresses_partial_update(client, patch, mac)?;

    // Update Intf with primary MAC
    let mut intf_patch = PatchedWritableVMInterfaceRequest::default();
    intf_patch.primary_mac_address = Some(Some(Value::from(mac)));
    virtualization_interfaces_partial_update(client, intf_patch, intf_id)?;

    Ok(intf_id)
}

/// Update a given NWI.
/// Creates a new Interface API payload and invokes the API call to update the interface.
///
/// # Parameters
/// - `client`: The API client instance to use.
/// - `device_id`: The ID of the device this NWI belongs to.
/// - `interface`: The information of the interface to update.
/// - `config_data`: The configuration data.
/// - `interface_id`: The ID of the interface to update.
///
/// # Returns
/// The ID of the interface.
fn update_nwi(
    client: &ThanixClient,
    device_id: i64,
    interface: &NetworkInformation,
    config_data: &CommonConfig,
    interface_id: &i64,
) -> NazaraResult<i64> {
    info!("Updating interface '{interface_id}' belonging to device '{device_id}'");

    let mac_addr = interface
        .mac_addr
        .clone()
        .ok_or(NazaraError::NetBoxApiError(
            "Missing \"mac_addr\" field".into(),
        ))?;

    // Check if MAC exists before creating the interface, otherwise create the MAC.
    let mac = match search_mac_address(client, &mac_addr)? {
        Some(x) => x,
        None => create_mac_address(
            client,
            MACAddressRequest {
                mac_address: mac_addr.clone(),
                assigned_object_id: Some(device_id as u64),
                assigned_object_type: Some("dcim.interface".to_string()),
                custom_fields: Some(HashMap::new()),
                ..Default::default()
            },
        )?,
    };

    let mut payload = translator::information_to_interface(&config_data, interface, &device_id);

    payload.primary_mac_address = None;
    update_interface(client, payload, *interface_id)?;

    // Assign Intf to MAC
    let mut patch = PatchedMACAddressRequest::default();
    patch.assigned_object_id = Some(Some(*interface_id as u64));
    patch.assigned_object_type = Some(Some("dcim.interface".to_string()));
    dcim_mac_addresses_partial_update(client, patch, mac)?;

    // Update Intf with primary MAC
    let mut intf_patch = PatchedWritableInterfaceRequest::default();
    intf_patch.primary_mac_address = Some(Some(Value::from(mac)));
    dcim_interfaces_partial_update(client, intf_patch, *interface_id)?;

    Ok(*interface_id)
}

fn update_vm_nwi(
    client: &ThanixClient,
    device_id: i64,
    interface: &NetworkInformation,
    config_data: &CommonConfig,
    interface_id: &i64,
) -> NazaraResult<i64> {
    info!("Updating interface '{interface_id}' belonging to device '{device_id}'");

    let mac_addr = interface
        .mac_addr
        .clone()
        .ok_or(NazaraError::NetBoxApiError(
            "Missing \"mac_addr\" field".into(),
        ))?;

    // Check if MAC exists before creating the interface, otherwise create the MAC.
    let mac = match search_mac_address(client, &mac_addr)? {
        Some(x) => x,
        None => create_mac_address(
            client,
            MACAddressRequest {
                mac_address: mac_addr,
                assigned_object_id: Some(device_id as u64),
                assigned_object_type: Some("virtualization.vminterface".to_string()),
                custom_fields: Some(HashMap::new()),
                ..Default::default()
            },
        )?,
    };

    let mut payload = translator::information_to_vm_interface(&config_data, interface, &device_id);

    payload.primary_mac_address = None;
    update_vm_interface(client, payload, *interface_id)?;

    // Assign Intf to MAC
    let mut patch = PatchedMACAddressRequest::default();
    patch.assigned_object_id = Some(Some(*interface_id as u64));
    patch.assigned_object_type = Some(Some("virtualization.vminterface".to_string()));
    dcim_mac_addresses_partial_update(client, patch, mac)?;

    // Update Intf with primary MAC
    let mut intf_patch = PatchedWritableVMInterfaceRequest::default();
    intf_patch.primary_mac_address = Some(Some(Value::from(mac)));
    virtualization_interfaces_partial_update(client, intf_patch, *interface_id)?;

    Ok(*interface_id)
}

/// Search for a pair of IP addresses.
/// Checks if the `ipv4` and/or `ìpv6` addresses of the given [`NetworkInformation`] are set and
/// invokes [`search_ip`] on each or both of these addresses.
///
/// # Parameters
/// - `client`: The API client instance to use.
/// - `interface`: The interface this address belongs to.
/// - `device_id`: The ID of the device these Addresses are linked to.
///
/// # Returns
/// A tuple of the IDs of each the IPv4 and IPv6 addresses if they are registered.
/// The first field represents the IPv4 Address, the second the IPv6 address.
/// If one or both are not already registered the value will be `None`.
fn search_device_ips(
    client: &ThanixClient,
    interface: &NetworkInformation,
    device_id: Option<i64>,
) -> NazaraResult<(Option<i64>, Option<i64>)> {
    let mut result: (Option<i64>, Option<i64>) = (None, None);
    if let Some(ipv4_address) = interface.v4ip {
        result = (
            search_ip(client, &ipv4_address.to_string(), device_id)?,
            result.1,
        );
    }

    if let Some(ipv6_address) = interface.v6ip {
        result = (
            result.0,
            search_ip(client, &ipv6_address.to_string(), device_id)?,
        );
    }
    Ok(result)
}

fn search_vm_ips(
    client: &ThanixClient,
    interface: &NetworkInformation,
    vm_id: Option<i64>,
) -> NazaraResult<(Option<i64>, Option<i64>)> {
    let mut result: (Option<i64>, Option<i64>) = (None, None);
    if let Some(ipv4_address) = interface.v4ip {
        result = (
            search_vm_ip(client, &ipv4_address.to_string(), vm_id)?,
            result.1,
        );
    }

    if let Some(ipv6_address) = interface.v6ip {
        result = (
            result.0,
            search_vm_ip(client, &ipv6_address.to_string(), vm_id)?,
        );
    }
    Ok(result)
}

/// Creates the given interface's IPv4 and/or IPv6 address(es).
///
/// # Parameters
/// - `client`: The API client instance to use.
/// - `interface`: The interface to get the IP Addresses from.
/// - `interface_id`: The ID of the interface these addresses belong to.
fn create_ips(
    client: &ThanixClient,
    interface: &NetworkInformation,
    interface_id: i64,
    is_vm: bool,
) -> NazaraResult<()> {
    if let Some(ipv4_address) = interface.v4ip {
        let ipv4_payload: WritableIPAddressRequest =
            translator::information_to_ip(ipv4_address, interface_id, is_vm);

        create_ip(client, ipv4_payload)?;
    }

    if let Some(ipv6_address) = interface.v6ip {
        let ipv6_payload: WritableIPAddressRequest =
            translator::information_to_ip(ipv6_address, interface_id, is_vm);

        create_ip(client, ipv6_payload)?;
    };
    Ok(())
}

/// Patch device "primary-ip" fields after registration or on update.
fn patch_device_primary_ips(
    client: &ThanixClient,
    config_data: &ConfigData,
    machine: &Machine,
    device_id: i64,
) -> NazaraResult<()> {
    println!("Applying primary IPs to entry...");

    let mut patch = PatchedWritableDeviceWithConfigContextRequest::default();

    if let Some(primary_v4_str) = &config_data.common.primary_ip4 {
        for interface in &machine.network_information {
            if let Some(ipv4) = interface.v4ip {
                if ipv4.to_string() == *primary_v4_str {
                    let (ipv4_id, _) = search_device_ips(client, interface, Some(device_id))?;
                    if let Some(v4_id) = ipv4_id {
                        patch.primary_ip4 = Some(Some(Value::from(v4_id)));
                    }
                    break;
                }
            }
        }
    }

    if let Some(primary_v6_str) = &config_data.common.primary_ip6 {
        for interface in &machine.network_information {
            if let Some(ipv6) = interface.v6ip {
                if ipv6.to_string() == *primary_v6_str {
                    let (_, ipv6_id) = search_device_ips(client, interface, Some(device_id))?;
                    if let Some(v6_id) = ipv6_id {
                        patch.primary_ip6 = Some(Some(Value::from(v6_id)));
                    }
                    break;
                }
            }
        }
    }

    update_device(client, patch, device_id)?;
    info!("Patched primary IPs for device {device_id}");
    Ok(())
}

/// Patch VM "primary-ip" fields after registration or on update.
fn patch_vm_primary_ips(
    client: &ThanixClient,
    config_data: &ConfigData,
    machine: &Machine,
    vm_id: i64,
) -> NazaraResult<()> {
    println!("Applying primary IPs to entry...");

    let mut patch = PatchedWritableVirtualMachineWithConfigContextRequest::default();

    if let Some(primary_v4_str) = &config_data.common.primary_ip4 {
        for interface in &machine.network_information {
            if let Some(ipv4) = interface.v4ip {
                if ipv4.to_string() == *primary_v4_str {
                    let (ipv4_id, _) = search_vm_ips(client, interface, Some(vm_id))?;
                    if let Some(v4_id) = ipv4_id {
                        patch.primary_ip4 = Some(Some(Value::from(v4_id)));
                    }
                    break;
                }
            }
        }
    }

    if let Some(primary_v6_str) = &config_data.common.primary_ip6 {
        for interface in &machine.network_information {
            if let Some(ipv6) = interface.v6ip {
                if ipv6.to_string() == *primary_v6_str {
                    let (_, ipv6_id) = search_vm_ips(client, interface, Some(vm_id))?;
                    if let Some(v6_id) = ipv6_id {
                        patch.primary_ip6 = Some(Some(Value::from(v6_id)));
                    }
                    break;
                }
            }
        }
    }

    update_vm(client, patch, vm_id)?;
    info!("Patched primary IPs for VM '{vm_id}'");
    Ok(())
}