diff --git a/rust/samples/sample.conf b/rust/samples/sample.conf new file mode 100644 index 0000000..b2eee5e --- /dev/null +++ b/rust/samples/sample.conf @@ -0,0 +1,13 @@ +# The following is a sample configuration file. +# This will be used to provision a +# managed postgres instance and managed redis instance. + +[database] +name = "" +user = "" +enable_high_availability = false +plan = "free" +version = "11" + +[redis] +plan = "free" diff --git a/rust/src/iaas/config.rs b/rust/src/iaas/config.rs new file mode 100644 index 0000000..818c7d0 --- /dev/null +++ b/rust/src/iaas/config.rs @@ -0,0 +1,57 @@ +#![allow(unused)] +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; +use serde::Deserialize; +use std::fs; +use toml; + +use super::db::DatabaseConf; +use super::redis::RedisConf; + +#[derive(Debug, Deserialize)] +pub struct Conf { + pub database: DatabaseConf, + pub redis: RedisConf, +} + +impl Conf { + pub fn generate_random_string(&self, length: usize) -> String { + thread_rng() + .sample_iter(&Alphanumeric) + .take(length) + .map(char::from) + .collect() + } + + pub fn populate_blank_values(config: &mut Conf) { + if config.database.name.as_deref() == Some("") { + config.database.name = Some(format!("db_{}", config.generate_random_string(10))); + } + + if config.database.user.as_deref() == Some("") { + config.database.user = Some(format!("user_{}", config.generate_random_string(10))); + } + } + + pub fn read_configuration_file() -> Self { + let conf_path = "./samples/sample.conf"; + let contents = fs::read_to_string(conf_path) + .expect(format!("Unable to read <{conf_path:?}>").as_str()); + + // Parse config. file. + let mut config: Conf = toml::from_str(&contents).expect("Unable to parse config. file!"); + + // Populate any /"" fields. + Self::populate_blank_values(&mut config); + + //////////////////////// + // Debug logs. + /////////////////////// + // println!("[DEBUG] -> {:?}", config); + + Self { + database: config.database, + redis: config.redis, + } + } +} diff --git a/rust/src/iaas/db.rs b/rust/src/iaas/db.rs new file mode 100644 index 0000000..2e32fd4 --- /dev/null +++ b/rust/src/iaas/db.rs @@ -0,0 +1,11 @@ +#![allow(unused)] +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct DatabaseConf { + pub name: Option, + pub user: Option, + pub enable_high_availability: bool, + pub plan: String, + pub version: String, +} diff --git a/rust/src/iaas/mod.rs b/rust/src/iaas/mod.rs index 296a703..c891ec6 100644 --- a/rust/src/iaas/mod.rs +++ b/rust/src/iaas/mod.rs @@ -1 +1,7 @@ -pub mod prelude; \ No newline at end of file +pub mod config; +pub mod db; +pub mod prelude; +pub mod redis; + +pub use config::*; +pub use db::*; diff --git a/rust/src/iaas/redis.rs b/rust/src/iaas/redis.rs new file mode 100644 index 0000000..c2d9443 --- /dev/null +++ b/rust/src/iaas/redis.rs @@ -0,0 +1,7 @@ +#![allow(unused)] +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct RedisConf { + pub plan: String, +} diff --git a/rust/src/iaas/samples/postgres.conf b/rust/src/iaas/samples/postgres.conf deleted file mode 100644 index bef9448..0000000 --- a/rust/src/iaas/samples/postgres.conf +++ /dev/null @@ -1,3 +0,0 @@ -database { - name = "" -} \ No newline at end of file diff --git a/rust/src/main.rs b/rust/src/main.rs index c7f67e0..8b6b5f2 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -24,10 +24,9 @@ async fn main() { // let services = ServiceManager::find_service_by_environment("image", "10").await; //////////////////////////////////////////////// /// - /// 2. Using Terraform for resource provisioning - let input = ""; - - + /// 2. Using simple .conf files for resource provisioning + let config = config::Conf::read_configuration_file(); + println!("{:?}", config); } /// Checks for regression of service management functions