Skip to content

Commit

Permalink
Correctly handle connecting built-in bridge for new devices
Browse files Browse the repository at this point in the history
Signed-off-by: James Rhodes <[email protected]>
  • Loading branch information
jarhodes314 committed Apr 8, 2024
1 parent 8dd0acd commit 7c85bdc
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions crates/core/tedge/src/cli/connect/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,6 @@ fn new_bridge(
config_location: &TEdgeConfigLocation,
device_type: &str,
) -> Result<(), ConnectError> {
if bridge_config.bridge_location == BridgeLocation::BuiltIn {
println!("Deleting mosquitto bridge configuration in favour of built-in bridge");
clean_up(config_location, bridge_config)?;
restart_mosquitto(bridge_config, service_manager, config_location)?;
return Ok(());
}
println!("Checking if {} is available.\n", service_manager.name());
let service_manager_result = service_manager.check_operational();

Expand All @@ -473,8 +467,10 @@ fn new_bridge(
);
}

println!("Checking if configuration for requested bridge already exists.\n");
bridge_config_exists(config_location, bridge_config)?;
if bridge_config.bridge_location == BridgeLocation::Mosquitto {
println!("Checking if configuration for requested bridge already exists.\n");
bridge_config_exists(config_location, bridge_config)?;
}

println!("Validating the bridge certificates.\n");
bridge_config.validate()?;
Expand All @@ -484,13 +480,20 @@ fn new_bridge(
c8y_direct_connection::create_device_with_direct_connection(bridge_config, device_type)?;
}

println!("Saving configuration for requested bridge.\n");
if let Err(err) =
write_bridge_config_to_file(config_location, bridge_config, common_mosquitto_config)
{
// We want to preserve previous errors and therefore discard result of this function.
let _ = clean_up(config_location, bridge_config);
return Err(err);
if bridge_config.bridge_location == BridgeLocation::Mosquitto {
println!("Saving configuration for requested bridge.\n");

if let Err(err) =
write_bridge_config_to_file(config_location, bridge_config, common_mosquitto_config)
{
// We want to preserve previous errors and therefore discard result of this function.
let _ = clean_up(config_location, bridge_config);
return Err(err);
}
} else {
println!("Deleting mosquitto bridge configuration in favour of built-in bridge");
clean_up(config_location, bridge_config)?;
restart_mosquitto(bridge_config, service_manager, config_location)?;
}

if let Err(err) = service_manager_result {
Expand Down

0 comments on commit 7c85bdc

Please sign in to comment.