Revision fc15b997

View differences:

include/ClusterPool.h
32 32
 */
33 33
class ClusterPool
34 34
{
35
private:
36

  
35
public:
36
    /**
37
     *  Cluster name for the default cluster
38
     */
39
    static const string DEFAULT_CLUSTER_NAME;
37 40

  
41
private:
38 42
    // -------------------------------------------------------------------------
39 43
    // Friends
40 44
    // -------------------------------------------------------------------------
41

  
42 45
    friend class HostPool;
43 46

  
44

  
45 47
    /* ---------------------------------------------------------------------- */
46 48
    /* Attributes                                                             */
47 49
    /* ---------------------------------------------------------------------- */
48

  
49 50
    /**
50 51
     *  This map stores the clusters
51 52
     */
......
121 122
     *
122 123
     *    @return 0 on success
123 124
     */
124
    void dump(ostringstream& oss, int id, string name);
125
    void dump_cluster(ostringstream& oss, int id, string name);
125 126

  
126 127

  
127 128
    /* ---------------------------------------------------------------------- */
include/Host.h
20 20
#include "PoolSQL.h"
21 21
#include "HostShare.h"
22 22
#include "HostTemplate.h"
23
#include "ClusterPool.h"
23 24

  
24 25
using namespace std;
25 26

  
......
90 91

  
91 92
        if ( state != DISABLED) //Don't change the state is host is disabled
92 93
        {
93
        	if (success == true)
94
        	{
95
        		state = MONITORED;
96
        	}
97
        	else
98
        	{
99
        		state = ERROR;
100
        	}
94
            if (success == true)
95
            {
96
                state = MONITORED;
97
            }
98
            else
99
            {
100
                state = ERROR;
101
            }
101 102
        }
102 103
    };
103 104

  
......
107 108
     */
108 109
    void disable()
109 110
    {
110
    	state = DISABLED;
111
        state = DISABLED;
111 112
    };
112 113

  
113 114
    /**
......
116 117
     */
117 118
    void enable()
118 119
    {
119
    	state = INIT;
120
        state = INIT;
120 121
    };
121 122

  
122 123
    /**
123 124
     *  Returns host host_name
124 125
     *     @return host_name Host's hostname
125 126
     */
126
 	const string& get_hostname() const
127
    const string& get_hostname() const
127 128
    {
128
	    return hostname;
129
	};
129
        return hostname;
130
    };
130 131

  
131 132
    /** Update host counters and update the whole host on the DB
132 133
     *    @param parse_str string with values to be parsed
......
188 189
        return last_monitored;
189 190
    };
190 191

  
191
    int set_cluster(string cluster_name)
192
    /**
193
     *  Sets the cluster for this host
194
     *    @return time_t last monitored time
195
     */
196
    int set_cluster(const string& cluster_name)
192 197
    {
193 198
        cluster = cluster_name;
194 199
        return 0;
include/HostPool.h
87 87
    };
88 88

  
89 89
    /**
90
     * Get the 10 least monitored hosts
90
     * Get the least monitored hosts
91 91
     *   @param discovered hosts, map to store the retrieved hosts hids and
92 92
     *   hostnames
93 93
     *   @param host_limit max. number of hosts to monitor at a time
......
147 147
     */
148 148
    int dump(ostringstream& oss, const string& where);
149 149

  
150

  
150
    /* ---------------------------------------------------------------------- */
151 151
    /* ---------------------------------------------------------------------- */
152 152
    /* Methods for cluster management                                         */
153 153
    /* ---------------------------------------------------------------------- */
154
/*
155
    ClusterPool * cluster_pool()
156
    {
157
        return &cluster_pool;
158
    }
159
//*/
154
    /* ---------------------------------------------------------------------- */
160 155

  
161 156
    /**
162 157
     *  Returns true if the clid is an id for an existing cluster
......
164 159
     *
165 160
     *  @return true if the clid is an id for an existing cluster
166 161
     */
