From: Michael Friedrich Date: Wed, 11 Nov 2015 10:55:20 +0000 (+0100) Subject: DB IDO: Fix group membership updates for runtime created objects X-Git-Tag: v2.4.0~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0285bcefb536cf8ee3d41ee8c17340ca7725d324;p=icinga2 DB IDO: Fix group membership updates for runtime created objects fixes #10604 --- diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index 20a3fb3d2..f3c47ab18 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "db_ido/hostdbobject.hpp" +#include "db_ido/hostgroupdbobject.hpp" #include "db_ido/dbtype.hpp" #include "db_ido/dbvalue.hpp" #include "db_ido/dbevents.hpp" @@ -180,6 +181,36 @@ void HostDbObject::OnConfigUpdate(void) { Host::Ptr host = static_pointer_cast(GetObject()); + /* groups */ + Array::Ptr groups = host->GetGroups(); + + if (groups) { + ObjectLock olock(groups); + BOOST_FOREACH(const String& groupName, groups) { + HostGroup::Ptr group = HostGroup::GetByName(groupName); + + DbQuery query1; + query1.Table = DbType::GetByName("HostGroup")->GetTable() + "_members"; + query1.Type = DbQueryDelete; + query1.Category = DbCatConfig; + query1.WhereCriteria = new Dictionary(); + query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query1.WhereCriteria->Set("hostgroup_id", DbValue::FromObjectInsertID(group)); + query1.WhereCriteria->Set("host_object_id", host); + OnQuery(query1); + + DbQuery query2; + query2.Table = DbType::GetByName("HostGroup")->GetTable() + "_members"; + query2.Type = DbQueryInsert; + query2.Category = DbCatConfig; + query2.Fields = new Dictionary(); + query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query2.Fields->Set("hostgroup_id", DbValue::FromObjectInsertID(group)); + query2.Fields->Set("host_object_id", host); + OnQuery(query2); + } + } + /* parents */ BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) { Host::Ptr parent = dynamic_pointer_cast(checkable); diff --git a/lib/db_ido/hostgroupdbobject.cpp b/lib/db_ido/hostgroupdbobject.cpp index 8a9ce1cb3..731d1ed4f 100644 --- a/lib/db_ido/hostgroupdbobject.cpp +++ b/lib/db_ido/hostgroupdbobject.cpp @@ -50,29 +50,3 @@ Dictionary::Ptr HostGroupDbObject::GetStatusFields(void) const { return Empty; } - -void HostGroupDbObject::OnConfigUpdate(void) -{ - HostGroup::Ptr group = static_pointer_cast(GetObject()); - - DbQuery query1; - query1.Table = DbType::GetByName("HostGroup")->GetTable() + "_members"; - query1.Type = DbQueryDelete; - query1.Category = DbCatConfig; - query1.WhereCriteria = new Dictionary(); - query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ - query1.WhereCriteria->Set("hostgroup_id", DbValue::FromObjectInsertID(group)); - OnQuery(query1); - - BOOST_FOREACH(const Host::Ptr& host, group->GetMembers()) { - DbQuery query2; - query2.Table = DbType::GetByName("HostGroup")->GetTable() + "_members"; - query2.Type = DbQueryInsert; - query2.Category = DbCatConfig; - query2.Fields = new Dictionary(); - query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ - query2.Fields->Set("hostgroup_id", DbValue::FromObjectInsertID(group)); - query2.Fields->Set("host_object_id", host); - OnQuery(query2); - } -} diff --git a/lib/db_ido/hostgroupdbobject.hpp b/lib/db_ido/hostgroupdbobject.hpp index 4d092b8d2..6c447ac0a 100644 --- a/lib/db_ido/hostgroupdbobject.hpp +++ b/lib/db_ido/hostgroupdbobject.hpp @@ -42,9 +42,6 @@ public: virtual Dictionary::Ptr GetConfigFields(void) const override; virtual Dictionary::Ptr GetStatusFields(void) const override; -protected: - virtual void OnConfigUpdate(void) override; - private: static void MembersChangedHandler(const HostGroup::Ptr& hgfilter); }; diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index 1894a7221..e5269151e 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "db_ido/servicedbobject.hpp" +#include "db_ido/servicegroupdbobject.hpp" #include "db_ido/dbtype.hpp" #include "db_ido/dbvalue.hpp" #include "db_ido/dbevents.hpp" @@ -179,6 +180,36 @@ void ServiceDbObject::OnConfigUpdate(void) { Service::Ptr service = static_pointer_cast(GetObject()); + /* groups */ + Array::Ptr groups = service->GetGroups(); + + if (groups) { + ObjectLock olock(groups); + BOOST_FOREACH(const String& groupName, groups) { + ServiceGroup::Ptr group = ServiceGroup::GetByName(groupName); + + DbQuery query1; + query1.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members"; + query1.Type = DbQueryDelete; + query1.Category = DbCatConfig; + query1.WhereCriteria = new Dictionary(); + query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query1.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group)); + query1.WhereCriteria->Set("service_object_id", service); + OnQuery(query1); + + DbQuery query2; + query2.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members"; + query2.Type = DbQueryInsert; + query2.Category = DbCatConfig; + query2.Fields = new Dictionary(); + query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query2.Fields->Set("servicegroup_id", DbValue::FromObjectInsertID(group)); + query2.Fields->Set("service_object_id", service); + OnQuery(query2); + } + } + /* service dependencies */ Log(LogDebug, "ServiceDbObject") << "service dependencies for '" << service->GetName() << "'"; diff --git a/lib/db_ido/servicegroupdbobject.cpp b/lib/db_ido/servicegroupdbobject.cpp index 3d5f92baa..c96e06665 100644 --- a/lib/db_ido/servicegroupdbobject.cpp +++ b/lib/db_ido/servicegroupdbobject.cpp @@ -49,29 +49,3 @@ Dictionary::Ptr ServiceGroupDbObject::GetStatusFields(void) const { return Empty; } - -void ServiceGroupDbObject::OnConfigUpdate(void) -{ - ServiceGroup::Ptr group = static_pointer_cast(GetObject()); - - DbQuery query1; - query1.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members"; - query1.Type = DbQueryDelete; - query1.Category = DbCatConfig; - query1.WhereCriteria = new Dictionary(); - query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ - query1.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group)); - OnQuery(query1); - - BOOST_FOREACH(const Service::Ptr& service, group->GetMembers()) { - DbQuery query2; - query2.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members"; - query2.Type = DbQueryInsert; - query2.Category = DbCatConfig; - query2.Fields = new Dictionary(); - query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ - query2.Fields->Set("servicegroup_id", DbValue::FromObjectInsertID(group)); - query2.Fields->Set("service_object_id", service); - OnQuery(query2); - } -} diff --git a/lib/db_ido/servicegroupdbobject.hpp b/lib/db_ido/servicegroupdbobject.hpp index 99f304b70..35a9636f0 100644 --- a/lib/db_ido/servicegroupdbobject.hpp +++ b/lib/db_ido/servicegroupdbobject.hpp @@ -41,9 +41,6 @@ public: virtual Dictionary::Ptr GetConfigFields(void) const override; virtual Dictionary::Ptr GetStatusFields(void) const override; - -protected: - virtual void OnConfigUpdate(void) override; }; } diff --git a/lib/db_ido/userdbobject.cpp b/lib/db_ido/userdbobject.cpp index 1e67a3e12..c9199db61 100644 --- a/lib/db_ido/userdbobject.cpp +++ b/lib/db_ido/userdbobject.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "db_ido/userdbobject.hpp" +#include "db_ido/usergroupdbobject.hpp" #include "db_ido/dbtype.hpp" #include "db_ido/dbvalue.hpp" #include "icinga/user.hpp" @@ -80,6 +81,36 @@ void UserDbObject::OnConfigUpdate(void) Dictionary::Ptr fields = new Dictionary(); User::Ptr user = static_pointer_cast(GetObject()); + /* groups */ + Array::Ptr groups = user->GetGroups(); + + if (groups) { + ObjectLock olock(groups); + BOOST_FOREACH(const String& groupName, groups) { + UserGroup::Ptr group = UserGroup::GetByName(groupName); + + DbQuery query1; + query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; + query1.Type = DbQueryDelete; + query1.Category = DbCatConfig; + query1.WhereCriteria = new Dictionary(); + query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query1.WhereCriteria->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); + query1.WhereCriteria->Set("contact_object_id", user); + OnQuery(query1); + + DbQuery query2; + query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; + query2.Type = DbQueryInsert; + query2.Category = DbCatConfig; + query2.Fields = new Dictionary(); + query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + query2.Fields->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); + query2.Fields->Set("contact_object_id", user); + OnQuery(query2); + } + } + /* contact addresses */ Log(LogDebug, "UserDbObject") << "contact addresses for '" << user->GetName() << "'"; diff --git a/lib/db_ido/usergroupdbobject.cpp b/lib/db_ido/usergroupdbobject.cpp index d124d8d73..d73d46d11 100644 --- a/lib/db_ido/usergroupdbobject.cpp +++ b/lib/db_ido/usergroupdbobject.cpp @@ -47,29 +47,3 @@ Dictionary::Ptr UserGroupDbObject::GetStatusFields(void) const { return Empty; } - -void UserGroupDbObject::OnConfigUpdate(void) -{ - UserGroup::Ptr group = static_pointer_cast(GetObject()); - - DbQuery query1; - query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; - query1.Type = DbQueryDelete; - query1.Category = DbCatConfig; - query1.WhereCriteria = new Dictionary(); - query1.WhereCriteria->Set("instance_id", 0); - query1.WhereCriteria->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); - OnQuery(query1); - - BOOST_FOREACH(const User::Ptr& user, group->GetMembers()) { - DbQuery query2; - query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; - query2.Type = DbQueryInsert; - query2.Category = DbCatConfig; - query2.Fields = new Dictionary(); - query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ - query2.Fields->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); - query2.Fields->Set("contact_object_id", user); - OnQuery(query2); - } -} diff --git a/lib/db_ido/usergroupdbobject.hpp b/lib/db_ido/usergroupdbobject.hpp index cd3e2decf..399040c5a 100644 --- a/lib/db_ido/usergroupdbobject.hpp +++ b/lib/db_ido/usergroupdbobject.hpp @@ -41,9 +41,6 @@ public: virtual Dictionary::Ptr GetConfigFields(void) const override; virtual Dictionary::Ptr GetStatusFields(void) const override; - -protected: - virtual void OnConfigUpdate(void) override; }; }