~citrix-openstack/nova/xenapi

« back to all changes in this revision

Viewing changes to nova/virt/xenapi_conn.py

  • Committer: Armando Migliaccio
  • Date: 2010-12-15 18:01:29 UTC
  • mfrom: (436.1.3 xenapi-unittests)
  • Revision ID: armando.migliaccio@citrix.com-20101215180129-bgbowrctxzpspgyt
brougth clean-up from unittests branch and tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
**Related Flags**
38
38
 
39
 
:xenapi_use_fake_session:     To be set for unit testing
40
39
:xenapi_connection_url:  URL for connection to XenServer/Xen Cloud Platform.
41
40
:xenapi_connection_username:  Username for connection to XenServer/Xen Cloud
42
41
                              Platform (default: root).
59
58
 
60
59
from nova import utils
61
60
from nova import flags
62
 
from nova.virt.xenapi import load_sdk
63
61
from nova.virt.xenapi.vmops import VMOps
64
62
from nova.virt.xenapi.volumeops import VolumeOps
65
63
 
66
64
FLAGS = flags.FLAGS
67
65
 
68
 
flags.DEFINE_boolean('xenapi_use_fake_session',
69
 
                    False,
70
 
                    'Set to true in order to use the fake XenAPI SDK')
71
66
flags.DEFINE_string('xenapi_connection_url',
72
67
                    None,
73
68
                    'URL for connection to XenServer/Xen Cloud Platform.'
123
118
 
124
119
    def spawn(self, instance):
125
120
        """ Create VM instance """
126
 
        self._vmops.spawn(instance)
 
121
        return self._vmops.spawn(instance)
127
122
 
128
123
    def reboot(self, instance):
129
124
        """ Reboot VM instance """
159
154
class XenAPISession(object):
160
155
    """ The session to invoke XenAPI SDK calls """
161
156
    def __init__(self, url, user, pw):
162
 
        # This is loaded late so that there's no need to install this
163
 
        # library when not using XenAPI.
164
 
        self.XenAPI = load_sdk(FLAGS)
165
 
        if FLAGS.xenapi_use_fake_session:
166
 
            self._session = self.XenAPI.FakeSession(url)
167
 
        else:
168
 
            self._session = self.XenAPI.Session(url)
 
157
        self.XenAPI = self.get_imported_xenapi()
 
158
        self._session = self._create_session(url)
169
159
        self._session.login_with_password(user, pw)
170
160
 
 
161
    def get_imported_xenapi(self):
 
162
        """Stubout point. This can be replaced with a mock xenapi module."""
 
163
        return __import__('XenAPI')
 
164
 
171
165
    def get_xenapi(self):
172
166
        """ Return the xenapi object """
173
167
        return self._session.xenapi
200
194
        reactor.callLater(0, self._poll_task, task, d)
201
195
        return d
202
196
 
 
197
    def _create_session(self, url):
 
198
        """Stubout point. This can be replaced with a mock session."""
 
199
        return self.XenAPI.Session(url)
 
200
 
203
201
    @utils.deferredToThread
204
202
    def _poll_task(self, task, deferred):
205
203
        """Poll the given XenAPI task, and fire the given Deferred if we
220
218
                             error_info)
221
219
                deferred.errback(self.XenAPI.Failure(error_info))
222
220
            #logging.debug('Polling task %s done.', task)
223
 
        except self.XenAPI.Failure, exc:
 
221
        except Exception, exc:
224
222
            logging.warn(exc)
225
223
            deferred.errback(exc)
226
224