Skip to content

Commit

Permalink
split off device creation request
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Apr 16, 2024
1 parent f7a9345 commit 58c42fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 54 deletions.
58 changes: 25 additions & 33 deletions src/publisher/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,10 @@
extern crate thanix_client;

use reqwest::{blocking::Client, Error as ReqwestError};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json;
use thanix_client::util::ThanixClient;

use crate::collectors::{dmi_collector::DmiInformation, network_collector::NetworkInformation};
use thanix_client::{paths::{dcim_devices_create, DcimDevicesCreateResponse}, types::WritableDeviceWithConfigContextRequest, util::ThanixClient};

use super::{publisher, publisher_exceptions};

/// Represents the combined system information required to create a new machine or update an existing one.
///
/// # Members
///
/// * `dmi_information: DmiInformation` - DMI (Desktop Management Interface) details of the system such as model and manufacturer.
/// * `network_information: NetworkInformation` - A list of network interface details.
/// * `system_location: String` A string representing the location or office the system is located at. Read from the config file.
#[derive(Serialize)]
pub struct SystemData {
pub dmi_information: DmiInformation,
pub network_information: Vec<NetworkInformation>,
pub system_name: String,
pub system_location: String,
pub device_role: String,
}

/// Encapsulates the payload required to create a new machine in NetBox.
///
/// This struct is serialized into JSON format and sent as the body of the HTTP request to create a new machine.
///
/// # Members
///
/// * `system_information: SystemData` - System Information to include in the request.
#[derive(Serialize)]
pub struct CreateMachinePayload {
pub system_information: SystemData,
}

/// Tests connection to the NetBox API.
///
/// This method attempts to retrieve the API root from the NetBox API to affirm that it is reachable.
Expand Down Expand Up @@ -77,3 +45,27 @@ pub fn test_connection(client: &ThanixClient) -> Result<(), publisher_exceptions
Err(e) => Err(publisher_exceptions::NetBoxApiError::Reqwest(e)),
}
}

pub fn create_device(client: &ThanixClient, payload: &WritableDeviceWithConfigContextRequest) {
println!("Creating Device in NetBox...");

match dcim_devices_create(client, *payload) {
Ok(response) => {
match response {
DcimDevicesCreateResponse::Http201(created_device) => {
// TODO
println!(
"[success] Device creation successful!\nYour machine can be found under the ID {}.", created_device.id
);
}
DcimDevicesCreateResponse::Other(other_response) => {
// TODO
todo!("Other Response codes from creation not yet implemented!");
}
}
}
Err(err) => {
panic!("{}", err); // Handle this better
}
}
}
23 changes: 2 additions & 21 deletions src/publisher/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use thanix_client::{

use crate::{
configuration::config_parser::ConfigData,
publisher::{api_client::test_connection, translator},
publisher::{api_client::{create_device, test_connection}, translator},
Machine,
};

Expand Down Expand Up @@ -88,26 +88,7 @@ pub fn register_machine(
todo!("Device update not yet implemented.") // TODO Implement machine update
}
None => {
match paths::dcim_devices_create(&client, payload) {
Ok(response) => {
// Check response code 201, handle other. (Should not happen)
match response {
DcimDevicesCreateResponse::Http201(created_device) => {
// TODO
todo!("Device creation not yet implemented!")
}
DcimDevicesCreateResponse::Other(other_response) => {
// TODO
todo!(
"Unexpected response code on device creation not handled yet!"
)
}
}
}
Err(err) => {
panic!("{}", err) // Handle failure correctly
}
}
create_device(client, &payload);
}
}
}
Expand Down

0 comments on commit 58c42fc

Please sign in to comment.