]> granicus.if.org Git - icinga2/blob - lib/remote/deleteobjecthandler.cpp
Remove unused includes
[icinga2] / lib / remote / deleteobjecthandler.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/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>
28 #include <set>
29
30 using namespace icinga;
31
32 REGISTER_URLHANDLER("/v1/objects", DeleteObjectHandler);
33
34 bool DeleteObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
35 {
36         if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
37                 return false;
38
39         if (request.RequestMethod != "DELETE")
40                 return false;
41
42         Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
43
44         if (!type) {
45                 HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
46                 return true;
47         }
48
49         QueryDescription qd;
50         qd.Types.insert(type->GetName());
51         qd.Permission = "objects/delete/" + type->GetName();
52
53         params->Set("type", type->GetName());
54
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]);
59         }
60
61         std::vector<Value> objs;
62
63         try {
64                 objs = FilterUtility::GetFilterTargets(qd, params, user);
65         } catch (const std::exception& ex) {
66                 HttpUtility::SendJsonError(response, params, 404,
67                         "No objects found.",
68                         HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
69                 return true;
70         }
71
72         bool cascade = HttpUtility::GetLastParameter(params, "cascade");
73
74         ArrayData results;
75
76         bool success = true;
77
78         for (const ConfigObject::Ptr& obj : objs) {
79                 int code;
80                 String status;
81                 Array::Ptr errors = new Array();
82
83                 if (!ConfigObjectUtility::DeleteObject(obj, cascade, errors)) {
84                         code = 500;
85                         status = "Object could not be deleted.";
86                         success = false;
87                 } else {
88                         code = 200;
89                         status = "Object was deleted.";
90                 }
91
92                 results.push_back(new Dictionary({
93                         { "type", type->GetName() },
94                         { "name", obj->GetName() },
95                         { "code", code },
96                         { "status", status },
97                         { "errors", errors }
98                 }));
99         }
100
101         Dictionary::Ptr result = new Dictionary({
102                 { "results", new Array(std::move(results)) }
103         });
104
105         if (!success)
106                 response.SetStatus(500, "One or more objects could not be deleted");
107         else
108                 response.SetStatus(200, "OK");
109
110         HttpUtility::SendJsonBody(response, params, result);
111
112         return true;
113 }
114