Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion oca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from group import Group, GroupPool
from template import VmTemplate, VmTemplatePool
from exceptions import OpenNebulaException
from cluster import Cluster, ClusterPool
from datastore import Datastore, DatastorePool


CONNECTED = -3
Expand Down Expand Up @@ -123,5 +125,6 @@ def call(self, function, *args):
__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
VirtualMachinePool, User, UserPool,
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED]
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
Cluster, ClusterPool, Datastore, DatastorePool]

56 changes: 56 additions & 0 deletions oca/cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template, extractString

class Cluster(PoolElement):
METHODS = {
#'info' : 'cluster.info',
'allocate' : 'cluster.allocate',
'delete' : 'cluster.delete',
#'enable' : 'cluster.enable',
#'update' : 'cluster.update'
}

XML_TYPES = {
'id' : int,
'name' : extractString,
'host_ids' : ['HOSTS', lambda hosts: map(lambda host_id: int(host_id.text), hosts)],
'datastore_ids' : ['DATASTORES', lambda datastores: map(lambda datastore_id: int(datastore_id.text), datastores)],
'vnet_ids' : ['VNETS', lambda vnets: map(lambda vnet_id: int(vnet_id.text), vnets)],
'template' : ['TEMPLATE', Template],
}

ELEMENT_NAME = 'CLUSTER'

@staticmethod
def allocate(client, cluster_name):
'''
Adds a cluster to the cluster list

Arguments

``cluster_name``
Clustername to add
'''
cluster_id = client.call(Cluster.METHODS['allocate'], cluster_name)
return cluster_id

def __init__(self, xml, client):
super(Cluster, self).__init__(xml, client)
self._convert_types()

def __repr__(self):
return '<oca.Cluster("%s")>' % self.name


class ClusterPool(Pool):
METHODS = {
'info' : 'clusterpool.info',
}

def __init__(self, client):
super(ClusterPool, self).__init__('CLUSTER_POOL', 'CLUSTER', client)

def _factory(self, xml):
c = Cluster(xml, self.client)
return c

69 changes: 69 additions & 0 deletions oca/datastore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template, extractString

class Datastore(PoolElement):
METHODS = {
#'info' : 'datastore.info',
'allocate' : 'datastore.allocate',
'delete' : 'datastore.delete',
#'enable' : 'datastore.enable',
#'update' : 'datastore.update'
}

XML_TYPES = {
'id' : int,
'name' : extractString,
'uid' : int,
'gid' : int,
'uname' : extractString,
'gname' : extractString,
#'permissions' : Permissions,
'ds_mad' : extractString,
'tm_mad' : extractString,
'base_path' : extractString,
'type' : int,
'disk_type' : int,
#'state' : ???,
'cluster_id' : int,
'cluster' : extractString,
'total_mb' : int,
'free_mb' : int,
'used_mb' : int,
'image_ids' : ['IMAGES', lambda images: map(lambda image_id: int(image_id.text), images)],
'template' : ['TEMPLATE', Template],
}

ELEMENT_NAME = 'DATASTORE'

@staticmethod
def allocate(client, datastore_template):
'''
Adds a datastore to the datastore list

Arguments

``datastore_template``
Template for the datastore to add
'''
datastore_id = client.call(Datastore.METHODS['allocate'], datastore_template)
return datastore_id

def __init__(self, xml, client):
super(Datastore, self).__init__(xml, client)
self._convert_types()

def __repr__(self):
return '<oca.Datastore("%s")>' % self.name


class DatastorePool(Pool):
METHODS = {
'info' : 'datastorepool.info',
}

def __init__(self, client):
super(DatastorePool, self).__init__('DATASTORE_POOL', 'DATASTORE', client)

def _factory(self, xml):
c = Datastore(xml, self.client)
return c
6 changes: 3 additions & 3 deletions oca/group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template
from pool import Pool, PoolElement, Template, extractString


class Group(PoolElement):
Expand All @@ -11,7 +11,7 @@ class Group(PoolElement):

XML_TYPES = {
'id' : int,
'name' : str,
'name' : extractString,
'template' : ['TEMPLATE', Template],
'users' : ['USERS', lambda users: [int(i.text) for i in users]],
#'resource_providers': handled separately
Expand Down Expand Up @@ -54,7 +54,7 @@ class GroupPool(Pool):
}

def __init__(self, client):
super(GroupPool, self).__init__('GROUP_POOL', 'POOL', client)
super(GroupPool, self).__init__('GROUP_POOL', 'GROUP', client)

def _factory(self, xml):
i = Group(xml, self.client)
Expand Down
48 changes: 29 additions & 19 deletions oca/host.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template
from pool import Pool, PoolElement, Template, extractString


class HostShare(Template):
Expand All @@ -17,36 +17,46 @@ class Host(PoolElement):
}

INIT = 0
MONITORING = 1
MONITORING_MONITORED = 1 # Currently monitoring, previously MONITORED
MONITORED = 2
ERROR = 3
DISABLED = 4
HOST_STATES = ['INIT', 'MONITORING', 'MONITORED', 'ERROR', 'DISABLED']
MONITORING_ERROR = 5 # Currently monitoring, previously ERROR
MONITORING_INIT = 6 # Currently monitoring, previously initialized
MONITORING_DISABLED = 7 # Currently monitoring, previously DISABLED
HOST_STATES = ['INIT', 'MONITORING_MONITORED', 'MONITORED', 'ERROR', 'DISABLED',
'MONITORING_ERROR', 'MONITORING_INIT', 'MONITORING_DISABLED']

