Skip to content

Commit

Permalink
refactor: move uuid generation to AgentInfo's concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
jr0me committed Aug 8, 2024
1 parent 17e4911 commit 60d4e6d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/agent/agent_info/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ set(VCPKG_MANIFEST_DIR ${CMAKE_SOURCE_DIR}/../../)
project(AgentInfo)

find_package(SQLiteCpp REQUIRED)
find_package(Boost REQUIRED COMPONENTS uuid)

add_library(AgentInfo src/agent_info.cpp src/agent_info_persistance.cpp)
target_include_directories(AgentInfo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(AgentInfo PRIVATE SQLiteCpp)
target_link_libraries(AgentInfo PRIVATE SQLiteCpp Boost::uuid)

if(BUILD_TESTS)
enable_testing()
Expand Down
10 changes: 10 additions & 0 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

#include <agent_info_persistance.hpp>

#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

AgentInfo::AgentInfo()
{
AgentInfoPersistance agentInfoPersistance;
m_name = agentInfoPersistance.GetName();
m_ip = agentInfoPersistance.GetIP();
m_uuid = agentInfoPersistance.GetUUID();

if (m_uuid.empty())
{
AgentInfoPersistance agentInfoPersistance;
m_uuid = boost::uuids::to_string(boost::uuids::random_generator()());
agentInfoPersistance.SetUUID(m_uuid);
}
}

AgentInfo::AgentInfo(const std::string& name, const std::string& ip, const std::string& uuid)
Expand Down
2 changes: 1 addition & 1 deletion src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues)
const AgentInfo agentInfo;
EXPECT_EQ(agentInfo.GetName(), "");
EXPECT_EQ(agentInfo.GetIP(), "");
EXPECT_EQ(agentInfo.GetUUID(), "");
EXPECT_NE(agentInfo.GetUUID(), "");
}

TEST_F(AgentInfoTest, TestParameterizedConstructor)
Expand Down
8 changes: 1 addition & 7 deletions src/agent/include/register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ namespace registration
std::string password;
};

struct AgentInfoOptionalData
{
std::optional<std::string> name;
std::optional<std::string> ip;
};

bool RegisterAgent(const UserCredentials& userCredentials, const AgentInfoOptionalData& agentInfoOptionalData);
bool RegisterAgent(const UserCredentials& userCredentials);
} // namespace registration
22 changes: 14 additions & 8 deletions src/agent/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <agent.hpp>
#include <agent_info.hpp>
#include <cmd_ln_parser.hpp>
#include <register.hpp>

Expand All @@ -17,17 +18,22 @@ int main(int argc, char* argv[])
{
const auto user = cmdParser.getOptionValue("--user");
const auto password = cmdParser.getOptionValue("--password");
const auto name = cmdParser.OptionExists("--name")
? std::make_optional<std::string>(cmdParser.getOptionValue("--name"))
: std::nullopt;
const auto ip = cmdParser.OptionExists("--ip")
? std::make_optional<std::string>(cmdParser.getOptionValue("--ip"))
: std::nullopt;

AgentInfo agentInfo;

if (cmdParser.OptionExists("--name"))
{
agentInfo.SetName(cmdParser.getOptionValue("--name"));
}

if (cmdParser.OptionExists("--ip"))
{
agentInfo.SetIP(cmdParser.getOptionValue("--ip"));
}

const registration::UserCredentials userCredentials {user, password};
const registration::AgentInfoOptionalData agentInfoOptionalData {name, ip};

if (registration::RegisterAgent(userCredentials, agentInfoOptionalData))
if (registration::RegisterAgent(userCredentials))
{
std::cout << "Agent registered." << std::endl;
}
Expand Down
29 changes: 2 additions & 27 deletions src/agent/src/register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
#include <nlohmann/json.hpp>

namespace beast = boost::beast;
namespace http = beast::http;
namespace uuids = boost::uuids;
using tcp = boost::asio::ip::tcp;
using json = nlohmann::json;

Expand Down Expand Up @@ -44,33 +41,11 @@ namespace
const http::response<http::dynamic_body> res = http_client::SendHttpRequest(reqParams);
return res.result();
}

AgentInfo GenerateAgentInfo(const registration::AgentInfoOptionalData& agentInfoOptionalData)
{
AgentInfo agentInfo;
if (agentInfo.GetUUID().empty())
{
agentInfo.SetUUID(to_string(uuids::random_generator()()));
}

if (agentInfoOptionalData.name.has_value())
{
agentInfo.SetName(agentInfoOptionalData.name.value());
}

if (agentInfoOptionalData.ip.has_value())
{
agentInfo.SetIP(agentInfoOptionalData.ip.value());
}

return agentInfo;
}

} // namespace

namespace registration
{
bool RegisterAgent(const UserCredentials& userCredentials, const AgentInfoOptionalData& agentInfoOptionalData)
bool RegisterAgent(const UserCredentials& userCredentials)
{
const configuration::ConfigurationParser configurationParser;
const auto managerIp = configurationParser.GetConfig<std::string>("agent", "manager_ip");
Expand All @@ -85,7 +60,7 @@ namespace registration
return false;
}

const auto agentInfo = GenerateAgentInfo(agentInfoOptionalData);
const AgentInfo agentInfo {};

if (const auto registrationResultCode = SendRegistrationRequest(
managerIp, port, token.value(), agentInfo.GetUUID(), agentInfo.GetName(), agentInfo.GetIP());
Expand Down
3 changes: 1 addition & 2 deletions src/agent/tests/register_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
TEST(RegistrationTest, RegistrationTest)
{
const registration::UserCredentials userCredentials {"user", "123456"};
const registration::AgentInfoOptionalData agentInfoOptionalData {"name", "192.168.56.1.2"};
const bool res = registration::RegisterAgent(userCredentials, agentInfoOptionalData);
const bool res = registration::RegisterAgent(userCredentials);
ASSERT_TRUE(res);
}

Expand Down

0 comments on commit 60d4e6d

Please sign in to comment.