Skip to content

Commit b1263cb

Browse files
committed
Add initial cluster support.
Clusters can be listed, created and deleted. Host creation now accepts the new cluster_id parameter. If no cluster_id is passed to host.allocate, the host is not assigned to a cluster.
1 parent fb45cfe commit b1263cb

7 files changed

Lines changed: 157 additions & 3 deletions

File tree

oca/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from group import Group, GroupPool
1515
from template import VmTemplate, VmTemplatePool
1616
from exceptions import OpenNebulaException
17+
from cluster import Cluster, ClusterPool
1718

1819

1920
CONNECTED = -3
@@ -123,5 +124,6 @@ def call(self, function, *args):
123124
__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
124125
VirtualMachinePool, User, UserPool,
125126
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
126-
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED]
127+
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
128+
Cluster, ClusterPool]
127129

oca/cluster.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: UTF-8 -*-
2+
from pool import Pool, PoolElement, Template
3+
4+
class Cluster(PoolElement):
5+
METHODS = {
6+
#'info' : 'cluster.info',
7+
'allocate' : 'cluster.allocate',
8+
'delete' : 'cluster.delete',
9+
#'enable' : 'cluster.enable',
10+
#'update' : 'cluster.update'
11+
}
12+
13+
XML_TYPES = {
14+
'id' : int,
15+
'name' : str,
16+
'host_ids' : ['HOSTS', lambda hosts: map(lambda host_id: int(host_id.text), hosts)],
17+
'datastore_ids' : ['DATASTORES', lambda datastores: map(lambda datastore_id: int(datastore_id.text), datastores)],
18+
'vnet_ids' : ['VNETS', lambda vnets: map(lambda vnet_id: int(vnet_id.text), vnets)],
19+
'template' : ['TEMPLATE', Template],
20+
}
21+
22+
ELEMENT_NAME = 'CLUSTER'
23+
24+
@staticmethod
25+
def allocate(client, cluster_name):
26+
'''
27+
Adds a cluster to the cluster list
28+
29+
Arguments
30+
31+
``cluster_name``
32+
Clustername to add
33+
'''
34+
cluster_id = client.call(Cluster.METHODS['allocate'], cluster_name)
35+
return cluster_id
36+
37+
def __init__(self, xml, client):
38+
super(Cluster, self).__init__(xml, client)
39+
self._convert_types()
40+
41+
def __repr__(self):
42+
return '<oca.Cluster("%s")>' % self.name
43+
44+
45+
class ClusterPool(Pool):
46+
METHODS = {
47+
'info' : 'clusterpool.info',
48+
}
49+
50+
def __init__(self, client):
51+
super(ClusterPool, self).__init__('CLUSTER_POOL', 'CLUSTER', client)
52+
53+
def _factory(self, xml):
54+
c = Cluster(xml, self.client)
55+
return c
56+

oca/host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Host(PoolElement):
4646
ELEMENT_NAME = 'HOST'
4747

4848
@staticmethod
49-
def allocate(client, hostname, im, vmm, tm):
49+
def allocate(client, hostname, im, vmm, tm, cluster_id=-1):
5050
'''
5151
Adds a host to the host list
5252
@@ -64,7 +64,7 @@ def allocate(client, hostname, im, vmm, tm):
6464
``tm``
6565
Transfer manager
6666
'''
67-
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm)
67+
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm, cluster_id)
6868
return host_id
6969

7070
def __init__(self, xml, client):

oca/tests/fixtures/cluster.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<CLUSTER>
2+
<ID>101</ID>
3+
<NAME>oneCluster</NAME>
4+
<HOSTS>
5+
<ID>2</ID>
6+
<ID>3</ID>
7+
</HOSTS>
8+
<DATASTORES>
9+
<ID>4</ID>
10+
<ID>5</ID>
11+
</DATASTORES>
12+
<VNETS>
13+
<ID>6</ID>
14+
<ID>7</ID>
15+
</VNETS>
16+
<TEMPLATE>
17+
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
18+
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
19+
</TEMPLATE>
20+
</CLUSTER>

oca/tests/fixtures/clusterpool.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<CLUSTER_POOL>
2+
<CLUSTER>
3+
<ID>101</ID>
4+
<NAME>oneCluster</NAME>
5+
<HOSTS/>
6+
<DATASTORES/>
7+
<VNETS/>
8+
<TEMPLATE>
9+
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
10+
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
11+
</TEMPLATE>
12+
</CLUSTER>
13+
<CLUSTER>
14+
<ID>102</ID>
15+
<NAME>anotherCluster</NAME>
16+
<HOSTS/>
17+
<DATASTORES/>
18+
<VNETS/>
19+
<TEMPLATE>
20+
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
21+
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
22+
</TEMPLATE>
23+
</CLUSTER>
24+
</CLUSTER_POOL>

oca/tests/test_cluster.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: UTF-8 -*-
2+
import os
3+
4+
from mock import Mock
5+
6+
import oca
7+
8+
9+
class TestCluster:
10+
def setUp(self):
11+
self.client = oca.Client('test:test')
12+
self.xml = open(os.path.join(os.path.dirname(oca.__file__),
13+
'tests/fixtures/cluster.xml')).read()
14+
15+
def test_allocate(self):
16+
self.client.call = Mock(return_value=3)
17+
assert oca.Cluster.allocate(self.client, 'test') == 3
18+
19+
def test_repr(self):
20+
self.client.call = Mock()
21+
cluster = oca.Cluster(self.xml, self.client)
22+
assert repr(cluster) == '<oca.Cluster("oneCluster")>'
23+
24+
def test_convert_types(self):
25+
cluster = oca.Cluster(self.xml, None)
26+
cluster._convert_types()
27+
assert cluster.id == 101
28+
assert cluster.name == "oneCluster"
29+
assert cluster.host_ids == [2,3]
30+
assert cluster.datastore_ids == [4,5]
31+
assert cluster.vnet_ids == [6,7]
32+
assert cluster.template.reserved_cpu is None

oca/tests/test_clusterpool.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: UTF-8 -*-
2+
import os
3+
4+
from mock import Mock
5+
6+
import oca
7+
8+
9+
class TestClusterPool:
10+
def setUp(self):
11+
self.client = oca.Client('test:test')
12+
self.xml = open(os.path.join(os.path.dirname(oca.__file__),
13+
'tests/fixtures/clusterpool.xml')).read()
14+
15+
def test_info(self):
16+
self.client.call = Mock(return_value=self.xml)
17+
pool = oca.ClusterPool(self.client)
18+
pool.info()
19+
assert len(pool) == 2
20+
assert pool[1].name == "anotherCluster"

0 commit comments

Comments
 (0)