Skip to content

Commit

Permalink
Rename Configuration -> RemoteConfiguration for clarity
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Ying Wang <[email protected]>
  • Loading branch information
cywang117 committed Sep 13, 2023
1 parent 4b99cc7 commit a02bba2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
23 changes: 13 additions & 10 deletions src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::config_json::{
get_api_endpoint, get_root_certificate, merge_config_json, read_config_json, write_config_json,
ConfigMap,
};
use crate::remote::{config_url, fetch_configuration, Configuration};
use crate::remote::{config_url, fetch_configuration, RemoteConfiguration};
use crate::schema::{read_os_config_schema, OsConfigSchema};
use crate::systemd;
use anyhow::Result;
Expand Down Expand Up @@ -39,13 +39,13 @@ pub fn reconfigure(args: &Args, config_json: &ConfigMap, joining: bool) -> Resul

let root_certificate = get_root_certificate(config_json)?;

let configuration = fetch_configuration(
let remote_config = fetch_configuration(
&config_url(&api_endpoint, &args.config_route),
root_certificate,
!joining,
)?;

let has_config_changes = has_config_changes(&schema, &configuration)?;
let has_config_changes = has_config_changes(&schema, &remote_config)?;

if !has_config_changes {
info!("No configuration changes");
Expand All @@ -65,7 +65,7 @@ pub fn reconfigure(args: &Args, config_json: &ConfigMap, joining: bool) -> Resul
args,
config_json,
&schema,
&configuration,
&remote_config,
has_config_changes,
joining,
);
Expand All @@ -81,7 +81,7 @@ fn reconfigure_core(
args: &Args,
config_json: &ConfigMap,
schema: &OsConfigSchema,
configuration: &Configuration,
remote_config: &RemoteConfiguration,
has_config_changes: bool,
joining: bool,
) -> Result<()> {
Expand All @@ -90,16 +90,19 @@ fn reconfigure_core(
}

if has_config_changes {
configure_services(schema, configuration)?;
configure_services(schema, remote_config)?;
}

Ok(())
}

fn has_config_changes(schema: &OsConfigSchema, configuration: &Configuration) -> Result<bool> {
fn has_config_changes(
schema: &OsConfigSchema,
remote_config: &RemoteConfiguration,
) -> Result<bool> {
for service in &schema.services {
for (name, config_file) in &service.files {
let future = configuration.get_config_contents(&service.id, name)?;
let future = remote_config.get_config_contents(&service.id, name)?;
let current = get_config_contents(&config_file.path);

if future != current {
Expand All @@ -111,7 +114,7 @@ fn has_config_changes(schema: &OsConfigSchema, configuration: &Configuration) ->
Ok(false)
}

fn configure_services(schema: &OsConfigSchema, configuration: &Configuration) -> Result<()> {
fn configure_services(schema: &OsConfigSchema, remote_config: &RemoteConfiguration) -> Result<()> {
for service in &schema.services {
for systemd_service in &service.systemd_services {
systemd::stop_service(systemd_service)?;
Expand All @@ -126,7 +129,7 @@ fn configure_services(schema: &OsConfigSchema, configuration: &Configuration) ->
names.sort();
for name in names {
let config_file = &service.files[name as &str];
let contents = configuration.get_config_contents(&service.id, name)?;
let contents = remote_config.get_config_contents(&service.id, name)?;
let mode = fs::parse_mode(&config_file.perm)?;
fs::write_file(Path::new(&config_file.path), contents, mode)?;
info!("{} updated", &config_file.path);
Expand Down
14 changes: 7 additions & 7 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::schema::validate_schema_version;
use anyhow::{anyhow, Context, Result};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Configuration {
pub struct RemoteConfiguration {
pub services: HashMap<String, HashMap<String, String>>,
pub schema_version: String,
}

impl Configuration {
impl RemoteConfiguration {
pub fn get_config_contents<'a>(
&'a self,
service_id: &str,
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn fetch_configuration(
config_url: &str,
root_certificate: Option<reqwest::Certificate>,
retry: bool,
) -> Result<Configuration> {
) -> Result<RemoteConfiguration> {
fetch_configuration_impl(config_url, root_certificate, retry)
.context("Fetching configuration failed")
}
Expand All @@ -51,7 +51,7 @@ fn fetch_configuration_impl(
config_url: &str,
root_certificate: Option<reqwest::Certificate>,
retry: bool,
) -> Result<Configuration> {
) -> Result<RemoteConfiguration> {
let client = build_reqwest_client(root_certificate)?;

let request_fn = if retry {
Expand Down Expand Up @@ -159,10 +159,10 @@ mod tests {
}"#;

#[test]
fn parse_configuration_v1() {
let parsed: Configuration = serde_json::from_str(JSON_DATA).unwrap();
fn parse_configuration() {
let parsed: RemoteConfiguration = serde_json::from_str(JSON_DATA).unwrap();

let expected = Configuration {
let expected = RemoteConfiguration {
services: hashmap! {
"openvpn".into() => hashmap!{
"config".into() => "main configuration here".into(),
Expand Down

0 comments on commit a02bba2

Please sign in to comment.