2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
#include "remote/typequeryhandler.hpp"
|
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
#include "base/configtype.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/types", TypeQueryHandler);
|
|
|
|
|
2018-01-04 06:11:04 +01:00
|
|
|
class TypeTargetProvider final : public TargetProvider
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(TypeTargetProvider);
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
void FindTargets(const String& type,
|
2017-12-19 15:50:05 +01:00
|
|
|
const std::function<void (const Value&)>& addTarget) const override
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& target : Type::GetAllTypes()) {
|
2015-08-26 10:58:59 +02:00
|
|
|
addTarget(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
Value GetTargetByName(const String& type, const String& name) const override
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
|
|
|
Type::Ptr ptype = Type::GetByName(name);
|
|
|
|
|
|
|
|
if (!ptype)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Type does not exist."));
|
|
|
|
|
|
|
|
return ptype;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
bool IsValidType(const String& type) const override
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
|
|
|
return type == "Type";
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
String GetPluralName(const String& type) const override
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
|
|
|
return "types";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
bool TypeQueryHandler::HandleRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-15 10:33:01 +01:00
|
|
|
const ApiUser::Ptr& user,
|
|
|
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
|
|
|
const Url::Ptr& url,
|
|
|
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
2019-02-15 11:51:12 +01:00
|
|
|
const Dictionary::Ptr& params,
|
|
|
|
boost::asio::yield_context& yc,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-15 10:33:01 +01:00
|
|
|
)
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
|
|
if (url->GetPath().size() > 3)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (request.method() != http::verb::get)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
QueryDescription qd;
|
|
|
|
qd.Types.insert("Type");
|
2016-08-18 19:00:14 +02:00
|
|
|
qd.Permission = "types";
|
2015-08-26 10:58:59 +02:00
|
|
|
qd.Provider = new TypeTargetProvider();
|
|
|
|
|
|
|
|
if (params->Contains("type"))
|
|
|
|
params->Set("name", params->Get("type"));
|
|
|
|
|
|
|
|
params->Set("type", "Type");
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (url->GetPath().size() >= 3)
|
|
|
|
params->Set("name", url->GetPath()[2]);
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2016-02-04 22:40:01 +01:00
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
try {
|
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user);
|
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 15:50:05 +01:00
|
|
|
"No objects found.",
|
2018-04-05 17:22:56 +02:00
|
|
|
DiagnosticInformation(ex));
|
2016-02-04 22:40:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData results;
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& obj : objs) {
|
2015-08-26 10:58:59 +02:00
|
|
|
Dictionary::Ptr result1 = new Dictionary();
|
2018-01-11 11:17:38 +01:00
|
|
|
results.push_back(result1);
|
2015-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr resultAttrs = new Dictionary();
|
|
|
|
result1->Set("name", obj->GetName());
|
2015-08-29 12:05:44 +02:00
|
|
|
result1->Set("plural_name", obj->GetPluralName());
|
2015-08-26 10:58:59 +02:00
|
|
|
if (obj->GetBaseType())
|
|
|
|
result1->Set("base", obj->GetBaseType()->GetName());
|
|
|
|
result1->Set("abstract", obj->IsAbstract());
|
|
|
|
result1->Set("fields", resultAttrs);
|
|
|
|
|
|
|
|
Dictionary::Ptr prototype = dynamic_pointer_cast<Dictionary>(obj->GetPrototype());
|
|
|
|
Array::Ptr prototypeKeys = new Array();
|
|
|
|
result1->Set("prototype_keys", prototypeKeys);
|
|
|
|
|
|
|
|
if (prototype) {
|
|
|
|
ObjectLock olock(prototype);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : prototype) {
|
2015-08-26 10:58:59 +02:00
|
|
|
prototypeKeys->Add(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int baseFieldCount = 0;
|
|
|
|
|
|
|
|
if (obj->GetBaseType())
|
|
|
|
baseFieldCount = obj->GetBaseType()->GetFieldCount();
|
|
|
|
|
|
|
|
for (int fid = baseFieldCount; fid < obj->GetFieldCount(); fid++) {
|
|
|
|
Field field = obj->GetFieldInfo(fid);
|
|
|
|
|
|
|
|
Dictionary::Ptr fieldInfo = new Dictionary();
|
|
|
|
resultAttrs->Set(field.Name, fieldInfo);
|
|
|
|
|
|
|
|
fieldInfo->Set("id", fid);
|
|
|
|
fieldInfo->Set("type", field.TypeName);
|
|
|
|
if (field.RefTypeName)
|
|
|
|
fieldInfo->Set("ref_type", field.RefTypeName);
|
2015-09-22 09:42:30 +02:00
|
|
|
if (field.Attributes & FANavigation)
|
|
|
|
fieldInfo->Set("navigation_name", field.NavigationName);
|
2015-08-26 10:58:59 +02:00
|
|
|
fieldInfo->Set("array_rank", field.ArrayRank);
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
fieldInfo->Set("attributes", new Dictionary({
|
|
|
|
{ "config", static_cast<bool>(field.Attributes & FAConfig) },
|
|
|
|
{ "state", static_cast<bool>(field.Attributes & FAState) },
|
|
|
|
{ "required", static_cast<bool>(field.Attributes & FARequired) },
|
|
|
|
{ "navigation", static_cast<bool>(field.Attributes & FANavigation) },
|
|
|
|
{ "no_user_modify", static_cast<bool>(field.Attributes & FANoUserModify) },
|
|
|
|
{ "no_user_view", static_cast<bool>(field.Attributes & FANoUserView) },
|
|
|
|
{ "deprecated", static_cast<bool>(field.Attributes & FADeprecated) }
|
|
|
|
}));
|
2015-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
});
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|