mirror of https://github.com/Icinga/icinga2.git
Cli: Fix repository <type> list not showing objects
Still requires parsing of config objects into printable dictionaries. refs #7255
This commit is contained in:
parent
05d642f69d
commit
d9c8252f17
|
@ -146,6 +146,12 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm
|
||||||
{
|
{
|
||||||
Dictionary::Ptr attrs = RepositoryUtility::GetArgumentAttributes(ap);
|
Dictionary::Ptr attrs = RepositoryUtility::GetArgumentAttributes(ap);
|
||||||
|
|
||||||
|
/* shortcut for list command */
|
||||||
|
if (m_Command == RepositoryCommandList) {
|
||||||
|
RepositoryUtility::PrintObjects(std::cout, m_Type);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!attrs->Contains("name")) {
|
if (!attrs->Contains("name")) {
|
||||||
Log(LogCritical, "cli", "Object requires a name (Hint: 'name=<objectname>')!");
|
Log(LogCritical, "cli", "Object requires a name (Hint: 'name=<objectname>')!");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -172,10 +178,8 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm
|
||||||
attrs->Set("import", imports);
|
attrs->Set("import", imports);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_Command == RepositoryCommandList) {
|
|
||||||
RepositoryUtility::PrintObjects(std::cout, m_Type);
|
if (m_Command == RepositoryCommandAdd) {
|
||||||
}
|
|
||||||
else if (m_Command == RepositoryCommandAdd) {
|
|
||||||
RepositoryUtility::AddObject(name, m_Type, attrs);
|
RepositoryUtility::AddObject(name, m_Type, attrs);
|
||||||
}
|
}
|
||||||
else if (m_Command == RepositoryCommandRemove) {
|
else if (m_Command == RepositoryCommandRemove) {
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include <boost/algorithm/string/replace.hpp>
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
#include <boost/algorithm/string/split.hpp>
|
#include <boost/algorithm/string/split.hpp>
|
||||||
#include <boost/algorithm/string/classification.hpp>
|
#include <boost/algorithm/string/classification.hpp>
|
||||||
|
#include <boost/regex.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -65,9 +66,8 @@ String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, cons
|
||||||
{
|
{
|
||||||
String path = GetRepositoryConfigPath() + "/";
|
String path = GetRepositoryConfigPath() + "/";
|
||||||
|
|
||||||
if (type == "Host") {
|
if (type == "Host")
|
||||||
path += "hosts";
|
path += "hosts";
|
||||||
}
|
|
||||||
else if (type == "Service")
|
else if (type == "Service")
|
||||||
path += "hosts/" + object->Get("host_name");
|
path += "hosts/" + object->Get("host_name");
|
||||||
else if (type == "Zone")
|
else if (type == "Zone")
|
||||||
|
@ -78,6 +78,23 @@ String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, cons
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool RepositoryUtility::FilterRepositoryObjects(const String& type, const String& path)
|
||||||
|
{
|
||||||
|
if (type == "Host") {
|
||||||
|
boost::regex expr("hosts/[^/]*.conf", boost::regex::icase);
|
||||||
|
boost::smatch what;
|
||||||
|
return boost::regex_search(path.GetData(), what, expr);
|
||||||
|
}
|
||||||
|
else if (type == "Service")
|
||||||
|
return Utility::Match("*hosts/*/*.conf", path);
|
||||||
|
else if (type == "Zone")
|
||||||
|
return Utility::Match("*zones/*.conf", path);
|
||||||
|
else if (type == "Endpoints")
|
||||||
|
return Utility::Match("*endpoints/*.conf", path);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
String RepositoryUtility::GetRepositoryObjectConfigFilePath(const String& type, const Dictionary::Ptr& object)
|
String RepositoryUtility::GetRepositoryObjectConfigFilePath(const String& type, const Dictionary::Ptr& object)
|
||||||
{
|
{
|
||||||
String path = GetRepositoryObjectConfigPath(type, object);
|
String path = GetRepositoryObjectConfigPath(type, object);
|
||||||
|
@ -97,12 +114,18 @@ void RepositoryUtility::PrintObjects(std::ostream& fp, const String& type)
|
||||||
std::vector<String> objects = GetObjects(); //full path
|
std::vector<String> objects = GetObjects(); //full path
|
||||||
|
|
||||||
BOOST_FOREACH(const String& object, objects) {
|
BOOST_FOREACH(const String& object, objects) {
|
||||||
Dictionary::Ptr obj = GetObjectFromRepository(object);
|
if (!FilterRepositoryObjects(type, object)) {
|
||||||
|
Log(LogDebug, "cli")
|
||||||
if (obj) {
|
<< "Ignoring object '" << object << "'. Type '" << type << "' does not match.";
|
||||||
fp << "Object Name: " << object << "\n";
|
continue;
|
||||||
fp << JsonEncode(obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fp << "Object Path: " << object << "\n";
|
||||||
|
|
||||||
|
Dictionary::Ptr obj = GetObjectFromRepository(object); //TODO: config parser not implemented yet!
|
||||||
|
|
||||||
|
if (obj)
|
||||||
|
fp << JsonEncode(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +226,7 @@ bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const Str
|
||||||
//Fixme
|
//Fixme
|
||||||
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
|
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
|
||||||
|
|
||||||
Dictionary::Ptr obj = GetObjectFromRepository(path);
|
Dictionary::Ptr obj = GetObjectFromRepository(path); //TODO
|
||||||
|
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
Log(LogCritical, "cli")
|
Log(LogCritical, "cli")
|
||||||
|
@ -305,9 +328,9 @@ Dictionary::Ptr RepositoryUtility::GetObjectFromRepositoryChangeLog(const String
|
||||||
std::vector<String> RepositoryUtility::GetObjects(void)
|
std::vector<String> RepositoryUtility::GetObjects(void)
|
||||||
{
|
{
|
||||||
std::vector<String> objects;
|
std::vector<String> objects;
|
||||||
String path = GetRepositoryConfigPath() + "/";
|
String path = GetRepositoryConfigPath();
|
||||||
|
|
||||||
Utility::Glob(path + "/*.conf",
|
Utility::GlobRecursive(path, "*.conf",
|
||||||
boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(objects)), GlobFile);
|
boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(objects)), GlobFile);
|
||||||
|
|
||||||
return objects;
|
return objects;
|
||||||
|
|
|
@ -44,6 +44,8 @@ public:
|
||||||
|
|
||||||
static String GetRepositoryChangeLogPath(void);
|
static String GetRepositoryChangeLogPath(void);
|
||||||
|
|
||||||
|
static bool FilterRepositoryObjects(const String& type, const String& path);
|
||||||
|
|
||||||
static void PrintObjects(std::ostream& fp, const String& type);
|
static void PrintObjects(std::ostream& fp, const String& type);
|
||||||
|
|
||||||
static void PrintChangeLog(std::ostream& fp);
|
static void PrintChangeLog(std::ostream& fp);
|
||||||
|
|
Loading…
Reference in New Issue