2015-07-21 10:10:13 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
2018-10-18 03:27:04 -04:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2015-07-21 10:10:13 -04:00
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
#include "remote/configpackageshandler.hpp"
|
|
|
|
|
#include "remote/configpackageutility.hpp"
|
2015-07-21 10:10:13 -04:00
|
|
|
#include "remote/httputility.hpp"
|
2015-09-28 02:57:25 -04:00
|
|
|
#include "remote/filterutility.hpp"
|
2015-07-21 10:10:13 -04:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
REGISTER_URLHANDLER("/v1/config/packages", ConfigPackagesHandler);
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2016-05-10 09:16:35 -04:00
|
|
|
bool ConfigPackagesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
|
2015-07-21 10:10:13 -04:00
|
|
|
{
|
2015-09-28 10:08:14 -04:00
|
|
|
if (request.RequestUrl->GetPath().size() > 4)
|
|
|
|
|
return false;
|
2015-07-28 07:57:59 -04:00
|
|
|
|
2015-07-21 10:10:13 -04:00
|
|
|
if (request.RequestMethod == "GET")
|
2017-12-20 09:31:05 -05:00
|
|
|
HandleGet(user, request, response, params);
|
2015-07-21 10:10:13 -04:00
|
|
|
else if (request.RequestMethod == "POST")
|
2016-05-10 09:16:35 -04:00
|
|
|
HandlePost(user, request, response, params);
|
2015-07-21 10:10:13 -04:00
|
|
|
else if (request.RequestMethod == "DELETE")
|
2016-05-10 09:16:35 -04:00
|
|
|
HandleDelete(user, request, response, params);
|
2015-07-21 10:10:13 -04:00
|
|
|
else
|
2015-09-28 10:08:14 -04:00
|
|
|
return false;
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-07-28 07:57:59 -04:00
|
|
|
return true;
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 09:31:05 -05:00
|
|
|
void ConfigPackagesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
|
2015-07-21 10:10:13 -04:00
|
|
|
{
|
2015-09-28 02:57:25 -04:00
|
|
|
FilterUtility::CheckPermission(user, "config/query");
|
|
|
|
|
|
2018-03-07 07:27:31 -05:00
|
|
|
std::vector<String> packages;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
packages = ConfigPackageUtility::GetPackages();
|
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Could not retrieve packages.",
|
2018-04-06 03:53:54 -04:00
|
|
|
DiagnosticInformation(ex));
|
2018-03-07 07:27:31 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
ArrayData results;
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2017-09-20 10:40:02 -04:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticMutex());
|
|
|
|
|
for (const String& package : packages) {
|
2018-01-11 05:17:38 -05:00
|
|
|
results.emplace_back(new Dictionary({
|
|
|
|
|
{ "name", package },
|
|
|
|
|
{ "stages", Array::FromVector(ConfigPackageUtility::GetStages(package)) },
|
|
|
|
|
{ "active-stage", ConfigPackageUtility::GetActiveStage(package) }
|
|
|
|
|
}));
|
2017-09-20 10:40:02 -04:00
|
|
|
}
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
|
});
|
2015-07-21 10:10:13 -04:00
|
|
|
|
|
|
|
|
response.SetStatus(200, "OK");
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-10 09:16:35 -04:00
|
|
|
void ConfigPackagesHandler::HandlePost(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
|
2015-07-21 10:10:13 -04:00
|
|
|
{
|
2015-09-28 02:57:25 -04:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
|
2015-07-21 10:10:13 -04:00
|
|
|
if (request.RequestUrl->GetPath().size() >= 4)
|
2015-08-28 11:58:29 -04:00
|
|
|
params->Set("package", request.RequestUrl->GetPath()[3]);
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName)) {
|
2018-04-05 11:21:14 -04:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid package name '" + packageName + "'.");
|
2015-07-21 10:10:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2017-09-20 10:40:02 -04:00
|
|
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticMutex());
|
2015-08-28 11:58:29 -04:00
|
|
|
ConfigPackageUtility::CreatePackage(packageName);
|
2015-07-21 10:10:13 -04:00
|
|
|
} catch (const std::exception& ex) {
|
2018-04-05 11:21:14 -04:00
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Could not create package '" + packageName + "'.",
|
2018-04-06 03:53:54 -04:00
|
|
|
DiagnosticInformation(ex));
|
2017-12-18 05:04:40 -05:00
|
|
|
return;
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result1 = new Dictionary({
|
|
|
|
|
{ "code", 200 },
|
2018-04-06 03:53:54 -04:00
|
|
|
{ "package", packageName },
|
2018-01-11 05:17:38 -05:00
|
|
|
{ "status", "Created package." }
|
|
|
|
|
});
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
|
{ "results", new Array({ result1 }) }
|
|
|
|
|
});
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-09-22 11:58:12 -04:00
|
|
|
response.SetStatus(200, "OK");
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-10 09:16:35 -04:00
|
|
|
void ConfigPackagesHandler::HandleDelete(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
|
2015-07-21 10:10:13 -04:00
|
|
|
{
|
2015-09-28 02:57:25 -04:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
|
2015-07-21 10:10:13 -04:00
|
|
|
if (request.RequestUrl->GetPath().size() >= 4)
|
2015-08-28 11:58:29 -04:00
|
|
|
params->Set("package", request.RequestUrl->GetPath()[3]);
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-08-28 11:58:29 -04:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName)) {
|
2018-04-05 11:21:14 -04:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid package name '" + packageName + "'.");
|
2015-07-21 10:10:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2015-08-28 11:58:29 -04:00
|
|
|
ConfigPackageUtility::DeletePackage(packageName);
|
2015-07-21 10:10:13 -04:00
|
|
|
} catch (const std::exception& ex) {
|
2018-04-06 03:53:54 -04:00
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Failed to delete package '" + packageName + "'.",
|
|
|
|
|
DiagnosticInformation(ex));
|
2018-04-12 13:24:08 -04:00
|
|
|
return;
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-06 03:53:54 -04:00
|
|
|
Dictionary::Ptr result1 = new Dictionary({
|
|
|
|
|
{ "code", 200 },
|
|
|
|
|
{ "package", packageName },
|
2018-04-12 13:24:08 -04:00
|
|
|
{ "status", "Deleted package." }
|
2018-04-06 03:53:54 -04:00
|
|
|
});
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
2018-04-06 03:53:54 -04:00
|
|
|
{ "results", new Array({ result1 }) }
|
2018-01-11 05:17:38 -05:00
|
|
|
});
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2018-04-06 03:53:54 -04:00
|
|
|
response.SetStatus(200, "OK");
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|