Implement support for retrieving a list of templates from the API

fixes #11941
This commit is contained in:
Gunnar Beutner 2016-06-15 07:37:03 +02:00
parent f7f976b962
commit ace6882b59
4 changed files with 200 additions and 2 deletions

View File

@ -626,6 +626,32 @@ Example for deleting the host object `example.localdomain`:
]
}
## <a id="icinga2-api-config-templates"></a> Config Templates
Provides methods to manage configuration templates:
* [querying templates](9-icinga2-api.md#icinga2-api-config-templates-query)
### <a id="icinga2-api-config-templates-query"></a> Querying Templates
You can request information about configuration templates by sending
a `GET` query to the `/v1/templates/<type>` URL endpoint. `<type` has
to be replaced with the plural name of the object type you are interested
in:
$ curl -k -s -u root:icinga 'https://localhost:5665/v1/templates/hosts'
A list of all available configuration types is available in the
[object types](6-object-types.md#object-types) chapter.
A [filter](9-icinga2-api.md#icinga2-api-filters) may be provided for this query type.
Instead of using a filter you can optionally specify the template name in the
URL path when querying a single object:
$ curl -k -s -u root:icinga 'https://localhost:5665/v1/templates/hosts/generic-host'
The result set contains the type and name of the template.
## <a id="icinga2-api-actions"></a> Actions

View File

@ -29,8 +29,8 @@ set(remote_SOURCES
endpoint.cpp endpoint.thpp eventshandler.cpp eventqueue.cpp filterutility.cpp
httpchunkedencoding.cpp httpclientconnection.cpp httpserverconnection.cpp httphandler.cpp httprequest.cpp httpresponse.cpp
httputility.cpp infohandler.cpp jsonrpc.cpp jsonrpcconnection.cpp jsonrpcconnection-heartbeat.cpp
messageorigin.cpp modifyobjecthandler.cpp statushandler.cpp objectqueryhandler.cpp typequeryhandler.cpp
url.cpp zone.cpp zone.thpp
messageorigin.cpp modifyobjecthandler.cpp statushandler.cpp objectqueryhandler.cpp templatequeryhandler.cpp
typequeryhandler.cpp url.cpp zone.cpp zone.thpp
)
if(ICINGA2_UNITY_BUILD)

View File

@ -0,0 +1,133 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://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/templatequeryhandler.hpp"
#include "remote/httputility.hpp"
#include "remote/filterutility.hpp"
#include "config/configitem.hpp"
#include "base/configtype.hpp"
#include "base/scriptglobal.hpp"
#include "base/logger.hpp"
#include <boost/algorithm/string.hpp>
#include <set>
using namespace icinga;
REGISTER_URLHANDLER("/v1/templates", TemplateQueryHandler);
class TemplateTargetProvider : public TargetProvider
{
public:
DECLARE_PTR_TYPEDEFS(TemplateTargetProvider);
virtual void FindTargets(const String& type,
const boost::function<void (const Value&)>& addTarget) const override
{
std::vector<ConfigItem::Ptr> targets = ConfigItem::GetItems(type);
BOOST_FOREACH(const ConfigItem::Ptr& target, targets) {
if (target->IsAbstract())
addTarget(target);
}
}
virtual Value GetTargetByName(const String& type, const String& name) const override
{
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(type, name);
if (!item || !item->IsAbstract())
BOOST_THROW_EXCEPTION(std::invalid_argument("Template does not exist."));
return item;
}
virtual bool IsValidType(const String& type) const override
{
Type::Ptr ptype = Type::GetByName(type);
if (!ptype)
return false;
return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
}
virtual String GetPluralName(const String& type) const override
{
return Type::GetByName(type)->GetPluralName();
}
};
bool TemplateQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
{
if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
return false;
if (request.RequestMethod != "GET")
return false;
Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
if (!type) {
HttpUtility::SendJsonError(response, 400, "Invalid type specified.");
return true;
}
QueryDescription qd;
qd.Types.insert(type->GetName());
qd.Permission = "templates/query/" + type->GetName();
qd.Provider = new TemplateTargetProvider();
params->Set("type", type->GetName());
if (request.RequestUrl->GetPath().size() >= 4) {
String attr = type->GetName();
boost::algorithm::to_lower(attr);
params->Set(attr, request.RequestUrl->GetPath()[3]);
}
std::vector<Value> objs;
try {
objs = FilterUtility::GetFilterTargets(qd, params, user);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, 404,
"No templates found.",
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
return true;
}
Array::Ptr results = new Array();
BOOST_FOREACH(const ConfigItem::Ptr& obj, objs) {
Dictionary::Ptr result1 = new Dictionary();
results->Add(result1);
result1->Set("type", obj->GetType());
result1->Set("name", obj->GetName());
}
Dictionary::Ptr result = new Dictionary();
result->Set("results", results);
response.SetStatus(200, "OK");
HttpUtility::SendJsonBody(response, result);
return true;
}

View File

@ -0,0 +1,39 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://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. *
******************************************************************************/
#ifndef TEMPLATEQUERYHANDLER_H
#define TEMPLATEQUERYHANDLER_H
#include "remote/httphandler.hpp"
namespace icinga
{
class I2_REMOTE_API TemplateQueryHandler : public HttpHandler
{
public:
DECLARE_PTR_TYPEDEFS(TemplateQueryHandler);
virtual bool HandleRequest(const ApiUser::Ptr& user, HttpRequest& request,
HttpResponse& response, const Dictionary::Ptr& params) override;
};
}
#endif /* TEMPLATEQUERYHANDLER_H */