1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #include "remote/deleteobjecthandler.hpp"
4 #include "remote/configobjectutility.hpp"
5 #include "remote/httputility.hpp"
6 #include "remote/filterutility.hpp"
7 #include "remote/apiaction.hpp"
8 #include "config/configitem.hpp"
9 #include "base/exception.hpp"
10 #include <boost/algorithm/string/case_conv.hpp>
11 #include <set>
12 
13 using namespace icinga;
14 
15 REGISTER_URLHANDLER("/v1/objects", DeleteObjectHandler);
16 
HandleRequest(AsioTlsStream & stream,const ApiUser::Ptr & user,boost::beast::http::request<boost::beast::http::string_body> & request,const Url::Ptr & url,boost::beast::http::response<boost::beast::http::string_body> & response,const Dictionary::Ptr & params,boost::asio::yield_context & yc,HttpServerConnection & server)17 bool DeleteObjectHandler::HandleRequest(
18 	AsioTlsStream& stream,
19 	const ApiUser::Ptr& user,
20 	boost::beast::http::request<boost::beast::http::string_body>& request,
21 	const Url::Ptr& url,
22 	boost::beast::http::response<boost::beast::http::string_body>& response,
23 	const Dictionary::Ptr& params,
24 	boost::asio::yield_context& yc,
25 	HttpServerConnection& server
26 )
27 {
28 	namespace http = boost::beast::http;
29 
30 	if (url->GetPath().size() < 3 || url->GetPath().size() > 4)
31 		return false;
32 
33 	if (request.method() != http::verb::delete_)
34 		return false;
35 
36 	Type::Ptr type = FilterUtility::TypeFromPluralName(url->GetPath()[2]);
37 
38 	if (!type) {
39 		HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
40 		return true;
41 	}
42 
43 	QueryDescription qd;
44 	qd.Types.insert(type->GetName());
45 	qd.Permission = "objects/delete/" + type->GetName();
46 
47 	params->Set("type", type->GetName());
48 
49 	if (url->GetPath().size() >= 4) {
50 		String attr = type->GetName();
51 		boost::algorithm::to_lower(attr);
52 		params->Set(attr, url->GetPath()[3]);
53 	}
54 
55 	std::vector<Value> objs;
56 
57 	try {
58 		objs = FilterUtility::GetFilterTargets(qd, params, user);
59 	} catch (const std::exception& ex) {
60 		HttpUtility::SendJsonError(response, params, 404,
61 			"No objects found.",
62 			DiagnosticInformation(ex));
63 		return true;
64 	}
65 
66 	bool cascade = HttpUtility::GetLastParameter(params, "cascade");
67 	bool verbose = HttpUtility::GetLastParameter(params, "verbose");
68 
69 	ArrayData results;
70 
71 	bool success = true;
72 
73 	for (const ConfigObject::Ptr& obj : objs) {
74 		int code;
75 		String status;
76 		Array::Ptr errors = new Array();
77 		Array::Ptr diagnosticInformation = new Array();
78 
79 		if (!ConfigObjectUtility::DeleteObject(obj, cascade, errors, diagnosticInformation)) {
80 			code = 500;
81 			status = "Object could not be deleted.";
82 			success = false;
83 		} else {
84 			code = 200;
85 			status = "Object was deleted.";
86 		}
87 
88 		Dictionary::Ptr result = new Dictionary({
89 			{ "type", type->GetName() },
90 			{ "name", obj->GetName() },
91 			{ "code", code },
92 			{ "status", status },
93 			{ "errors", errors }
94 		});
95 
96 		if (verbose)
97 			result->Set("diagnostic_information", diagnosticInformation);
98 
99 		results.push_back(result);
100 	}
101 
102 	Dictionary::Ptr result = new Dictionary({
103 		{ "results", new Array(std::move(results)) }
104 	});
105 
106 	if (!success)
107 		response.result(http::status::internal_server_error);
108 	else
109 		response.result(http::status::ok);
110 
111 	HttpUtility::SendJsonBody(response, params, result);
112 
113 	return true;
114 }
115 
116