]> granicus.if.org Git - icinga2/blob - components/livestatus/hostgroupstable.cpp
livestatus: use generic Value() as row accessor instead of Object()
[icinga2] / components / livestatus / hostgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/hostgroupstable.h"
21 #include "icinga/hostgroup.h"
22 #include "base/dynamictype.h"
23 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26 using namespace livestatus;
27
28 HostGroupsTable::HostGroupsTable(void)
29 {
30         AddColumns(this);
31 }
32
33 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
34     const Column::ObjectAccessor& objectAccessor)
35 {
36         table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor));
37         table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor));
38         table->AddColumn(prefix + "members", Column(&HostGroupsTable::MembersAccessor, objectAccessor));
39 }
40
41 String HostGroupsTable::GetName(void) const
42 {
43         return "hostgroups";
44 }
45
46 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
47 {
48         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("HostGroup")) {
49                 addRowFn(object);
50         }
51 }
52
53 Value HostGroupsTable::NameAccessor(const Value& row)
54 {
55         return static_cast<HostGroup::Ptr>(row)->GetName();
56 }
57
58 Value HostGroupsTable::AliasAccessor(const Value& row)
59 {
60         return static_cast<HostGroup::Ptr>(row)->GetName();
61 }
62
63 Value HostGroupsTable::MembersAccessor(const Value& row)
64 {
65         Array::Ptr members = boost::make_shared<Array>();
66
67         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
68                 members->Add(host->GetName());
69         }
70
71         return members;
72 }