167
    bool exists_cluster(int clid)
162
   /* bool exists_cluster(int clid)
168 163
    {
169 164
        return cluster_pool.exists(clid);
170
    };
165
    };*/
171 166

  
172 167
    /**
173 168
     *  Allocates a new cluster in the pool
......
222 217

  
223 218
        it = cluster_pool.cluster_names.find(clid);
224 219

  
225
        if(it == cluster_pool.cluster_names.end())
220
        if (it == cluster_pool.cluster_names.end())
226 221
        {
227 222
            return -1;
228 223
        }
......
230 225
        return host->set_cluster( it->second );
231 226
    };
232 227

  
233

  
234 228
    /**
235
     *  Removes the host from the given cluster.
236
     *  The result is the same as assigning the host to
237
     *  the default cluster.
229
     *  Removes the host from the given cluster setting the default one.
238 230
     *    @param host The host to assign
239 231
     *
240 232
     *    @return 0 on success
241 233
     */
242
    int remove_cluster(Host* host)
234
    int set_default_cluster(Host* host)
243 235
    {
244
        // To remove a host from a cluster means
245
        // moving it to the default cluster.
246
        return set_cluster(host, 0);
236
        return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_NAME);
247 237
    };
248 238

  
249 239
private:
250

  
251
    ClusterPool         cluster_pool;
252

  
240
    /**
241
     *  ClusterPool, clusters defined and persistance functionality
242
     */
243
    ClusterPool  cluster_pool;
253 244

  
254 245
    /**
255 246
     *  Factory method to produce Host objects
src/host/ClusterPool.cc
25 25
    "oid INTEGER PRIMARY KEY, cluster_name VARCHAR(128), "
26 26
    "UNIQUE(cluster_name) )";
27 27

  
28
const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";
29

  
28 30
/* -------------------------------------------------------------------------- */
29 31

  
30 32
int ClusterPool::allocate(int * clid, string name, SqlDB *db)
......
74 76

  
75 77
    if(it != cluster_names.end())
76 78
    {
77
        dump(oss, it->first, it->second);
79
        dump_cluster(oss, it->first, it->second);
78 80
    }
79 81

  
80 82
    return oss.str();
......
116 118

  
117 119
    for(it=cluster_names.begin();it!=cluster_names.end();it++)
118 120
    {
119
        dump(oss, it->first, it->second);
121
        dump_cluster(oss, it->first, it->second);
120 122
    }
121 123

  
122 124
    oss << "</CLUSTER_POOL>";
......
125 127
}
126 128

  
127 129
/* -------------------------------------------------------------------------- */
130
/* -------------------------------------------------------------------------- */
128 131

  
129 132
int ClusterPool::insert(int oid, string name, SqlDB *db)
130 133
{
......
158 161
}
159 162

  
160 163
/* -------------------------------------------------------------------------- */
164
/* -------------------------------------------------------------------------- */
161 165

  
162
void ClusterPool::dump(ostringstream& oss, int id, string name)
166
void ClusterPool::dump_cluster(ostringstream& oss, int id, string name)
163 167
{
164 168
    oss <<
165 169
        "<CLUSTER>"     <<
src/host/Host.cc
40 40
        vmm_mad_name(_vmm_mad_name),
41 41
        tm_mad_name(_tm_mad_name),
42 42
        last_monitored(0),
43
        host_template(id),
44
        cluster("default")
43
        cluster(ClusterPool::DEFAULT_CLUSTER_NAME),
44
        host_template(id)
45 45
        {};
46 46

  
47 47

  
......
123 123
    }
124 124

  
125 125
    // Get the template
126

  
127 126
    rc = host_template.select(db);
128 127

  
129 128
    if ( rc != 0 )
......
132 131
    }
133 132

  
134 133
    // Select the host shares from the DB
135

  
136 134
    rc = host_share.select(db);
137 135

  
138 136
    if ( rc != 0 )
