Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Implement device creation #65

Merged
merged 14 commits into from
Apr 22, 2024
Merged
6 changes: 5 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ publisher:
api-client:
- src/publisher/api_client/*

translator:
- src/publisher/translator.rs
- src/publisher/trans_validation.rs

dependencies:
- Cargo.toml
- Cargo.lock
Expand All @@ -37,4 +41,4 @@ code-quality:
- .pre-commit-config.yaml

maintenance:
- .github/*
- .github/*
11 changes: 10 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ If you discover a security vulnerability in our code, please inform us privately

If you wish to fix a vulnerability, please also inform us and stand by for our green light. We would still like to investigate the vulnerability for ourselves to get an overview over the severity and scope of the problem. *Please refrain from publishing a fix until we came back to you or have published a security advisory*.

#### Thank you for your contribution!
# Disclaimer

By contributing to this project you agree to surrender your contribution to the Nazara Project.

Your contribution to this project will be subject to the same licensing terms as the rest of the project.

This is a standard practice in open-source projects to ensure that all contributions are compatible with the project's overall license and to maintain consistency in the project's legal framework.

It's important for contributors to be aware of this agreement to ensure that their contributions are properly integrated into the project without any legal conflicts.
#### Thank you for your contribution!
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ toml = "0.7.6"
# This may not work in your environment. You can get your schema by visiting
# https://your.netbox-instance.com/api/schema.
# The yaml file will be downloaded and you can generate your client by using https://github.com/The-Nazara-Project/Thanix.
thanix_client = "0.7.0"
thanix_client = "1.0.0"
# Uncomment this line if you are using a custom thanix client implementation.
# Change the path to be relative to this Cargo.toml file.
# thanix_client = { path = "path/to/your/client/" }
# thanix_client = { path = "path/to/your/client/crate" }

[dev-dependencies]
mockall = "0.11.4"
39 changes: 39 additions & 0 deletions src/collectors/dmi_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* 3. REMOVE DEBUG PRINT STATEMENTS.
* */

use crate::collectors::collector_exceptions;

use super::collector_exceptions::UnableToCollectDataError;
use super::util::{find_table, split_output};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -81,6 +83,7 @@ pub struct ChassisInformation {
/// * max_speed: `String`- The maximum speed of the CPU.
/// * voltage: `String` - The voltage the CPU runs at.
/// * status: `String` - Shows if the socket is enabled/disabled and populated/empty.
/// * arch: `String` - The architecture of the CPU (x86_64, etc).
#[derive(Serialize, Debug)]
pub struct CpuInformation {
pub version: String,
Expand All @@ -90,6 +93,7 @@ pub struct CpuInformation {
pub max_speed: String,
pub voltage: String,
pub status: String,
pub arch: Option<String>,
}

/// List of possible system parameters to collect dmi information from.
Expand Down Expand Up @@ -361,6 +365,7 @@ fn dmidecode_cpu<T: DmiDecodeTable>(_param: T) -> CpuInformation {
max_speed: String::new(),
voltage: String::new(),
status: String::new(),
arch: None,
};

let mut table_found: bool = false;
Expand Down Expand Up @@ -419,10 +424,42 @@ fn dmidecode_cpu<T: DmiDecodeTable>(_param: T) -> CpuInformation {
}
}
}
cpu_information.arch = match get_architecture() {
Ok(arch) => Some(arch),
Err(e) => {
eprintln!(
"\x1b[31m[error]\x1b[0m Failure to get cpu information via `uname`!\n{}",
e
);
None
}
};
println!("\x1b[32m[success]\x1b[0m CPU information collection completed.");
return cpu_information;
}

/// Gets the architecture name through `uname`.
///
/// *This will override any information entered in the config file's `platform` field!*
///
/// # Returns
/// - Ok(arch_name): `string` - Name of the CPU architecture.
/// - Err(UnableToCollectDataError)
fn get_architecture() -> Result<String, UnableToCollectDataError> {
println!("Running uname to collect CPU architecture...");
let output = match Command::new("uname").arg("-p").output() {
Ok(output) => output,
Err(e) => {
let exc: UnableToCollectDataError = UnableToCollectDataError {
message: String::from(format!("\x1b[31m[error]\x1b[0m An error occured while attempting to execute `uname -p`! {}", e))
};
return Err(exc);
}
};

Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

#[cfg(test)]
pub mod dmi_collector_tests {
use super::*;
Expand Down Expand Up @@ -533,6 +570,7 @@ pub mod dmi_collector_tests {
max_speed: "4000 MHz".to_string(),
voltage: "1.2 V".to_string(),
status: "Populated, Enabled".to_string(),
arch: Some("x86_64".to_string()),
};

let result = dmidecode_cpu(MockDmiDecodeTable {});
Expand All @@ -544,5 +582,6 @@ pub mod dmi_collector_tests {
assert_eq!(expected.max_speed, result.max_speed);
assert_eq!(expected.voltage, result.voltage);
assert_eq!(expected.status, result.status);
assert_eq!(expected.arch, result.arch);
}
}
24 changes: 12 additions & 12 deletions src/collectors/network_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ use super::collector_exceptions;
/// * interface_speed - `u32` the speed of the interface.
#[derive(Serialize, Debug)]
pub struct NetworkInformation {
name: String,
interface_speed: Option<u32>,
v4ip: Option<IpAddr>,
v4broadcast: Option<IpAddr>,
v4netmask: Option<IpAddr>,
v6ip: Option<IpAddr>,
v6broadcast: Option<IpAddr>,
v6netmask: Option<IpAddr>,
mac_addr: Option<String>,
index: Option<u32>,
is_physical: bool,
is_connected: bool,
pub name: String,
pub interface_speed: Option<u32>,
pub v4ip: Option<IpAddr>,
pub v4broadcast: Option<IpAddr>,
pub v4netmask: Option<IpAddr>,
pub v6ip: Option<IpAddr>,
pub v6broadcast: Option<IpAddr>,
pub v6netmask: Option<IpAddr>,
pub mac_addr: Option<String>,
pub index: Option<u32>,
pub is_physical: bool,
pub is_connected: bool,
}

/// Collect information about all network interfaces.
Expand Down
Loading
Loading