1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "remote/templatequeryhandler.hpp"
21 #include "remote/httputility.hpp"
22 #include "remote/filterutility.hpp"
23 #include "config/configitem.hpp"
24 #include "base/configtype.hpp"
25 #include "base/scriptglobal.hpp"
26 #include "base/logger.hpp"
27 #include <boost/algorithm/string/case_conv.hpp>
30 using namespace icinga;
32 REGISTER_URLHANDLER("/v1/templates", TemplateQueryHandler);
34 class TemplateTargetProvider final : public TargetProvider
37 DECLARE_PTR_TYPEDEFS(TemplateTargetProvider);
39 static Dictionary::Ptr GetTargetForTemplate(const ConfigItem::Ptr& item)
41 DebugInfo di = item->GetDebugInfo();
43 return new Dictionary({
44 { "name", item->GetName() },
45 { "type", item->GetType()->GetName() },
46 { "location", new Dictionary({
48 { "first_line", di.FirstLine },
49 { "first_column", di.FirstColumn },
50 { "last_line", di.LastLine },
51 { "last_column", di.LastColumn }
56 void FindTargets(const String& type,
57 const std::function<void (const Value&)>& addTarget) const override
59 Type::Ptr ptype = Type::GetByName(type);
61 for (const ConfigItem::Ptr& item : ConfigItem::GetItems(ptype)) {
62 if (item->IsAbstract())
63 addTarget(GetTargetForTemplate(item));
67 Value GetTargetByName(const String& type, const String& name) const override
69 Type::Ptr ptype = Type::GetByName(type);
71 ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(ptype, name);
73 if (!item || !item->IsAbstract())
74 BOOST_THROW_EXCEPTION(std::invalid_argument("Template does not exist."));
76 return GetTargetForTemplate(item);
79 bool IsValidType(const String& type) const override
81 Type::Ptr ptype = Type::GetByName(type);
86 return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
89 String GetPluralName(const String& type) const override
91 return Type::GetByName(type)->GetPluralName();
95 bool TemplateQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
97 if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
100 if (request.RequestMethod != "GET")
103 Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
106 HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
111 qd.Types.insert(type->GetName());
112 qd.Permission = "templates/query/" + type->GetName();
113 qd.Provider = new TemplateTargetProvider();
115 params->Set("type", type->GetName());
117 if (request.RequestUrl->GetPath().size() >= 4) {
118 String attr = type->GetName();
119 boost::algorithm::to_lower(attr);
120 params->Set(attr, request.RequestUrl->GetPath()[3]);
123 std::vector<Value> objs;
126 objs = FilterUtility::GetFilterTargets(qd, params, user, "tmpl");
127 } catch (const std::exception& ex) {
128 HttpUtility::SendJsonError(response, params, 404,
129 "No templates found.",
130 HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
134 Dictionary::Ptr result = new Dictionary({
135 { "results", new Array(std::move(objs)) }
138 response.SetStatus(200, "OK");
139 HttpUtility::SendJsonBody(response, params, result);