direct-io.hg

view tools/python/xen/xm/XenAPI.py @ 12740:92127156ec49

Added server proxy and session manager for Xen-API clients.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Dec 06 10:47:31 2006 +0000 (2006-12-06)
parents
children c91f6506b270
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2006 XenSource Inc.
16 #============================================================================
17 #
18 # Parts of this file are based upon xmlrpclib.py, the XML-RPC client
19 # interface included in the Python distribution.
20 #
21 # Copyright (c) 1999-2002 by Secret Labs AB
22 # Copyright (c) 1999-2002 by Fredrik Lundh
23 #
24 # By obtaining, using, and/or copying this software and/or its
25 # associated documentation, you agree that you have read, understood,
26 # and will comply with the following terms and conditions:
27 #
28 # Permission to use, copy, modify, and distribute this software and
29 # its associated documentation for any purpose and without fee is
30 # hereby granted, provided that the above copyright notice appears in
31 # all copies, and that both that copyright notice and this permission
32 # notice appear in supporting documentation, and that the name of
33 # Secret Labs AB or the author not be used in advertising or publicity
34 # pertaining to distribution of the software without specific, written
35 # prior permission.
36 #
37 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
38 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
39 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
40 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
41 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
43 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
44 # OF THIS SOFTWARE.
45 # --------------------------------------------------------------------
47 import xmlrpclib
49 import xen.util.xmlrpclib2
52 class Failure(Exception):
53 def __init__(self, details):
54 self.details = details
56 def __str__(self):
57 return "Xen-API failure: %s" % str(self.details)
60 class Session(xen.util.xmlrpclib2.ServerProxy):
61 """A server proxy and session manager for communicating with Xend using
62 the Xen-API.
64 Example:
66 session = Session('http://localhost:9363/')
67 session.login_with_password('me', 'mypassword')
68 session.xenapi.VM.start(vm_uuid)
69 session.xenapi.session.logout()
71 For now, this class also supports the legacy XML-RPC API, using
72 session.xend.domain('Domain-0') and similar. This support will disappear
73 once there is a working Xen-API replacement for every call in the legacy
74 API.
75 """
77 def __init__(self, uri, transport=None, encoding=None, verbose=0,
78 allow_none=1):
79 xen.util.xmlrpclib2.ServerProxy.__init__(self, uri, transport,
80 encoding, verbose,
81 allow_none)
82 self._session = None
85 def _xen_request(self, methodname, params):
86 full_params = (self._session,) + params
87 return _parse_result(getattr(self, methodname)(*full_params))
90 def _login(self, method, username, password):
91 self._session = _parse_result(
92 getattr(self, 'session.%s' % method)(username, password))
95 def __getattr__(self, name):
96 if name == 'xenapi':
97 return _Dispatcher(self._xen_request, None)
98 elif name.startswith('login'):
99 return lambda u, p: self._login(name, u, p)
100 else:
101 return xen.util.xmlrpclib2.ServerProxy.__getattr__(self, name)
104 def _parse_result(result):
105 if 'Status' not in result:
106 raise xmlrpclib.Fault(500, 'Missing Status in response from server')
107 if result['Status'] == 'Success':
108 if 'Value' in result:
109 return result['Value']
110 else:
111 raise xmlrpclib.Fault(500,
112 'Missing Value in response from server')
113 else:
114 if 'ErrorDescription' in result:
115 raise Failure(result['ErrorDescription'])
116 else:
117 raise xmlrpclib.Fault(
118 500, 'Missing ErrorDescription in response from server')
121 # Based upon _Method from xmlrpclib.
122 class _Dispatcher:
123 def __init__(self, send, name):
124 self.__send = send
125 self.__name = name
126 def __getattr__(self, name):
127 if self.__name is None:
128 return _Dispatcher(self.__send, name)
129 else:
130 return _Dispatcher(self.__send, "%s.%s" % (self.__name, name))
131 def __call__(self, *args):
132 return self.__send(self.__name, args)