Switch server start, server stop to SDK

Switch the server start and server stop commands from novaclient to SDK.

Change-Id: I5ebfa6b2468d5f20b99ea0eab1aea9377be09b8c
This commit is contained in:
Thrivikram Mudunuri 2021-11-30 00:56:39 -05:00 committed by Stephen Finucane
parent ce8171bad9
commit 9241514137
3 changed files with 54 additions and 45 deletions

View File

@ -4582,7 +4582,7 @@ class StartServer(command.Command):
_description = _("Start server(s)")
def get_parser(self, prog_name):
parser = super(StartServer, self).get_parser(prog_name)
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
@ -4601,20 +4601,28 @@ class StartServer(command.Command):
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute
compute_client = self.app.client_manager.sdk_connection.compute
for server in parsed_args.server:
utils.find_resource(
compute_client.servers,
server,
all_tenants=parsed_args.all_projects,
).start()
try:
server_id = compute_client.find_server(
name=server,
details=False,
all_projects=parsed_args.all_projects,
ignore_missing=False,
).id
except sdk_exceptions.HttpException as exc:
if exc.status_code == 403:
msg = _("Policy doesn't allow passing all-projects")
raise exceptions.Forbidden(msg)
compute_client.start_server(server_id)
class StopServer(command.Command):
_description = _("Stop server(s)")
def get_parser(self, prog_name):
parser = super(StopServer, self).get_parser(prog_name)
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
@ -4633,13 +4641,21 @@ class StopServer(command.Command):
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute
compute_client = self.app.client_manager.sdk_connection.compute
for server in parsed_args.server:
utils.find_resource(
compute_client.servers,
server,
all_tenants=parsed_args.all_projects,
).stop()
try:
server_id = compute_client.find_server(
name=server,
details=False,
all_projects=parsed_args.all_projects,
ignore_missing=False,
).id
except sdk_exceptions.HttpException as exc:
if exc.status_code == 403:
msg = _("Policy doesn't allow passing all-projects")
raise exceptions.Forbidden(msg)
compute_client.stop_server(server_id)
class SuspendServer(command.Command):

View File

@ -8217,28 +8217,19 @@ class TestServerSsh(TestServer):
class TestServerStart(TestServer):
def setUp(self):
super(TestServerStart, self).setUp()
super().setUp()
# Get the command object to test
self.cmd = server.StartServer(self.app, None)
# Set methods to be tested.
self.methods = {
'start': None,
}
def test_server_start_one_server(self):
self.run_method_with_servers('start', 1)
self.run_method_with_sdk_servers('start_server', 1)
def test_server_start_multi_servers(self):
self.run_method_with_servers('start', 3)
self.run_method_with_sdk_servers('start_server', 3)
@mock.patch.object(common_utils, 'find_resource')
def test_server_start_with_all_projects(self, mock_find_resource):
servers = self.setup_servers_mock(count=1)
mock_find_resource.side_effect = compute_fakes.FakeServer.get_servers(
servers, 0,
)
def test_server_start_with_all_projects(self):
servers = self.setup_sdk_servers_mock(count=1)
arglist = [
servers[0].id,
@ -8251,36 +8242,30 @@ class TestServerStart(TestServer):
self.cmd.take_action(parsed_args)
mock_find_resource.assert_called_once_with(
mock.ANY, servers[0].id, all_tenants=True,
self.sdk_client.find_server.assert_called_once_with(
name=servers[0].id,
details=False,
all_projects=True,
ignore_missing=False,
)
class TestServerStop(TestServer):
def setUp(self):
super(TestServerStop, self).setUp()
super().setUp()
# Get the command object to test
self.cmd = server.StopServer(self.app, None)
# Set methods to be tested.
self.methods = {
'stop': None,
}
def test_server_stop_one_server(self):
self.run_method_with_servers('stop', 1)
self.run_method_with_sdk_servers('stop_server', 1)
def test_server_stop_multi_servers(self):
self.run_method_with_servers('stop', 3)
self.run_method_with_sdk_servers('stop_server', 3)
@mock.patch.object(common_utils, 'find_resource')
def test_server_start_with_all_projects(self, mock_find_resource):
def test_server_start_with_all_projects(self):
servers = self.setup_servers_mock(count=1)
mock_find_resource.side_effect = compute_fakes.FakeServer.get_servers(
servers, 0,
)
arglist = [
servers[0].id,
@ -8293,8 +8278,11 @@ class TestServerStop(TestServer):
self.cmd.take_action(parsed_args)
mock_find_resource.assert_called_once_with(
mock.ANY, servers[0].id, all_tenants=True,
self.sdk_client.find_server.assert_called_once_with(
name=servers[0].id,
details=False,
all_projects=True,
ignore_missing=False,
)

View File

@ -0,0 +1,5 @@
---
features:
- |
Migrate ``server start`` and ``server stop`` commands from novaclient to
sdk.