......
143 141
    return 0;
144 142
}
145 143

  
146

  
147 144
/* ------------------------------------------------------------------------ */
148 145
/* ------------------------------------------------------------------------ */
149 146

  
......
421 418

  
422 419
ostream& operator<<(ostream& os, Host& host)
423 420
{
424
	string host_str;
421
    string host_str;
425 422

  
426
	os << host.to_xml(host_str);
423
    os << host.to_xml(host_str);
427 424

  
428 425
    return os;
429 426
};
430 427

  
431

  
432 428
/* ------------------------------------------------------------------------ */
433 429
/* ------------------------------------------------------------------------ */
434 430

  
435 431
string& Host::to_xml(string& xml) const
436 432
{
437 433
    string template_xml;
438
	string share_xml;
434
    string share_xml;
439 435
    ostringstream   oss;
440 436

  
441 437
    oss <<
......
448 444
       "<TM_MAD>"        << tm_mad_name    << "</TM_MAD>"        <<
449 445
       "<LAST_MON_TIME>" << last_monitored << "</LAST_MON_TIME>" <<
450 446
       "<CLUSTER>"       << cluster        << "</CLUSTER>"       <<
451
 	   host_share.to_xml(share_xml)  <<
447
       host_share.to_xml(share_xml)  <<
452 448
       host_template.to_xml(template_xml) <<
453
	"</HOST>";
449
    "</HOST>";
454 450

  
455 451
    xml = oss.str();
456 452

  
......
463 459
string& Host::to_str(string& str) const
464 460
{
465 461
    string template_str;
466
	string share_str;
462
    string share_str;
467 463

  
468 464
    ostringstream   os;
469 465

  
470 466
    os <<
471
		"ID      =  "  << oid            << endl <<
472
    	"NAME = "      << hostname       << endl <<
473
    	"STATE    = "  << state          << endl <<
474
    	"IM MAD   = "  << im_mad_name    << endl <<
475
    	"VMM MAD  = "  << vmm_mad_name   << endl <<
476
    	"TM MAD   = "  << tm_mad_name    << endl <<
477
    	"LAST_MON = "  << last_monitored << endl <<
467
        "ID      =  "  << oid            << endl <<
468
        "NAME = "      << hostname       << endl <<
469
        "STATE    = "  << state          << endl <<
470
        "IM MAD   = "  << im_mad_name    << endl <<
471
        "VMM MAD  = "  << vmm_mad_name   << endl <<
472
        "TM MAD   = "  << tm_mad_name    << endl <<
473
        "LAST_MON = "  << last_monitored << endl <<
478 474
        "CLUSTER  = "  << cluster        << endl <<
479 475
        "ATTRIBUTES"   << endl << host_template.to_str(template_str) << endl <<
480 476
        "HOST SHARES"  << endl << host_share.to_str(share_str) <<endl;
481 477

  
482
	str = os.str();
478
    str = os.str();
483 479

  
484
	return str;
480
    return str;
485 481
}
src/host/HostPool.cc
18 18
/* Host Pool                                                    			  */
19 19
/* ************************************************************************** */
20 20

  
21
#include <stdexcept>
22

  
21 23
#include "HostPool.h"
22 24
#include "ClusterPool.h"
23 25
#include "NebulaLog.h"
24 26

  
27
/* -------------------------------------------------------------------------- */
28
/* -------------------------------------------------------------------------- */
29

  
25 30
int HostPool::init_cb(void *nil, int num, char **values, char **names)
26 31
{
27 32
    if ( num != 2 || values == 0 || values[0] == 0 )
......
51 56

  
52 57
    if (cluster_pool.cluster_names.empty())
53 58
    {
54
        string default_name = "default";
55

  
56
        // Insert the "default" cluster
57
        int rc = cluster_pool.insert(0, default_name, db);
59
        int rc = cluster_pool.insert(0, ClusterPool::DEFAULT_CLUSTER_NAME, db);
58 60

  
59 61
        if(rc != 0)
60 62
        {
61
            NebulaLog::log("HOST",Log::ERROR,
62
                "Cluster pool is empty but couldn't create default cluster.");
63
            throw runtime_error("Could not create default cluster HostPool");
63 64
        }
64 65
    }
65 66
}
......
184 185
{
185 186
    int                         rc;
186 187
    map<int, string>::iterator  it;
188
    string                      cluster_name;
187 189

  
188 190
    it = cluster_pool.cluster_names.find(clid);
189 191

  
192
    if ( it == cluster_pool.cluster_names.end() )
193
    {
194
        return -1;
195
    }
196

  
197
    cluster_name = it->second;
198

  
190 199
    // try to drop the cluster from the pool and DB
191 200
    rc = cluster_pool.drop(clid, db);
192 201

  
193 202
    // Move the hosts assigned to the deleted cluster to the default one
194 203
    if( rc == 0 )
195 204
    {
196
        // Search the hosts assigned to this cluster
197 205
        Host*                   host;
198 206
        vector<int>             hids;
199 207
        vector<int>::iterator   hid_it;
200 208

  
201
        string cluster_name = it->second;
202
        string where        = "cluster = '" + cluster_name + "'";
203

  
209
        string                  where = "cluster = '" + cluster_name + "'";
204 210

  
205 211
        search(hids, Host::table, where);
206 212

  
......
208 214
        {
209 215
            host = get(*hid_it, true);
210 216

  
211
            remove_cluster(host);
217
            if ( host == 0 )
218
            {
219
                continue;
220
            }
221

  
222
            set_default_cluster(host);
223

  
212 224
            update(host);
213 225

  
214 226
            host->unlock();
src/host/test/HostPoolTest.cc
33 33
{
34 34
    "<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
35 35
    "<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
36
    "<LAST_MON_TIME>0000000000</LAST_MON_TIME><HOST_SHARE><HID>0</HID>"
36
    "<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID>"
37 37
    "<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
38 38
    "<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
39 39
    "<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
......
42 42

  
43 43
    "<HOST><ID>1</ID><NAME>Second host</NAME><STATE>0</STATE>"
44 44
    "<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
45
    "<LAST_MON_TIME>0000000000</LAST_MON_TIME><HOST_SHARE><HID>1</HID>"
45
    "<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID>"
46 46
    "<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
47 47
    "<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
48 48
    "<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
......
53 53
// This xml dump result has the LAST_MON_TIMEs modified to 0000000000
54 54
const string xml_dump =
55 55
    "<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
56
    "M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>000000"
57
    "0000</LAST_MON_TIME><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
56
    "M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
57
    "</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
58 58
    "_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
59 59
    ">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
60 60
    "MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
61 61
    "SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
62 62
    "<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
63
    "D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_M"
64
    "ON_TIME><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
63
    "D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
64
    "ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
65 65
    "M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
66 66
    "<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
67 67
    "U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
68 68
    "SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
69 69
    "AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
70
    "M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_MON_TIME><HOS"
70
    "M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
71 71
    "T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
72 72
    "_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
73 73
    "MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
74 74
    "U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
75 75
    "NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
76 76
    "name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
77
    "<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_MON_TIME><HOST_SHAR"
77
    "<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
78 78
    "E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
79 79
    ">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
80 80
    "U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
81 81
    "D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
82 82
    "MS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
83 83
    "ATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad"
84
    "</TM_MAD><LAST_MON_TIME>0000000000</LAST_MON_TIME><HOST_SHARE><HID>4</HID>"
84
    "</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>4</HID>"
85 85
    "<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
86 86
    "<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0"
87 87
    "</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED"
......
90 90

  
91 91
const string xml_dump_like_a =
92 92
    "<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
93
    "M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>000000"
94
    "0000</LAST_MON_TIME><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
93
    "M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
94
    "</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
95 95
    "_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
96 96
    ">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
97 97
    "MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
98 98
    "SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
99 99
    "<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
100
    "D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_M"
101
    "ON_TIME><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
100
    "D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
101
    "ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
102 102
    "M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
103 103
    "<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
104 104
    "U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
105 105
    "SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
106 106
    "AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
107
    "M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_MON_TIME><HOS"
107
    "M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
108 108
    "T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
109 109
    "_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
110 110
    "MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
111 111
    "U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
112 112
    "NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
113 113
    "name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
114
    "<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0000000000</LAST_MON_TIME><HOST_SHAR"
114
    "<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
115 115
    "E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
116 116
    ">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
117 117
    "U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
118 118
    "D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
119 119
    "MS>0</RUNNING_VMS></HOST_SHARE></HOST></HOST_POOL>";
120 120

  
121
const string replacement = "0000000000";
122 121

  
122
const string cluster_default =
123
    "<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
123 124

  
125
const string cluster_xml_dump =
126
    "<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
127

  
128
const string host_0_cluster =
129
    "<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
124 130
/* ************************************************************************* */
125 131
/* ************************************************************************* */
126 132

  
......
136 142
    CPPUNIT_TEST (discover);
137 143
    CPPUNIT_TEST (duplicates);
138 144

  
145
    CPPUNIT_TEST (cluster_init);
146
    CPPUNIT_TEST (cluster_allocate);
147
    CPPUNIT_TEST (cluster_drop);
148
    CPPUNIT_TEST (cluster_id);
149
    CPPUNIT_TEST (cluster_dump);
150
    CPPUNIT_TEST (set_cluster);
151
    CPPUNIT_TEST (remove_cluster);
152

  
139 153
    CPPUNIT_TEST_SUITE_END ();
140 154

  
141 155
protected:
......
168 182

  
169 183
        CPPUNIT_ASSERT( name == names[index] );
170 184

  
171
        // Get the xml and replace the LAST_MON_TIME to 0, so we can compare
172
        // it with a prepared string.
185
        // Get the xml
173 186
        host->to_xml(xml_str);
174
        xml_str.replace( xml_str.find("<LAST_MON_TIME>")+15, 10, "0000000000");
175

  
176 187
        CPPUNIT_ASSERT( xml_str == xmls[index]);
177

  
178 188
    };
179 189

  
180 190

  
......
215 225
        CPPUNIT_ASSERT( host->get_state() == Host::DISABLED );
216 226
    };
217 227

  
228
    /* ********************************************************************* */
229

  
218 230
    void duplicates()
219 231
    {
220 232
        int rc, oid_0, oid_1;
......
249 261
        CPPUNIT_ASSERT( host->get_tm_mad() == tm_mad_2 );
250 262
    }
251 263

  
264
    /* ********************************************************************* */
252 265

  
253 266
    void dump()
254 267
    {
......
265 278
        rc = ((HostPool*)pool)->dump(oss, "");
266 279
        CPPUNIT_ASSERT(rc == 0);
267 280

  
268
        // To be able to compare one string to another, the monitoring times
269
        // have to be changed
270 281
        string result = oss.str();
271
        result.replace( 142, 10, replacement);
272
        result.replace( 648, 10, replacement);
273
        result.replace(1154, 10, replacement);
274
        result.replace(1666, 10, replacement);
275
        result.replace(2170, 10, replacement);
276 282

  
277 283
        CPPUNIT_ASSERT( result == xml_dump );
278 284
    }
279 285

  
286
    /* ********************************************************************* */
287

  
280 288
    void dump_where()
281 289
    {
282 290
        string names[] = {"a", "a name", "a_name", "another name", "host"};
......
292 300
        rc = ((HostPool*)pool)->dump(oss, "host_name LIKE 'a%'");
293 301
        CPPUNIT_ASSERT(rc == 0);
294 302

  
295
        // To be able to compare one string to another, the monitoring times
296
        // have to be changed
297
        string result = oss.str();
298
        result.replace( 142, 10, replacement);
299
        result.replace( 648, 10, replacement);
300
        result.replace(1154, 10, replacement);
301
        result.replace(1666, 10, replacement);
302 303

  
304
        string result = oss.str();
303 305

  
304 306
        CPPUNIT_ASSERT( result == xml_dump_like_a );
305 307
    }
306 308

  
309
    /* ********************************************************************* */
310

  
307 311
    void discover()
308 312
    {
309 313
        int  rc, oid, i;
......
332 336
        }
333 337

  
334 338
        // Discover the enabled hosts
335
        rc = hp->discover(&dh);
339
        rc = hp->discover(&dh, 100);
336 340
        CPPUNIT_ASSERT(rc == 0);
337 341
        CPPUNIT_ASSERT(dh.size() == 8);
338 342

  
......
346 350
            CPPUNIT_ASSERT(host->isEnabled());
347 351
        }
348 352
    }
353

  
354
    /* ********************************************************************* */
355
    /* ********************************************************************* */
356

  
357
    void cluster_init()
358
    {
359
        HostPool * hp = static_cast<HostPool *>(pool);
360

  
361
        CPPUNIT_ASSERT( hp->info_cluster(0) == cluster_default );
362
    }
363

  
364
    /* ********************************************************************* */
365

  
366
    void cluster_allocate()
367
    {
368
        HostPool * hp = static_cast<HostPool *>(pool);
369
        int clid, rc;
370

  
371
        rc = hp->allocate_cluster(&clid, "new_cluster");
372
        CPPUNIT_ASSERT( rc == clid );
373
        CPPUNIT_ASSERT( clid == 1 );
374

  
375
        CPPUNIT_ASSERT( hp->info_cluster(clid) ==
376
                "<CLUSTER><ID>1</ID><NAME>new_cluster</NAME></CLUSTER>");
377

  
378
        // Try to allocate using the same name
379
        rc = hp->allocate_cluster(&clid, "new_cluster");
380
        CPPUNIT_ASSERT( rc == clid );
381
        CPPUNIT_ASSERT( clid == -1 );
382
    }
383

  
384
    /* ********************************************************************* */
385

  
386
    void cluster_drop()
387
    {
388
        HostPool * hp = static_cast<HostPool *>(pool);
389
        int clid, rc;
390

  
391
        // Drop a non-existing cluster
392
        rc = hp->drop_cluster(20);
393
        CPPUNIT_ASSERT( rc == -1 );
394

  
395
        // Allocate a cluster and drop it
396
        rc = hp->allocate_cluster(&clid, "new_cluster");
397
        CPPUNIT_ASSERT( clid == 1);
398

  
399
        rc = hp->drop_cluster(clid);
400
        CPPUNIT_ASSERT( rc == 0 );
401

  
402
        // Try to drop the default cluster, should fail
403
        rc = hp->drop_cluster(0);
404
        CPPUNIT_ASSERT( rc == -1 );
405
    }
406

  
407
    /* ********************************************************************* */
408

  
409
    void cluster_id()
410
    {
411
        HostPool * hp = static_cast<HostPool *>(pool);
412
        int clid, rc;
413
        ostringstream oss;
414

  
415
        // Allocate some clusters
416
        rc = hp->allocate_cluster(&clid, "cluster_a");
417
        CPPUNIT_ASSERT( rc == 1 );
418

  
419
        rc = hp->allocate_cluster(&clid, "cluster_b");
420
        CPPUNIT_ASSERT( rc == 2 );
421

  
422
        rc = hp->allocate_cluster(&clid, "cluster_c");
423
        CPPUNIT_ASSERT( rc == 3 );
424

  
425
        rc = hp->allocate_cluster(&clid, "cluster_d");
426
        CPPUNIT_ASSERT( rc == 4 );
427

  
428
        // Drop id 2
429
        rc = hp->drop_cluster(2);
430
        CPPUNIT_ASSERT( rc == 0 );
431

  
432
        // Next one should use id 5, because the biggest id is 4
433
        rc = hp->allocate_cluster(&clid, "cluster_e");
434
        CPPUNIT_ASSERT( rc == 5 );
435

  
436
        // Drop id 5
437
        rc = hp->drop_cluster(5);
438
        CPPUNIT_ASSERT( rc == 0 );
439

  
440
        // Next one should use id 5, because the biggest id is 4 again
441
        rc = hp->allocate_cluster(&clid, "cluster_f");
442
        CPPUNIT_ASSERT( rc == 5 );
443

  
444
    }
445

  
446
    /* ********************************************************************* */
447

  
448
    void cluster_dump()
449
    {
450
        HostPool * hp = static_cast<HostPool *>(pool);
451
        int clid, rc;
452
        ostringstream oss;
453

  
454
        // Allocate some clusters
455
        rc = hp->allocate_cluster(&clid, "cluster_a");
456
        CPPUNIT_ASSERT( rc == 1 );
457

  
458
        rc = hp->allocate_cluster(&clid, "cluster_b");
459
        CPPUNIT_ASSERT( rc == 2 );
460

  
461
        rc = hp->allocate_cluster(&clid, "cluster_c");
462
        CPPUNIT_ASSERT( rc == 3 );
463

  
464
        rc = hp->allocate_cluster(&clid, "cluster_d");
465
        CPPUNIT_ASSERT( rc == 4 );
466

  
467

  
468
        // Drop one of them
469
        rc = hp->drop_cluster(2);
470
        CPPUNIT_ASSERT( rc == 0 );
471

  
472
        // dump the pool
473
        rc = hp->dump_cluster(oss);
474
        CPPUNIT_ASSERT( oss.str() == cluster_xml_dump );
475
    }
476

  
477
    /* ********************************************************************* */
478

  
479
    void set_cluster()
480
    {
481
        HostPool *  hp = static_cast<HostPool *>(pool);
482
        Host*       host;
483
        int         clid, rc, oid;
484
        string      xml_str;
485

  
486
        // Allocate a host
487
        oid = allocate(0);
488

  
489
        host = hp->get(0, false);
490

  
491
        rc = hp->allocate_cluster(&clid, "cluster_a");
492
        CPPUNIT_ASSERT( rc == 1 );
493

  
494
        rc = hp->set_cluster(host, clid);
495
        CPPUNIT_ASSERT( rc == 0 );
496

  
497
        host->to_xml(xml_str);
498
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
499

  
500

  
501
        // Try to set a non-existing cluster
502
        rc = hp->set_cluster(host, 20);
503
        CPPUNIT_ASSERT( rc == -1 );
504

  
505
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
506

  
507
    }
508

  
509
    /* ********************************************************************* */
510

  
511
    void remove_cluster()
512
    {
513
        HostPool *  hp = static_cast<HostPool *>(pool);
514
        Host*       host;
515
        int         clid, rc, oid;
516
        string      xml_str;
517

  
518
        // Allocate a host
519
        oid = allocate(0);
520

  
521
        host = hp->get(0, false);
522

  
523
        rc = hp->allocate_cluster(&clid, "cluster_a");
524
        CPPUNIT_ASSERT( rc == 1 );
525

  
526
        // Set host 0 to cluster 1
527
        rc = hp->set_cluster(host, clid);
528
        CPPUNIT_ASSERT( rc == 0 );
529

  
530
        // Check
531
        host->to_xml(xml_str);
532
        CPPUNIT_ASSERT( xml_str == host_0_cluster);
533

  
534
        // Remove the cluster
535
        rc = hp->set_default_cluster(host);
536
        CPPUNIT_ASSERT( rc == 0 );
537

  
538
        // The host should have been moved to the default cluster
539
        host->to_xml(xml_str);
540
        check(0, host);
541
    }
349 542
};
350 543

  
544

  
351 545
/* ************************************************************************* */
352 546
/* ************************************************************************* */
353 547
/* ************************************************************************* */
src/rm/RequestManagerClusterAdd.cc
54 54
        goto error_authenticate;
