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
52 changes: 28 additions & 24 deletions oca/__init__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
# -*- coding: UTF-8 -*-
import http.client
import os
import hashlib
import re
import socket

import http.client
import xmlrpc.client

from .cluster import Cluster, ClusterPool
from .datastore import Datastore, DatastorePool
from .exceptions import OpenNebulaException
from .group import Group, GroupPool
from .host import Host, HostPool
from .vm import VirtualMachine, VirtualMachinePool
from .user import User, UserPool
from .image import Image, ImagePool
from .vn import VirtualNetwork, VirtualNetworkPool
from .group import Group, GroupPool
from .template import VmTemplate, VmTemplatePool
from .exceptions import OpenNebulaException
from .cluster import Cluster, ClusterPool
from .datastore import Datastore, DatastorePool

from .user import User, UserPool
from .vm import VirtualMachine, VirtualMachinePool
from .vn import VirtualNetwork, VirtualNetworkPool

CONNECTED = -3
ALL = -2
CONNECTED_AND_GROUP = -1


class TimeoutHTTPConnection(http.client.HTTPConnection):
def connect(self):
http.client.HTTPConnection.connect(self)
self.sock.settimeout(self.timeout)


class TimeoutHTTP(http.client.HTTPConnection):
_connection_class = TimeoutHTTPConnection

def set_timeout(self, timeout):
self._conn.timeout = timeout


class ProxiedTransport(xmlrpc.client.Transport):
def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, *args, **kwargs):
xmlrpc.client.Transport.__init__(self, *args, **kwargs)
self.timeout = timeout

def set_proxy(self, proxy):
self.proxy = proxy

def make_connection(self, host):
self.realhost = host
h = http.client.HTTPConnection(self.proxy)
return h

def send_request(self, connection, handler, request_body):
connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))

def send_host(self, connection, host):
connection.putheader('Host', self.realhost)


class Client(object):
'''
"""
The client class, represents the connection with the core and handles the
xml-rpc calls(see http://www.opennebula.org/documentation:rel3.2:api)
'''
"""
DEFAULT_ONE_AUTH = "~/.one/one_auth"
ONE_AUTH_RE = re.compile('^(.+?):(.+)$')
DEFAULT_ONE_ADDRESS = "http://localhost:2633/RPC2"
Expand Down Expand Up @@ -93,10 +98,9 @@ def __init__(self, secret=None, address=None, proxy=None):
self.server = xmlrpc.client.ServerProxy(self.one_address, transport=p)
else:
self.server = xmlrpc.client.ServerProxy(self.one_address)


def call(self, function, *args):
'''
"""
Calls rpc function.

Arguments
Expand All @@ -107,7 +111,7 @@ def call(self, function, *args):
``args``
function arguments

'''
"""
try:
func = getattr(self.server.one, function)
ret = func(self.one_auth, *args)
Expand All @@ -117,21 +121,21 @@ def call(self, function, *args):
data = ''
is_success = False
except socket.error as e:
#connection error
# connection error
raise e
if not is_success:
raise OpenNebulaException(data)
return data

def version(self):
'''
"""
Get the version of the connected OpenNebula server.
'''
"""
return self.call('system.version')

__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
VirtualMachinePool, User, UserPool,
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
Cluster, ClusterPool, Datastore, DatastorePool]

__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
VirtualMachinePool, User, UserPool,
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
Cluster, ClusterPool, Datastore, DatastorePool]
30 changes: 15 additions & 15 deletions oca/cluster.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
# -*- 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'
# '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: [int(host_id.text) for host_id in hosts]],
'datastore_ids' : ['DATASTORES', lambda datastores: [int(datastore_id.text) for datastore_id in datastores]],
'vnet_ids' : ['VNETS', lambda vnets: [int(vnet_id.text) for vnet_id in vnets]],
'template' : ['TEMPLATE', Template],
'id': int,
'name': extractString,
'host_ids': ['HOSTS', lambda hosts: [int(host_id.text) for host_id in hosts]],
'datastore_ids': ['DATASTORES', lambda datastores: [int(datastore_id.text) for datastore_id in datastores]],
'vnet_ids': ['VNETS', lambda vnets: [int(vnet_id.text) for vnet_id in 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

Expand All @@ -44,7 +45,7 @@ def __repr__(self):

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

def __init__(self, client):
Expand All @@ -53,4 +54,3 @@ def __init__(self, client):
def _factory(self, xml):
c = Cluster(xml, self.client)
return c

57 changes: 29 additions & 28 deletions oca/datastore.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
# -*- 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'
# '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: [int(image_id.text) for image_id in images]],
'template' : ['TEMPLATE', Template],
'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: [int(image_id.text) for image_id in 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

Expand All @@ -58,7 +59,7 @@ def __repr__(self):

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

def __init__(self, client):
Expand Down
1 change: 0 additions & 1 deletion oca/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

class OpenNebulaException(Exception):
pass

33 changes: 16 additions & 17 deletions oca/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

class Group(PoolElement):
METHODS = {
'info' : 'group.info',
'allocate' : 'group.allocate',
'delete' : 'group.delete',
'info': 'group.info',
'allocate': 'group.allocate',
'delete': 'group.delete',
}

XML_TYPES = {
'id' : int,
'name' : extractString,
'template' : ['TEMPLATE', Template],
'users' : ['USERS', lambda users: [int(i.text) for i in users]],
#'resource_providers': handled separately
#'datastore_quota': handled separately
#'network_quota': handled separately
#'vm_quota': handled separately
#'image_quota'
#'default_group_quotas'
'id': int,
'name': extractString,
'template': ['TEMPLATE', Template],
'users': ['USERS', lambda users: [int(i.text) for i in users]],
# 'resource_providers': handled separately
# 'datastore_quota': handled separately
# 'network_quota': handled separately
# 'vm_quota': handled separately
# 'image_quota'
# 'default_group_quotas'
}

ELEMENT_NAME = 'GROUP'

@staticmethod
def allocate(client, group_name):
'''
"""
Allocates a new group in OpenNebula

Arguments
Expand All @@ -36,7 +36,7 @@ def allocate(client, group_name):

``group``
a string containing the group name
'''
"""
group_id = client.call(Group.METHODS['allocate'], group_name)
return group_id

Expand All @@ -50,7 +50,7 @@ def __repr__(self):

class GroupPool(Pool):
METHODS = {
'info' : 'grouppool.info',
'info': 'grouppool.info',
}

def __init__(self, client):
Expand All @@ -60,4 +60,3 @@ def _factory(self, xml):
i = Group(xml, self.client)
i._convert_types()
return i

Loading