]> granicus.if.org Git - icinga2/blob - lib/remote/actionshandler.cpp
Remove unused includes
[icinga2] / lib / remote / actionshandler.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/actionshandler.hpp"
21 #include "remote/httputility.hpp"
22 #include "remote/filterutility.hpp"
23 #include "remote/apiaction.hpp"
24 #include "base/exception.hpp"
25 #include "base/logger.hpp"
26 #include <set>
27
28 using namespace icinga;
29
30 REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
31
32 bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
33 {
34         if (request.RequestUrl->GetPath().size() != 3)
35                 return false;
36
37         if (request.RequestMethod != "POST")
38                 return false;
39
40         String actionName = request.RequestUrl->GetPath()[2];
41
42         ApiAction::Ptr action = ApiAction::GetByName(actionName);
43
44         if (!action) {
45                 HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist.");
46                 return true;
47         }
48
49         QueryDescription qd;
50
51         const std::vector<String>& types = action->GetTypes();
52         std::vector<Value> objs;
53
54         String permission = "actions/" + actionName;
55
56         if (!types.empty()) {
57                 qd.Types = std::set<String>(types.begin(), types.end());
58                 qd.Permission = permission;
59
60                 try {
61                         objs = FilterUtility::GetFilterTargets(qd, params, user);
62                 } catch (const std::exception& ex) {
63                         HttpUtility::SendJsonError(response, params, 404,
64                                 "No objects found.",
65                                 HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
66                         return true;
67                 }
68         } else {
69                 FilterUtility::CheckPermission(user, permission);
70                 objs.emplace_back(nullptr);
71         }
72
73         ArrayData results;
74
75         Log(LogNotice, "ApiActionHandler")
76                 << "Running action " << actionName;
77
78         for (const ConfigObject::Ptr& obj : objs) {
79                 try {
80                         results.emplace_back(action->Invoke(obj, params));
81                 } catch (const std::exception& ex) {
82                         Dictionary::Ptr fail = new Dictionary({
83                                 { "code", 500 },
84                                 { "status", "Action execution failed: '" + DiagnosticInformation(ex, false) + "'." }
85                         });
86
87                         if (HttpUtility::GetLastParameter(params, "verboseErrors"))
88                                 fail->Set("diagnostic information", DiagnosticInformation(ex));
89
90                         results.emplace_back(std::move(fail));
91                 }
92         }
93
94         Dictionary::Ptr result = new Dictionary({
95                 { "results", new Array(std::move(results)) }
96         });
97
98         response.SetStatus(200, "OK");
99         HttpUtility::SendJsonBody(response, params, result);
100
101         return true;
102 }