11# -*- coding: UTF-8 -*-
2+ import http .client
23import os
3- import hashlib
44import re
55import socket
6-
7- import http .client
86import xmlrpc .client
97
8+ from .cluster import Cluster , ClusterPool
9+ from .datastore import Datastore , DatastorePool
10+ from .exceptions import OpenNebulaException
11+ from .group import Group , GroupPool
1012from .host import Host , HostPool
11- from .vm import VirtualMachine , VirtualMachinePool
12- from .user import User , UserPool
1313from .image import Image , ImagePool
14- from .vn import VirtualNetwork , VirtualNetworkPool
15- from .group import Group , GroupPool
1614from .template import VmTemplate , VmTemplatePool
17- from .exceptions import OpenNebulaException
18- from .cluster import Cluster , ClusterPool
19- from .datastore import Datastore , DatastorePool
20-
15+ from .user import User , UserPool
16+ from .vm import VirtualMachine , VirtualMachinePool
17+ from .vn import VirtualNetwork , VirtualNetworkPool
2118
2219CONNECTED = - 3
2320ALL = - 2
2421CONNECTED_AND_GROUP = - 1
2522
23+
2624class TimeoutHTTPConnection (http .client .HTTPConnection ):
2725 def connect (self ):
2826 http .client .HTTPConnection .connect (self )
2927 self .sock .settimeout (self .timeout )
3028
29+
3130class TimeoutHTTP (http .client .HTTPConnection ):
3231 _connection_class = TimeoutHTTPConnection
3332
3433 def set_timeout (self , timeout ):
3534 self ._conn .timeout = timeout
3635
36+
3737class ProxiedTransport (xmlrpc .client .Transport ):
3838 def __init__ (self , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT , * args , ** kwargs ):
3939 xmlrpc .client .Transport .__init__ (self , * args , ** kwargs )
4040 self .timeout = timeout
41+
4142 def set_proxy (self , proxy ):
4243 self .proxy = proxy
44+
4345 def make_connection (self , host ):
4446 self .realhost = host
4547 h = http .client .HTTPConnection (self .proxy )
4648 return h
49+
4750 def send_request (self , connection , handler , request_body ):
4851 connection .putrequest ("POST" , 'http://%s%s' % (self .realhost , handler ))
52+
4953 def send_host (self , connection , host ):
5054 connection .putheader ('Host' , self .realhost )
5155
56+
5257class Client (object ):
53- '''
58+ """
5459 The client class, represents the connection with the core and handles the
5560 xml-rpc calls(see http://www.opennebula.org/documentation:rel3.2:api)
56- '''
61+ """
5762 DEFAULT_ONE_AUTH = "~/.one/one_auth"
5863 ONE_AUTH_RE = re .compile ('^(.+?):(.+)$' )
5964 DEFAULT_ONE_ADDRESS = "http://localhost:2633/RPC2"
@@ -93,10 +98,9 @@ def __init__(self, secret=None, address=None, proxy=None):
9398 self .server = xmlrpc .client .ServerProxy (self .one_address , transport = p )
9499 else :
95100 self .server = xmlrpc .client .ServerProxy (self .one_address )
96-
97101
98102 def call (self , function , * args ):
99- '''
103+ """
100104 Calls rpc function.
101105
102106 Arguments
@@ -107,7 +111,7 @@ def call(self, function, *args):
107111 ``args``
108112 function arguments
109113
110- '''
114+ """
111115 try :
112116 func = getattr (self .server .one , function )
113117 ret = func (self .one_auth , * args )
@@ -117,21 +121,21 @@ def call(self, function, *args):
117121 data = ''
118122 is_success = False
119123 except socket .error as e :
120- #connection error
124+ # connection error
121125 raise e
122126 if not is_success :
123127 raise OpenNebulaException (data )
124128 return data
125129
126130 def version (self ):
127- '''
131+ """
128132 Get the version of the connected OpenNebula server.
129- '''
133+ """
130134 return self .call ('system.version' )
131135
132- __all__ = [Client , OpenNebulaException , Host , HostPool , VirtualMachine ,
133- VirtualMachinePool , User , UserPool ,
134- Image , ImagePool , VirtualNetwork , VirtualNetworkPool ,
135- Group , GroupPool , VmTemplate , VmTemplatePool , ALL , CONNECTED ,
136- Cluster , ClusterPool , Datastore , DatastorePool ]
137136
137+ __all__ = [Client , OpenNebulaException , Host , HostPool , VirtualMachine ,
138+ VirtualMachinePool , User , UserPool ,
139+ Image , ImagePool , VirtualNetwork , VirtualNetworkPool ,
140+ Group , GroupPool , VmTemplate , VmTemplatePool , ALL , CONNECTED ,
141+ Cluster , ClusterPool , Datastore , DatastorePool ]
0 commit comments