From 35c75d95b5772099de99810d7679ebc3ec7665a1 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 24 Oct 2014 12:42:57 +0200 Subject: [PATCH] Cli: Parse repository arguments as 'name=...' Require 'host_name' for service objects Rename --template to --import refs #7255 --- lib/cli/repositoryobjectcommand.cpp | 56 +++++++++++------------------ lib/cli/repositoryutility.cpp | 32 +++++++++++++---- lib/cli/repositoryutility.hpp | 1 + 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/lib/cli/repositoryobjectcommand.cpp b/lib/cli/repositoryobjectcommand.cpp index 65e1b2b11..12311a619 100644 --- a/lib/cli/repositoryobjectcommand.cpp +++ b/lib/cli/repositoryobjectcommand.cpp @@ -24,8 +24,6 @@ #include #include #include -#include -#include #include #include @@ -121,13 +119,7 @@ void RepositoryObjectCommand::InitParameters(boost::program_options::options_des boost::program_options::options_description& hiddenDesc) const { visibleDesc.add_options() - ("name", po::value(), "The name of the object") - ("template", po::value >(), "Import the defined template(s) into the object. Must be defined and included separately in Icinga 2"); - - if (m_Type == "Service") { - visibleDesc.add_options() - ("host", po::value(), "The host name related to this service object"); - } + ("import", po::value >(), "Import the defined template(s) into the object. Must be defined and included separately in Icinga 2"); } std::vector RepositoryObjectCommand::GetPositionalSuggestions(const String& word) const @@ -147,53 +139,47 @@ std::vector RepositoryObjectCommand::GetPositionalSuggestions(const Stri */ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm, const std::vector& ap) const { - if (ap.empty()) { - Log(LogCritical, "cli") - << "No object name given. Bailing out."; - return 1; - } - String name = ap[0]; + Dictionary::Ptr attrs = RepositoryUtility::GetArgumentAttributes(ap); - std::vector tokens; - Dictionary::Ptr attr = make_shared(); - - std::vector attrs = ap; - attrs.erase(attrs.begin()); //remove name + if (!attrs->Contains("name")) { + Log(LogCritical, "cli", "Object requires a name (Hint: 'name=')!"); + return 1; + } - BOOST_FOREACH(const String& kv, attrs) { - boost::algorithm::split(tokens, kv, boost::is_any_of("=")); + String name = attrs->Get("name"); - if (tokens.size() == 2) { - attr->Set(tokens[0], tokens[1]); - } else - Log(LogWarning, "cli") - << "Cannot parse passed attributes for object '" << name << "': " << boost::algorithm::join(tokens, "="); + if (m_Type == "Service") { + if (!attrs->Contains("host_name")) { + Log(LogCritical, "cli", "Service objects require the 'host_name' attribute."); + return 1; + } } - if (vm.count("template")) { - Array::Ptr templates = make_shared(); + if (vm.count("import")) { + Array::Ptr imports = make_shared(); - BOOST_FOREACH(const String& tmpl, vm["template"].as >()) { - templates->Add(tmpl); + BOOST_FOREACH(const String& import, vm["import"].as >()) { + imports->Add(import); } - if (templates->GetLength() > 0) - attr->Set("templates", templates); + //Update object attributes + if (imports->GetLength() > 0) + attrs->Set("import", imports); } if (m_Command == RepositoryCommandList) { RepositoryUtility::PrintObjects(std::cout, m_Type); } else if (m_Command == RepositoryCommandAdd) { - RepositoryUtility::AddObject(name, m_Type, attr); + RepositoryUtility::AddObject(name, m_Type, attrs); } else if (m_Command == RepositoryCommandRemove) { RepositoryUtility::RemoveObject(name, m_Type); } else if (m_Command == RepositoryCommandSet) { Log(LogWarning, "cli") - << "Not implemented yet.\n"; + << "Not supported yet. Please check the roadmap at https://dev.icinga.org\n"; return 1; } else { Log(LogCritical, "cli") diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp index 51872dc36..1c8f2e7f8 100644 --- a/lib/cli/repositoryutility.cpp +++ b/lib/cli/repositoryutility.cpp @@ -31,11 +31,31 @@ #include #include #include +#include +#include #include #include using namespace icinga; +Dictionary::Ptr RepositoryUtility::GetArgumentAttributes(const std::vector& arguments) +{ + Dictionary::Ptr attr = make_shared(); + + BOOST_FOREACH(const String& kv, arguments) { + std::vector tokens; + boost::algorithm::split(tokens, kv, boost::is_any_of("=")); + + if (tokens.size() == 2) { + attr->Set(tokens[0], tokens[1]); + } else + Log(LogWarning, "cli") + << "Cannot parse passed attributes: " << boost::algorithm::join(tokens, "="); + } + + return attr; +} + String RepositoryUtility::GetRepositoryDPath(void) { return Application::GetSysconfDir() + "/icinga2/repository.d"; @@ -377,17 +397,17 @@ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, co return; } - if (object->Contains("templates")) { - Array::Ptr templates = object->Get("templates"); + if (object->Contains("import")) { + Array::Ptr imports = object->Get("import"); - ObjectLock olock(templates); - BOOST_FOREACH(const String& tmpl, templates) { - fp << "\t" << "import \"" << tmpl << "\"\n"; + ObjectLock olock(imports); + BOOST_FOREACH(const String& import, imports) { + fp << "\t" << "import \"" << import << "\"\n"; } } BOOST_FOREACH(const Dictionary::Pair& kv, object) { - if (kv.first == "templates") { + if (kv.first == "import") { continue; } else { fp << "\t" << kv.first << " = "; diff --git a/lib/cli/repositoryutility.hpp b/lib/cli/repositoryutility.hpp index ebdfcb282..45d56ae70 100644 --- a/lib/cli/repositoryutility.hpp +++ b/lib/cli/repositoryutility.hpp @@ -36,6 +36,7 @@ namespace icinga class RepositoryUtility { public: + static Dictionary::Ptr GetArgumentAttributes(const std::vector& arguments); static String GetRepositoryDPath(void); static String GetRepositoryDObjectsPath(const String& type, const String& hostname = Empty); static String GetRepositoryChangeLogPath(void); -- 2.40.0