]> granicus.if.org Git - icinga2/blob - lib/remote/templatequeryhandler.cpp
Remove unused includes
[icinga2] / lib / remote / templatequeryhandler.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
28 #include <set>
29
30 using namespace icinga;
31
32 REGISTER_URLHANDLER("/v1/templates", TemplateQueryHandler);
33
34 class TemplateTargetProvider final : public TargetProvider
35 {
36 public:
37         DECLARE_PTR_TYPEDEFS(TemplateTargetProvider);
38
39         static Dictionary::Ptr GetTargetForTemplate(const ConfigItem::Ptr& item)
40         {
41                 DebugInfo di = item->GetDebugInfo();
42
43                 return new Dictionary({
44                         { "name", item->GetName() },
45                         { "type", item->GetType()->GetName() },
46                         { "location", new Dictionary({
47                                 { "path", di.Path },
48                                 { "first_line", di.FirstLine },
49                                 { "first_column", di.FirstColumn },
50                                 { "last_line", di.LastLine },
51                                 { "last_column", di.LastColumn }
52                         }) }
53                 });
54         }
55
56         void FindTargets(const String& type,
57                 const std::function<void (const Value&)>& addTarget) const override
58         {
59                 Type::Ptr ptype = Type::GetByName(type);
60
61                 for (const ConfigItem::Ptr& item : ConfigItem::GetItems(ptype)) {
62                         if (item->IsAbstract())
63                                 addTarget(GetTargetForTemplate(item));
64                 }
65         }
66
67         Value GetTargetByName(const String& type, const String& name) const override
68         {
69                 Type::Ptr ptype = Type::GetByName(type);
70
71                 ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(ptype, name);
72
73                 if (!item || !item->IsAbstract())
74                         BOOST_THROW_EXCEPTION(std::invalid_argument("Template does not exist."));
75
76                 return GetTargetForTemplate(item);
77         }
78
79         bool IsValidType(const String& type) const override
80         {
81                 Type::Ptr ptype = Type::GetByName(type);
82
83                 if (!ptype)
84                         return false;
85
86                 return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
87         }
88
89         String GetPluralName(const String& type) const override
90         {
91                 return Type::GetByName(type)->GetPluralName();
92         }
93 };
94
95 bool TemplateQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
96 {
97         if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
98                 return false;
99
100         if (request.RequestMethod != "GET")
101                 return false;
102
103         Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
104
105         if (!type) {
106                 HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
107                 return true;
108         }
109
110         QueryDescription qd;
111         qd.Types.insert(type->GetName());
112         qd.Permission = "templates/query/" + type->GetName();
113         qd.Provider = new TemplateTargetProvider();
114
115         params->Set("type", type->GetName());
116
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]);
121         }
122
123         std::vector<Value> objs;
124
125         try {
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) : "");
131                 return true;
132         }
133
134         Dictionary::Ptr result = new Dictionary({
135                 { "results", new Array(std::move(objs)) }
136         });
137
138         response.SetStatus(200, "OK");
139         HttpUtility::SendJsonBody(response, params, result);
140
141         return true;
142 }