Merge "Compute: Add user id support for keypair"

This commit is contained in:
Zuul 2020-11-04 00:28:41 +00:00 committed by Gerrit Code Review
commit cb6659d7cd
3 changed files with 250 additions and 5 deletions

View File

@ -64,10 +64,20 @@ class CreateKeypair(command.ShowOne):
"(Supported by API versions '2.2' - '2.latest')" "(Supported by API versions '2.2' - '2.latest')"
), ),
) )
parser.add_argument(
'--user',
metavar='<user>',
help=_(
'The owner of the keypair. (admin only) (name or ID). '
'Requires ``--os-compute-api-version`` 2.10 or greater.'
),
)
identity_common.add_user_domain_option_to_parser(parser)
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.compute
identity_client = self.app.client_manager.identity
public_key = parsed_args.public_key public_key = parsed_args.public_key
if public_key: if public_key:
@ -89,12 +99,26 @@ class CreateKeypair(command.ShowOne):
if compute_client.api_version < api_versions.APIVersion('2.2'): if compute_client.api_version < api_versions.APIVersion('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'
) )
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
kwargs['key_type'] = parsed_args.type kwargs['key_type'] = parsed_args.type
if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'):
msg = _(
'--os-compute-api-version 2.10 or greater is required to '
'support the --user option'
)
raise exceptions.CommandError(msg)
kwargs['user_id'] = identity_common.find_user(
identity_client,
parsed_args.user,
parsed_args.user_domain,
).id
keypair = compute_client.keypairs.create(**kwargs) keypair = compute_client.keypairs.create(**kwargs)
private_key = parsed_args.private_key private_key = parsed_args.private_key
@ -139,16 +163,43 @@ class DeleteKeypair(command.Command):
nargs='+', nargs='+',
help=_("Name of key(s) to delete (name only)") help=_("Name of key(s) to delete (name only)")
) )
parser.add_argument(
'--user',
metavar='<user>',
help=_(
'The owner of the keypair. (admin only) (name or ID). '
'Requires ``--os-compute-api-version`` 2.10 or greater.'
),
)
identity_common.add_user_domain_option_to_parser(parser)
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.compute
identity_client = self.app.client_manager.identity
kwargs = {}
result = 0 result = 0
if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'):
msg = _(
'--os-compute-api-version 2.10 or greater is required to '
'support the --user option'
)
raise exceptions.CommandError(msg)
kwargs['user_id'] = identity_common.find_user(
identity_client,
parsed_args.user,
parsed_args.user_domain,
).id
for n in parsed_args.name: for n in parsed_args.name:
try: try:
data = utils.find_resource( data = utils.find_resource(
compute_client.keypairs, n) compute_client.keypairs, n)
compute_client.keypairs.delete(data.name) 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 "
@ -261,12 +312,39 @@ class ShowKeypair(command.ShowOne):
default=False, default=False,
help=_("Show only bare public key paired with the generated key") help=_("Show only bare public key paired with the generated key")
) )
parser.add_argument(
'--user',
metavar='<user>',
help=_(
'The owner of the keypair. (admin only) (name or ID). '
'Requires ``--os-compute-api-version`` 2.10 or greater.'
),
)
identity_common.add_user_domain_option_to_parser(parser)
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.compute
keypair = utils.find_resource(compute_client.keypairs, identity_client = self.app.client_manager.identity
parsed_args.name)
kwargs = {}
if parsed_args.user:
if compute_client.api_version < api_versions.APIVersion('2.10'):
msg = _(
'--os-compute-api-version 2.10 or greater is required to '
'support the --user option'
)
raise exceptions.CommandError(msg)
kwargs['user_id'] = identity_common.find_user(
identity_client,
parsed_args.user,
parsed_args.user_domain,
).id
keypair = utils.find_resource(
compute_client.keypairs, parsed_args.name, **kwargs)
info = {} info = {}
info.update(keypair._info) info.update(keypair._info)

