icinga2_api/config.rs
1//! Configuration related code
2
3use std::path::{Path, PathBuf};
4
5use serde::Deserialize;
6
7/// this represents the configuration for an Icinga instance we connect to
8#[derive(Debug, Clone, Deserialize)]
9pub struct Icinga2Instance {
10 /// the URL to connect to, without the v1 component or anything after that
11 pub url: String,
12 /// the CA certificate to use to validate the server certificate
13 pub ca_certificate: Option<PathBuf>,
14 /// username
15 pub username: String,
16 /// password
17 pub password: String,
18}
19
20impl Icinga2Instance {
21 /// create a new Icinga2 instance from a TOML config file
22 ///
23 /// # Errors
24 /// this fails if the configuration file can not be found or parsed
25 /// or the CA certificate file mentioned in the configuration file
26 /// can not be found or parsed
27 pub fn from_config_file(path: &Path) -> Result<Self, crate::error::Error> {
28 let content =
29 std::fs::read_to_string(path).map_err(crate::error::Error::CouldNotReadConfigFile)?;
30 let config: crate::config::Icinga2Instance =
31 toml::from_str(&content).map_err(crate::error::Error::CouldNotParseConfig)?;
32 Ok(config)
33 }
34}