Skip to content

Commit

Permalink
Added <config> module.
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanghat committed Jul 12, 2024
1 parent 7789ec8 commit 4569ad3
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
13 changes: 13 additions & 0 deletions rust/samples/sample.conf
Original file line number Diff line number Diff line change
@@ -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"
57 changes: 57 additions & 0 deletions rust/src/iaas/config.rs
Original file line number Diff line number Diff line change
@@ -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 <black>/"" fields.
Self::populate_blank_values(&mut config);

////////////////////////
// Debug logs.
///////////////////////
// println!("[DEBUG] -> {:?}", config);

Self {
database: config.database,
redis: config.redis,
}
}
}
11 changes: 11 additions & 0 deletions rust/src/iaas/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![allow(unused)]
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct DatabaseConf {
pub name: Option<String>,
pub user: Option<String>,
pub enable_high_availability: bool,
pub plan: String,
pub version: String,
}
8 changes: 7 additions & 1 deletion rust/src/iaas/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
pub mod prelude;
pub mod config;
pub mod db;
pub mod prelude;
pub mod redis;

pub use config::*;
pub use db::*;
7 changes: 7 additions & 0 deletions rust/src/iaas/redis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(unused)]
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct RedisConf {
pub plan: String,
}
3 changes: 0 additions & 3 deletions rust/src/iaas/samples/postgres.conf

This file was deleted.

7 changes: 3 additions & 4 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4569ad3

Please sign in to comment.