Skip to content

Commit c7e3529

Browse files
committed
Add pagination helpers
Add some pagination helpers to configure pagination parameters for various commands. Two pagination schemes are supported, based on what we currently support across OSC commands: marker-based pagination and offset-based pagination. Change-Id: I551bb4c3ff0568c6df5244a1d0f0669497bee58f Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
1 parent 8b253cb commit c7e3529

17 files changed

Lines changed: 115 additions & 247 deletions
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
from osc_lib.cli import parseractions
14+
15+
from openstackclient.i18n import _
16+
17+
18+
# TODO(stephenfin): Consider moving these to osc-lib since they're broadly
19+
# useful
20+
21+
22+
def add_marker_pagination_option_to_parser(parser):
23+
"""Add marker-based pagination options to the parser.
24+
25+
APIs that use marker-based paging use the marker and limit query parameters
26+
to paginate through items in a collection.
27+
28+
Marker-based pagination is often used in cases where the length of the
29+
total set of items is either changing frequently, or where the total length
30+
might not be known upfront.
31+
"""
32+
parser.add_argument(
33+
'--limit',
34+
metavar='<limit>',
35+
type=int,
36+
action=parseractions.NonNegativeAction,
37+
help=_(
38+
'The maximum number of entries to return. If the value exceeds '
39+
'the server-defined maximum, then the maximum value will be used.'
40+
),
41+
)
42+
parser.add_argument(
43+
'--marker',
44+
metavar='<marker>',
45+
default=None,
46+
help=_(
47+
'The first position in the collection to return results from. '
48+
'This should be a value that was returned in a previous request.'
49+
),
50+
)
51+
52+
53+
def add_offset_pagination_option_to_parser(parser):
54+
"""Add offset-based pagination options to the parser.
55+
56+
APIs that use offset-based paging use the offset and limit query parameters
57+
to paginate through items in a collection.
58+
59+
Offset-based pagination is often used where the list of items is of a fixed
60+
and predetermined length.
61+
"""
62+
parser.add_argument(
63+
'--limit',
64+
metavar='<limit>',
65+
type=int,
66+
action=parseractions.NonNegativeAction,
67+
help=_(
68+
'The maximum number of entries to return. If the value exceeds '
69+
'the server-defined maximum, then the maximum value will be used.'
70+
),
71+
)
72+
parser.add_argument(
73+
'--offset',
74+
metavar='<offset>',
75+
type=int,
76+
action=parseractions.NonNegativeAction,
77+
default=None,
78+
help=_(
79+
'The (zero-based) offset of the first item in the collection to '
80+
'return.'
81+
),
82+
)

openstackclient/compute/v2/flavor.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from osc_lib import exceptions
2626
from osc_lib import utils
2727

28+
from openstackclient.common import pagination
2829
from openstackclient.i18n import _
2930
from openstackclient.identity import common as identity_common
3031

@@ -292,22 +293,7 @@ def get_parser(self, prog_name):
292293
default=False,
293294
help=_("List additional fields in output"),
294295
)
295-
parser.add_argument(
296-
'--marker',
297-
metavar="<flavor-id>",
298-
help=_("The last flavor ID of the previous page"),
299-
)
300-
parser.add_argument(
301-
'--limit',
302-
type=int,
303-
metavar='<num-flavors>',
304-
help=_(
305-
'Maximum number of flavors to display. This is also '
306-
'configurable on the server. The actual limit used will be '
307-
'the lower of the user-supplied value and the server '
308-
'configuration-derived value'
309-
),
310-
)
296+
pagination.add_marker_pagination_option_to_parser(parser)
311297
return parser
312298

313299
def take_action(self, parsed_args):

openstackclient/compute/v2/hypervisor.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from osc_lib import exceptions
2626
from osc_lib import utils
2727

28+
from openstackclient.common import pagination
2829
from openstackclient.i18n import _
2930

3031

