Skip to content

Commit 1c51443

Browse files
committed
Refactor HostAPI.service_update
Change If32bca070185937ef83f689b7163d965a89ec10a is going to reflect a compute service's disabled status with the COMPUTE_STATUS_DISABLED trait in placement. This change does some prep work so that a common method in the HostAPI (service_update) can be used to abstract that for pre-2.53 and post-2.53 service update APIs. In the pre-2.53 case, the Service is retrieved using the host and binary which requires looking up the HostMapping. In the post-2.53 case, we already have the Service object and just need to save the changes. Part of blueprint pre-filter-disabled-computes Change-Id: Id916ccd21542c90bf80caec246dfdba72a9c58b8
1 parent 168d34c commit 1c51443

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

nova/api/openstack/compute/services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ def _update(self, context, host, binary, payload):
204204
raise webob.exc.HTTPBadRequest(explanation=msg)
205205

206206
try:
207-
self.host_api.service_update(context, host, binary, payload)
207+
self.host_api.service_update_by_host_and_binary(
208+
context, host, binary, payload)
208209
except (exception.HostBinaryNotFound,
209210
exception.HostMappingNotFound) as exc:
210211
raise webob.exc.HTTPNotFound(explanation=exc.format_message())
@@ -403,7 +404,7 @@ def update(self, req, id, body):
403404
raise webob.exc.HTTPBadRequest(explanation=msg)
404405

405406
# Now save our updates to the service record in the database.
406-
service.save()
407+
self.host_api.service_update(context, service)
407408

408409
# Return the full service record details.
409410
additional_fields = ['forced_down']

nova/compute/api.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,22 +5090,44 @@ def service_get_by_compute_host(self, context, host_name):
50905090
"""Get service entry for the given compute hostname."""
50915091
return objects.Service.get_by_compute_host(context, host_name)
50925092

5093-
def _service_update(self, context, host_name, binary, params_to_update):
5094-
"""Performs the actual service update operation."""
5095-
service = objects.Service.get_by_args(context, host_name, binary)
5096-
service.update(params_to_update)
5093+
def service_update(self, context, service):
5094+
"""Performs the actual service update operation.
5095+
5096+
:param context: nova auth RequestContext
5097+
:param service: nova.objects.Service object with changes already
5098+
set on the object
5099+
"""
50975100
service.save()
5101+
# TODO(mriedem): Reflect COMPUTE_STATUS_DISABLED trait changes to the
5102+
# associated compute node resource providers if the service's disabled
5103+
# status changed.
50985104
return service
50995105

51005106
@target_host_cell
5101-
def service_update(self, context, host_name, binary, params_to_update):
5107+
def service_update_by_host_and_binary(self, context, host_name, binary,
5108+
params_to_update):
51025109
"""Enable / Disable a service.
51035110
5111+
Determines the cell that the service is in using the HostMapping.
5112+
51045113
For compute services, this stops new builds and migrations going to
51055114
the host.
5115+
5116+
See also ``service_update``.
5117+
5118+
:param context: nova auth RequestContext
5119+
:param host_name: hostname of the service
5120+
:param binary: service binary (really only supports "nova-compute")
5121+
:param params_to_update: dict of changes to make to the Service object
5122+
:raises: HostMappingNotFound if the host is not mapped to a cell
5123+
:raises: HostBinaryNotFound if a services table record is not found
5124+
with the given host_name and binary
51065125
"""
5107-
return self._service_update(context, host_name, binary,
5108-
params_to_update)
5126+
# TODO(mriedem): Service.get_by_args is deprecated; we should use
5127+
# get_by_compute_host here (remember to update the "raises" docstring).
5128+
service = objects.Service.get_by_args(context, host_name, binary)
5129+
service.update(params_to_update)
5130+
return self.service_update(context, service)
51095131

51105132
def _service_delete(self, context, service_id):
51115133
"""Performs the actual Service deletion operation."""

nova/tests/unit/compute/test_host_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def _do_test(mock_service_get_by_compute_host):
320320

321321
_do_test()
322322

323-
def test_service_update(self):
323+
def test_service_update_by_host_and_binary(self):
324324
host_name = 'fake-host'
325325
binary = 'nova-compute'
326326
params_to_update = dict(disabled=True)
@@ -333,7 +333,7 @@ def _do_test(mock_service_update, mock_service_get_by_host_and_binary):
333333
mock_service_get_by_host_and_binary.return_value = expected_result
334334
mock_service_update.return_value = expected_result
335335

336-
result = self.host_api.service_update(
336+
result = self.host_api.service_update_by_host_and_binary(
337337
self.ctxt, host_name, binary, params_to_update)
338338
self._compare_obj(result, expected_result)
339339

0 commit comments

Comments
 (0)