ido: Implement contactgroup members.

This commit is contained in:
Gunnar Beutner 2013-08-05 09:49:19 +02:00
parent 5d09af0a6e
commit bd0ad338f8
6 changed files with 50 additions and 4 deletions

View File

@ -32,6 +32,7 @@ static boost::mutex l_Mutex;
static std::map<String, std::vector<User::WeakPtr> > l_MembersCache;
static bool l_MembersCacheNeedsUpdate = false;
static Timer::Ptr l_MembersCacheTimer;
boost::signals2::signal<void (void)> 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();
}

View File

@ -48,6 +48,8 @@ public:
static void InvalidateMembersCache(void);
static boost::signals2::signal<void (void)> OnMembersChanged;
protected:
virtual void OnRegistrationCompleted(void);

View File

@ -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 <boost/foreach.hpp>

View File

@ -54,7 +54,6 @@ Dictionary::Ptr ServiceGroupDbObject::GetStatusFields(void) const
return Empty;
}
void ServiceGroupDbObject::OnConfigUpdate(void)
{
MembersChangedHandler();

View File

@ -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 <boost/foreach.hpp>
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<Dictionary>();
query1.WhereCriteria->Set("instance_id", 0);
OnQuery(query1);
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("UserGroup")) {
UserGroup::Ptr ug = static_pointer_cast<UserGroup>(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<Dictionary>();
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);
}
}
}

View File

@ -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);
};
}