From e8e99af4d371c2410a12b8b62e18b2982a9a8932 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 16 Apr 2019 13:36:00 +0000 Subject: [PATCH] Return None from get_server_by_id on 404 The interface for get_server_by_id SHOULD be None if not found like the rest of the cloud layer. At some point in the past that got broken. Fix it. Change-Id: Ibd4fdc22418679c3562faaabe8fe3d307e8e8283 --- openstack/cloud/_compute.py | 19 +++++++++++++++---- ...et-server-by-id-none-3e8538800fa09d82.yaml | 7 +++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/get-server-by-id-none-3e8538800fa09d82.yaml diff --git a/openstack/cloud/_compute.py b/openstack/cloud/_compute.py index a20e52942..a883ad3c7 100644 --- a/openstack/cloud/_compute.py +++ b/openstack/cloud/_compute.py @@ -27,6 +27,7 @@ from openstack.cloud import exc from openstack.cloud import meta from openstack.cloud import _normalize from openstack.cloud import _utils +from openstack import exceptions from openstack import proxy from openstack import utils @@ -568,10 +569,20 @@ class ComputeCloudMixin(_normalize.Normalizer): return meta.add_server_interfaces(self, server) def get_server_by_id(self, id): - data = proxy._json_response( - self.compute.get('/servers/{id}'.format(id=id))) - server = self._get_and_munchify('server', data) - return meta.add_server_interfaces(self, self._normalize_server(server)) + """Get a server by ID. + + :param id: ID of the server. + + :returns: A server dict or None if no matching server is found. + """ + try: + data = proxy._json_response( + self.compute.get('/servers/{id}'.format(id=id))) + server = self._get_and_munchify('server', data) + return meta.add_server_interfaces( + self, self._normalize_server(server)) + except exceptions.ResourceNotFound: + return None def get_server_group(self, name_or_id=None, filters=None): """Get a server group by name or ID. diff --git a/releasenotes/notes/get-server-by-id-none-3e8538800fa09d82.yaml b/releasenotes/notes/get-server-by-id-none-3e8538800fa09d82.yaml new file mode 100644 index 000000000..b3ef3fec3 --- /dev/null +++ b/releasenotes/notes/get-server-by-id-none-3e8538800fa09d82.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + The ``get_server_by_id`` method is supposed to return ``None`` if the + server in question can't be found, but a regression was introduced + causing it to raise ``ResourceNotFound`` instead. This has been corrected + and ``get_server_by_id`` returns ``None`` correctly again.