1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "remote/typequeryhandler.hpp"
4 #include "remote/httputility.hpp"
5 #include "remote/filterutility.hpp"
6 #include "base/configtype.hpp"
7 #include "base/scriptglobal.hpp"
8 #include "base/logger.hpp"
9 #include <set>
10
11 using namespace icinga;
12
13 REGISTER_URLHANDLER("/v1/types", TypeQueryHandler);
14
15 class TypeTargetProvider final : public TargetProvider
16 {
17 public:
18 DECLARE_PTR_TYPEDEFS(TypeTargetProvider);
19
FindTargets(const String & type,const std::function<void (const Value &)> & addTarget) const20 void FindTargets(const String& type,
21 const std::function<void (const Value&)>& addTarget) const override
22 {
23 for (const Type::Ptr& target : Type::GetAllTypes()) {
24 addTarget(target);
25 }
26 }
27
GetTargetByName(const String & type,const String & name) const28 Value GetTargetByName(const String& type, const String& name) const override
29 {
30 Type::Ptr ptype = Type::GetByName(name);
31
32 if (!ptype)
33 BOOST_THROW_EXCEPTION(std::invalid_argument("Type does not exist."));
34
35 return ptype;
36 }
37
IsValidType(const String & type) const38 bool IsValidType(const String& type) const override
39 {
40 return type == "Type";
41 }
42
GetPluralName(const String & type) const43 String GetPluralName(const String& type) const override
44 {
45 return "types";
46 }
47 };
48
HandleRequest(AsioTlsStream & stream,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,const Dictionary::Ptr & params,boost::asio::yield_context & yc,HttpServerConnection & server)49 bool TypeQueryHandler::HandleRequest(
50 AsioTlsStream& stream,
51 const ApiUser::Ptr& user,
52 boost::beast::http::request<boost::beast::http::string_body>& request,
53 const Url::Ptr& url,
54 boost::beast::http::response<boost::beast::http::string_body>& response,
55 const Dictionary::Ptr& params,
56 boost::asio::yield_context& yc,
57 HttpServerConnection& server
58 )
59 {
60 namespace http = boost::beast::http;
61
62 if (url->GetPath().size() > 3)
63 return false;
64
65 if (request.method() != http::verb::get)
66 return false;
67
68 QueryDescription qd;
69 qd.Types.insert("Type");
70 qd.Permission = "types";
71 qd.Provider = new TypeTargetProvider();
72
73 if (params->Contains("type"))
74 params->Set("name", params->Get("type"));
75
76 params->Set("type", "Type");
77
78 if (url->GetPath().size() >= 3)
79 params->Set("name", url->GetPath()[2]);
80
81 std::vector<Value> objs;
82
83 try {
84 objs = FilterUtility::GetFilterTargets(qd, params, user);
85 } catch (const std::exception& ex) {
86 HttpUtility::SendJsonError(response, params, 404,
87 "No objects found.",
88 DiagnosticInformation(ex));
89 return true;
90 }
91
92 ArrayData results;
93
94 for (const Type::Ptr& obj : objs) {
95 Dictionary::Ptr result1 = new Dictionary();
96 results.push_back(result1);
97
98 Dictionary::Ptr resultAttrs = new Dictionary();
99 result1->Set("name", obj->GetName());
100 result1->Set("plural_name", obj->GetPluralName());
101 if (obj->GetBaseType())
102 result1->Set("base", obj->GetBaseType()->GetName());
103 result1->Set("abstract", obj->IsAbstract());
104 result1->Set("fields", resultAttrs);
105
106 Dictionary::Ptr prototype = dynamic_pointer_cast<Dictionary>(obj->GetPrototype());
107 Array::Ptr prototypeKeys = new Array();
108 result1->Set("prototype_keys", prototypeKeys);
109
110 if (prototype) {
111 ObjectLock olock(prototype);
112 for (const Dictionary::Pair& kv : prototype) {
113 prototypeKeys->Add(kv.first);
114 }
115 }
116
117 int baseFieldCount = 0;
118
119 if (obj->GetBaseType())
120 baseFieldCount = obj->GetBaseType()->GetFieldCount();
121
122 for (int fid = baseFieldCount; fid < obj->GetFieldCount(); fid++) {
123 Field field = obj->GetFieldInfo(fid);
124
125 Dictionary::Ptr fieldInfo = new Dictionary();
126 resultAttrs->Set(field.Name, fieldInfo);
127
128 fieldInfo->Set("id", fid);
129 fieldInfo->Set("type", field.TypeName);
130 if (field.RefTypeName)
131 fieldInfo->Set("ref_type", field.RefTypeName);
132 if (field.Attributes & FANavigation)
133 fieldInfo->Set("navigation_name", field.NavigationName);
134 fieldInfo->Set("array_rank", field.ArrayRank);
135
136 fieldInfo->Set("attributes", new Dictionary({
137 { "config", static_cast<bool>(field.Attributes & FAConfig) },
138 { "state", static_cast<bool>(field.Attributes & FAState) },
139 { "required", static_cast<bool>(field.Attributes & FARequired) },
140 { "navigation", static_cast<bool>(field.Attributes & FANavigation) },
141 { "no_user_modify", static_cast<bool>(field.Attributes & FANoUserModify) },
142 { "no_user_view", static_cast<bool>(field.Attributes & FANoUserView) },
143 { "deprecated", static_cast<bool>(field.Attributes & FADeprecated) }
144 }));
145 }
146 }
147
148 Dictionary::Ptr result = new Dictionary({
149 { "results", new Array(std::move(results)) }
150 });
151
152 response.result(http::status::ok);
153 HttpUtility::SendJsonBody(response, params, result);
154
155 return true;
156 }
157