Cli: Allow to import multiple templates, drop zone argument

refs #7255
This commit is contained in:
Michael Friedrich 2014-10-23 20:42:56 +02:00
parent 27b15a5714
commit 1929e0c990
2 changed files with 24 additions and 9 deletions

View 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);

View 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") {