Browse Source

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
Monty Taylor 5 days ago
parent
commit
e8e99af4d3

+ 15
- 4
openstack/cloud/_compute.py View File

@@ -27,6 +27,7 @@ from openstack.cloud import exc
27 27
 from openstack.cloud import meta
28 28
 from openstack.cloud import _normalize
29 29
 from openstack.cloud import _utils
30
+from openstack import exceptions
30 31
 from openstack import proxy
31 32
 from openstack import utils
32 33
 
@@ -568,10 +569,20 @@ class ComputeCloudMixin(_normalize.Normalizer):
568 569
             return meta.add_server_interfaces(self, server)
569 570
 
570 571
     def get_server_by_id(self, id):
571
-        data = proxy._json_response(
572
-            self.compute.get('/servers/{id}'.format(id=id)))
573
-        server = self._get_and_munchify('server', data)
574
-        return meta.add_server_interfaces(self, self._normalize_server(server))
572
+        """Get a server by ID.
573
+
574
+        :param id: ID of the server.
575
+
576
+        :returns: A server dict or None if no matching server is found.
577
+        """
578
+        try:
579
+            data = proxy._json_response(
580
+                self.compute.get('/servers/{id}'.format(id=id)))
581
+            server = self._get_and_munchify('server', data)
582
+            return meta.add_server_interfaces(
583
+                self, self._normalize_server(server))
584
+        except exceptions.ResourceNotFound:
585
+            return None
575 586
 
576 587
     def get_server_group(self, name_or_id=None, filters=None):
577 588
         """Get a server group by name or ID.

+ 7
- 0
releasenotes/notes/get-server-by-id-none-3e8538800fa09d82.yaml View File

@@ -0,0 +1,7 @@
1
+---
2
+fixes:
3
+  - |
4
+    The ``get_server_by_id`` method is supposed to return ``None`` if the
5
+    server in question can't be found, but a regression was introduced
6
+    causing it to raise ``ResourceNotFound`` instead. This has been corrected
7
+    and ``get_server_by_id`` returns ``None`` correctly again.

Loading…
Cancel
Save