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/deleteobjecthandler.hpp"
21 #include "remote/configobjectutility.hpp"
22 #include "remote/httputility.hpp"
23 #include "remote/filterutility.hpp"
24 #include "remote/apiaction.hpp"
25 #include "config/configitem.hpp"
26 #include "base/exception.hpp"
27 #include <boost/algorithm/string/case_conv.hpp>
30 using namespace icinga;
32 REGISTER_URLHANDLER("/v1/objects", DeleteObjectHandler);
34 bool DeleteObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
36 if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
39 if (request.RequestMethod != "DELETE")
42 Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
45 HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
50 qd.Types.insert(type->GetName());
51 qd.Permission = "objects/delete/" + type->GetName();
53 params->Set("type", type->GetName());
55 if (request.RequestUrl->GetPath().size() >= 4) {
56 String attr = type->GetName();
57 boost::algorithm::to_lower(attr);
58 params->Set(attr, request.RequestUrl->GetPath()[3]);
61 std::vector<Value> objs;
64 objs = FilterUtility::GetFilterTargets(qd, params, user);
65 } catch (const std::exception& ex) {
66 HttpUtility::SendJsonError(response, params, 404,
68 HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
72 bool cascade = HttpUtility::GetLastParameter(params, "cascade");
78 for (const ConfigObject::Ptr& obj : objs) {
81 Array::Ptr errors = new Array();
83 if (!ConfigObjectUtility::DeleteObject(obj, cascade, errors)) {
85 status = "Object could not be deleted.";
89 status = "Object was deleted.";
92 results.push_back(new Dictionary({
93 { "type", type->GetName() },
94 { "name", obj->GetName() },
101 Dictionary::Ptr result = new Dictionary({
102 { "results", new Array(std::move(results)) }
106 response.SetStatus(500, "One or more objects could not be deleted");
108 response.SetStatus(200, "OK");
110 HttpUtility::SendJsonBody(response, params, result);