Skip to content

Commit ac1d86c

Browse files
author
Tang Chen
committed
Refactor SetService --enable/disable option
This patch changes the following: 1. --enable/disable option should follow the rules in the doc below: http://docs.openstack.org/developer/python-openstackclient/command-options.html#boolean-options 2. "--disable-resion" is specified but not "--disable", an exception is raised instead of igoring "--disable-reason" option. Change-Id: I92e9234111e661bfe7119a8e19389a87c874ab0c
1 parent 81718be commit ac1d86c

4 files changed

Lines changed: 57 additions & 49 deletions

File tree

doc/source/command-objects/compute-service.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ Set service command
6363
.. _compute-service-set:
6464
.. option:: --enable
6565

66-
Enable service (default)
66+
Enable service
6767

6868
.. option:: --disable
6969

7070
Disable service
7171

7272
.. option:: --disable-reason <reason>
7373

74-
Reason for disabling the service (in quotes). Note that when the service
75-
is enabled, this option is ignored.
74+
Reason for disabling the service (in quotes). Should be used with --disable option.
7675

7776
.. describe:: <host>
7877

openstackclient/compute/v2/service.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Service action implementations"""
1717

1818
from openstackclient.common import command
19+
from openstackclient.common import exceptions
1920
from openstackclient.common import utils
2021
from openstackclient.i18n import _
2122

@@ -110,40 +111,47 @@ def get_parser(self, prog_name):
110111
enabled_group = parser.add_mutually_exclusive_group()
111112
enabled_group.add_argument(
112113
"--enable",
113-
dest="enabled",
114-
default=True,
115114
action="store_true",
116-
help=_("Enable a service (default)")
115+
help=_("Enable service")
117116
)
118117
enabled_group.add_argument(
119118
"--disable",
120-
dest="enabled",
121-
action="store_false",
122-
help=_("Disable a service")
119+
action="store_true",
120+
help=_("Disable service")
123121
)
124122
parser.add_argument(
125123
"--disable-reason",
126124
default=None,
127125
metavar="<reason>",
128-
help=_("Reason for disabling the service (in quotas). Note that "
129-
"when the service is enabled, this option is ignored.")
126+
help=_("Reason for disabling the service (in quotas). "
127+
"Should be used with --disable option.")
130128
)
131129
return parser
132130

133131
def take_action(self, parsed_args):
134132
compute_client = self.app.client_manager.compute
135133
cs = compute_client.services
136134

137-
if not parsed_args.enabled:
135+
if (parsed_args.enable or not parsed_args.disable) and \
136+
parsed_args.disable_reason:
137+
msg = _("Cannot specify option --disable-reason without "
138+
"--disable specified.")
139+
raise exceptions.CommandError(msg)
140+
141+
enabled = None
142+
if parsed_args.enable:
143+
enabled = True
144+
if parsed_args.disable:
145+
enabled = False
146+
147+
if enabled is None:
148+
return
149+
elif enabled:
150+
cs.enable(parsed_args.host, parsed_args.service)
151+
else:
138152
if parsed_args.disable_reason:
139153
cs.disable_log_reason(parsed_args.host,
140154
parsed_args.service,
141155
parsed_args.disable_reason)
142156
else:
143157
cs.disable(parsed_args.host, parsed_args.service)
144-
else:
145-
if parsed_args.disable_reason:
146-
msg = _("argument --disable-reason has been ignored")
147-
self.log.info(msg)
148-
149-
cs.enable(parsed_args.host, parsed_args.service)

openstackclient/tests/compute/v2/test_service.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# under the License.
1414
#
1515

16-
import mock
17-
16+
from openstackclient.common import exceptions
1817
from openstackclient.compute.v2 import service
1918
from openstackclient.tests.compute.v2 import fakes as compute_fakes
2019

@@ -128,14 +127,31 @@ def setUp(self):
128127

129128
self.cmd = service.SetService(self.app, None)
130129

130+
def test_set_nothing(self):
131+
arglist = [
132+
self.service.host,
133+
self.service.binary,
134+
]
135+
verifylist = [
136+
('host', self.service.host),
137+
('service', self.service.binary),
138+
]
139+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
140+
result = self.cmd.take_action(parsed_args)
141+
142+
self.service_mock.enable.assert_not_called()
143+
self.service_mock.disable.assert_not_called()
144+
self.service_mock.disable_log_reason.assert_not_called()
145+
self.assertIsNone(result)
146+
131147
def test_service_set_enable(self):
132148
arglist = [
133149
'--enable',
134150
self.service.host,
135151
self.service.binary,
136152
]
137153
verifylist = [
138-
('enabled', True),
154+
('enable', True),
139155
('host', self.service.host),
140156
('service', self.service.binary),
141157
]
@@ -156,7 +172,7 @@ def test_service_set_disable(self):
156172
self.service.binary,
157173
]
158174
verifylist = [
159-
('enabled', False),
175+
('disable', True),
160176
('host', self.service.host),
161177
('service', self.service.binary),
162178
]
@@ -179,7 +195,7 @@ def test_service_set_disable_with_reason(self):
179195
self.service.binary,
180196
]
181197
verifylist = [
182-
('enabled', False),
198+
('disable', True),
183199
('disable_reason', reason),
184200
('host', self.service.host),
185201
('service', self.service.binary),
@@ -203,24 +219,13 @@ def test_service_set_only_with_disable_reason(self):
203219
self.service.binary,
204220
]
205221
verifylist = [
206-
('enabled', True),
207222
('disable_reason', reason),
208223
('host', self.service.host),
209224
('service', self.service.binary),
210225
]
211226
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
212-
213-
with mock.patch.object(self.cmd.log, 'info') as mock_log:
214-
result = self.cmd.take_action(parsed_args)
215-
216-
msg = "argument --disable-reason has been ignored"
217-
mock_log.assert_called_once_with(msg)
218-
219-
self.service_mock.enable.assert_called_with(
220-
self.service.host,
221-
self.service.binary
222-
)
223-
self.assertIsNone(result)
227+
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
228+
parsed_args)
224229

225230
def test_service_set_enable_with_disable_reason(self):
226231
reason = 'earthquake'
@@ -231,21 +236,11 @@ def test_service_set_enable_with_disable_reason(self):
231236
self.service.binary,
232237
]
233238
verifylist = [
234-
('enabled', True),
239+
('enable', True),
235240
('disable_reason', reason),
236241
('host', self.service.host),
237242
('service', self.service.binary),
238243
]
239244
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
240-
241-
with mock.patch.object(self.cmd.log, 'info') as mock_log:
242-
result = self.cmd.take_action(parsed_args)
243-
244-
msg = "argument --disable-reason has been ignored"
245-
mock_log.assert_called_once_with(msg)
246-
247-
self.service_mock.enable.assert_called_with(
248-
self.service.host,
249-
self.service.binary
250-
)
251-
self.assertIsNone(result)
245+
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
246+
parsed_args)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
upgrade:
3+
- An exception is not raised by command ``service set`` when nothing
4+
specified. Instead, the service is not enabled by default. And if
5+
``--disable-resion`` is specified but not ``--disable``, an
6+
exception will be raised.

0 commit comments

Comments
 (0)