icinga2/lib/remote/actionshandler.cpp

121 lines
3.9 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2018-01-02 12:06:00 +01:00
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "remote/actionshandler.hpp"
#include "remote/httputility.hpp"
#include "remote/filterutility.hpp"
#include "remote/apiaction.hpp"
#include "base/exception.hpp"
#include "base/logger.hpp"
#include <set>
using namespace icinga;
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
{
2015-09-28 16:08:14 +02:00
if (request.RequestUrl->GetPath().size() != 3)
return false;
2015-09-28 16:08:14 +02:00
if (request.RequestMethod != "POST")
return false;
String actionName = request.RequestUrl->GetPath()[2];
ApiAction::Ptr action = ApiAction::GetByName(actionName);
if (!action) {
HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist.");
return true;
}
QueryDescription qd;
const std::vector<String>& types = action->GetTypes();
std::vector<Value> objs;
2015-09-28 08:57:25 +02:00
String permission = "actions/" + actionName;
if (!types.empty()) {
qd.Types = std::set<String>(types.begin(), types.end());
2015-09-28 08:57:25 +02:00
qd.Permission = permission;
try {
2015-09-28 08:57:25 +02:00
objs = FilterUtility::GetFilterTargets(qd, params, user);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, params, 404,
"No objects found.",
2018-04-06 10:30:27 +02:00
DiagnosticInformation(ex));
return true;
}
2015-09-28 08:57:25 +02:00
} else {
FilterUtility::CheckPermission(user, permission);
objs.emplace_back(nullptr);
2015-09-28 08:57:25 +02:00
}
ArrayData results;
Log(LogNotice, "ApiActionHandler")
<< "Running action " << actionName;
2018-04-06 10:30:27 +02:00
bool verbose = false;
if (params)
verbose = HttpUtility::GetLastParameter(params, "verbose");
for (const ConfigObject::Ptr& obj : objs) {
try {
results.emplace_back(action->Invoke(obj, params));
} catch (const std::exception& ex) {
Dictionary::Ptr fail = new Dictionary({
{ "code", 500 },
{ "status", "Action execution failed: '" + DiagnosticInformation(ex, false) + "'." }
});
2018-04-06 10:30:27 +02:00
/* Exception for actions. Normally we would handle this inside SendJsonError(). */
if (verbose)
fail->Set("diagnostic_information", DiagnosticInformation(ex));
results.emplace_back(std::move(fail));
}
}
2018-04-06 10:43:33 +02:00
int statusCode = 500;
2018-04-06 10:03:09 +02:00
String statusMessage = "No action executed successfully";
for (const Dictionary::Ptr& res : results) {
2018-04-06 10:03:09 +02:00
if (res->Contains("code") && res->Get("code") == 200) {
statusCode = 200;
statusMessage = "OK";
break;
}
}
2018-04-06 10:03:09 +02:00
response.SetStatus(statusCode, statusMessage);
Dictionary::Ptr result = new Dictionary({
{ "results", new Array(std::move(results)) }
});
HttpUtility::SendJsonBody(response, params, result);
return true;
}