Skip to content

Commit ac18385

Browse files
committed
Merge pull request #11 from python-oca/opennebula-4.10
Opennebula 4.10
2 parents ae03280 + 4b0d2c9 commit ac18385

26 files changed

+956
-291
lines changed

oca/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from group import Group, GroupPool
1515
from template import VmTemplate, VmTemplatePool
1616
from exceptions import OpenNebulaException
17+
from cluster import Cluster, ClusterPool
18+
from datastore import Datastore, DatastorePool
1719

1820

1921
CONNECTED = -3
@@ -129,5 +131,6 @@ def version(self):
129131
__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
130132
VirtualMachinePool, User, UserPool,
131133
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
132-
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED]
134+
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
135+
Cluster, ClusterPool, Datastore, DatastorePool]
133136

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, extractString
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' : extractString,
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/datastore.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: UTF-8 -*-
2+
from pool import Pool, PoolElement, Template, extractString
3+
4+
class Datastore(PoolElement):
5+
METHODS = {
6+
#'info' : 'datastore.info',
7+
'allocate' : 'datastore.allocate',
8+
'delete' : 'datastore.delete',
9+
#'enable' : 'datastore.enable',
10+
#'update' : 'datastore.update'
11+
}
12+
13+
XML_TYPES = {
14+
'id' : int,
15+
'name' : extractString,
16+
'uid' : int,
17+
'gid' : int,
18+
'uname' : extractString,
19+
'gname' : extractString,
20+
#'permissions' : Permissions,
21+
'ds_mad' : extractString,
22+
'tm_mad' : extractString,
23+
'base_path' : extractString,
24+
'type' : int,
25+
'disk_type' : int,
26+
#'state' : ???,
27+
'cluster_id' : int,
28+
'cluster' : extractString,
29+
'total_mb' : int,
30+
'free_mb' : int,
31+
'used_mb' : int,
32+
'image_ids' : ['IMAGES', lambda images: map(lambda image_id: int(image_id.text), images)],
33+
'template' : ['TEMPLATE', Template],
34+
}
35+
36+
ELEMENT_NAME = 'DATASTORE'
37+
38+
@staticmethod
39+
def allocate(client, datastore_template):
40+
'''
41+
Adds a datastore to the datastore list
42+
43+
Arguments
44+
45+
``datastore_template``
46+
Template for the datastore to add
47+
'''
48+
datastore_id = client.call(Datastore.METHODS['allocate'], datastore_template)
49+
return datastore_id
50+
51+
def __init__(self, xml, client):
52+
super(Datastore, self).__init__(xml, client)
53+
self._convert_types()
54+
55+
def __repr__(self):
56+
return '<oca.Datastore("%s")>' % self.name
57+
58+
59+
class DatastorePool(Pool):
60+
METHODS = {
61+
'info' : 'datastorepool.info',
62+
}
63+
64+
def __init__(self, client):
65+
super(DatastorePool, self).__init__('DATASTORE_POOL', 'DATASTORE', client)
66+
67+
def _factory(self, xml):
68+
c = Datastore(xml, self.client)
69+
return c

oca/group.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: UTF-8 -*-
2-
from pool import Pool, PoolElement, Template
2+
from pool import Pool, PoolElement, Template, extractString
33

44

55
class Group(PoolElement):
@@ -11,7 +11,7 @@ class Group(PoolElement):
1111

1212
XML_TYPES = {
1313
'id' : int,
14-
'name' : str,
14+
'name' : extractString,
1515
'template' : ['TEMPLATE', Template],
1616
'users' : ['USERS', lambda users: [int(i.text) for i in users]],
1717
#'resource_providers': handled separately
@@ -54,7 +54,7 @@ class GroupPool(Pool):
5454
}
5555

5656
def __init__(self, client):
57-
super(GroupPool, self).__init__('GROUP_POOL', 'POOL', client)
57+
super(GroupPool, self).__init__('GROUP_POOL', 'GROUP', client)
5858

5959
def _factory(self, xml):
6060
i = Group(xml, self.client)

oca/host.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: UTF-8 -*-
2-
from pool import Pool, PoolElement, Template
2+
from pool import Pool, PoolElement, Template, extractString
33

44

55
class HostShare(Template):
@@ -17,36 +17,46 @@ class Host(PoolElement):
1717
}
1818

1919
INIT = 0
20-
MONITORING = 1
20+
MONITORING_MONITORED = 1 # Currently monitoring, previously MONITORED
2121
MONITORED = 2
2222
ERROR = 3
2323
DISABLED = 4
24-
HOST_STATES = ['INIT', 'MONITORING', 'MONITORED', 'ERROR', 'DISABLED']
24+
MONITORING_ERROR = 5 # Currently monitoring, previously ERROR
25+
MONITORING_INIT = 6 # Currently monitoring, previously initialized
26+
MONITORING_DISABLED = 7 # Currently monitoring, previously DISABLED
27+
HOST_STATES = ['INIT', 'MONITORING_MONITORED', 'MONITORED', 'ERROR', 'DISABLED',
28+
'MONITORING_ERROR', 'MONITORING_INIT', 'MONITORING_DISABLED']
2529

