Skip to content

Commit

Permalink
qnap: Add update config feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lcruz99 committed Dec 10, 2024
1 parent 03578c9 commit 687836b
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 19 deletions.
50 changes: 43 additions & 7 deletions clis/teliod/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{num::NonZeroU64, path::PathBuf, str::FromStr};

use serde::{de, Deserialize, Deserializer, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use smart_default::SmartDefault;
use std::fs;
use tracing::{debug, info, level_filters::LevelFilter};
Expand All @@ -22,7 +22,7 @@ impl std::ops::Mul<std::time::Duration> for Percentage {
}
}

#[derive(PartialEq, Eq, Clone, Debug, Deserialize, SmartDefault)]
#[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize, SmartDefault)]
#[serde(default)]
pub struct MqttConfig {
/// Starting backoff time for mqtt retry, has to be at least one. (in seconds)
Expand All @@ -34,7 +34,10 @@ pub struct MqttConfig {

/// Percentage of the expiry period after which new mqtt token will be requested
#[default(reconnect_after_expiry_default())]
#[serde(deserialize_with = "deserialize_percent")]
#[serde(
deserialize_with = "deserialize_percent",
serialize_with = "serialize_percent"
)]
pub reconnect_after_expiry: Percentage,

/// Path to a mqtt pem certificate to be used when connecting to Notification Center
Expand Down Expand Up @@ -83,16 +86,22 @@ impl DeviceIdentity {
}
}

#[derive(PartialEq, Eq, Deserialize, Debug)]
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct TeliodDaemonConfig {
#[serde(deserialize_with = "deserialize_log_level")]
#[serde(
deserialize_with = "deserialize_log_level",
serialize_with = "serialize_log_level"
)]
pub log_level: LevelFilter,
pub log_file_path: String,
pub interface: InterfaceConfig,

pub app_user_uid: Uuid,

#[serde(deserialize_with = "deserialize_authentication_token")]
#[serde(
deserialize_with = "deserialize_authentication_token",
serialize_with = "serialize_authentication_token"
)]
pub authentication_token: String,

/// Path to a http pem certificate to be used when connecting to CoreApi
Expand All @@ -115,6 +124,13 @@ where
Ok(Percentage(value))
}

fn serialize_percent<S>(percentage: &Percentage, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u8(percentage.0)
}

fn deserialize_log_level<'de, D>(deserializer: D) -> Result<LevelFilter, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -126,6 +142,13 @@ where
})
}

fn serialize_log_level<S>(log_level: &LevelFilter, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&log_level.to_string())
}

fn deserialize_authentication_token<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<String, D::Error> {
Expand All @@ -138,7 +161,20 @@ fn deserialize_authentication_token<'de, D: Deserializer<'de>>(
}
}

#[derive(PartialEq, Eq, Deserialize, Debug)]
fn serialize_authentication_token<S>(auth_token: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if auth_token.len() == 64 && auth_token.chars().all(|c| c.is_ascii_hexdigit()) {
serializer.serialize_str(auth_token)
} else {
Err(serde::ser::Error::custom(
"Invalid authentication token format",
))
}
}

#[derive(Default, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct InterfaceConfig {
pub name: String,
pub config_provider: InterfaceConfigurationProvider,
Expand Down
5 changes: 3 additions & 2 deletions clis/teliod/src/configure_interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::TeliodError;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::process::Command;
use tracing::{error, info};
Expand All @@ -24,9 +24,10 @@ fn execute(command: &mut Command) -> Result<(), TeliodError> {
}
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[derive(Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum InterfaceConfigurationProvider {
#[default]
Manual,
Ifconfig,
Iproute,
Expand Down
Loading

0 comments on commit 687836b

Please sign in to comment.