Skip to content

Commit 147d4b8

Browse files
committed
Add basic Datastore implementation including tests
1 parent b1263cb commit 147d4b8

6 files changed

Lines changed: 249 additions & 1 deletion

File tree

oca/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from template import VmTemplate, VmTemplatePool
1616
from exceptions import OpenNebulaException
1717
from cluster import Cluster, ClusterPool
18+
from datastore import Datastore, DatastorePool
1819

1920

2021
CONNECTED = -3
@@ -125,5 +126,5 @@ def call(self, function, *args):
125126
VirtualMachinePool, User, UserPool,
126127
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
127128
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
128-
Cluster, ClusterPool]
129+
Cluster, ClusterPool, Datastore, DatastorePool]
129130

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
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' : str,
16+
'uid' : int,
17+
'gid' : int,
18+
'uname' : str,
19+
'gname' : str,
20+
#'permissions' : Permissions,
21+
'ds_mad' : str,
22+
'tm_mad' : str,
23+
'base_path' : str,
24+
'type' : int,
25+
'disk_type' : int,
26+
#'state' : ???,
27+
'cluster_id' : int,
28+
'cluster' : lambda element: element if isinstance(element, str) else '',
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/tests/fixtures/datastore.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<DATASTORE>
2+
<ID>100</ID>
3+
<UID>0</UID>
4+
<GID>0</GID>
5+
<UNAME>oneadmin</UNAME>
6+
<GNAME>oneadmin</GNAME>
7+
<NAME>Custom-DS</NAME>
8+
<PERMISSIONS>
9+
<OWNER_U>1</OWNER_U>
10+
<OWNER_M>1</OWNER_M>
11+
<OWNER_A>0</OWNER_A>
12+
<GROUP_U>1</GROUP_U>
13+
<GROUP_M>0</GROUP_M>
14+
<GROUP_A>0</GROUP_A>
15+
<OTHER_U>0</OTHER_U>
16+
<OTHER_M>0</OTHER_M>
17+
<OTHER_A>0</OTHER_A>
18+
</PERMISSIONS>
19+
<DS_MAD><![CDATA[fs]]></DS_MAD>
20+
<TM_MAD><![CDATA[ssh]]></TM_MAD>
21+
<BASE_PATH><![CDATA[/var/lib/one//datastores/100]]></BASE_PATH>
22+
<TYPE>0</TYPE>
23+
<DISK_TYPE>0</DISK_TYPE>
24+
<STATE>0</STATE>
25+
<CLUSTER_ID>-1</CLUSTER_ID>
26+
<CLUSTER/>
27+
<TOTAL_MB>9952</TOTAL_MB>
28+
<FREE_MB>8999</FREE_MB>
29+
<USED_MB>425</USED_MB>
30+
<IMAGES>
31+
<ID>2</ID>
32+
<ID>3</ID>
33+
</IMAGES>
34+
<TEMPLATE>
35+
<BASE_PATH><![CDATA[/var/lib/one//datastores/]]></BASE_PATH>
36+
<CLONE_TARGET><![CDATA[SYSTEM]]></CLONE_TARGET>
37+
<DISK_TYPE><![CDATA[FILE]]></DISK_TYPE>
38+
<DS_MAD><![CDATA[fs]]></DS_MAD>
39+
<LN_TARGET><![CDATA[SYSTEM]]></LN_TARGET>
40+
<TM_MAD><![CDATA[ssh]]></TM_MAD>
41+
</TEMPLATE>
42+
</DATASTORE>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<DATASTORE_POOL>
2+
<DATASTORE>
3+
<ID>0</ID>
4+
<UID>0</UID>
5+
<GID>0</GID>
6+
<UNAME>oneadmin</UNAME>
7+
<GNAME>oneadmin</GNAME>
8+
<NAME>system</NAME>
9+
<PERMISSIONS>
10+
<OWNER_U>1</OWNER_U>
11+
<OWNER_M>1</OWNER_M>
12+
<OWNER_A>0</OWNER_A>
13+
<GROUP_U>1</GROUP_U>
14+
<GROUP_M>0</GROUP_M>
15+
<GROUP_A>0</GROUP_A>
16+
<OTHER_U>0</OTHER_U>
17+
<OTHER_M>0</OTHER_M>
18+
<OTHER_A>0</OTHER_A>
19+
</PERMISSIONS>
20+
<DS_MAD><![CDATA[-]]></DS_MAD>
21+
<TM_MAD><![CDATA[shared]]></TM_MAD>
22+
<BASE_PATH><![CDATA[/var/lib/one//datastores/0]]></BASE_PATH>
23+
<TYPE>1</TYPE>
24+
<DISK_TYPE>0</DISK_TYPE>
25+
<STATE>0</STATE>
26+
<CLUSTER_ID>-1</CLUSTER_ID>
27+
<CLUSTER>test</CLUSTER>
28+
<TOTAL_MB>0</TOTAL_MB>
29+
<FREE_MB>0</FREE_MB>
30+
<USED_MB>0</USED_MB>
31+
<IMAGES/>
32+
<TEMPLATE>
33+
<BASE_PATH><![CDATA[/var/lib/one//datastores/]]></BASE_PATH>
34+
<SHARED><![CDATA[YES]]></SHARED>
35+
<TM_MAD><![CDATA[shared]]></TM_MAD>
36+
<TYPE><![CDATA[SYSTEM_DS]]></TYPE>
37+
</TEMPLATE>
38+
</DATASTORE>
39+
<DATASTORE>
40+
<ID>1</ID>
41+
<UID>0</UID>
42+
<GID>0</GID>
43+
<UNAME>oneadmin</UNAME>
44+
<GNAME>oneadmin</GNAME>
45+
<NAME>default</NAME>
46+
<PERMISSIONS>
47+
<OWNER_U>1</OWNER_U>
48+
<OWNER_M>1</OWNER_M>
49+
<OWNER_A>0</OWNER_A>
50+
<GROUP_U>1</GROUP_U>
51+
<GROUP_M>0</GROUP_M>
52+
<GROUP_A>0</GROUP_A>
53+
<OTHER_U>0</OTHER_U>
54+
<OTHER_M>0</OTHER_M>
55+
<OTHER_A>0</OTHER_A>
56+
</PERMISSIONS>
57+
<DS_MAD><![CDATA[fs]]></DS_MAD>
58+
<TM_MAD><![CDATA[shared]]></TM_MAD>
59+
<BASE_PATH><![CDATA[/var/lib/one//datastores/1]]></BASE_PATH>
60+
<TYPE>0</TYPE>
61+
<DISK_TYPE>0</DISK_TYPE>
62+
<STATE>0</STATE>
63+
<CLUSTER_ID>-1</CLUSTER_ID>
64+
<CLUSTER/>
65+
<TOTAL_MB>9952</TOTAL_MB>
66+
<FREE_MB>8999</FREE_MB>
67+
<USED_MB>425</USED_MB>
68+
<IMAGES/>
69+
<TEMPLATE>
70+
<BASE_PATH><![CDATA[/var/lib/one//datastores/]]></BASE_PATH>
71+
<CLONE_TARGET><![CDATA[SYSTEM]]></CLONE_TARGET>
72+
<DISK_TYPE><![CDATA[FILE]]></DISK_TYPE>
73+
<DS_MAD><![CDATA[fs]]></DS_MAD>
74+
<LN_TARGET><![CDATA[NONE]]></LN_TARGET>
75+
<TM_MAD><![CDATA[shared]]></TM_MAD>
76+
<TYPE><![CDATA[IMAGE_DS]]></TYPE>
77+
</TEMPLATE>
78+
</DATASTORE>
79+
</DATASTORE_POOL>

oca/tests/test_datastore.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: UTF-8 -*-
2+
import os
3+
4+
from mock import Mock
5+
6+
import oca
7+
8+
9+
class TestDatastore:
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/datastore.xml')).read()
14+
15+
def test_allocate(self):
16+
self.client.call = Mock(return_value=3)
17+
assert oca.Datastore.allocate(self.client, 'test') == 3
18+
19+
def test_repr(self):
20+
self.client.call = Mock()
21+
datastore = oca.Datastore(self.xml, self.client)
22+
assert repr(datastore) == '<oca.Datastore("Custom-DS")>'
23+
24+
def test_convert_types(self):
25+
cluster = oca.Datastore(self.xml, None)
26+
cluster._convert_types()
27+
assert cluster.id == 100
28+
assert cluster.name == "Custom-DS"
29+
assert cluster.uid == 0
30+
assert cluster.gid == 0
31+
assert cluster.uname == 'oneadmin'
32+
assert cluster.gname == 'oneadmin'
33+
assert cluster.ds_mad == 'fs'
34+
assert cluster.tm_mad == 'ssh'
35+
assert cluster.base_path == '/var/lib/one//datastores/100'
36+
assert cluster.type == 0
37+
assert cluster.disk_type == 0
38+
assert cluster.cluster_id == -1
39+
assert cluster.cluster == ""
40+
assert cluster.total_mb == 9952
41+
assert cluster.free_mb == 8999
42+
assert cluster.used_mb == 425
43+
assert cluster.image_ids == [2,3]
44+
assert cluster.template.disk_type == 'FILE'

oca/tests/test_integration.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import oca
2+
3+
class TestIntegration:
4+
def setUp(self):
5+
self.client = oca.Client("oneadmin:one", "http://localhost:2633/RPC2")
6+
cp = oca.ClusterPool(self.client)
7+
cp.info()
8+
for cluster in cp:
9+
cluster.delete()
10+
11+
def test_integration(self):
12+
self.cluster_id = oca.Cluster.allocate(self.client, "IT-Cluster")
13+
#self.datastore_id = oca.

0 commit comments

Comments
 (0)