Switch 'openstack keypair' ops to use SDK

Let's continue our journey and start using SDK for the keypair
operations

Depends-On: https://review.opendev.org/#/c/761883/
Change-Id: Id411e70b8e1a79c0e88a0e22be7ff37e5c30fcda
This commit is contained in:
Artem Goncharov 2020-11-09 13:14:53 +01:00
parent cb6659d7cd
commit 512ba114a1
3 changed files with 128 additions and 146 deletions

View File

@ -20,7 +20,7 @@ import logging
import os import os
import sys import sys
from novaclient import api_versions from openstack import utils as sdk_utils
from osc_lib.command import command from osc_lib.command import command
from osc_lib import exceptions from osc_lib import exceptions
from osc_lib import utils from osc_lib import utils
@ -32,6 +32,19 @@ from openstackclient.identity import common as identity_common
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def _get_keypair_columns(item, hide_pub_key=False, hide_priv_key=False):
# To maintain backwards compatibility we need to rename sdk props to
# whatever OSC was using before
column_map = {}
hidden_columns = ['links', 'location']
if hide_pub_key:
hidden_columns.append('public_key')
if hide_priv_key:
hidden_columns.append('private_key')
return utils.get_osc_show_columns_for_sdk_resource(
item, column_map, hidden_columns)
class CreateKeypair(command.ShowOne): class CreateKeypair(command.ShowOne):
_description = _("Create new public or private key for server ssh access") _description = _("Create new public or private key for server ssh access")
@ -76,9 +89,13 @@ class CreateKeypair(command.ShowOne):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
kwargs = {
'name': parsed_args.name
}
public_key = parsed_args.public_key public_key = parsed_args.public_key
if public_key: if public_key:
try: try:
@ -91,12 +108,10 @@ class CreateKeypair(command.ShowOne):
"exception": e} "exception": e}
) )
kwargs = { kwargs['public_key'] = public_key
'name': parsed_args.name,
'public_key': public_key,
}
if parsed_args.type: if parsed_args.type:
if compute_client.api_version < api_versions.APIVersion('2.2'): if not sdk_utils.supports_microversion(compute_client, '2.2'):
msg = _( msg = _(
'--os-compute-api-version 2.2 or greater is required to ' '--os-compute-api-version 2.2 or greater is required to '
'support the --type option' 'support the --type option'
@ -106,7 +121,7 @@ class CreateKeypair(command.ShowOne):
kwargs['key_type'] = parsed_args.type kwargs['key_type'] = parsed_args.type
if parsed_args.user: if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'): if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _( msg = _(
'--os-compute-api-version 2.10 or greater is required to ' '--os-compute-api-version 2.10 or greater is required to '
'support the --user option' 'support the --user option'
@ -119,7 +134,7 @@ class CreateKeypair(command.ShowOne):
parsed_args.user_domain, parsed_args.user_domain,
).id ).id
keypair = compute_client.keypairs.create(**kwargs) keypair = compute_client.create_keypair(**kwargs)
private_key = parsed_args.private_key private_key = parsed_args.private_key
# Save private key into specified file # Save private key into specified file
@ -139,14 +154,12 @@ class CreateKeypair(command.ShowOne):
# NOTE(dtroyer): how do we want to handle the display of the private # NOTE(dtroyer): how do we want to handle the display of the private
# key when it needs to be communicated back to the user # key when it needs to be communicated back to the user
# For now, duplicate nova keypair-add command output # For now, duplicate nova keypair-add command output
info = {}
if public_key or private_key: if public_key or private_key:
info.update(keypair._info) display_columns, columns = _get_keypair_columns(
if 'public_key' in info: keypair, hide_pub_key=True, hide_priv_key=True)
del info['public_key'] data = utils.get_item_properties(keypair, columns)
if 'private_key' in info:
del info['private_key'] return (display_columns, data)
return zip(*sorted(info.items()))
else: else:
sys.stdout.write(keypair.private_key) sys.stdout.write(keypair.private_key)
return ({}, {}) return ({}, {})
@ -175,14 +188,14 @@ class DeleteKeypair(command.Command):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
kwargs = {} kwargs = {}
result = 0 result = 0
if parsed_args.user: if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'): if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _( msg = _(
'--os-compute-api-version 2.10 or greater is required to ' '--os-compute-api-version 2.10 or greater is required to '
'support the --user option' 'support the --user option'
@ -197,9 +210,8 @@ class DeleteKeypair(command.Command):
for n in parsed_args.name: for n in parsed_args.name:
try: try:
data = utils.find_resource( compute_client.delete_keypair(
compute_client.keypairs, n) n, **kwargs, ignore_missing=False)
compute_client.keypairs.delete(data.name, **kwargs)
except Exception as e: except Exception as e:
result += 1 result += 1
LOG.error(_("Failed to delete key with name " LOG.error(_("Failed to delete key with name "
@ -240,11 +252,11 @@ class ListKeypair(command.Lister):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
if parsed_args.project: if parsed_args.project:
if compute_client.api_version < api_versions.APIVersion('2.10'): if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _( msg = _(
'--os-compute-api-version 2.10 or greater is required to ' '--os-compute-api-version 2.10 or greater is required to '
'support the --project option' 'support the --project option'
@ -263,9 +275,9 @@ class ListKeypair(command.Lister):
data = [] data = []
for user in users: for user in users:
data.extend(compute_client.keypairs.list(user_id=user.id)) data.extend(compute_client.keypairs(user_id=user.id))
elif parsed_args.user: elif parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'): if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _( msg = _(
'--os-compute-api-version 2.10 or greater is required to ' '--os-compute-api-version 2.10 or greater is required to '
'support the --user option' 'support the --user option'
@ -278,16 +290,16 @@ class ListKeypair(command.Lister):
parsed_args.user_domain, parsed_args.user_domain,
) )
data = compute_client.keypairs.list(user_id=user.id) data = compute_client.keypairs(user_id=user.id)
else: else:
data = compute_client.keypairs.list() data = compute_client.keypairs()
columns = ( columns = (
"Name", "Name",
"Fingerprint" "Fingerprint"
) )
if compute_client.api_version >= api_versions.APIVersion('2.2'): if sdk_utils.supports_microversion(compute_client, '2.2'):
columns += ("Type", ) columns += ("Type", )
return ( return (
@ -324,13 +336,13 @@ class ShowKeypair(command.ShowOne):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
kwargs = {} kwargs = {}
if parsed_args.user: if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'): if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _( msg = _(
'--os-compute-api-version 2.10 or greater is required to ' '--os-compute-api-version 2.10 or greater is required to '
'support the --user option' 'support the --user option'
@ -343,16 +355,14 @@ class ShowKeypair(command.ShowOne):
parsed_args.user_domain, parsed_args.user_domain,
).id ).id
keypair = utils.find_resource( keypair = compute_client.find_keypair(
compute_client.keypairs, parsed_args.name, **kwargs) parsed_args.name, **kwargs, ignore_missing=False)
info = {}
info.update(keypair._info)
if not parsed_args.public_key: if not parsed_args.public_key:
del info['public_key'] display_columns, columns = _get_keypair_columns(
return zip(*sorted(info.items())) keypair, hide_pub_key=True)
data = utils.get_item_properties(keypair, columns)
return (display_columns, data)
else: else:
# NOTE(dtroyer): a way to get the public key in a similar form
# as the private key in the create command
sys.stdout.write(keypair.public_key) sys.stdout.write(keypair.public_key)
return ({}, {}) return ({}, {})

View File

@ -19,8 +19,8 @@ from unittest.mock import call
import uuid import uuid
from novaclient import api_versions from novaclient import api_versions
from openstack import utils as sdk_utils
from osc_lib import exceptions from osc_lib import exceptions
from osc_lib import utils
from openstackclient.compute.v2 import keypair from openstackclient.compute.v2 import keypair
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
@ -34,10 +34,6 @@ class TestKeypair(compute_fakes.TestComputev2):
def setUp(self): def setUp(self):
super(TestKeypair, self).setUp() super(TestKeypair, self).setUp()
# Get a shortcut to the KeypairManager Mock
self.keypairs_mock = self.app.client_manager.compute.keypairs
self.keypairs_mock.reset_mock()
# Initialize the user mock # Initialize the user mock
self.users_mock = self.app.client_manager.identity.users self.users_mock = self.app.client_manager.identity.users
self.users_mock.reset_mock() self.users_mock.reset_mock()
@ -47,6 +43,14 @@ class TestKeypair(compute_fakes.TestComputev2):
loaded=True, loaded=True,
) )
self.app.client_manager.sdk_connection = mock.Mock()
self.app.client_manager.sdk_connection.compute = mock.Mock()
self.sdk_client = self.app.client_manager.sdk_connection.compute
self.sdk_client.keypairs = mock.Mock()
self.sdk_client.create_keypair = mock.Mock()
self.sdk_client.delete_keypair = mock.Mock()
self.sdk_client.find_keypair = mock.Mock()
class TestKeypairCreate(TestKeypair): class TestKeypairCreate(TestKeypair):
@ -71,7 +75,7 @@ class TestKeypairCreate(TestKeypair):
# Get the command object to test # Get the command object to test
self.cmd = keypair.CreateKeypair(self.app, None) self.cmd = keypair.CreateKeypair(self.app, None)
self.keypairs_mock.create.return_value = self.keypair self.sdk_client.create_keypair.return_value = self.keypair
def test_key_pair_create_no_options(self): def test_key_pair_create_no_options(self):
@ -85,9 +89,8 @@ class TestKeypairCreate(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with( self.sdk_client.create_keypair.assert_called_with(
name=self.keypair.name, name=self.keypair.name
public_key=None
) )
self.assertEqual({}, columns) self.assertEqual({}, columns)
@ -97,7 +100,7 @@ class TestKeypairCreate(TestKeypair):
# overwrite the setup one because we want to omit private_key # overwrite the setup one because we want to omit private_key
self.keypair = compute_fakes.FakeKeypair.create_one_keypair( self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
no_pri=True) no_pri=True)
self.keypairs_mock.create.return_value = self.keypair self.sdk_client.create_keypair.return_value = self.keypair
self.data = ( self.data = (
self.keypair.fingerprint, self.keypair.fingerprint,
@ -124,7 +127,7 @@ class TestKeypairCreate(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with( self.sdk_client.create_keypair.assert_called_with(
name=self.keypair.name, name=self.keypair.name,
public_key=self.keypair.public_key, public_key=self.keypair.public_key,
) )
@ -151,9 +154,8 @@ class TestKeypairCreate(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with( self.sdk_client.create_keypair.assert_called_with(
name=self.keypair.name, name=self.keypair.name,
public_key=None,
) )
mock_open.assert_called_once_with(tmp_pk_file, 'w+') mock_open.assert_called_once_with(tmp_pk_file, 'w+')
@ -162,14 +164,12 @@ class TestKeypairCreate(TestKeypair):
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_keypair_create_with_key_type(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
self.app.client_manager.compute.api_version = api_versions.APIVersion( def test_keypair_create_with_key_type(self, sm_mock):
'2.2')
for key_type in ['x509', 'ssh']: for key_type in ['x509', 'ssh']:
self.keypair = compute_fakes.FakeKeypair.create_one_keypair( self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
no_pri=True) no_pri=True)
self.keypairs_mock.create.return_value = self.keypair self.sdk_client.create_keypair.return_value = self.keypair
self.data = ( self.data = (
self.keypair.fingerprint, self.keypair.fingerprint,
@ -195,7 +195,7 @@ class TestKeypairCreate(TestKeypair):
m_file.read.return_value = 'dummy' m_file.read.return_value = 'dummy'
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with( self.sdk_client.create_keypair.assert_called_with(
name=self.keypair.name, name=self.keypair.name,
public_key=self.keypair.public_key, public_key=self.keypair.public_key,
key_type=key_type, key_type=key_type,
@ -204,10 +204,8 @@ class TestKeypairCreate(TestKeypair):
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_keypair_create_with_key_type_pre_v22(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
self.app.client_manager.compute.api_version = api_versions.APIVersion( def test_keypair_create_with_key_type_pre_v22(self, sm_mock):
'2.1')
for key_type in ['x509', 'ssh']: for key_type in ['x509', 'ssh']:
arglist = [ arglist = [
'--public-key', self.keypair.public_key, '--public-key', self.keypair.public_key,
@ -235,11 +233,8 @@ class TestKeypairCreate(TestKeypair):
'--os-compute-api-version 2.2 or greater is required', '--os-compute-api-version 2.2 or greater is required',
str(ex)) str(ex))
def test_key_pair_create_with_user(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
def test_key_pair_create_with_user(self, sm_mock):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
self.keypair.name, self.keypair.name,
@ -252,20 +247,16 @@ class TestKeypairCreate(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with( self.sdk_client.create_keypair.assert_called_with(
name=self.keypair.name, name=self.keypair.name,
public_key=None,
user_id=identity_fakes.user_id, user_id=identity_fakes.user_id,
) )
self.assertEqual({}, columns) self.assertEqual({}, columns)
self.assertEqual({}, data) self.assertEqual({}, data)
def test_key_pair_create_with_user_pre_v210(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_key_pair_create_with_user_pre_v210(self, sm_mock):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9')
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
self.keypair.name, self.keypair.name,
@ -291,10 +282,6 @@ class TestKeypairDelete(TestKeypair):
def setUp(self): def setUp(self):
super(TestKeypairDelete, self).setUp() super(TestKeypairDelete, self).setUp()
self.keypairs_mock.get = compute_fakes.FakeKeypair.get_keypairs(
self.keypairs)
self.keypairs_mock.delete.return_value = None
self.cmd = keypair.DeleteKeypair(self.app, None) self.cmd = keypair.DeleteKeypair(self.app, None)
def test_keypair_delete(self): def test_keypair_delete(self):
@ -310,7 +297,8 @@ class TestKeypairDelete(TestKeypair):
ret = self.cmd.take_action(parsed_args) ret = self.cmd.take_action(parsed_args)
self.assertIsNone(ret) self.assertIsNone(ret)
self.keypairs_mock.delete.assert_called_with(self.keypairs[0].name) self.sdk_client.delete_keypair.assert_called_with(
self.keypairs[0].name, ignore_missing=False)
def test_delete_multiple_keypairs(self): def test_delete_multiple_keypairs(self):
arglist = [] arglist = []
@ -325,8 +313,8 @@ class TestKeypairDelete(TestKeypair):
calls = [] calls = []
for k in self.keypairs: for k in self.keypairs:
calls.append(call(k.name)) calls.append(call(k.name, ignore_missing=False))
self.keypairs_mock.delete.assert_has_calls(calls) self.sdk_client.delete_keypair.assert_has_calls(calls)
self.assertIsNone(result) self.assertIsNone(result)
def test_delete_multiple_keypairs_with_exception(self): def test_delete_multiple_keypairs_with_exception(self):
@ -340,29 +328,21 @@ class TestKeypairDelete(TestKeypair):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
find_mock_result = [self.keypairs[0], exceptions.CommandError] self.sdk_client.delete_keypair.side_effect = [
with mock.patch.object(utils, 'find_resource', None, exceptions.CommandError]
side_effect=find_mock_result) as find_mock: try:
try: self.cmd.take_action(parsed_args)
self.cmd.take_action(parsed_args) self.fail('CommandError should be raised.')
self.fail('CommandError should be raised.') except exceptions.CommandError as e:
except exceptions.CommandError as e: self.assertEqual('1 of 2 keys failed to delete.', str(e))
self.assertEqual('1 of 2 keys failed to delete.', str(e))
find_mock.assert_any_call( calls = []
self.keypairs_mock, self.keypairs[0].name) for k in arglist:
find_mock.assert_any_call(self.keypairs_mock, 'unexist_keypair') calls.append(call(k, ignore_missing=False))
self.sdk_client.delete_keypair.assert_has_calls(calls)
self.assertEqual(2, find_mock.call_count)
self.keypairs_mock.delete.assert_called_once_with(
self.keypairs[0].name
)
def test_keypair_delete_with_user(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
@mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
def test_keypair_delete_with_user(self, sm_mock):
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
self.keypairs[0].name self.keypairs[0].name
@ -376,12 +356,14 @@ class TestKeypairDelete(TestKeypair):
ret = self.cmd.take_action(parsed_args) ret = self.cmd.take_action(parsed_args)
self.assertIsNone(ret) self.assertIsNone(ret)
self.keypairs_mock.delete.assert_called_with( self.sdk_client.delete_keypair.assert_called_with(
self.keypairs[0].name, self.keypairs[0].name,
user_id=identity_fakes.user_id, user_id=identity_fakes.user_id,
ignore_missing=False
) )
def test_keypair_delete_with_user_pre_v210(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_keypair_delete_with_user_pre_v210(self, sm_mock):
self.app.client_manager.compute.api_version = \ self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9') api_versions.APIVersion('2.9')
@ -406,18 +388,19 @@ class TestKeypairDelete(TestKeypair):
class TestKeypairList(TestKeypair): class TestKeypairList(TestKeypair):
# Return value of self.keypairs_mock.list(). # Return value of self.sdk_client.keypairs().
keypairs = compute_fakes.FakeKeypair.create_keypairs(count=1) keypairs = compute_fakes.FakeKeypair.create_keypairs(count=1)
def setUp(self): def setUp(self):
super(TestKeypairList, self).setUp() super(TestKeypairList, self).setUp()
self.keypairs_mock.list.return_value = self.keypairs self.sdk_client.keypairs.return_value = self.keypairs
# Get the command object to test # Get the command object to test
self.cmd = keypair.ListKeypair(self.app, None) self.cmd = keypair.ListKeypair(self.app, None)
def test_keypair_list_no_options(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_keypair_list_no_options(self, sm_mock):
arglist = [] arglist = []
verifylist = [] verifylist = []
@ -430,7 +413,7 @@ class TestKeypairList(TestKeypair):
# Set expected values # Set expected values
self.keypairs_mock.list.assert_called_with() self.sdk_client.keypairs.assert_called_with()
self.assertEqual(('Name', 'Fingerprint'), columns) self.assertEqual(('Name', 'Fingerprint'), columns)
self.assertEqual( self.assertEqual(
@ -438,10 +421,8 @@ class TestKeypairList(TestKeypair):
tuple(data) tuple(data)
) )
def test_keypair_list_v22(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
self.app.client_manager.compute.api_version = \ def test_keypair_list_v22(self, sm_mock):
api_versions.APIVersion('2.2')
arglist = [] arglist = []
verifylist = [] verifylist = []
@ -454,7 +435,7 @@ class TestKeypairList(TestKeypair):
# Set expected values # Set expected values
self.keypairs_mock.list.assert_called_with() self.sdk_client.keypairs.assert_called_with()
self.assertEqual(('Name', 'Fingerprint', 'Type'), columns) self.assertEqual(('Name', 'Fingerprint', 'Type'), columns)
self.assertEqual( self.assertEqual(
@ -466,11 +447,8 @@ class TestKeypairList(TestKeypair):
tuple(data) tuple(data)
) )
def test_keypair_list_with_user(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
def test_keypair_list_with_user(self, sm_mock):
# Filtering by user is support for nova api 2.10 or above
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
users_mock = self.app.client_manager.identity.users users_mock = self.app.client_manager.identity.users
users_mock.reset_mock() users_mock.reset_mock()
@ -491,7 +469,7 @@ class TestKeypairList(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
users_mock.get.assert_called_with(identity_fakes.user_name) users_mock.get.assert_called_with(identity_fakes.user_name)
self.keypairs_mock.list.assert_called_with( self.sdk_client.keypairs.assert_called_with(
user_id=identity_fakes.user_id, user_id=identity_fakes.user_id,
) )
@ -505,10 +483,8 @@ class TestKeypairList(TestKeypair):
tuple(data) tuple(data)
) )
def test_keypair_list_with_user_pre_v210(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_keypair_list_with_user_pre_v210(self, sm_mock):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9')
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
@ -525,11 +501,8 @@ class TestKeypairList(TestKeypair):
self.assertIn( self.assertIn(
'--os-compute-api-version 2.10 or greater is required', str(ex)) '--os-compute-api-version 2.10 or greater is required', str(ex))
def test_keypair_list_with_project(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
def test_keypair_list_with_project(self, sm_mock):
# Filtering by user is support for nova api 2.10 or above
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
projects_mock = self.app.client_manager.identity.tenants projects_mock = self.app.client_manager.identity.tenants
projects_mock.reset_mock() projects_mock.reset_mock()
@ -557,7 +530,7 @@ class TestKeypairList(TestKeypair):
projects_mock.get.assert_called_with(identity_fakes.project_name) projects_mock.get.assert_called_with(identity_fakes.project_name)
users_mock.list.assert_called_with(tenant_id=identity_fakes.project_id) users_mock.list.assert_called_with(tenant_id=identity_fakes.project_id)
self.keypairs_mock.list.assert_called_with( self.sdk_client.keypairs.assert_called_with(
user_id=identity_fakes.user_id, user_id=identity_fakes.user_id,
) )
@ -571,10 +544,8 @@ class TestKeypairList(TestKeypair):
tuple(data) tuple(data)
) )
def test_keypair_list_with_project_pre_v210(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_keypair_list_with_project_pre_v210(self, sm_mock):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9')
arglist = ['--project', identity_fakes.project_name] arglist = ['--project', identity_fakes.project_name]
verifylist = [('project', identity_fakes.project_name)] verifylist = [('project', identity_fakes.project_name)]
@ -589,10 +560,6 @@ class TestKeypairList(TestKeypair):
def test_keypair_list_conflicting_user_options(self): def test_keypair_list_conflicting_user_options(self):
# Filtering by user is support for nova api 2.10 or above
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
'--project', identity_fakes.project_name, '--project', identity_fakes.project_name,
@ -610,7 +577,7 @@ class TestKeypairShow(TestKeypair):
def setUp(self): def setUp(self):
super(TestKeypairShow, self).setUp() super(TestKeypairShow, self).setUp()
self.keypairs_mock.get.return_value = self.keypair self.sdk_client.find_keypair.return_value = self.keypair
self.cmd = keypair.ShowKeypair(self.app, None) self.cmd = keypair.ShowKeypair(self.app, None)
@ -641,7 +608,7 @@ class TestKeypairShow(TestKeypair):
# overwrite the setup one because we want to omit private_key # overwrite the setup one because we want to omit private_key
self.keypair = compute_fakes.FakeKeypair.create_one_keypair( self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
no_pri=True) no_pri=True)
self.keypairs_mock.get.return_value = self.keypair self.sdk_client.find_keypair.return_value = self.keypair
self.data = ( self.data = (
self.keypair.fingerprint, self.keypair.fingerprint,
@ -660,8 +627,9 @@ class TestKeypairShow(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.get.assert_called_with( self.sdk_client.find_keypair.assert_called_with(
self.keypair.name, self.keypair.name,
ignore_missing=False
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
@ -685,12 +653,13 @@ class TestKeypairShow(TestKeypair):
self.assertEqual({}, columns) self.assertEqual({}, columns)
self.assertEqual({}, data) self.assertEqual({}, data)
def test_keypair_show_with_user(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=True)
def test_keypair_show_with_user(self, sm_mock):
# overwrite the setup one because we want to omit private_key # overwrite the setup one because we want to omit private_key
self.keypair = compute_fakes.FakeKeypair.create_one_keypair( self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
no_pri=True) no_pri=True)
self.keypairs_mock.get.return_value = self.keypair self.sdk_client.find_keypair.return_value = self.keypair
self.data = ( self.data = (
self.keypair.fingerprint, self.keypair.fingerprint,
@ -699,9 +668,6 @@ class TestKeypairShow(TestKeypair):
self.keypair.user_id self.keypair.user_id
) )
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,
self.keypair.name, self.keypair.name,
@ -715,14 +681,17 @@ class TestKeypairShow(TestKeypair):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.users_mock.get.assert_called_with(identity_fakes.user_name) self.users_mock.get.assert_called_with(identity_fakes.user_name)
self.keypairs_mock.get.assert_called_with( self.sdk_client.find_keypair.assert_called_with(
self.keypair.name, self.keypair.name,
ignore_missing=False,
user_id=identity_fakes.user_id
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_keypair_show_with_user_pre_v210(self): @mock.patch.object(sdk_utils, 'supports_microversion', return_value=False)
def test_keypair_show_with_user_pre_v210(self, sm_mock):
arglist = [ arglist = [
'--user', identity_fakes.user_name, '--user', identity_fakes.user_name,

View File

@ -0,0 +1,3 @@
---
Features:
- Switch 'openstack keypair' commands to use OpenStackSDK