View File

@ -38,6 +38,15 @@ class TestKeypair(compute_fakes.TestComputev2):
self.keypairs_mock = self.app.client_manager.compute.keypairs self.keypairs_mock = self.app.client_manager.compute.keypairs
self.keypairs_mock.reset_mock() self.keypairs_mock.reset_mock()
# Initialize the user mock
self.users_mock = self.app.client_manager.identity.users
self.users_mock.reset_mock()
self.users_mock.get.return_value = fakes.FakeResource(
None,
copy.deepcopy(identity_fakes.USER),
loaded=True,
)
class TestKeypairCreate(TestKeypair): class TestKeypairCreate(TestKeypair):
@ -226,6 +235,54 @@ 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):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [
'--user', identity_fakes.user_name,
self.keypair.name,
]
verifylist = [
('user', identity_fakes.user_name),
('name', self.keypair.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.create.assert_called_with(
name=self.keypair.name,
public_key=None,
user_id=identity_fakes.user_id,
)
self.assertEqual({}, columns)
self.assertEqual({}, data)
def test_key_pair_create_with_user_pre_v210(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9')
arglist = [
'--user', identity_fakes.user_name,
self.keypair.name,
]
verifylist = [
('user', identity_fakes.user_name),
('name', self.keypair.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.10 or greater is required', str(ex))
class TestKeypairDelete(TestKeypair): class TestKeypairDelete(TestKeypair):
@ -301,6 +358,51 @@ class TestKeypairDelete(TestKeypair):
self.keypairs[0].name self.keypairs[0].name
) )
def test_keypair_delete_with_user(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [
'--user', identity_fakes.user_name,
self.keypairs[0].name
]
verifylist = [
('user', identity_fakes.user_name),
('name', [self.keypairs[0].name]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ret = self.cmd.take_action(parsed_args)
self.assertIsNone(ret)
self.keypairs_mock.delete.assert_called_with(
self.keypairs[0].name,
user_id=identity_fakes.user_id,
)
def test_keypair_delete_with_user_pre_v210(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.9')
arglist = [
'--user', identity_fakes.user_name,
self.keypairs[0].name
]
verifylist = [
('user', identity_fakes.user_name),
('name', [self.keypairs[0].name]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.10 or greater is required', str(ex))
class TestKeypairList(TestKeypair): class TestKeypairList(TestKeypair):
@ -554,11 +656,14 @@ class TestKeypairShow(TestKeypair):
verifylist = [ verifylist = [
('name', self.keypair.name) ('name', self.keypair.name)
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.keypairs_mock.get.assert_called_with(
self.keypair.name,
)
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -579,3 +684,59 @@ class TestKeypairShow(TestKeypair):
self.assertEqual({}, columns) self.assertEqual({}, columns)
self.assertEqual({}, data) self.assertEqual({}, data)
def test_keypair_show_with_user(self):
# overwrite the setup one because we want to omit private_key
self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
no_pri=True)
self.keypairs_mock.get.return_value = self.keypair
self.data = (
self.keypair.fingerprint,
self.keypair.name,
self.keypair.type,
self.keypair.user_id
)
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.10')
arglist = [
'--user', identity_fakes.user_name,
self.keypair.name,
]
verifylist = [
('user', identity_fakes.user_name),
('name', self.keypair.name)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.users_mock.get.assert_called_with(identity_fakes.user_name)
self.keypairs_mock.get.assert_called_with(
self.keypair.name,
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)
def test_keypair_show_with_user_pre_v210(self):
arglist = [
'--user', identity_fakes.user_name,
self.keypair.name,
]
verifylist = [
('user', identity_fakes.user_name),
('name', self.keypair.name)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.10 or greater is required', str(ex))

View File

@ -0,0 +1,6 @@
---
features:
- |
Add ``--user`` option to the ``keypair create``, ``keypair delete``, and
``keypair show`` commands. Only available starting with
``--os-compute-api-version 2.10``.