]> granicus.if.org Git - icinga2/commitdiff
Implement support for retrieving a list of templates from the API
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 15 Jun 2016 05:37:03 +0000 (07:37 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 15 Jun 2016 05:41:51 +0000 (07:41 +0200)
fixes #11941

doc/9-icinga2-api.md
lib/remote/CMakeLists.txt
lib/remote/templatequeryhandler.cpp [new file with mode: 0644]
lib/remote/templatequeryhandler.hpp [new file with mode: 0644]

index 9ee4afea1ef5e397249f3093cffccc9ff928512b..7b12e34d687bb5b99cef247658640ac506014ba0 100644 (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
 
index 2afd437fc23f829d33361edab8758ad6b787b927..521186c83d744cd21334e28c21bd3b51172551d4 100644 (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)
diff --git a/lib/remote/templatequeryhandler.cpp b/lib/remote/templatequeryhandler.cpp
new file mode 100644 (file)
index 0000000..5e59c29
--- /dev/null
@@ -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;
+}
+
diff --git a/lib/remote/templatequeryhandler.hpp b/lib/remote/templatequeryhandler.hpp
new file mode 100644 (file)
index 0000000..d390e83
--- /dev/null
@@ -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 */