@@ -80,27 +81,7 @@ def get_parser(self, prog_name):
8081
"when using microversion 2.52 or lower"
8182
),
8283
)
83-
parser.add_argument(
84-
'--marker',
85-
metavar='<marker>',
86-
help=_(
87-
"The UUID of the last hypervisor of the previous page; "
88-
"displays list of hypervisors after 'marker'. "
89-
"(supported with --os-compute-api-version 2.33 or above)"
90-
),
91-
)
92-
parser.add_argument(
93-
'--limit',
94-
metavar='<limit>',
95-
type=int,
96-
help=_(
97-
"Maximum number of hypervisors to display. Note that there "
98-
"is a configurable max limit on the server, and the limit "
99-
"that is used will be the minimum of what is requested "
100-
"here and what is configured in the server. "
101-
"(supported with --os-compute-api-version 2.33 or above)"
102-
),
103-
)
84+
pagination.add_marker_pagination_option_to_parser(parser)
10485
parser.add_argument(
10586
'--long',
10687
action='store_true',

openstackclient/compute/v2/keypair.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from osc_lib import exceptions
2828
from osc_lib import utils
2929

30+
from openstackclient.common import pagination
3031
from openstackclient.i18n import _
3132
from openstackclient.identity import common as identity_common
3233

@@ -296,15 +297,7 @@ def get_parser(self, prog_name):
296297
),
297298
)
298299
identity_common.add_project_domain_option_to_parser(parser)
299-
parser.add_argument(
300-
'--marker',
301-
help=_('The last keypair ID of the previous page'),
302-
)
303-
parser.add_argument(
304-
'--limit',
305-
type=int,
306-
help=_('Maximum number of keypairs to display'),
307-
)
300+
pagination.add_marker_pagination_option_to_parser(parser)
308301
return parser
309302

310303
def take_action(self, parsed_args):

openstackclient/compute/v2/server.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from osc_lib import exceptions
3434
from osc_lib import utils
3535

36+
from openstackclient.common import pagination
3637
from openstackclient.i18n import _
3738
from openstackclient.identity import common as identity_common
3839
from openstackclient.network import common as network_common
@@ -2370,29 +2371,7 @@ def get_parser(self, prog_name):
23702371
'Mutually exclusive with "--no-name-lookup|-n" option.'
23712372
),
23722373
)
2373-
parser.add_argument(
2374-
'--marker',
2375-
metavar='<server>',
2376-
default=None,
2377-
help=_(
2378-
'The last server of the previous page. Display '
2379-
'list of servers after marker. Display all servers if not '
2380-
'specified. When used with ``--deleted``, the marker must '
2381-
'be an ID, otherwise a name or ID can be used.'
2382-
),
2383-
)
2384-
parser.add_argument(
2385-
'--limit',
2386-
metavar='<num-servers>',
2387-
type=int,
2388-
default=None,
2389-
help=_(
2390-
"Maximum number of servers to display. If limit equals -1, "
2391-
"all servers will be displayed. If limit is greater than "
2392-
"'osapi_max_limit' option of Nova API, "
2393-
"'osapi_max_limit' will be used instead."
2394-
),
2395-
)
2374+
pagination.add_marker_pagination_option_to_parser(parser)
23962375
parser.add_argument(
23972376
'--changes-before',
23982377
metavar='<changes-before>',

openstackclient/compute/v2/server_event.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from osc_lib import exceptions
2727
from osc_lib import utils
2828

29+
from openstackclient.common import pagination
2930
from openstackclient.i18n import _
3031

3132
LOG = logging.getLogger(__name__)
@@ -143,21 +144,7 @@ def get_parser(self, prog_name):
143144
"(supported with --os-compute-api-version 2.66 or above)"
144145
),
145146
)
146-
parser.add_argument(
147-
'--marker',
148-
help=_(
149-
'The last server event ID of the previous page '
150-
'(supported by --os-compute-api-version 2.58 or above)'
151-
),
152-
)
153-
parser.add_argument(
154-
'--limit',
155-
type=int,
156-
help=_(
157-
'Maximum number of server events to display '
158-
'(supported by --os-compute-api-version 2.58 or above)'
159-
),
160-
)
147+
pagination.add_marker_pagination_option_to_parser(parser)
161148
return parser
162149

