def test_get_instance(self, mock_get_uuid): mock_get_uuid.return_value = '1111' self.assertEqual('1111', vm.get_instance('ctx', 'ABC')) mock_get_uuid.side_effect = [ exception.InstanceNotFound({'instance_id': 'fake_instance'}), '222' ] self.assertEqual('222', vm.get_instance('ctx', 'ABC'))
def _handle_event(self, pvm_event, details, inst=None): """Handle an individual event. :param pvm_event: PowerVM Event Wrapper :param details: Parsed Details from the event :param inst: (Optional, Default: None) The pypowervm wrapper object that represents the VM instance. If None we try to look it up based on UUID. :return: returns the instance object or None (when it's not an instance event or action is not partition state change or NVRAM change) """ # See if this uri (from data) ends with a PowerVM UUID. if not pvm_util.is_instance_path(pvm_event.data): return None # If a vm event and one we handle, call the inst handler. pvm_uuid = pvm_util.get_req_path_uuid(pvm_event.data, preserve_case=True) if (pvm_event.data.endswith('LogicalPartition/' + pvm_uuid) and (self.inst_actions_handled & set(details))): if not inst: LOG.debug( 'PowerVM Nova Event Handler: Getting inst ' 'for id %s', pvm_uuid) inst = vm.get_instance(ctx.get_admin_context(), pvm_uuid) if inst: LOG.debug( 'Handle action "%(action)s" event for instance: ' '%(inst)s', dict(action=details, inst=inst.name)) self._handle_inst_event(inst, pvm_uuid, details) return inst return None
def _get_instance(inst, pvm_uuid): global _CONTEXT if inst is not None: return inst with lockutils.lock('get_context_once'): if _CONTEXT is None: _CONTEXT = ctx.get_admin_context() LOG.debug('PowerVM Nova Event Handler: Getting inst for id %s', pvm_uuid) return vm.get_instance(_CONTEXT, pvm_uuid)