diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
commit | 0915b3ef56dfac3113cce55a59a5765dc94976be (patch) | |
tree | a8fea11d50b4f083e1bf0f90025ece7f0824784a /lib/remote/apiaction.hpp | |
parent | Initial commit. (diff) | |
download | icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.tar.xz icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.zip |
Adding upstream version 2.13.6.upstream/2.13.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | lib/remote/apiaction.hpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/remote/apiaction.hpp b/lib/remote/apiaction.hpp new file mode 100644 index 0000000..f2719c1 --- /dev/null +++ b/lib/remote/apiaction.hpp @@ -0,0 +1,69 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#ifndef APIACTION_H +#define APIACTION_H + +#include "remote/i2-remote.hpp" +#include "base/registry.hpp" +#include "base/value.hpp" +#include "base/dictionary.hpp" +#include "base/configobject.hpp" +#include <vector> +#include <boost/algorithm/string/replace.hpp> + +namespace icinga +{ + +/** + * An action available over the external HTTP API. + * + * @ingroup remote + */ +class ApiAction final : public Object +{ +public: + DECLARE_PTR_TYPEDEFS(ApiAction); + + typedef std::function<Value(const ConfigObject::Ptr& target, const Dictionary::Ptr& params)> Callback; + + ApiAction(std::vector<String> registerTypes, Callback function); + + Value Invoke(const ConfigObject::Ptr& target, const Dictionary::Ptr& params); + + const std::vector<String>& GetTypes() const; + + static ApiAction::Ptr GetByName(const String& name); + static void Register(const String& name, const ApiAction::Ptr& action); + static void Unregister(const String& name); + +private: + std::vector<String> m_Types; + Callback m_Callback; +}; + +/** + * A registry for API actions. + * + * @ingroup remote + */ +class ApiActionRegistry : public Registry<ApiActionRegistry, ApiAction::Ptr> +{ +public: + static ApiActionRegistry *GetInstance(); +}; + +#define REGISTER_APIACTION(name, types, callback) \ + INITIALIZE_ONCE([]() { \ + String registerName = #name; \ + boost::algorithm::replace_all(registerName, "_", "-"); \ + std::vector<String> registerTypes; \ + String typeNames = types; \ + if (!typeNames.IsEmpty()) \ + registerTypes = typeNames.Split(";"); \ + ApiAction::Ptr action = new ApiAction(registerTypes, callback); \ + ApiActionRegistry::GetInstance()->Register(registerName, action); \ + }) + +} + +#endif /* APIACTION_H */ |