163150
def take_action(self, parsed_args):

openstackclient/compute/v2/server_group.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
from osc_lib import exceptions
2525
from osc_lib import utils
2626

27+
from openstackclient.common import pagination
2728
from openstackclient.i18n import _
2829

29-
3030
LOG = logging.getLogger(__name__)
3131

3232

@@ -191,28 +191,7 @@ def get_parser(self, prog_name):
191191
)
192192
# TODO(stephenfin): This should really be a --marker option, but alas
193193
# the API doesn't support that for some reason
194-
parser.add_argument(
195-
'--offset',
196-
metavar='<offset>',
197-
type=int,
198-
default=None,
199-
help=_(
200-
'Index from which to start listing servers. This should '
201-
'typically be a factor of --limit. Display all servers groups '
202-
'if not specified.'
203-
),
204-
)
205-
parser.add_argument(
206-
'--limit',
207-
metavar='<limit>',
208-
type=int,
209-
default=None,
210-
help=_(
211-
"Maximum number of server groups to display. "
212-
"If limit is greater than 'osapi_max_limit' option of Nova "
213-
"API, 'osapi_max_limit' will be used instead."
214-
),
215-
)
194+
pagination.add_offset_pagination_option_to_parser(parser)
216195
return parser
217196

218197
def take_action(self, parsed_args):

openstackclient/compute/v2/server_migration.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from osc_lib import exceptions
2020
from osc_lib import utils
2121

22+
from openstackclient.common import pagination
2223
from openstackclient.i18n import _
2324
from openstackclient.identity import common as identity_common
2425

@@ -54,28 +55,7 @@ def get_parser(self, prog_name):
5455
],
5556
help=_('Filter migrations by type'),
5657
)
57-
parser.add_argument(
58-
'--marker',
59-
metavar='<marker>',
60-
help=_(
61-
"The last migration of the previous page; displays list "
62-
"of migrations after 'marker'. Note that the marker is "
63-
"the migration UUID. "
64-
"(supported with --os-compute-api-version 2.59 or above)"
65-
),
66-
)
67-
parser.add_argument(
68-
'--limit',
69-
metavar='<limit>',
70-
type=int,
71-
help=_(
72-
"Maximum number of migrations to display. Note that there "
73-
"is a configurable max limit on the server, and the limit "
74-
"that is used will be the minimum of what is requested "
75-
"here and what is configured in the server. "
76-
"(supported with --os-compute-api-version 2.59 or above)"
77-
),
78-
)
58+
pagination.add_marker_pagination_option_to_parser(parser)
7959
parser.add_argument(
8060
'--changes-since',
8161
dest='changes_since',

openstackclient/image/v2/image.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from osc_lib import exceptions
3232
from osc_lib import utils
3333

34+
from openstackclient.common import pagination
3435
from openstackclient.common import progressbar
3536
from openstackclient.i18n import _
3637
from openstackclient.identity import common as identity_common
@@ -805,9 +806,9 @@ def get_parser(self, prog_name):
805806
default=False,
806807
help=_('List additional fields in output'),
807808
)
808-
809809
# --page-size has never worked, leave here for silent compatibility
810810
# We'll implement limit/marker differently later
811+
# TODO(stephenfin): Remove this in the next major version bump
811812
parser.add_argument(
812813
"--page-size",
813814
metavar="<size>",
@@ -823,22 +824,7 @@ def get_parser(self, prog_name):
823824
"specified separated by comma"
824825
),
825826
)
826-
parser.add_argument(
827-
"--limit",
828-
metavar="<num-images>",
829-
type=int,
830-
help=_("Maximum number of images to display."),
831-
)
832-
parser.add_argument(
833-
'--marker',
834-
metavar='<image>',
835-
default=None,
836-
help=_(
837-
"The last image of the previous page. Display "
838-
"list of images after marker. Display all images if not "
839-
"specified. (name or ID)"
840-
),
841-
)
827+
pagination.add_marker_pagination_option_to_parser(parser)
842828
return parser
843829

844830
def take_action(self, parsed_args):

0 commit comments

Comments
 (0)