Merge "Add --marker for 'watcher actionplan list'"

This commit is contained in:
Zuul
2017-11-30 13:54:39 +00:00
committed by Gerrit Code Review
5 changed files with 46 additions and 4 deletions

View File

@@ -161,17 +161,21 @@ def common_params_for_list(args, fields, field_labels):
args.sort_dir) args.sort_dir)
params['sort_dir'] = args.sort_dir params['sort_dir'] = args.sort_dir
marker = getattr(args, 'marker', None)
if marker is not None:
params['marker'] = marker
params['detail'] = args.detail params['detail'] = args.detail
return params return params
def common_filters(limit=None, sort_key=None, sort_dir=None): def common_filters(limit=None, sort_key=None, sort_dir=None, marker=None):
"""Generate common filters for any list request. """Generate common filters for any list request.
:param limit: maximum number of entities to return. :param limit: maximum number of entities to return.
:param sort_key: field to use for sorting. :param sort_key: field to use for sorting.
:param sort_dir: direction of sorting: 'asc' or 'desc'. :param sort_dir: direction of sorting: 'asc' or 'desc'.
:param marker: The last actionplan UUID of the previous page.
:returns: list of string filters. :returns: list of string filters.
""" """
filters = [] filters = []
@@ -181,6 +185,8 @@ def common_filters(limit=None, sort_key=None, sort_dir=None):
filters.append('sort_key=%s' % sort_key) filters.append('sort_key=%s' % sort_key)
if sort_dir is not None: if sort_dir is not None:
filters.append('sort_dir=%s' % sort_dir) filters.append('sort_dir=%s' % sort_dir)
if marker is not None:
filters.append('marker=%s' % marker)
return filters return filters

View File

@@ -97,7 +97,8 @@ class UtilsTest(test_utils.BaseTestCase):
class CommonParamsForListTest(test_utils.BaseTestCase): class CommonParamsForListTest(test_utils.BaseTestCase):
def setUp(self): def setUp(self):
super(CommonParamsForListTest, self).setUp() super(CommonParamsForListTest, self).setUp()
self.args = mock.Mock(limit=None, sort_key=None, sort_dir=None) self.args = mock.Mock(limit=None, marker=None,
sort_key=None, sort_dir=None)
self.args.detail = False self.args.detail = False
self.expected_params = {'detail': False} self.expected_params = {'detail': False}
@@ -117,6 +118,13 @@ class CommonParamsForListTest(test_utils.BaseTestCase):
utils.common_params_for_list, utils.common_params_for_list,
self.args, [], []) self.args, [], [])
def test_marker(self):
self.args.marker = 'e420a881-d7df-4de2-bbf3-378cc13d9b3a'
self.expected_params.update(
{'marker': 'e420a881-d7df-4de2-bbf3-378cc13d9b3a'})
self.assertEqual(self.expected_params,
utils.common_params_for_list(self.args, [], []))
def test_sort_key_and_sort_dir(self): def test_sort_key_and_sort_dir(self):
self.args.sort_key = 'field' self.args.sort_key = 'field'
self.args.sort_dir = 'desc' self.args.sort_dir = 'desc'

View File

@@ -94,6 +94,13 @@ fake_responses_pagination = {
{"action_plans": [ACTION_PLAN2]} {"action_plans": [ACTION_PLAN2]}
), ),
}, },
'/v1/action_plans/?marker=f8e47706-efcf-49a4-a5c4-af604eb492f2':
{
'GET': (
{},
{"action_plans": [ACTION_PLAN2]}
),
},
} }
fake_responses_sorting = { fake_responses_sorting = {
@@ -158,6 +165,19 @@ class ActionPlanManagerTest(testtools.TestCase):
self.assertEqual(expect, self.api.calls) self.assertEqual(expect, self.api.calls)
self.assertThat(action_plans, matchers.HasLength(2)) self.assertThat(action_plans, matchers.HasLength(2))
def test_action_plans_list_marker(self):
self.api = utils.FakeAPI(fake_responses_pagination)
self.mgr = watcherclient.v1.action_plan.ActionPlanManager(self.api)
action_plans = self.mgr.list(
marker='f8e47706-efcf-49a4-a5c4-af604eb492f2')
expect = [
('GET', '/v1/action_plans/?'
'marker=f8e47706-efcf-49a4-a5c4-af604eb492f2',
{}, None),
]
self.assertEqual(expect, self.api.calls)
self.assertThat(action_plans, matchers.HasLength(1))
def test_action_plans_list_sort_key(self): def test_action_plans_list_sort_key(self):
self.api = utils.FakeAPI(fake_responses_sorting) self.api = utils.FakeAPI(fake_responses_sorting)
self.mgr = watcherclient.v1.action_plan.ActionPlanManager(self.api) self.mgr = watcherclient.v1.action_plan.ActionPlanManager(self.api)

View File

@@ -31,7 +31,7 @@ class ActionPlanManager(base.Manager):
return '/v1/action_plans/%s' % id if id else '/v1/action_plans' return '/v1/action_plans/%s' % id if id else '/v1/action_plans'
def list(self, audit=None, limit=None, sort_key=None, def list(self, audit=None, limit=None, sort_key=None,
sort_dir=None, detail=False): sort_dir=None, detail=False, marker=None):
"""Retrieve a list of action plan. """Retrieve a list of action plan.
:param audit: Name of the audit :param audit: Name of the audit
@@ -52,13 +52,16 @@ class ActionPlanManager(base.Manager):
:param detail: Optional, boolean whether to return detailed information :param detail: Optional, boolean whether to return detailed information
about action plans. about action plans.
:param marker: The last actionplan UUID of the previous page;
displays list of actionplans after "marker".
:returns: A list of action plans. :returns: A list of action plans.
""" """
if limit is not None: if limit is not None:
limit = int(limit) limit = int(limit)
filters = utils.common_filters(limit, sort_key, sort_dir) filters = utils.common_filters(limit, sort_key, sort_dir, marker)
if audit is not None: if audit is not None:
filters.append('audit_uuid=%s' % audit) filters.append('audit_uuid=%s' % audit)

View File

@@ -125,6 +125,11 @@ class ListActionPlan(command.Lister):
help=_('Maximum number of action plans to return per request, ' help=_('Maximum number of action plans to return per request, '
'0 for no limit. Default is the maximum number used ' '0 for no limit. Default is the maximum number used '
'by the Watcher API Service.')) 'by the Watcher API Service.'))
parser.add_argument(
'--marker',
metavar='<actionplan>',
help=_('The last actionplan UUID of the previous page; '
'displays list of actionplans after "marker".'))
parser.add_argument( parser.add_argument(
'--sort-key', '--sort-key',
metavar='<field>', metavar='<field>',