~citrix-openstack/nova/xenapi

« back to all changes in this revision

Viewing changes to nova/virt/xenapi/vmops.py

refactored up nova/virt/xenapi/vmops _get_vm_opaque_ref() 
no longer inspects the param to check to see if it is an opaque ref
works better for unittests

Show diffs side-by-side

added added

removed removed

Lines of Context:
188
188
        """Refactored out the common code of many methods that receive either
189
189
        a vm name or a vm instance, and want a vm instance in return.
190
190
        """
191
 
        vm = None
192
 
        try:
193
 
            if instance_or_vm.startswith("OpaqueRef:"):
194
 
                # Got passed an opaque ref; return it
 
191
        # if instance_or_vm is a string it must be opaque ref or instance name
 
192
        if isinstance(instance_or_vm, basestring):
 
193
            obj = None
 
194
            try:
 
195
                # check for opaque ref
 
196
                obj = self._session.get_xenapi().VM.get_record(instance_or_vm)
195
197
                return instance_or_vm
196
 
            else:
197
 
                # Must be the instance name
198
 
                instance_name = instance_or_vm
199
 
        except (AttributeError, KeyError):
200
 
            # Note the the KeyError will only happen with fakes.py
201
 
            # Not a string; must be an ID or a vm instance
202
 
            if isinstance(instance_or_vm, (int, long)):
203
 
                ctx = context.get_admin_context()
204
 
                try:
205
 
                    instance_obj = db.instance_get(ctx, instance_or_vm)
206
 
                    instance_name = instance_obj.name
207
 
                except exception.NotFound:
208
 
                    # The unit tests screw this up, as they use an integer for
209
 
                    # the vm name. I'd fix that up, but that's a matter for
210
 
                    # another bug report. So for now, just try with the passed
211
 
                    # value
212
 
                    instance_name = instance_or_vm
213
 
            else:
214
 
                instance_name = instance_or_vm.name
215
 
        vm = VMHelper.lookup(self._session, instance_name)
216
 
        if vm is None:
 
198
            except self.XenAPI.Failure:
 
199
                # wasn't an opaque ref, must be an instance name
 
200
                instance_name = instance_or_vm
 
201
 
 
202
        # if instance_or_vm is an int/long it must be instance id
 
203
        elif isinstance(instance_or_vm, (int, long)):
 
204
            ctx = context.get_admin_context()
 
205
            try:
 
206
                instance_obj = db.instance_get(ctx, instance_or_vm)
 
207
                instance_name = instance_obj.name
 
208
            except exception.NotFound:
 
209
                # The unit tests screw this up, as they use an integer for
 
210
                # the vm name. I'd fix that up, but that's a matter for
 
211
                # another bug report. So for now, just try with the passed
 
212
                # value
 
213
                instance_name = instance_or_vm
 
214
 
 
215
        # otherwise instance_or_vm is an instance object
 
216
        else:
 
217
            instance_name = instance_or_vm.name
 
218
        vm_ref = VMHelper.lookup(self._session, instance_name)
 
219
        if vm_ref is None:
217
220
            raise exception.NotFound(
218
221
                            _('Instance not present %s') % instance_name)
219
 
        return vm
 
222
        return vm_ref
220
223
 
221
224
    def _acquire_bootlock(self, vm):
222
225
        """Prevent an instance from booting"""