2630
SHORT_HOST_STATES = {
27-
'INIT' : 'on',
28-
'MONITORING' : 'on',
29-
'MONITORED' : 'on',
30-
'ERROR' : 'err',
31-
'DISABLED' : 'off'
31+
'INIT': 'on',
32+
'MONITORING_MONITORED': 'on',
33+
'MONITORED': 'on',
34+
'ERROR': 'err',
35+
'DISABLED': 'off',
36+
'MONITORING_ERROR': 'on',
37+
'MONITORING_INIT': 'on',
38+
'MONITORING_DISABLED': 'on',
3239
}
3340

3441
XML_TYPES = {
35-
'id' : int,
36-
'name' : str,
37-
'state' : int,
38-
'im_mad' : str,
39-
'vm_mad' : str,
40-
'last_mon_time' : int,
41-
'vm_ids' : ['VMS', lambda vms: map(lambda vmid: int(vmid.text), vms)],
42-
'template' : ['TEMPLATE', Template],
43-
'host_share' : ['HOST_SHARE', HostShare],
42+
'id': int,
43+
'name': extractString,
44+
'state': int,
45+
'im_mad': extractString,
46+
'vm_mad': extractString,
47+
'vn_mad': extractString,
48+
'last_mon_time': int,
49+
'cluster': extractString,
50+
'cluster_id': int,
51+
'vm_ids': ['VMS', lambda vms: map(lambda vmid: int(vmid.text), vms)],
52+
'template': ['TEMPLATE', Template],
53+
'host_share': ['HOST_SHARE', HostShare],
4454
}
4555

4656
ELEMENT_NAME = 'HOST'
4757

4858
@staticmethod
49-
def allocate(client, hostname, im, vmm, tm):
59+
def allocate(client, hostname, im, vmm, tm, cluster_id=-1):
5060
'''
5161
Adds a host to the host list
5262
@@ -64,7 +74,7 @@ def allocate(client, hostname, im, vmm, tm):
6474
``tm``
6575
Transfer manager
6676
'''
67-
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm)
77+
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm, cluster_id)
6878
return host_id
6979

7080
def __init__(self, xml, client):

oca/image.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: UTF-8 -*-
2-
from pool import Pool, PoolElement, Template
2+
from pool import Pool, PoolElement, Template, extractString
33

44

55
class Image(PoolElement):
@@ -18,16 +18,26 @@ class Image(PoolElement):
1818
'id' : int,
1919
'uid' : int,
2020
'gid' : int,
21-
'uname' : str,
22-
'gname' : str,
23-
'name' : str,
21+
'uname' : extractString,
22+
'gname' : extractString,
23+
'name' : extractString,
24+
#'permissions' : ???,
2425
'type' : int,
25-
'public' : int,
26+
'disk_type' : int,
2627
'persistent' : int,
2728
'regtime' : int,
28-
'source' : str,
29+
'source' : extractString,
30+
'path' : extractString,
31+
'fstype' : extractString,
32+
'size' : int,
2933
'state' : int,
3034
'running_vms' : int,
35+
'cloning_ops' : int,
36+
'cloning_id' : int,
37+
'datastore_id': int,
38+
'datastore' : extractString,
39+
'vm_ids' : ["VMS", lambda vms: map(lambda vm_id: int(vm_id.text), vms)],
40+
'clone_ids' : ["CLONES", lambda clones: map(lambda clone_id: int(clone_id.text), clones)],
3141
'template' : ['TEMPLATE', Template],
3242
}
3343

@@ -176,7 +186,7 @@ class ImagePool(Pool):
176186
}
177187

178188
def __init__(self, client):
179-
super(ImagePool, self).__init__('IMAGE_POOL', 'POOL', client)
189+
super(ImagePool, self).__init__('IMAGE_POOL', 'IMAGE', client)
180190

181191
def _factory(self, xml):
182192
i = Image(xml, self.client)

oca/pool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class WrongNameError(OpenNebulaException):
1212
class WrongIdError(OpenNebulaException):
1313
pass
1414

15+
def extractString(xml_or_string):
16+
if isinstance(xml_or_string, str):
17+
return xml_or_string
18+
else:
19+
return xml_or_string.text or ''
1520

1621
class Template(object):
1722
def __init__(self, xml_element, multiple=[]):
@@ -60,7 +65,7 @@ def __getitem__(self, key):
6065
else:
6166
return value
6267
else:
63-
raise IndexError()
68+
raise IndexError("Key {0} not found!".format(key))
6469

6570
def __getattr__(self, name):
6671
try:
@@ -103,7 +108,7 @@ def info(self, filter=-3, range_start=-1, range_end=-1, *args):
103108
data = self.client.call(self.METHODS['info'], filter,
104109
range_start, range_end, *args)
105110
self._initialize_xml(data, self.pool_name)
106-
for element in self.xml:
111+
for element in self.xml.findall(self.element_name):
107112
self.append(self._factory(element))
108113

109114
def _factory(self):

oca/template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class VmTemplatePool(Pool):
9595
}
9696

9797
def __init__(self, client):
98-
super(VmTemplatePool, self).__init__('VMTEMPLATE_POOL', 'POOL', client)
98+
super(VmTemplatePool, self).__init__('VMTEMPLATE_POOL', 'VMTEMPLATE', client)
9999

100100
#def info(self,
101101

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>

0 commit comments

Comments
 (0)