diff --git a/lib/cli/agentupdateconfigcommand.cpp b/lib/cli/agentupdateconfigcommand.cpp index 6f5e7d20e..ff5963b46 100644 --- a/lib/cli/agentupdateconfigcommand.cpp +++ b/lib/cli/agentupdateconfigcommand.cpp @@ -74,6 +74,7 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v BOOST_FOREACH(const Dictionary::Ptr& agent, AgentUtility::GetAgents()) { Dictionary::Ptr repository = agent->Get("repository"); String zone = agent->Get("zone"); + String endpoint = agent->Get("endpoint"); ObjectLock olock(repository); BOOST_FOREACH(const Dictionary::Pair& kv, repository) { @@ -107,6 +108,35 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v } } } + + /* write a new zone and endpoint for the agent */ + Dictionary::Ptr endpoint_attrs = make_shared(); + + Dictionary::Ptr settings = agent->Get("settings"); + + if (settings) { + if (settings->Contains("host")) + endpoint_attrs->Set("host", settings->Get("host")); + if (settings->Contains("port")) + endpoint_attrs->Set("port", settings->Get("port")); + } + + if (!RepositoryUtility::AddObject(endpoint, "Endpoint", endpoint_attrs)) { + Log(LogCritical, "cli") + << "Cannot add agent endpoint '" << endpoint << "' to the config repository!\n"; + } + + Dictionary::Ptr zone_attrs = make_shared(); + Array::Ptr zone_members = make_shared(); + + zone_members->Add(endpoint); + zone_attrs->Set("endpoints", zone_members); + zone_attrs->Set("parent", agent->Get("parent_zone")); + + if (!RepositoryUtility::AddObject(zone, "Zone", zone_attrs)) { + Log(LogCritical, "cli") + << "Cannot add agent zone '" << zone << "' to the config repository!\n"; + } } Log(LogInformation, "cli", "Committing agent configuration."); diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp index f8ff2437f..a77d05336 100644 --- a/lib/cli/repositoryutility.cpp +++ b/lib/cli/repositoryutility.cpp @@ -84,7 +84,7 @@ String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, cons path += "hosts/" + object->Get("host_name"); else if (type == "Zone") path += "zones"; - else if (type == "Endpoints") + else if (type == "Endpoint") path += "endpoints"; return path; @@ -181,7 +181,7 @@ void RepositoryUtility::PrintChangeLog(std::ostream& fp) bool RepositoryUtility::AddObject(const String& name, const String& type, const Dictionary::Ptr& attrs) { /* add a new changelog entry by timestamp */ - String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast(Utility::GetTime())) + "-" + SHA256(name) + ".change"; + String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(Utility::GetTime()) + "-" + type + "-" + SHA256(name) + ".change"; Dictionary::Ptr change = make_shared(); @@ -197,7 +197,7 @@ bool RepositoryUtility::AddObject(const String& name, const String& type, const bool RepositoryUtility::RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attrs) { /* add a new changelog entry by timestamp */ - String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast(Utility::GetTime())) + "-" + SHA256(name) + ".change"; + String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(Utility::GetTime()) + "-" + type + "-" + SHA256(name) + ".change"; Dictionary::Ptr change = make_shared();