Microversion 2.35 adds keypairs pagination support

After this microversion novaclient allows to get several keypairs
with the help of new optional parameters 'limit' and 'marker'
which were added to keypair-list command.

Implements blueprint: keypairs-pagination

Change-Id: I659d9528cbf64481bd2b3f99008a57287404854e
This commit is contained in:
Pavel Kholkin 2016-07-18 15:26:40 +03:00
parent b932067fb3
commit d166968eac
8 changed files with 120 additions and 4 deletions

View File

@ -25,4 +25,4 @@ API_MIN_VERSION = api_versions.APIVersion("2.1")
# when client supported the max version, and bumped sequentially, otherwise
# the client may break due to server side new version may include some
# backward incompatible change.
API_MAX_VERSION = api_versions.APIVersion("2.34")
API_MAX_VERSION = api_versions.APIVersion("2.35")

View File

@ -91,3 +91,30 @@ class TestKeypairsNovaClientV210(base.TenantTestBase):
self.assertRaises(
ValueError,
self._get_column_value_from_single_row_table, output, "Name")
class TestKeypairsNovaClientV235(base.TenantTestBase):
"""Keypairs functional tests for v2.35 nova-api microversion."""
COMPUTE_API_VERSION = "2.35"
def test_create_and_list_keypair_with_marker_and_limit(self):
names = []
for i in range(3):
names.append(self.name_generate("v2_35"))
self.nova("keypair-add %s --user %s" % (names[i], self.user_id))
self.addCleanup(self.another_nova, "keypair-delete %s" % names[i])
# sort keypairs before pagination
names = sorted(names)
# list only one keypair after the first
output_1 = self.another_nova("keypair-list --limit 1 --marker %s" %
names[0])
output_2 = self.nova("keypair-list --limit 1 --marker %s --user %s" %
(names[0], self.user_id))
self.assertEqual(output_1, output_2)
# it should be table with only one second key-pair
self.assertEqual(
names[1], self._get_column_value_from_single_row_table(output_1,
"Name"))

View File

@ -1190,7 +1190,7 @@ class FakeHTTPClient(base_client.HTTPClient):
return (200, {}, {'keypair':
self.get_os_keypairs()[2]['keypairs'][0]['keypair']})
def get_os_keypairs(self, *kw):
def get_os_keypairs(self, user_id=None, limit=None, marker=None, *kw):
return (200, {}, {
"keypairs": [{"keypair": {
"public_key": "FAKE_SSH_RSA",

View File

@ -111,3 +111,19 @@ class KeypairsV22TestCase(KeypairsTest):
'public_key': pub_key,
'type': 'ssh'}})
self.assertIsInstance(kp, keypairs.Keypair)
class KeypairsV35TestCase(KeypairsTest):
def setUp(self):
super(KeypairsV35TestCase, self).setUp()
self.cs.api_version = api_versions.APIVersion("2.35")
def test_list_keypairs(self):
kps = self.cs.keypairs.list(user_id='test_user', marker='test_kp',
limit=3)
self.assert_request_id(kps, fakes.FAKE_REQUEST_ID_LIST)
self.assert_called('GET',
'/%s?limit=3&marker=test_kp&user_id=test_user'
% self.keypair_prefix)
for kp in kps:
self.assertIsInstance(kp, keypairs.Keypair)

View File

@ -2873,6 +2873,21 @@ class ShellTest(utils.TestCase):
self.run_command('keypair-list')
self.assert_called('GET', '/os-keypairs')
def test_keypair_list_with_user_id(self):
self.run_command('keypair-list --user test_user', api_version='2.10')
self.assert_called('GET', '/os-keypairs?user_id=test_user')
def test_keypair_list_with_limit_and_marker(self):
self.run_command('keypair-list --marker test_kp --limit 3',
api_version='2.35')
self.assert_called('GET', '/os-keypairs?limit=3&marker=test_kp')
def test_keypair_list_with_user_id_limit_and_marker(self):
self.run_command('keypair-list --user test_user --marker test_kp '
'--limit 3', api_version='2.35')
self.assert_called(
'GET', '/os-keypairs?limit=3&marker=test_kp&user_id=test_user')
def test_keypair_show(self):
self.run_command('keypair-show test')
self.assert_called('GET', '/os-keypairs/test')

View File

@ -19,6 +19,7 @@ Keypair interface (1.1 extension).
from novaclient import api_versions
from novaclient import base
from novaclient import utils
class Keypair(base.Resource):
@ -162,7 +163,7 @@ class KeypairManager(base.ManagerWithFind):
"""
return self._list('/%s' % self.keypair_prefix, 'keypairs')
@api_versions.wraps("2.10")
@api_versions.wraps("2.10", "2.34")
def list(self, user_id=None):
"""
Get a list of keypairs.
@ -172,3 +173,25 @@ class KeypairManager(base.ManagerWithFind):
query_string = "?user_id=%s" % user_id if user_id else ""
url = '/%s%s' % (self.keypair_prefix, query_string)
return self._list(url, 'keypairs')
@api_versions.wraps("2.35")
def list(self, user_id=None, marker=None, limit=None):
"""
Get a list of keypairs.
:param user_id: Id of key-pairs owner (Admin only).
:param marker: Begin returning keypairs that appear later in the
keypair list than that represented by this keypair name
(optional).
:param limit: maximum number of keypairs to return (optional).
"""
params = {}
if user_id:
params['user_id'] = user_id
if limit:
params['limit'] = int(limit)
if marker:
params['marker'] = str(marker)
query_string = utils.prepare_query_string(params)
url = '/%s%s' % (self.keypair_prefix, query_string)
return self._list(url, 'keypairs')

View File

@ -3119,7 +3119,7 @@ def do_keypair_list(cs, args):
utils.print_list(keypairs, columns)
@api_versions.wraps("2.10")
@api_versions.wraps("2.10", "2.34")
@utils.arg(
'--user',
metavar='<user-id>',
@ -3132,6 +3132,36 @@ def do_keypair_list(cs, args):
utils.print_list(keypairs, columns)
@api_versions.wraps("2.35")
@utils.arg(
'--user',
metavar='<user-id>',
default=None,
help=_('List key-pairs of specified user ID (Admin only).'))
@utils.arg(
'--marker',
dest='marker',
metavar='<marker>',
default=None,
help=_('The last keypair of the previous page; displays list of keypairs '
'after "marker".'))
@utils.arg(
'--limit',
dest='limit',
metavar='<limit>',
type=int,
default=None,
help=_("Maximum number of keypairs to display. If limit == -1, all "
"keypairs will be displayed. If limit is bigger than "
"'osapi_max_limit' option of Nova API, limit 'osapi_max_limit' "
"will be used instead."))
def do_keypair_list(cs, args):
"""Print a list of keypairs for a user"""
keypairs = cs.keypairs.list(args.user, args.marker, args.limit)
columns = _get_keypairs_list_columns(cs, args)
utils.print_list(keypairs, columns)
def _print_keypair(keypair):
kp = keypair._info.copy()
pk = kp.pop('public_key')

View File

@ -0,0 +1,5 @@
---
features:
- Added microversion v2.35 that adds pagination support for keypairs with
the help of new optional parameters 'limit' and 'marker' which were added
to keypair-list command.