]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/rook: add a context manager instead of open-coding exception handling
authorJeff Layton <jlayton@redhat.com>
Wed, 19 Dec 2018 14:20:57 +0000 (09:20 -0500)
committerJeff Layton <jlayton@redhat.com>
Mon, 14 Jan 2019 12:05:03 +0000 (07:05 -0500)
Suggested-by: Sebastian Wagner <sebastian.wagner@suse.com>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
src/pybind/mgr/rook/rook_cluster.py

index 32cf0e0946196f455bb095426dee91f949b2407f..cf5d5f9077c571a93e6950ee4918d9c9c6fd667d 100644 (file)
@@ -10,6 +10,7 @@ This module is runnable outside of ceph-mgr, useful for testing.
 from six.moves.urllib.parse import urljoin  # pylint: disable=import-error
 import logging
 import json
+from contextlib import contextmanager
 
 # Optional kubernetes imports to enable MgrModule.can_run
 # to behave cleanly.
@@ -191,6 +192,17 @@ class RookCluster(object):
 
         return pods_summary
 
+    @contextmanager
+    def ignore_409(self, what):
+        try:
+            yield
+        except ApiException as e:
+            if e.status == 409:
+                # Idempotent, succeed.
+                log.info("{} already exists".format(what))
+            else:
+                raise
+
     def add_filesystem(self, spec):
         # TODO use spec.placement
         # TODO use spec.min_size (and use max_size meaningfully)
@@ -214,17 +226,8 @@ class RookCluster(object):
             }
         }
 
-        try:
-            self.rook_api_post(
-                "cephfilesystems/",
-                body=rook_fs
-            )
-        except ApiException as e:
-            if e.status == 409:
-                log.info("CephFilesystem '{0}' already exists".format(spec.name))
-                # Idempotent, succeed.
-            else:
-                raise
+        with self.ignore_409("CephFilesystem '{0}' already exists".format(spec.name)):
+            self.rook_api_post("cephfilesystems/", body=rook_fs)
 
     def add_objectstore(self, spec):
   
@@ -257,17 +260,8 @@ class RookCluster(object):
             }
         }
         
-        try:
-            self.rook_api_post(
-                "cephobjectstores/",
-                body=rook_os
-            )
-        except ApiException as e:
-            if e.status == 409:
-                log.info("CephObjectStore '{0}' already exists".format(spec.name))
-                # Idempotent, succeed.                                                                                                                                                                     
-            else:
-                raise
+        with self.ignore_409("CephObjectStore '{0}' already exists".format(spec.name)):
+            self.rook_api_post("cephobjectstores/", body=rook_os)
 
     def rm_service(self, service_type, service_id):
         assert service_type in ("mds", "rgw")