Skip to content

Commit b5a2714

Browse files
Ritvik Vinodkumarstephenfin
authored andcommitted
Switch compute service list, delete and set to sdk.
Switch the compute service commands from novaclient to SDK. Change-Id: I16498905101d6c2702a3ccbaf8cf5e3098d51992
1 parent 0a887a4 commit b5a2714

4 files changed

Lines changed: 238 additions & 156 deletions

File tree

openstackclient/compute/v2/service.py

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import logging
1919

20-
from novaclient import api_versions
20+
from openstack import utils as sdk_utils
2121
from osc_lib.command import command
2222
from osc_lib import exceptions
2323
from osc_lib import utils
@@ -40,16 +40,24 @@ def get_parser(self, prog_name):
4040
help=_("Compute service(s) to delete (ID only). If using "
4141
"``--os-compute-api-version`` 2.53 or greater, the ID is "
4242
"a UUID which can be retrieved by listing compute services "
43-
"using the same 2.53+ microversion.")
43+
"using the same 2.53+ microversion. "
44+
"If deleting a compute service, be sure to stop the actual "
45+
"compute process on the physical host before deleting the "
46+
"service with this command. Failing to do so can lead to "
47+
"the running service re-creating orphaned compute_nodes "
48+
"table records in the database.")
4449
)
4550
return parser
4651

