]> granicus.if.org Git - icinga2/commitdiff
Fix: /v1/objects/<type> returns an HTTP error when there are no objects of that type
authorGunnar Beutner <gunnar@beutner.name>
Wed, 30 Sep 2015 11:26:19 +0000 (13:26 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Wed, 30 Sep 2015 11:26:19 +0000 (13:26 +0200)
fixes #10253

lib/remote/filterutility.cpp

index 2e3797aef1d64f6c2dd65974734a2c01842d1aa6..5070e2771f22eeb63e19eeeb9c89f17d51ca3f77 100644 (file)
@@ -34,15 +34,21 @@ Type::Ptr FilterUtility::TypeFromPluralName(const String& pluralName)
        String uname = pluralName;
        boost::algorithm::to_lower(uname);
 
-       BOOST_FOREACH(const ConfigType::Ptr& dtype, ConfigType::GetTypes()) {
-               Type::Ptr type = Type::GetByName(dtype->GetName());
-               ASSERT(type);
+       {
+               Dictionary::Ptr globals = ScriptGlobal::GetGlobals();
+               ObjectLock olock(globals);
+               BOOST_FOREACH(const Dictionary::Pair& kv, globals) {
+                       if (!kv.second.IsObjectType<Type>())
+                               continue;
+
+                       Type::Ptr type = kv.second;
 
-               String pname = type->GetPluralName();
-               boost::algorithm::to_lower(pname);
+                       String pname = type->GetPluralName();
+                       boost::algorithm::to_lower(pname);
 
-               if (uname == pname)
-                       return type;
+                       if (uname == pname)
+                               return type;
+               }
        }
 
        return Type::Ptr();
@@ -51,10 +57,11 @@ Type::Ptr FilterUtility::TypeFromPluralName(const String& pluralName)
 void ConfigObjectTargetProvider::FindTargets(const String& type, const boost::function<void (const Value&)>& addTarget) const
 {
        ConfigType::Ptr dtype = ConfigType::GetByName(type);
-       ASSERT(dtype);
 
-       BOOST_FOREACH(const ConfigObject::Ptr& object, dtype->GetObjects()) {
-               addTarget(object);
+       if (dtype) {
+               BOOST_FOREACH(const ConfigObject::Ptr& object, dtype->GetObjects()) {
+                       addTarget(object);
+               }
        }
 }
 
@@ -70,7 +77,12 @@ Value ConfigObjectTargetProvider::GetTargetByName(const String& type, const Stri
 
 bool ConfigObjectTargetProvider::IsValidType(const String& type) const
 {
-       return ConfigType::GetByName(type) != ConfigType::Ptr();
+       Type::Ptr ptype = Type::GetByName(type);
+
+       if (!ptype)
+               return false;
+
+       return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
 }
 
 String ConfigObjectTargetProvider::GetPluralName(const String& type) const