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.
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):
195
# check for opaque ref
196
obj = self._session.get_xenapi().VM.get_record(instance_or_vm)
195
197
return instance_or_vm
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()
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
212
instance_name = instance_or_vm
214
instance_name = instance_or_vm.name
215
vm = VMHelper.lookup(self._session, instance_name)
198
except self.XenAPI.Failure:
199
# wasn't an opaque ref, must be an instance name
200
instance_name = instance_or_vm
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()
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
213
instance_name = instance_or_vm
215
# otherwise instance_or_vm is an instance object
217
instance_name = instance_or_vm.name
218
vm_ref = VMHelper.lookup(self._session, instance_name)
217
220
raise exception.NotFound(
218
221
_('Instance not present %s') % instance_name)
221
224
def _acquire_bootlock(self, vm):
222
225
"""Prevent an instance from booting"""