Complete enroll route
This commit is contained in:
@ -25,28 +25,33 @@ pub struct DeviceId(pub String);
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Device {
|
||||
/// The device ID
|
||||
id: DeviceId,
|
||||
pub id: DeviceId,
|
||||
/// Information about the device
|
||||
device: DeviceInfo,
|
||||
pub info: DeviceInfo,
|
||||
/// Time at which device was initially enrolled
|
||||
pub time_create: u64,
|
||||
/// Time at which device was last updated
|
||||
pub time_update: u64,
|
||||
/// Name given to the device on the Web UI
|
||||
name: String,
|
||||
pub name: String,
|
||||
/// Description given to the device on the Web UI
|
||||
description: String,
|
||||
pub description: String,
|
||||
/// Specify whether the device has been validated or not. Validated devices are given a
|
||||
/// certificate
|
||||
pub validated: bool,
|
||||
/// Specify whether the device is enabled or not
|
||||
enabled: bool,
|
||||
/// Specify whether the device has been validated or not
|
||||
validated: bool,
|
||||
pub enabled: bool,
|
||||
/// Information about the relays handled by the device
|
||||
relays: Vec<DeviceRelay>,
|
||||
pub relays: Vec<DeviceRelay>,
|
||||
}
|
||||
|
||||
/// Structure that contains information about the minimal expected execution
|
||||
/// time of a device
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct DailyMinRuntime {
|
||||
min_runtime: usize,
|
||||
reset_time: usize,
|
||||
catch_up_hours: Vec<usize>,
|
||||
pub min_runtime: usize,
|
||||
pub reset_time: usize,
|
||||
pub catch_up_hours: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
|
||||
@ -63,4 +68,5 @@ pub struct DeviceRelay {
|
||||
minimal_downtime: usize,
|
||||
daily_runtime: Option<DailyMinRuntime>,
|
||||
depends_on: Vec<DeviceRelay>,
|
||||
conflicts_with: Vec<DeviceRelay>,
|
||||
}
|
||||
|
@ -1,7 +1,17 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::devices::device::{Device, DeviceId};
|
||||
use crate::devices::device::{Device, DeviceId, DeviceInfo};
|
||||
use crate::utils::time_utils::time_secs;
|
||||
use openssl::x509::X509Req;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum DevicesListError {
|
||||
#[error("Enrollment failed: a device with the same ID was already registered!")]
|
||||
EnrollFailedDeviceAlreadyExists,
|
||||
#[error("Persist device config failed: the configuration of the device was not found!")]
|
||||
PersistFailedDeviceNotFound,
|
||||
}
|
||||
|
||||
pub struct DevicesList(HashMap<DeviceId, Device>);
|
||||
|
||||
impl DevicesList {
|
||||
@ -33,4 +43,51 @@ impl DevicesList {
|
||||
pub fn exists(&self, id: &DeviceId) -> bool {
|
||||
self.0.contains_key(id)
|
||||
}
|
||||
|
||||
/// Enroll a new device
|
||||
pub fn enroll(
|
||||
&mut self,
|
||||
id: &DeviceId,
|
||||
info: &DeviceInfo,
|
||||
csr: &X509Req,
|
||||
) -> anyhow::Result<()> {
|
||||
if self.exists(id) {
|
||||
return Err(DevicesListError::EnrollFailedDeviceAlreadyExists.into());
|
||||
}
|
||||
|
||||
let device = Device {
|
||||
id: id.clone(),
|
||||
info: info.clone(),
|
||||
time_create: time_secs(),
|
||||
time_update: time_secs(),
|
||||
name: id.0.to_string(),
|
||||
description: "".to_string(),
|
||||
validated: false,
|
||||
enabled: false,
|
||||
relays: vec![],
|
||||
};
|
||||
|
||||
// First, write CSR
|
||||
std::fs::write(AppConfig::get().device_csr_path(id), csr.to_pem()?)?;
|
||||
|
||||
self.0.insert(id.clone(), device);
|
||||
self.persist_dev_config(id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Persist a device configuration on the filesystem
|
||||
fn persist_dev_config(&self, id: &DeviceId) -> anyhow::Result<()> {
|
||||
let dev = self
|
||||
.0
|
||||
.get(id)
|
||||
.ok_or_else(|| DevicesListError::PersistFailedDeviceNotFound)?;
|
||||
|
||||
std::fs::write(
|
||||
AppConfig::get().device_config_path(id),
|
||||
serde_json::to_string_pretty(dev)?,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user