2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2016-06-15 07:37:03 +02:00
|
|
|
|
|
|
|
#include "remote/templatequeryhandler.hpp"
|
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
#include "config/configitem.hpp"
|
|
|
|
#include "base/configtype.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
#include "base/logger.hpp"
|
2018-01-04 18:24:45 +01:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2016-06-15 07:37:03 +02:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/templates", TemplateQueryHandler);
|
|
|
|
|
2018-01-04 06:11:04 +01:00
|
|
|
class TemplateTargetProvider final : public TargetProvider
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(TemplateTargetProvider);
|
|
|
|
|
2016-06-16 08:28:57 +02:00
|
|
|
static Dictionary::Ptr GetTargetForTemplate(const ConfigItem::Ptr& item)
|
|
|
|
{
|
2016-08-27 19:25:38 +02:00
|
|
|
DebugInfo di = item->GetDebugInfo();
|
2018-01-11 11:17:38 +01:00
|
|
|
|
|
|
|
return new Dictionary({
|
|
|
|
{ "name", item->GetName() },
|
|
|
|
{ "type", item->GetType()->GetName() },
|
|
|
|
{ "location", new Dictionary({
|
|
|
|
{ "path", di.Path },
|
|
|
|
{ "first_line", di.FirstLine },
|
|
|
|
{ "first_column", di.FirstColumn },
|
|
|
|
{ "last_line", di.LastLine },
|
|
|
|
{ "last_column", di.LastColumn }
|
|
|
|
}) }
|
|
|
|
});
|
2016-06-16 08:28:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
void FindTargets(const String& type,
|
2017-12-19 15:50:05 +01:00
|
|
|
const std::function<void (const Value&)>& addTarget) const override
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
2017-05-11 14:21:30 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
|
|
|
|
for (const ConfigItem::Ptr& item : ConfigItem::GetItems(ptype)) {
|
2016-06-16 08:28:57 +02:00
|
|
|
if (item->IsAbstract())
|
|
|
|
addTarget(GetTargetForTemplate(item));
|
2016-06-15 07:37:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
Value GetTargetByName(const String& type, const String& name) const override
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
2017-05-11 14:21:30 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
|
|
|
|
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(ptype, name);
|
2016-06-15 07:37:03 +02:00
|
|
|
|
|
|
|
if (!item || !item->IsAbstract())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Template does not exist."));
|
|
|
|
|
2016-06-16 08:28:57 +02:00
|
|
|
return GetTargetForTemplate(item);
|
2016-06-15 07:37:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
bool IsValidType(const String& type) const override
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
|
|
|
|
if (!ptype)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
String GetPluralName(const String& type) const override
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
|
|
|
return Type::GetByName(type)->GetPluralName();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
bool TemplateQueryHandler::HandleRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-15 10:33:01 +01:00
|
|
|
const ApiUser::Ptr& user,
|
|
|
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
|
|
|
const Url::Ptr& url,
|
|
|
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
2019-02-15 11:51:12 +01:00
|
|
|
const Dictionary::Ptr& params,
|
|
|
|
boost::asio::yield_context& yc,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-15 10:33:01 +01:00
|
|
|
)
|
2016-06-15 07:37:03 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
|
|
if (url->GetPath().size() < 3 || url->GetPath().size() > 4)
|
2016-06-15 07:37:03 +02:00
|
|
|
return false;
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (request.method() != http::verb::get)
|
2016-06-15 07:37:03 +02:00
|
|
|
return false;
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
Type::Ptr type = FilterUtility::TypeFromPluralName(url->GetPath()[2]);
|
2016-06-15 07:37:03 +02:00
|
|
|
|
|
|
|
if (!type) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
|
2016-06-15 07:37:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryDescription qd;
|
|
|
|
qd.Types.insert(type->GetName());
|
|
|
|
qd.Permission = "templates/query/" + type->GetName();
|
|
|
|
qd.Provider = new TemplateTargetProvider();
|
|
|
|
|
|
|
|
params->Set("type", type->GetName());
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (url->GetPath().size() >= 4) {
|
2016-06-15 07:37:03 +02:00
|
|
|
String attr = type->GetName();
|
|
|
|
boost::algorithm::to_lower(attr);
|
2019-02-15 10:33:01 +01:00
|
|
|
params->Set(attr, url->GetPath()[3]);
|
2016-06-15 07:37:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
try {
|
2016-06-16 08:28:57 +02:00
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user, "tmpl");
|
2016-06-15 07:37:03 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 15:50:05 +01:00
|
|
|
"No templates found.",
|
2018-04-06 12:50:06 +02:00
|
|
|
DiagnosticInformation(ex));
|
2016-06-15 07:37:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array(std::move(objs)) }
|
|
|
|
});
|
2016-06-15 07:37:03 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2016-06-15 07:37:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|