From 956eabe96783727d6e86e3896cacf89fb893e0f5 Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Tue, 19 Jun 2018 12:59:03 +0000 Subject: [PATCH] compute: limit the service's force down command above 2.10 The force down action is added in Microversion 2.11, we should limit the command only can be executed when the microversion is above 2.10. Change-Id: I0a87e02e71ff025d30181fc17ebcd003a590f110 --- openstackclient/compute/v2/service.py | 26 ++++++++++++------- .../tests/unit/compute/v2/test_service.py | 10 ++++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/openstackclient/compute/v2/service.py b/openstackclient/compute/v2/service.py index 7331d29d01..18e6d9d95e 100644 --- a/openstackclient/compute/v2/service.py +++ b/openstackclient/compute/v2/service.py @@ -17,6 +17,7 @@ import logging +from novaclient import api_versions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -192,18 +193,23 @@ class SetService(command.Command): result += 1 force_down = None - try: - if parsed_args.down: - force_down = True - if parsed_args.up: - force_down = False - if force_down is not None: + if parsed_args.down: + force_down = True + if parsed_args.up: + force_down = False + if force_down is not None: + if compute_client.api_version < api_versions.APIVersion( + '2.11'): + msg = _('--os-compute-api-version 2.11 or later is ' + 'required') + raise exceptions.CommandError(msg) + try: cs.force_down(parsed_args.host, parsed_args.service, force_down=force_down) - except Exception: - state = "down" if force_down else "up" - LOG.error("Failed to set service state to %s", state) - result += 1 + except Exception: + state = "down" if force_down else "up" + LOG.error("Failed to set service state to %s", state) + result += 1 if result > 0: msg = _("Compute service %(service)s of host %(host)s failed to " diff --git a/openstackclient/tests/unit/compute/v2/test_service.py b/openstackclient/tests/unit/compute/v2/test_service.py index 8403efc9c8..bd29912341 100644 --- a/openstackclient/tests/unit/compute/v2/test_service.py +++ b/openstackclient/tests/unit/compute/v2/test_service.py @@ -15,7 +15,7 @@ import mock from mock import call - +from novaclient import api_versions from osc_lib import exceptions from openstackclient.compute.v2 import service @@ -340,6 +340,8 @@ class TestServiceSet(TestService): ('service', self.service.binary), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.11') result = self.cmd.take_action(parsed_args) self.service_mock.force_down.assert_called_once_with( self.service.host, self.service.binary, force_down=False) @@ -359,6 +361,8 @@ class TestServiceSet(TestService): ('service', self.service.binary), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.11') result = self.cmd.take_action(parsed_args) self.service_mock.force_down.assert_called_once_with( self.service.host, self.service.binary, force_down=True) @@ -380,6 +384,8 @@ class TestServiceSet(TestService): ('service', self.service.binary), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.11') result = self.cmd.take_action(parsed_args) self.service_mock.enable.assert_called_once_with( self.service.host, self.service.binary) @@ -402,6 +408,8 @@ class TestServiceSet(TestService): ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.11') with mock.patch.object(self.service_mock, 'enable', side_effect=Exception()): self.assertRaises(exceptions.CommandError,