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/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"
28 using namespace icinga;
30 REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
32 bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
34 if (request.RequestUrl->GetPath().size() != 3)
37 if (request.RequestMethod != "POST")
40 String actionName = request.RequestUrl->GetPath()[2];
42 ApiAction::Ptr action = ApiAction::GetByName(actionName);
45 HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist.");
51 const std::vector<String>& types = action->GetTypes();
52 std::vector<Value> objs;
54 String permission = "actions/" + actionName;
57 qd.Types = std::set<String>(types.begin(), types.end());
58 qd.Permission = permission;
61 objs = FilterUtility::GetFilterTargets(qd, params, user);
62 } catch (const std::exception& ex) {
63 HttpUtility::SendJsonError(response, params, 404,
65 HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
69 FilterUtility::CheckPermission(user, permission);
70 objs.emplace_back(nullptr);
75 Log(LogNotice, "ApiActionHandler")
76 << "Running action " << actionName;
78 for (const ConfigObject::Ptr& obj : objs) {
80 results.emplace_back(action->Invoke(obj, params));
81 } catch (const std::exception& ex) {
82 Dictionary::Ptr fail = new Dictionary({
84 { "status", "Action execution failed: '" + DiagnosticInformation(ex, false) + "'." }
87 if (HttpUtility::GetLastParameter(params, "verboseErrors"))
88 fail->Set("diagnostic information", DiagnosticInformation(ex));
90 results.emplace_back(std::move(fail));
94 Dictionary::Ptr result = new Dictionary({
95 { "results", new Array(std::move(results)) }
98 response.SetStatus(200, "OK");
99 HttpUtility::SendJsonBody(response, params, result);