2015-07-28 13:57:59 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/filterutility.hpp"
|
2015-07-29 13:39:58 +02:00
|
|
|
#include "remote/httputility.hpp"
|
2015-07-28 13:57:59 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include "config/expression.hpp"
|
|
|
|
#include "base/json.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2015-07-29 13:39:58 +02:00
|
|
|
#include "base/logger.hpp"
|
2015-07-28 13:57:59 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
Type::Ptr FilterUtility::TypeFromPluralName(const String& pluralName)
|
|
|
|
{
|
|
|
|
String uname = pluralName;
|
|
|
|
boost::algorithm::to_lower(uname);
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const ConfigType::Ptr& dtype, ConfigType::GetTypes()) {
|
2015-07-28 13:57:59 +02:00
|
|
|
Type::Ptr type = Type::GetByName(dtype->GetName());
|
|
|
|
ASSERT(type);
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
String pname = type->GetPluralName();
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(pname);
|
|
|
|
|
|
|
|
if (uname == pname)
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::Ptr();
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
void ConfigObjectTargetProvider::FindTargets(const String& type, const boost::function<void (const Value&)>& addTarget) const
|
2015-07-28 13:57:59 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigType::Ptr dtype = ConfigType::GetByName(type);
|
2015-07-28 13:57:59 +02:00
|
|
|
ASSERT(dtype);
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
BOOST_FOREACH(const ConfigObject::Ptr& object, dtype->GetObjects()) {
|
|
|
|
addTarget(object);
|
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
Value ConfigObjectTargetProvider::GetTargetByName(const String& type, const String& name) const
|
2015-07-28 13:57:59 +02:00
|
|
|
{
|
2015-08-26 10:58:59 +02:00
|
|
|
ConfigObject::Ptr obj = ConfigObject::GetObject(type, name);
|
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Object does not exist."));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObjectTargetProvider::IsValidType(const String& type) const
|
|
|
|
{
|
|
|
|
return ConfigType::GetByName(type) != ConfigType::Ptr();
|
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
String ConfigObjectTargetProvider::GetPluralName(const String& type) const
|
|
|
|
{
|
|
|
|
return Type::GetByName(type)->GetPluralName();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FilteredAddTarget(ScriptFrame& frame, const String& varName, Expression *ufilter, std::vector<Value>& result, const Value& target)
|
|
|
|
{
|
|
|
|
frame.Locals->Set(varName, target);
|
|
|
|
|
|
|
|
if (Convert::ToBool(ufilter->Evaluate(frame)))
|
|
|
|
result.push_back(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Value> FilterUtility::GetFilterTargets(const QueryDescription& qd, const Dictionary::Ptr& query)
|
|
|
|
{
|
|
|
|
std::vector<Value> result;
|
|
|
|
|
|
|
|
TargetProvider::Ptr provider;
|
|
|
|
|
|
|
|
if (qd.Provider)
|
|
|
|
provider = qd.Provider;
|
|
|
|
else
|
|
|
|
provider = new ConfigObjectTargetProvider();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& type, qd.Types) {
|
|
|
|
String attr = type;
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(attr);
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (attr == "type")
|
|
|
|
attr = "name";
|
|
|
|
|
|
|
|
if (query->Contains(attr))
|
|
|
|
result.push_back(provider->GetTargetByName(type, query->Get(attr)));
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
attr = provider->GetPluralName(type);
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(attr);
|
|
|
|
|
|
|
|
if (query->Contains(attr)) {
|
|
|
|
Array::Ptr names = query->Get(attr);
|
2015-07-30 08:30:17 +02:00
|
|
|
if (names) {
|
|
|
|
ObjectLock olock(names);
|
|
|
|
BOOST_FOREACH(const String& name, names) {
|
2015-08-26 10:58:59 +02:00
|
|
|
result.push_back(provider->GetTargetByName(type, name));
|
2015-07-30 08:30:17 +02:00
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:30:17 +02:00
|
|
|
if (query->Contains("filter") || result.empty()) {
|
2015-07-28 13:57:59 +02:00
|
|
|
if (!query->Contains("type"))
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Type must be specified when using a filter."));
|
|
|
|
|
2015-07-30 08:30:17 +02:00
|
|
|
String filter;
|
|
|
|
if (!query->Contains("filter"))
|
|
|
|
filter = "true";
|
|
|
|
else
|
|
|
|
filter = HttpUtility::GetLastParameter(query, "filter");
|
|
|
|
|
2015-07-29 13:39:58 +02:00
|
|
|
String type = HttpUtility::GetLastParameter(query, "type");
|
|
|
|
|
|
|
|
Log(LogInformation, "FilterUtility", filter);
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (!provider->IsValidType(type))
|
2015-07-28 13:57:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid type specified."));
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (qd.Types.find(type) == qd.Types.end())
|
2015-07-28 13:57:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid type specified for this query."));
|
|
|
|
|
2015-08-26 06:57:24 +02:00
|
|
|
Expression *ufilter = ConfigCompiler::CompileText("<API query>", filter);
|
2015-07-28 13:57:59 +02:00
|
|
|
ScriptFrame frame;
|
|
|
|
frame.Sandboxed = true;
|
|
|
|
|
2015-08-18 06:47:07 +02:00
|
|
|
Dictionary::Ptr filter_vars = query->Get("filter_vars");
|
|
|
|
if (filter_vars) {
|
|
|
|
ObjectLock olock(filter_vars);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, filter_vars) {
|
|
|
|
frame.Locals->Set(kv.first, kv.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
String varName = type;
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(varName);
|
|
|
|
|
|
|
|
try {
|
2015-08-26 10:58:59 +02:00
|
|
|
provider->FindTargets(type, boost::bind(&FilteredAddTarget, boost::ref(frame), varName, ufilter, boost::ref(result), _1));
|
2015-07-28 13:57:59 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
delete ufilter;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete ufilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|