SHORT_HOST_STATES = {
'INIT' : 'on',
'MONITORING' : 'on',
'MONITORED' : 'on',
'ERROR' : 'err',
'DISABLED' : 'off'
'INIT': 'on',
'MONITORING_MONITORED': 'on',
'MONITORED': 'on',
'ERROR': 'err',
'DISABLED': 'off',
'MONITORING_ERROR': 'on',
'MONITORING_INIT': 'on',
'MONITORING_DISABLED': 'on',
}

XML_TYPES = {
'id' : int,
'name' : str,
'state' : int,
'im_mad' : str,
'vm_mad' : str,
'last_mon_time' : int,
'vm_ids' : ['VMS', lambda vms: map(lambda vmid: int(vmid.text), vms)],
'template' : ['TEMPLATE', Template],
'host_share' : ['HOST_SHARE', HostShare],
'id': int,
'name': extractString,
'state': int,
'im_mad': extractString,
'vm_mad': extractString,
'vn_mad': extractString,
'last_mon_time': int,
'cluster': extractString,
'cluster_id': int,
'vm_ids': ['VMS', lambda vms: map(lambda vmid: int(vmid.text), vms)],
'template': ['TEMPLATE', Template],
'host_share': ['HOST_SHARE', HostShare],
}

ELEMENT_NAME = 'HOST'

@staticmethod
def allocate(client, hostname, im, vmm, tm):
def allocate(client, hostname, im, vmm, tm, cluster_id=-1):
'''
Adds a host to the host list

Expand All @@ -64,7 +74,7 @@ def allocate(client, hostname, im, vmm, tm):
``tm``
Transfer manager
'''
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm)
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm, cluster_id)
return host_id

def __init__(self, xml, client):
Expand Down
24 changes: 17 additions & 7 deletions oca/image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template
from pool import Pool, PoolElement, Template, extractString


class Image(PoolElement):
Expand All @@ -18,16 +18,26 @@ class Image(PoolElement):
'id' : int,
'uid' : int,
'gid' : int,
'uname' : str,
'gname' : str,
'name' : str,
'uname' : extractString,
'gname' : extractString,
'name' : extractString,
#'permissions' : ???,
'type' : int,
'public' : int,
'disk_type' : int,
'persistent' : int,
'regtime' : int,
'source' : str,
'source' : extractString,
'path' : extractString,
'fstype' : extractString,
'size' : int,
'state' : int,
'running_vms' : int,
'cloning_ops' : int,
'cloning_id' : int,
'datastore_id': int,
'datastore' : extractString,
'vm_ids' : ["VMS", lambda vms: map(lambda vm_id: int(vm_id.text), vms)],
'clone_ids' : ["CLONES", lambda clones: map(lambda clone_id: int(clone_id.text), clones)],
'template' : ['TEMPLATE', Template],
}

Expand Down Expand Up @@ -176,7 +186,7 @@ class ImagePool(Pool):
}

def __init__(self, client):
super(ImagePool, self).__init__('IMAGE_POOL', 'POOL', client)
super(ImagePool, self).__init__('IMAGE_POOL', 'IMAGE', client)

def _factory(self, xml):
i = Image(xml, self.client)
Expand Down
9 changes: 7 additions & 2 deletions oca/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class WrongNameError(OpenNebulaException):
class WrongIdError(OpenNebulaException):
pass

def extractString(xml_or_string):
if isinstance(xml_or_string, str):
return xml_or_string
else:
return xml_or_string.text or ''

class Template(object):
def __init__(self, xml_element, multiple=[]):
Expand Down Expand Up @@ -60,7 +65,7 @@ def __getitem__(self, key):
else:
return value
else:
raise IndexError()
raise IndexError("Key {0} not found!".format(key))

def __getattr__(self, name):
try:
Expand Down Expand Up @@ -103,7 +108,7 @@ def info(self, filter=-3, range_start=-1, range_end=-1, *args):
data = self.client.call(self.METHODS['info'], filter,
range_start, range_end, *args)
self._initialize_xml(data, self.pool_name)
for element in self.xml:
for element in self.xml.findall(self.element_name):
self.append(self._factory(element))

def _factory(self):
Expand Down
2 changes: 1 addition & 1 deletion oca/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class VmTemplatePool(Pool):
}

def __init__(self, client):
super(VmTemplatePool, self).__init__('VMTEMPLATE_POOL', 'POOL', client)
super(VmTemplatePool, self).__init__('VMTEMPLATE_POOL', 'VMTEMPLATE', client)

#def info(self,

Expand Down
20 changes: 20 additions & 0 deletions oca/tests/fixtures/cluster.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<CLUSTER>
<ID>101</ID>
<NAME>oneCluster</NAME>
<HOSTS>
<ID>2</ID>
<ID>3</ID>
</HOSTS>
<DATASTORES>
<ID>4</ID>
<ID>5</ID>
</DATASTORES>
<VNETS>
<ID>6</ID>
<ID>7</ID>
</VNETS>
<TEMPLATE>
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
</TEMPLATE>
</CLUSTER>
24 changes: 24 additions & 0 deletions oca/tests/fixtures/clusterpool.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<CLUSTER_POOL>
<CLUSTER>
<ID>101</ID>
<NAME>oneCluster</NAME>
<HOSTS/>
<DATASTORES/>
<VNETS/>
<TEMPLATE>
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
</TEMPLATE>
</CLUSTER>
<CLUSTER>
<ID>102</ID>
<NAME>anotherCluster</NAME>
<HOSTS/>
<DATASTORES/>
<VNETS/>
<TEMPLATE>
<RESERVED_CPU><![CDATA[]]></RESERVED_CPU>
<RESERVED_MEM><![CDATA[]]></RESERVED_MEM>
</TEMPLATE>
</CLUSTER>
</CLUSTER_POOL>
Loading