]> granicus.if.org Git - icinga2/commitdiff
Cli: Allow to import multiple templates, drop zone argument
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 23 Oct 2014 18:42:56 +0000 (20:42 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 23 Oct 2014 18:42:56 +0000 (20:42 +0200)
refs #7255

lib/cli/repositoryobjectcommand.cpp
lib/cli/repositoryutility.cpp

index d2524113a74c000cd92ddb181b9d248c81c433bd..9cb99d07773459b1530cdca6bc314eb5282d55d8 100644 (file)
@@ -121,9 +121,7 @@ void RepositoryObjectCommand::InitParameters(boost::program_options::options_des
 {
        visibleDesc.add_options()
                ("name", po::value<std::string>(), "The name of the object")
-               ("zone", po::value<std::string>(), "The name of the zone, e.g. the agent where this object is bound to")
-               ("template", po::value<std::string>(), "Import the defined template into the object. This template must be defined and included separately in Icinga 2")
-               ("name", po::value<std::string>(), "The name of the object");
+               ("template", po::value<std::vector<std::string> >(), "Import the defined template(s) into the object. Must be defined and included separately in Icinga 2");
 
        if (m_Type == "Service") {
                visibleDesc.add_options()
@@ -172,11 +170,16 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm
                            << "Cannot parse passed attributes for object '" << name << "': " << boost::algorithm::join(tokens, "=");
        }
 
-       if (vm.count("zone"))
-               attr->Set("zone", String(vm["zone"].as<std::string>()));
+       if (vm.count("template")) {
+               Array::Ptr templates = make_shared<Array>();
 
-       if (vm.count("template"))
-               attr->Set("templates", String(vm["template"].as<std::string>()));
+               BOOST_FOREACH(const String& tmpl, vm["template"].as<std::vector<std::string> >()) {
+                       templates->Add(tmpl);
+               }
+
+               if (templates->GetLength() > 0)
+                       attr->Set("templates", templates);
+       }
 
        if (m_Command == RepositoryCommandList) {
                RepositoryUtility::PrintObjects(std::cout, m_Type);
index 70177f1f7d9d174fee92a7f02b2a3ea8cd08d388..51872dc361571ace7222f9246773c4f8fcbcbbac 100644 (file)
@@ -43,6 +43,7 @@ String RepositoryUtility::GetRepositoryDPath(void)
 
 String RepositoryUtility::GetRepositoryDObjectsPath(const String& type, const String& hostname)
 {
+       //TODO find a better way to retrieve the objects path
        if (type == "Host")
                return GetRepositoryDPath() + "/hosts";
        else if (type == "Service")
@@ -371,8 +372,19 @@ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, co
 {
        fp << "object " << type << " \"" << name << "\" {\n";
 
-       if (object->Contains("templates"))
-               fp << "\t" << "import \"" << object->Get("templates") << "\"\n";
+       if (!object) {
+               fp << "}\n";
+               return;
+       }
+
+       if (object->Contains("templates")) {
+               Array::Ptr templates = object->Get("templates");
+
+               ObjectLock olock(templates);
+               BOOST_FOREACH(const String& tmpl, templates) {
+                       fp << "\t" << "import \"" << tmpl << "\"\n";
+               }
+       }
 
        BOOST_FOREACH(const Dictionary::Pair& kv, object) {
                if (kv.first == "templates") {