1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ 2 3 #ifndef APIACTION_H 4 #define APIACTION_H 5 6 #include "remote/i2-remote.hpp" 7 #include "base/registry.hpp" 8 #include "base/value.hpp" 9 #include "base/dictionary.hpp" 10 #include "base/configobject.hpp" 11 #include <vector> 12 #include <boost/algorithm/string/replace.hpp> 13 14 namespace icinga 15 { 16 17 /** 18 * An action available over the external HTTP API. 19 * 20 * @ingroup remote 21 */ 22 class ApiAction final : public Object 23 { 24 public: 25 DECLARE_PTR_TYPEDEFS(ApiAction); 26 27 typedef std::function<Value(const ConfigObject::Ptr& target, const Dictionary::Ptr& params)> Callback; 28 29 ApiAction(std::vector<String> registerTypes, Callback function); 30 31 Value Invoke(const ConfigObject::Ptr& target, const Dictionary::Ptr& params); 32 33 const std::vector<String>& GetTypes() const; 34 35 static ApiAction::Ptr GetByName(const String& name); 36 static void Register(const String& name, const ApiAction::Ptr& action); 37 static void Unregister(const String& name); 38 39 private: 40 std::vector<String> m_Types; 41 Callback m_Callback; 42 }; 43 44 /** 45 * A registry for API actions. 46 * 47 * @ingroup remote 48 */ 49 class ApiActionRegistry : public Registry<ApiActionRegistry, ApiAction::Ptr> 50 { 51 public: 52 static ApiActionRegistry *GetInstance(); 53 }; 54 55 #define REGISTER_APIACTION(name, types, callback) \ 56 INITIALIZE_ONCE([]() { \ 57 String registerName = #name; \ 58 boost::algorithm::replace_all(registerName, "_", "-"); \ 59 std::vector<String> registerTypes; \ 60 String typeNames = types; \ 61 if (!typeNames.IsEmpty()) \ 62 registerTypes = typeNames.Split(";"); \ 63 ApiAction::Ptr action = new ApiAction(registerTypes, callback); \ 64 ApiActionRegistry::GetInstance()->Register(registerName, action); \ 65 }) 66 67 } 68 69 #endif /* APIACTION_H */ 70