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