compute: Migrate 'reboot server' to SDK

Change-Id: Ibad4078f680d3b2615b9ca6f6c72c4fd28030b55
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2022-12-19 16:50:16 +00:00
parent 5282bd2622
commit 864f51f427
3 changed files with 133 additions and 14 deletions

View File

@ -25,7 +25,6 @@ import os
from cliff import columns as cliff_columns
import iso8601
from novaclient import api_versions
from novaclient.v2 import servers
from openstack import exceptions as sdk_exceptions
from openstack import utils as sdk_utils
from osc_lib.cli import format_columns
@ -3064,7 +3063,7 @@ class RebootServer(command.Command):
_description = _("Perform a hard or soft server reboot")
def get_parser(self, prog_name):
parser = super(RebootServer, self).get_parser(prog_name)
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
@ -3075,16 +3074,16 @@ class RebootServer(command.Command):
'--hard',
dest='reboot_type',
action='store_const',
const=servers.REBOOT_HARD,
default=servers.REBOOT_SOFT,
const='HARD',
default='SOFT',
help=_('Perform a hard reboot'),
)
group.add_argument(
'--soft',
dest='reboot_type',
action='store_const',
const=servers.REBOOT_SOFT,
default=servers.REBOOT_SOFT,
const='SOFT',
default='SOFT',
help=_('Perform a soft reboot'),
)
parser.add_argument(
@ -3101,21 +3100,23 @@ class RebootServer(command.Command):
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.flush()
compute_client = self.app.client_manager.compute
server = utils.find_resource(
compute_client.servers, parsed_args.server)
server.reboot(parsed_args.reboot_type)
compute_client = self.app.client_manager.sdk_connection.compute
server_id = compute_client.find_server(
parsed_args.server,
ignore_missing=False,
).id
compute_client.reboot_server(server_id, parsed_args.reboot_type)
if parsed_args.wait:
# We use osc-lib's wait_for_status since that allows for a callback
if utils.wait_for_status(
compute_client.servers.get,
server.id,
compute_client.get_server,
server_id,
callback=_show_progress,
):
self.app.stdout.write(_('Complete\n'))
else:
LOG.error(_('Error rebooting server: %s'),
server.id)
LOG.error(_('Error rebooting server: %s'), server_id)
self.app.stdout.write(_('Error rebooting server\n'))
raise SystemExit

View File

@ -5827,6 +5827,120 @@ class TestServerMigrate(TestServer):
self.assertNotCalled(self.servers_mock.live_migrate)
class TestServerReboot(TestServer):
def setUp(self):
super().setUp()
self.sdk_client.reboot_server.return_value = None
self.cmd = server.RebootServer(self.app, None)
def test_server_reboot(self):
servers = self.setup_sdk_servers_mock(count=1)
arglist = [
servers[0].id,
]
verifylist = [
('server', servers[0].id),
('reboot_type', 'SOFT'),
('wait', False),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.sdk_client.reboot_server.assert_called_once_with(
servers[0].id,
'SOFT',
)
self.assertIsNone(result)
def test_server_reboot_with_hard(self):
servers = self.setup_sdk_servers_mock(count=1)
arglist = [
'--hard',
servers[0].id,
]
verifylist = [
('server', servers[0].id),
('reboot_type', 'HARD'),
('wait', False),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.sdk_client.reboot_server.assert_called_once_with(
servers[0].id,
'HARD',
)
self.assertIsNone(result)
@mock.patch.object(common_utils, 'wait_for_status', return_value=True)
def test_server_reboot_with_wait(self, mock_wait_for_status):
servers = self.setup_sdk_servers_mock(count=1)
arglist = [
'--wait',
servers[0].id,
]
verifylist = [
('server', servers[0].id),
('reboot_type', 'SOFT'),
('wait', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.assertIsNone(result)
self.sdk_client.reboot_server.assert_called_once_with(
servers[0].id,
'SOFT',
)
mock_wait_for_status.assert_called_once_with(
self.sdk_client.get_server,
servers[0].id,
callback=mock.ANY,
)
@mock.patch.object(server.LOG, 'error')
@mock.patch.object(common_utils, 'wait_for_status', return_value=False)
def test_server_reboot_with_wait_fails(
self,
mock_wait_for_status,
mock_log,
):
servers = self.setup_sdk_servers_mock(count=1)
arglist = [
'--wait',
servers[0].id,
]
verifylist = [
('server', servers[0].id),
('reboot_type', 'SOFT'),
('wait', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(SystemExit, self.cmd.take_action, parsed_args)
self.assertIn('Error rebooting server', mock_log.call_args[0][0])
self.sdk_client.reboot_server.assert_called_once_with(
servers[0].id,
'SOFT',
)
mock_wait_for_status.assert_called_once_with(
self.sdk_client.get_server,
servers[0].id,
callback=mock.ANY,
)
class TestServerPause(TestServer):
def setUp(self):

View File

@ -0,0 +1,4 @@
---
features:
- |
Migrate ``server reboot`` command from novaclient to SDK.