def find(self, base_url=None, **kwargs): """Find a single item with attributes matching ``**kwargs``. :param base_url: if provided, the generated URL will be appended to it """ kwargs = self._filter_kwargs(kwargs) rl = self._list( '%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), 'query': '?%s' % parse.urlencode(kwargs) if kwargs else '', }, self.collection_key) num = len(rl) if num == 0: msg = _("No %(name)s matching %(args)s.") % { 'name': self.resource_class.__name__, 'args': kwargs } raise exceptions.NotFound(msg) elif num > 1: raise exceptions.NoUniqueMatch else: return rl[0]
def test_get_overclouds(self): mock_heat = mock.Mock() mock_stacks = mock.Mock() mock_heat.stacks = mock_stacks mock_stacks.list.return_value = [ mock.Mock(id='111', stack_name='overcloud'), mock.Mock(id='222', stack_name='some-other-stack'), mock.Mock(id='333', stack_name='other-overcloud'), ] output_result = { "output": { "output_key": "AnsibleHostVarsMap", "output_value": {} } } mock_stacks.output_show.side_effect = [ output_result, heat_exc.NotFound(), output_result ] result = list(tlo.get_overclouds(mock_heat)) self.assertEqual([{ 'id': '111', 'stack_name': 'overcloud' }, { 'id': '333', 'stack_name': 'other-overcloud' }], result)
def get_ng_stack(stack_id, resolve_outputs=False): try: return self.mock_stacks[stack_id] except KeyError: # In this case we intentionally didn't add the stack # to the mock_stacks dict to simulte a not found error. # For this reason raise heat NotFound exception. raise heatexc.NotFound("stack not found")
def find(self, **kwargs): """Find a single item with attributes matching ``**kwargs``. This isn't very efficient: it loads the entire list then filters on the Python side. """ matches = self.findall(**kwargs) num_matches = len(matches) if num_matches == 0: msg = _("No %(name)s matching %(args)s.") % { 'name': self.resource_class.__name__, 'args': kwargs } raise exceptions.NotFound(msg) elif num_matches > 1: raise exceptions.NoUniqueMatch() else: return matches[0]