From fa9d0232bce930ef5fd56f3053d2ea11aee04ac2 Mon Sep 17 00:00:00 2001 From: Nicolas Gomez Palacios Date: Tue, 5 Nov 2024 10:19:11 -0300 Subject: [PATCH] fix(CentralizedConfiguration): unifies duplicate code when validating and downloading files. --- .../src/centralized_configuration.cpp | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/agent/centralized_configuration/src/centralized_configuration.cpp b/src/agent/centralized_configuration/src/centralized_configuration.cpp index 9fcb472746..9f56cdfed8 100644 --- a/src/agent/centralized_configuration/src/centralized_configuration.cpp +++ b/src/agent/centralized_configuration/src/centralized_configuration.cpp @@ -10,6 +10,8 @@ namespace centralized_configuration { try { + std::vector groupIds {}; + std::string messageOnSuccess {}; if (command == "set-group") { @@ -20,19 +22,10 @@ namespace centralized_configuration module_command::Status::FAILURE, "CentralizedConfiguration group set failed, no group list"}; - const auto groupIds = parameters[0].get>(); + groupIds = parameters[0].get>(); + messageOnSuccess = "CentralizedConfiguration group set"; m_setGroupIdFunction(groupIds); - - for (const auto& groupId : groupIds) - { - m_downloadGroupFilesFunction(groupId, std::filesystem::temp_directory_path().string()); - } - - // TODO validate groupFiles, apply configuration - - co_return module_command::CommandExecutionResult {module_command::Status::SUCCESS, - "CentralizedConfiguration group set"}; } else { @@ -44,33 +37,36 @@ namespace centralized_configuration { if (m_getGroupIdFunction && m_downloadGroupFilesFunction) { - const auto groupIds = m_getGroupIdFunction(); - - for (const auto& groupId : groupIds) - { - m_downloadGroupFilesFunction(groupId, std::filesystem::temp_directory_path().string()); - } - - // TODO validate groupFiles, apply configuration - - co_return module_command::CommandExecutionResult {module_command::Status::SUCCESS, - "CentralizedConfiguration group updated"}; + groupIds = m_getGroupIdFunction(); + messageOnSuccess = "CentralizedConfiguration group updated"; } else { co_return module_command::CommandExecutionResult { - module_command::Status::FAILURE, "CentralizedConfiguration group set failed, no function set"}; + module_command::Status::FAILURE, + "CentralizedConfiguration group update failed, no function set"}; } } + else + { + co_return module_command::CommandExecutionResult {module_command::Status::FAILURE, + "CentralizedConfiguration command not recognized"}; + } + + for (const auto& groupId : groupIds) + { + m_downloadGroupFilesFunction(groupId, std::filesystem::temp_directory_path().string()); + } + + // TODO validate groupFiles, apply configuration + + co_return module_command::CommandExecutionResult {module_command::Status::SUCCESS, messageOnSuccess}; } catch (const nlohmann::json::exception&) { co_return module_command::CommandExecutionResult { module_command::Status::FAILURE, "CentralizedConfiguration error while parsing parameters"}; } - - co_return module_command::CommandExecutionResult {module_command::Status::FAILURE, - "CentralizedConfiguration command not recognized"}; } void CentralizedConfiguration::SetGroupIdFunction(SetGroupIdFunctionType setGroupIdFunction)