The-Nazara-Project / Nazara

A CLI application to create and update machines and VMs in Netbox.
GNU General Public License v3.0
10 stars 2 forks source link

Translate collected information into JSON for API #30

Closed ByteOtter closed 1 year ago

ByteOtter commented 1 year ago

What does this PR change?

Add a translation module which takes the information collected by the collectors module and translates it into a JSON format (using the serde_json crate) to pass to the Netbox API.

Rough design:

Add impl blocks to the NetworkInformation and DmiInformation structs, which implement a serialize_to_json function. This function can serialize the struct to a JSON format. This is useful as it allows us for example for the network_collector as we have multiple instances of the NetworkInformation struct which need to be serialized individually to have a correct JSON in the end.

The translator module would then just get called to serialize these structs and build the findal JSON to be handed to Netbox.

Sketch of the impl block:

impl NetworkInformation {
    fn serialize_to_json(&self) -> String {
        match serde_json::to_string(&self) {
            Ok(json) => json,
            Err(err) => {
                println!(
                    "Warning: Could not serialize interface '{}'. ({})",
                    &self.name, err
                );
                String::new() // Subject to change
            }
        }
    }
}

Tick the applicable box:

Links

Fixes: #5

Documentation