From bd0ad338f89028a27a46a1886c0edcf034594d80 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 5 Aug 2013 09:49:19 +0200 Subject: [PATCH] ido: Implement contactgroup members. --- lib/icinga/usergroup.cpp | 9 +++++++-- lib/icinga/usergroup.h | 2 ++ lib/ido/hostgroupdbobject.cpp | 1 - lib/ido/servicegroupdbobject.cpp | 1 - lib/ido/usergroupdbobject.cpp | 33 ++++++++++++++++++++++++++++++++ lib/ido/usergroupdbobject.h | 8 ++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/icinga/usergroup.cpp b/lib/icinga/usergroup.cpp index 8af17a911..915d6b89b 100644 --- a/lib/icinga/usergroup.cpp +++ b/lib/icinga/usergroup.cpp @@ -32,6 +32,7 @@ static boost::mutex l_Mutex; static std::map > l_MembersCache; static bool l_MembersCacheNeedsUpdate = false; static Timer::Ptr l_MembersCacheTimer; +boost::signals2::signal UserGroup::OnMembersChanged; REGISTER_TYPE(UserGroup); @@ -136,6 +137,10 @@ void UserGroup::RefreshMembersCache(void) } } - boost::mutex::scoped_lock lock(l_Mutex); - l_MembersCache.swap(newMembersCache); + { + boost::mutex::scoped_lock lock(l_Mutex); + l_MembersCache.swap(newMembersCache); + } + + OnMembersChanged(); } diff --git a/lib/icinga/usergroup.h b/lib/icinga/usergroup.h index 549da0060..b4e9a56d5 100644 --- a/lib/icinga/usergroup.h +++ b/lib/icinga/usergroup.h @@ -48,6 +48,8 @@ public: static void InvalidateMembersCache(void); + static boost::signals2::signal OnMembersChanged; + protected: virtual void OnRegistrationCompleted(void); diff --git a/lib/ido/hostgroupdbobject.cpp b/lib/ido/hostgroupdbobject.cpp index a97580a10..fab1b7a36 100644 --- a/lib/ido/hostgroupdbobject.cpp +++ b/lib/ido/hostgroupdbobject.cpp @@ -23,7 +23,6 @@ #include "icinga/hostgroup.h" #include "base/objectlock.h" #include "base/initialize.h" -#include "base/logger_fwd.h" #include "base/dynamictype.h" #include diff --git a/lib/ido/servicegroupdbobject.cpp b/lib/ido/servicegroupdbobject.cpp index 984b38ff6..e527c264b 100644 --- a/lib/ido/servicegroupdbobject.cpp +++ b/lib/ido/servicegroupdbobject.cpp @@ -54,7 +54,6 @@ Dictionary::Ptr ServiceGroupDbObject::GetStatusFields(void) const return Empty; } - void ServiceGroupDbObject::OnConfigUpdate(void) { MembersChangedHandler(); diff --git a/lib/ido/usergroupdbobject.cpp b/lib/ido/usergroupdbobject.cpp index 3d45d2e6d..cada32fc4 100644 --- a/lib/ido/usergroupdbobject.cpp +++ b/lib/ido/usergroupdbobject.cpp @@ -22,11 +22,14 @@ #include "ido/dbvalue.h" #include "icinga/usergroup.h" #include "base/objectlock.h" +#include "base/initialize.h" +#include "base/dynamictype.h" #include using namespace icinga; REGISTER_DBTYPE(UserGroup, "contactgroup", DbObjectTypeContactGroup, "contactgroup_object_id", UserGroupDbObject); +INITIALIZE_ONCE(UserGroupDbObject, &UserGroupDbObject::StaticInitialize); UserGroupDbObject::UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2) : DbObject(type, name1, name2) @@ -46,3 +49,33 @@ Dictionary::Ptr UserGroupDbObject::GetStatusFields(void) const { return Empty; } + +void UserGroupDbObject::OnConfigUpdate(void) +{ + MembersChangedHandler(); +} + +void UserGroupDbObject::MembersChangedHandler(void) +{ + DbQuery query1; + query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set("instance_id", 0); + OnQuery(query1); + + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("UserGroup")) { + UserGroup::Ptr ug = static_pointer_cast(object); + + BOOST_FOREACH(const User::Ptr& user, ug->GetMembers()) { + DbQuery query2; + query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; + query2.Type = DbQueryInsert; + query2.Fields = boost::make_shared(); + query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query2.Fields->Set("contactgroup_id", DbValue::FromObjectInsertID(ug)); + query2.Fields->Set("contact_object_id", user); + OnQuery(query2); + } + } +} diff --git a/lib/ido/usergroupdbobject.h b/lib/ido/usergroupdbobject.h index 84123eb1b..4e5de55d1 100644 --- a/lib/ido/usergroupdbobject.h +++ b/lib/ido/usergroupdbobject.h @@ -38,8 +38,16 @@ public: UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2); + static void StaticInitialize(void); + virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; + +protected: + virtual void OnConfigUpdate(void); + +private: + static void MembersChangedHandler(void); }; }