4752
def take_action(self, parsed_args):
48-
compute_client = self.app.client_manager.compute
53+
compute_client = self.app.client_manager.sdk_connection.compute
4954
result = 0
5055
for s in parsed_args.service:
5156
try:
52-
compute_client.services.delete(s)
57+
compute_client.delete_service(
58+
s,
59+
ignore_missing=False
60+
)
5361
except Exception as e:
5462
result += 1
5563
LOG.error(_("Failed to delete compute service with "
@@ -91,38 +99,40 @@ def get_parser(self, prog_name):
9199
return parser
92100

93101
def take_action(self, parsed_args):
94-
compute_client = self.app.client_manager.compute
102+
compute_client = self.app.client_manager.sdk_connection.compute
103+
columns = (
104+
"id",
105+
"binary",
106+
"host",
107+
"availability_zone",
108+
"status",
109+
"state",
110+
"updated_at",
111+
)
112+
column_headers = (
113+
"ID",
114+
"Binary",
115+
"Host",
116+
"Zone",
117+
"Status",
118+
"State",
119+
"Updated At",
120+
)
95121
if parsed_args.long:
96-
columns = (
97-
"ID",
98-
"Binary",
99-
"Host",
100-
"Zone",
101-
"Status",
102-
"State",
103-
"Updated At",
104-
"Disabled Reason"
105-
)
106-
has_forced_down = (
107-
compute_client.api_version >= api_versions.APIVersion('2.11'))
108-
if has_forced_down:
109-
columns += ("Forced Down",)
110-
else:
111-
columns = (
112-
"ID",
113-
"Binary",
114-
"Host",
115-
"Zone",
116-
"Status",
117-
"State",
118-
"Updated At"
119-
)
120-
data = compute_client.services.list(parsed_args.host,
121-
parsed_args.service)
122-
return (columns,
123-
(utils.get_item_properties(
124-
s, columns,
125-
) for s in data))
122+
columns += ("disabled_reason",)
123+
column_headers += ("Disabled Reason",)
124+
if sdk_utils.supports_microversion(compute_client, '2.11'):
125+
columns += ("is_forced_down",)
126+
column_headers += ("Forced Down",)
127+
128+
data = compute_client.services(
129+
host=parsed_args.host,
130+
binary=parsed_args.service
131+
)
132+
return (
133+
column_headers,
134+
(utils.get_item_properties(s, columns) for s in data)
135+
)
126136

127137

128138
class SetService(command.Command):
@@ -175,15 +185,15 @@ def get_parser(self, prog_name):
175185
return parser
176186

177187
@staticmethod
178-
def _find_service_by_host_and_binary(cs, host, binary):
188+
def _find_service_by_host_and_binary(compute_client, host, binary):
179189
"""Utility method to find a compute service by host and binary
180190
181191
:param host: the name of the compute service host
182192
:param binary: the compute service binary, e.g. nova-compute
183193
:returns: novaclient.v2.services.Service dict-like object
184194
:raises: CommandError if no or multiple results were found
185195
"""
186-
services = cs.list(host=host, binary=binary)
196+
services = list(compute_client.services(host=host, binary=binary))
187197
# Did we find anything?
188198
if not len(services):
189199
msg = _('Compute service for host "%(host)s" and binary '
@@ -202,8 +212,7 @@ def _find_service_by_host_and_binary(cs, host, binary):
202212
return services[0]
203213

204214
def take_action(self, parsed_args):
205-
compute_client = self.app.client_manager.compute
206-
cs = compute_client.services
215+
compute_client = self.app.client_manager.sdk_connection.compute
207216

208217
if (parsed_args.enable or not parsed_args.disable) and \
209218
parsed_args.disable_reason:
@@ -216,14 +225,17 @@ def take_action(self, parsed_args):
216225
# services. If 2.53+ is used we need to find the nova-compute
217226
# service using the --host and --service (binary) values.
218227
requires_service_id = (
219-
compute_client.api_version >= api_versions.APIVersion('2.53'))
228+
sdk_utils.supports_microversion(compute_client, '2.53'))
220229
service_id = None
221230
if requires_service_id:
222231
# TODO(mriedem): Add an --id option so users can pass the service
223232
# id (as a uuid) directly rather than make us look it up using
224233
# host/binary.
225234
service_id = SetService._find_service_by_host_and_binary(
226-
cs, parsed_args.host, parsed_args.service).id
235+
compute_client,
236+
parsed_args.host,
237+
parsed_args.service
238+
).id
227239

228240
result = 0
229241
enabled = None
@@ -235,21 +247,18 @@ def take_action(self, parsed_args):
235247

236248
if enabled is not None:
237249
if enabled:
238-
args = (service_id,) if requires_service_id else (
239-
parsed_args.host, parsed_args.service)
240-
cs.enable(*args)
250+
compute_client.enable_service(
251+
service_id,
252+
parsed_args.host,
253+
parsed_args.service
254+
)
241255
else:
242-
if parsed_args.disable_reason:
243-
args = (service_id, parsed_args.disable_reason) if \
244-
requires_service_id else (
245-
parsed_args.host,
246-
parsed_args.service,
247-
parsed_args.disable_reason)
248-
cs.disable_log_reason(*args)
249-
else:
250-
args = (service_id,) if requires_service_id else (
251-
parsed_args.host, parsed_args.service)
252-
cs.disable(*args)
256+
compute_client.disable_service(
257+
service_id,
258+
parsed_args.host,
259+
parsed_args.service,
260+
parsed_args.disable_reason
261+
)
253262
except Exception:
254263
status = "enabled" if enabled else "disabled"
255264
LOG.error("Failed to set service status to %s", status)
@@ -261,15 +270,17 @@ def take_action(self, parsed_args):
261270
if parsed_args.up:
262271
force_down = False
263272
if force_down is not None:
264-
if compute_client.api_version < api_versions.APIVersion(
265-
'2.11'):
273+
if not sdk_utils.supports_microversion(compute_client, '2.11'):
266274
msg = _('--os-compute-api-version 2.11 or later is '
267275
'required')
268276
raise exceptions.CommandError(msg)
269277
try:
270-
args = (service_id, force_down) if requires_service_id else (
271-
parsed_args.host, parsed_args.service, force_down)
272-
cs.force_down(*args)
278+
compute_client.update_service_forced_down(
279+
service_id,
280+
parsed_args.host,
281+
parsed_args.service,
282+
force_down
283+
)
273284
except Exception:
274285
state = "down" if force_down else "up"
275286
LOG.error("Failed to set service state to %s", state)

openstackclient/tests/unit/compute/v2/fakes.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from openstack.compute.v2 import flavor as _flavor
2323
from openstack.compute.v2 import server
2424
from openstack.compute.v2 import server_interface as _server_interface
25+
from openstack.compute.v2 import service
2526
from openstack.compute.v2 import volume_attachment
2627

2728
from openstackclient.api import compute_v2
@@ -764,7 +765,7 @@ def create_one_service(attrs=None):
764765
:param dict attrs:
765766
A dictionary with all attributes
766767
:return:
767-
A FakeResource object, with id, host, binary, and so on
768+
A fake Service object, with id, host, binary, and so on
768769
"""
769770
attrs = attrs or {}
770771

@@ -774,21 +775,18 @@ def create_one_service(attrs=None):
774775
'host': 'host-' + uuid.uuid4().hex,
775776
'binary': 'binary-' + uuid.uuid4().hex,
776777
'status': 'enabled',
777-
'zone': 'zone-' + uuid.uuid4().hex,
778+
'availability_zone': 'zone-' + uuid.uuid4().hex,
778779
'state': 'state-' + uuid.uuid4().hex,
779780
'updated_at': 'time-' + uuid.uuid4().hex,
780781
'disabled_reason': 'earthquake',
781782
# Introduced in API microversion 2.11
782-
'forced_down': False,
783+
'is_forced_down': False,
783784
}
784785

785786
# Overwrite default attributes.
786787
service_info.update(attrs)
787788

788-
service = fakes.FakeResource(info=copy.deepcopy(service_info),
789-
loaded=True)
790-
791-
return service
789+
return service.Service(**service_info)
792790

793791
@staticmethod
794792
def create_services(attrs=None, count=2):

0 commit comments

Comments
 (0)