2015-07-21 16:10:13 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/configstageshandler.hpp"
|
2015-08-28 17:58:29 +02:00
|
|
|
#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/application.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include <boost/foreach.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;
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/config/stages", ConfigStagesHandler);
|
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
bool ConfigStagesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
|
2015-07-21 16:10:13 +02:00
|
|
|
{
|
2015-09-28 16:08:14 +02:00
|
|
|
if (request.RequestUrl->GetPath().size() > 5)
|
|
|
|
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")
|
|
|
|
HandlePost(user, request, response);
|
|
|
|
else if (request.RequestMethod == "DELETE")
|
|
|
|
HandleDelete(user, request, response);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigStagesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
|
|
|
|
{
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/query");
|
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
Dictionary::Ptr params = HttpUtility::FetchRequestParameters(request);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (request.RequestUrl->GetPath().size() >= 5)
|
|
|
|
params->Set("stage", request.RequestUrl->GetPath()[4]);
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-29 13:39:58 +02:00
|
|
|
String stageName = HttpUtility::GetLastParameter(params, "stage");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName))
|
2015-09-28 16:08:14 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
|
2015-09-22 17:58:12 +02:00
|
|
|
|
|
|
|
if (!ConfigPackageUtility::ValidateName(stageName))
|
2015-09-28 16:08:14 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 400, "Invalid stage name.");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
Array::Ptr results = new Array();
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
std::vector<std::pair<String, bool> > paths = ConfigPackageUtility::GetFiles(packageName, stageName);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String prefixPath = ConfigPackageUtility::GetPackageDir() + "/" + packageName + "/" + stageName + "/";
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
typedef std::pair<String, bool> kv_pair;
|
|
|
|
BOOST_FOREACH(const kv_pair& kv, paths) {
|
|
|
|
Dictionary::Ptr stageInfo = new Dictionary();
|
|
|
|
stageInfo->Set("type", (kv.second ? "directory" : "file"));
|
|
|
|
stageInfo->Set("name", kv.first.SubStr(prefixPath.GetLength()));
|
|
|
|
results->Add(stageInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
|
|
|
result->Set("results", results);
|
|
|
|
|
|
|
|
response.SetStatus(200, "OK");
|
|
|
|
HttpUtility::SendJsonBody(response, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigStagesHandler::HandlePost(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
|
|
|
|
{
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
Dictionary::Ptr params = HttpUtility::FetchRequestParameters(request);
|
|
|
|
|
|
|
|
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-09-22 17:58:12 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName))
|
2015-09-28 16:08:14 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr files = params->Get("files");
|
|
|
|
|
|
|
|
String stageName;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!files)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Parameter 'files' must be specified."));
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
stageName = ConfigPackageUtility::CreateStage(packageName, files);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
/* validate the config. on success, activate stage and reload */
|
2015-08-28 17:58:29 +02:00
|
|
|
ConfigPackageUtility::AsyncTryActivateStage(packageName, stageName);
|
2015-07-21 16:10:13 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2015-09-22 17:58:12 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 500,
|
|
|
|
"Stage creation failed.",
|
2015-10-27 15:26:19 +01:00
|
|
|
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr result1 = new Dictionary();
|
|
|
|
|
2015-09-29 18:25:48 +02:00
|
|
|
result1->Set("package", packageName);
|
|
|
|
result1->Set("stage", stageName);
|
2015-09-22 17:58:12 +02:00
|
|
|
result1->Set("code", 200);
|
|
|
|
result1->Set("status", "Created stage.");
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigStagesHandler::HandleDelete(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
|
|
|
|
{
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
Dictionary::Ptr params = HttpUtility::FetchRequestParameters(request);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (request.RequestUrl->GetPath().size() >= 5)
|
|
|
|
params->Set("stage", request.RequestUrl->GetPath()[4]);
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-29 13:39:58 +02:00
|
|
|
String stageName = HttpUtility::GetLastParameter(params, "stage");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName))
|
2015-09-28 16:08:14 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(stageName))
|
2015-09-28 16:08:14 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 400, "Invalid stage name.");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
try {
|
2015-08-28 17:58:29 +02:00
|
|
|
ConfigPackageUtility::DeleteStage(packageName, stageName);
|
2015-07-21 16:10:13 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2015-09-22 17:58:12 +02:00
|
|
|
return HttpUtility::SendJsonError(response, 500,
|
|
|
|
"Failed to delete stage.",
|
2015-10-27 15:26:19 +01:00
|
|
|
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr result1 = new Dictionary();
|
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
result1->Set("code", 200);
|
2015-09-28 16:08:14 +02:00
|
|
|
result1->Set("status", "Stage deleted.");
|
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);
|
|
|
|
}
|
|
|
|
|