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(404, msg) elif num > 1: raise exceptions.NoUniqueMatch else: return rl[0]
def test_share_snapshot_instance_set_instance_not_found(self): arglist = [ self.share_snapshot_instance.id, '--status', self.snapshot_instance_status ] verifylist = [('snapshot_instance', self.share_snapshot_instance.id), ('status', self.snapshot_instance_status)] self.share_snapshot_instances_mock.reset_state.side_effect = ( api_exceptions.NotFound()) parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises(osc_exceptions.CommandError, self.cmd.take_action, parsed_args)
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]
def test_share_unset_property_exception(self): arglist = [ '--property', 'Manila', self._share.id, ] verifylist = [ ('property', ['Manila']), ('share', self._share.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.cmd.take_action(parsed_args) self.shares_mock.delete_metadata.assert_called_with( self._share.id, parsed_args.property) # 404 Not Found would be raised, if property 'Manila' doesn't exist self.shares_mock.delete_metadata.side_effect = exceptions.NotFound() self.assertRaises( osc_exceptions.CommandError, self.cmd.take_action, parsed_args)