55 55
    }
56 56

  
57

  
58
    // Check if cluster exists
59
    if ( !ClusterAdd::hpool->exists_cluster(clid) )
60
    {
61
        goto error_cluster;
62
    }
63

  
64 57
    // Check if host exists
65 58
    host = ClusterAdd::hpool->get(hid,true);
66 59

  
......
97 90
    oss << "User not authorized to add hosts to clusters";
98 91
    goto error_common;
99 92

  
100
error_cluster:
101
    oss << "Error getting cluster with CLID = " << clid;
102
    goto error_common;
103

  
104 93
error_host_get:
105 94
    oss << "The host " << hid << " does not exists";
106 95
    goto error_common;
......
108 97
error_cluster_add:
109 98
    host->unlock();
110 99

  
111
    oss << "Can not add host " << hid << " to cluster " << clid << 
100
    oss << "Can not add host " << hid << " to cluster " << clid <<
112 101
           ", returned error code [" << rc << "]";
113 102
    goto error_common;
114 103

  
src/rm/RequestManagerClusterDelete.cc
23 23
void RequestManager::ClusterDelete::execute(
24 24
    xmlrpc_c::paramList const& paramList,
25 25
    xmlrpc_c::value *   const  retval)
26
{ 
26
{
27 27
    string              session;
28 28

  
29 29
    // <clid> of the cluster to delete from the HostPool
......
43 43

  
44 44
    // Only oneadmin can delete clusters
45 45
    rc = ClusterDelete::upool->authenticate(session);
46
    
46

  
47 47
    if ( rc != 0 )
48 48
    {
49 49
        goto error_authenticate;
50 50
    }
51 51

  
52

  
53
    // Check if cluster exists
54
    if ( !ClusterDelete::hpool->exists_cluster(clid) )
55
    {
56
        goto error_cluster;
57
    }
58

  
59 52
    rc = ClusterDelete::hpool->drop_cluster(clid);
60 53

  
61 54
    if ( rc != 0 )
......
78 71
    oss << "User not authorized to delete clusters";
79 72
    goto error_common;
80 73

  
81
error_cluster:
82
    oss << "Error getting cluster with CLID = " << clid;
83
    goto error_common;
84

  
85 74
error_cluster_delete:
86
    oss << "Can not delete cluster with CLID " << clid << 
75
    oss << "Can not delete cluster with CLID " << clid <<
87 76
           " from the ClusterPool, returned error code [" << rc << "]";
88 77
    goto error_common;
89 78

  
src/rm/RequestManagerClusterInfo.cc
50 50
        goto error_authenticate;
51 51
    }
52 52

  
53
    // Check if cluster exists
54
    rc = ClusterInfo::hpool->exists_cluster(clid);
53
    info = ClusterInfo::hpool->info_cluster(clid);
55 54

  
56
    if ( rc != 0 )
55
    // Cluster does not exists
56
    if ( info.empty() )
57 57
    {
58 58
        goto error_cluster;
59 59
    }
60 60

  
61
    info = ClusterInfo::hpool->info_cluster(clid);
62

  
63 61
    // All nice, return the cluster info to the client
64 62
    arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS
65 63
    arrayData.push_back(xmlrpc_c::value_string(info));
src/rm/RequestManagerClusterRemove.cc
62 62
    }
63 63

  
64 64
    // Remove host from cluster
65
    rc = ClusterRemove::hpool->remove_cluster(host);
65
    rc = ClusterRemove::hpool->set_default_cluster(host);
66 66

  
67 67
    if ( rc != 0 )
68 68
    {

Also available in: Unified diff