compute: Add missing options for 'server set'

Add a new '--no-password' option to unset the password on an existing
server. In addition, add a new '--password' option that replaces the
interactive '--root-password' option. This makes sense given no other
commands uses interactive password options.

Checks that rely on specific API microversions now run before we execute
any action, to avoid situations where an update is only partially
applied.

Change-Id: Ibf8717efdd418a2d95215b4d9ab2acf0d57c4a70
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-11-18 15:38:18 +00:00
parent 9a976ada8e
commit 8a0f3fc6a8
3 changed files with 87 additions and 17 deletions

View File

@ -3472,18 +3472,35 @@ class SetServer(command.Command):
metavar='<new-name>', metavar='<new-name>',
help=_('New server name'), help=_('New server name'),
) )
parser.add_argument( password_group = parser.add_mutually_exclusive_group()
password_group.add_argument(
'--password',
help=_('Set the server password'),
)
password_group.add_argument(
'--no-password',
action='store_true',
help=_(
'Clear the admin password for the server from the metadata '
'service; note that this action does not actually change the '
'server password'
),
)
# TODO(stephenfin): Remove this in a future major version
password_group.add_argument(
'--root-password', '--root-password',
action="store_true", action="store_true",
help=_('Set new root password (interactive only)'), help=argparse.SUPPRESS,
) )
parser.add_argument( parser.add_argument(
'--property', '--property',
metavar='<key=value>', metavar='<key=value>',
action=parseractions.KeyValueAction, action=parseractions.KeyValueAction,
dest='properties', dest='properties',
help=_('Property to add/change for this server ' help=_(
'(repeat option to set multiple properties)'), 'Property to add/change for this server '
'(repeat option to set multiple properties)'
),
) )
parser.add_argument( parser.add_argument(
'--state', '--state',
@ -3494,8 +3511,10 @@ class SetServer(command.Command):
parser.add_argument( parser.add_argument(
'--description', '--description',
metavar='<description>', metavar='<description>',
help=_('New server description (supported by ' help=_(
'--os-compute-api-version 2.19 or above)'), 'New server description '
'(supported by --os-compute-api-version 2.19 or above)'
),
) )
parser.add_argument( parser.add_argument(
'--tag', '--tag',
@ -3519,6 +3538,22 @@ class SetServer(command.Command):
parsed_args.server, parsed_args.server,
) )
if parsed_args.description:
if server.api_version < api_versions.APIVersion("2.19"):
msg = _(
'--os-compute-api-version 2.19 or greater is required to '
'support the --description option'
)
raise exceptions.CommandError(msg)
if parsed_args.tags:
if server.api_version < api_versions.APIVersion('2.26'):
msg = _(
'--os-compute-api-version 2.26 or greater is required to '
'support the --tag option'
)
raise exceptions.CommandError(msg)
if parsed_args.name: if parsed_args.name:
server.update(name=parsed_args.name) server.update(name=parsed_args.name)
@ -3536,22 +3571,15 @@ class SetServer(command.Command):
else: else:
msg = _("Passwords do not match, password unchanged") msg = _("Passwords do not match, password unchanged")
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
elif parsed_args.password:
server.change_password(parsed_args.password)
elif parsed_args.no_password:
server.clear_password()
if parsed_args.description: if parsed_args.description:
if server.api_version < api_versions.APIVersion("2.19"):
msg = _("Description is not supported for "
"--os-compute-api-version less than 2.19")
raise exceptions.CommandError(msg)
server.update(description=parsed_args.description) server.update(description=parsed_args.description)
if parsed_args.tags: if parsed_args.tags:
if server.api_version < api_versions.APIVersion('2.26'):
msg = _(
'--os-compute-api-version 2.26 or greater is required to '
'support the --tag option'
)
raise exceptions.CommandError(msg)
for tag in parsed_args.tags: for tag in parsed_args.tags:
server.add_tag(tag=tag) server.add_tag(tag=tag)

View File

@ -6207,6 +6207,7 @@ class TestServerSet(TestServer):
'update': None, 'update': None,
'reset_state': None, 'reset_state': None,
'change_password': None, 'change_password': None,
'clear_password': None,
'add_tag': None, 'add_tag': None,
'set_tags': None, 'set_tags': None,
} }
@ -6290,6 +6291,37 @@ class TestServerSet(TestServer):
self.fake_servers[0], parsed_args.properties) self.fake_servers[0], parsed_args.properties)
self.assertIsNone(result) self.assertIsNone(result)
def test_server_set_with_password(self):
arglist = [
'--password', 'foo',
'foo_vm',
]
verifylist = [
('password', 'foo'),
('server', 'foo_vm'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.fake_servers[0].change_password.assert_called_once_with('foo')
def test_server_set_with_no_password(self):
arglist = [
'--no-password',
'foo_vm',
]
verifylist = [
('no_password', True),
('server', 'foo_vm'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.fake_servers[0].clear_password.assert_called_once_with()
# TODO(stephenfin): Remove this in a future major version
@mock.patch.object(getpass, 'getpass', @mock.patch.object(getpass, 'getpass',
return_value=mock.sentinel.fake_pass) return_value=mock.sentinel.fake_pass)
def test_server_set_with_root_password(self, mock_getpass): def test_server_set_with_root_password(self, mock_getpass):

View File

@ -0,0 +1,10 @@
---
features:
- |
Add ``--no-password`` option to ``server set`` command, allowing users
to clear the admin password from the metadata service. Note that this does
not actually change the server password.
upgrade:
- |
The ``server set --root-password`` option has been deprecated in favour of
a non-interactive ``--password`` option.