diff --git a/.gitignore b/.gitignore index 2731c8f..ba797f1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.coverage oca.egg* .tox +.idea \ No newline at end of file diff --git a/oca/__init__.py b/oca/__init__.py index 3582129..3d827b1 100644 --- a/oca/__init__.py +++ b/oca/__init__.py @@ -16,6 +16,7 @@ from exceptions import OpenNebulaException from cluster import Cluster, ClusterPool from datastore import Datastore, DatastorePool +from zone import Zone, ZonePool CONNECTED = -3 @@ -119,7 +120,7 @@ def call(self, function, *args): #connection error raise e if not is_success: - raise OpenNebulaException(data) + raise OpenNebulaException(data, return_code) return data def version(self): @@ -132,5 +133,5 @@ def version(self): VirtualMachinePool, User, UserPool, Image, ImagePool, VirtualNetwork, VirtualNetworkPool, Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED, - Cluster, ClusterPool, Datastore, DatastorePool] + Cluster, ClusterPool, Datastore, DatastorePool, Zone, ZonePool] diff --git a/oca/exceptions.py b/oca/exceptions.py index c308d45..41d0b57 100644 --- a/oca/exceptions.py +++ b/oca/exceptions.py @@ -2,5 +2,6 @@ class OpenNebulaException(Exception): - pass - + def __init__(self, data, return_code): + self.return_code = return_code + super(OpenNebulaException, self).__init__(data) diff --git a/oca/pool.py b/oca/pool.py index 422dc01..e526255 100644 --- a/oca/pool.py +++ b/oca/pool.py @@ -13,7 +13,7 @@ class WrongIdError(OpenNebulaException): pass def extractString(xml_or_string): - if isinstance(xml_or_string, str): + if isinstance(xml_or_string, basestring): return xml_or_string else: return xml_or_string.text or '' @@ -53,7 +53,7 @@ def __init__(self, xml=None): def _initialize_xml(self, xml, root_element): hidden_character=u"\u200b" - self.xml = ET.fromstring(xml.replace(hidden_character,"")) + self.xml = ET.fromstring(xml.replace(hidden_character,"").encode("utf-8")) if self.xml.tag != root_element.upper(): self.xml = None self._convert_types() diff --git a/oca/template.py b/oca/template.py index 531b81b..7de3998 100644 --- a/oca/template.py +++ b/oca/template.py @@ -21,7 +21,7 @@ class VmTemplate(PoolElement): 'uname' : str, 'gname' : str, 'regtime' : int, - 'template' : ['TEMPLATE', Template] + 'template' : ['TEMPLATE', Template, ['DISK', 'OS', 'NIC', 'GRAPHICS', 'CONTEXT']] } ELEMENT_NAME = 'VMTEMPLATE' @@ -76,14 +76,16 @@ def chown(self, uid=-1, gid=-1): ''' self.client.call(VmTemplate.METHODS['chown'], self.id, uid, gid) - def instantiate(self, name=''): + def instantiate(self, name='', hold=False, extra_template=''): ''' Creates a VM instance from a VmTemplate ``name`` name of the VM instance ''' - self.client.call(VmTemplate.METHODS['instantiate'], self.id, name) + # id = self.client.call(VmTemplate.METHODS['instantiate'], self.id, name) + id = self.client.call(VmTemplate.METHODS['instantiate'], self.id, name, hold, extra_template) + return id def __repr__(self): return '' % self.name diff --git a/oca/vm.py b/oca/vm.py index 765be98..86db87b 100644 --- a/oca/vm.py +++ b/oca/vm.py @@ -18,6 +18,7 @@ class VirtualMachine(PoolElement): 'delete': 'vm.delete', 'chown': 'vm.chown', 'update': 'vm.update', + 'rename': 'vm.rename', } INIT = 0 @@ -92,7 +93,7 @@ class VirtualMachine(PoolElement): 'stime': int, 'etime': int, 'deploy_id': extractString, - 'template': ['TEMPLATE', Template, ['NIC', 'DISK']], + 'template': ['TEMPLATE', Template, ['NIC', 'DISK', 'GRAPHICS']], 'user_template': ['USER_TEMPLATE', Template], 'history_records': ['HISTORY_RECORDS', lambda x: [History(i) for i in x] if x is not None else []], @@ -248,6 +249,20 @@ def delete(self): ''' self._action('delete') + def rename(self, name): + ''' + Rename the VM. + ''' + data = self.client.call(self.METHODS['rename'], self.id, name) + return data + + def action(self, action): + ''' + submits an action to be performed on a virtual machine. + ''' + data = self.client.call(self.METHODS['action'], action, self.id) + return data + def _action(self, action): self.client.call(self.METHODS['action'], action, self.id) diff --git a/oca/zone.py b/oca/zone.py new file mode 100644 index 0000000..f4f9d5c --- /dev/null +++ b/oca/zone.py @@ -0,0 +1,35 @@ +# coding: utf-8 +from pool import Pool, PoolElement, Template, extractString + + +class Zone(PoolElement): + METHODS = { + } + + XML_TYPES = { + 'id': int, + 'name': extractString, + 'template': ['TEMPLATE', Template], + } + + ELEMENT_NAME = 'ZONE' + + def __init__(self, xml, client): + super(Zone, self).__init__(xml, client) + self._convert_types() + + def __repr__(self): + return '' % self.name + + +class ZonePool(Pool): + METHODS = { + 'info': 'zonepool.info', + } + + def __init__(self, client): + super(ZonePool, self).__init__('ZONE_POOL', 'ZONE', client) + + def _factory(self, xml): + c = Zone(xml, self.client) + return c diff --git a/setup.py b/setup.py index f7f83d4..ed97fa7 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ # -*- coding: UTF-8 -*- -__version__ = '4.15.0a1' +__version__ = '4.15.anjuke16' import os