]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/nfs: clean up rados object naming code
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 27 Jan 2022 22:06:02 +0000 (17:06 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 17 Mar 2022 14:03:24 +0000 (10:03 -0400)
The naming of rados objects used to store the nfs config was spread
all over the code, including inline f-strings, not-static methods,
etc.
This change unifies the naming by putting constant string prefixes
and name generating functions into the utils.py file.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 88266144423e6876dc392bc6ea59e32393024323)

src/pybind/mgr/nfs/cluster.py
src/pybind/mgr/nfs/export.py
src/pybind/mgr/nfs/utils.py

index bebfd9973ba4c9fe3f3106078390ed1b7bc8763d..1d80544113243a58b6a721c9c7fbec764eda14b1 100644 (file)
@@ -10,7 +10,8 @@ from ceph.deployment.service_spec import NFSServiceSpec, PlacementSpec, IngressS
 import orchestrator
 
 from .exception import NFSInvalidOperation, ClusterNotFound
-from .utils import available_clusters, restart_nfs_service
+from .utils import (available_clusters, restart_nfs_service, conf_obj_name,
+                    user_conf_obj_name)
 from .export import NFSRados, exception_handler
 
 if TYPE_CHECKING:
@@ -48,12 +49,6 @@ class NFSCluster:
     def __init__(self, mgr: 'Module') -> None:
         self.mgr = mgr
 
-    def _get_common_conf_obj_name(self, cluster_id: str) -> str:
-        return f'conf-nfs.{cluster_id}'
-
-    def _get_user_conf_obj_name(self, cluster_id: str) -> str:
-        return f'userconf-nfs.{cluster_id}'
-
     def _call_orch_apply_nfs(
             self,
             cluster_id: str,
@@ -87,17 +82,18 @@ class NFSCluster:
                                   port=port)
             completion = self.mgr.apply_nfs(spec)
             orchestrator.raise_if_exception(completion)
-        log.debug("Successfully deployed nfs daemons with cluster id %s and placement %s", cluster_id, placement)
+        log.debug("Successfully deployed nfs daemons with cluster id %s and placement %s",
+                  cluster_id, placement)
 
     def create_empty_rados_obj(self, cluster_id: str) -> None:
-        common_conf = self._get_common_conf_obj_name(cluster_id)
-        self._rados(cluster_id).write_obj('', self._get_common_conf_obj_name(cluster_id))
+        common_conf = conf_obj_name(cluster_id)
+        self._rados(cluster_id).write_obj('', conf_obj_name(cluster_id))
         log.info("Created empty object:%s", common_conf)
 
     def delete_config_obj(self, cluster_id: str) -> None:
         self._rados(cluster_id).remove_all_obj()
         log.info("Deleted %s object and all objects in %s",
-                 self._get_common_conf_obj_name(cluster_id), cluster_id)
+                 conf_obj_name(cluster_id), cluster_id)
 
     def create_nfs_cluster(
             self,
@@ -216,7 +212,7 @@ class NFSCluster:
         try:
             if cluster_id in available_clusters(self.mgr):
                 rados_obj = self._rados(cluster_id)
-                conf = rados_obj.read_obj(self._get_user_conf_obj_name(cluster_id))
+                conf = rados_obj.read_obj(user_conf_obj_name(cluster_id))
                 return 0, conf or "", ""
             raise ClusterNotFound()
         except Exception as e:
@@ -228,8 +224,8 @@ class NFSCluster:
                 rados_obj = self._rados(cluster_id)
                 if rados_obj.check_user_config():
                     return 0, "", "NFS-Ganesha User Config already exists"
-                rados_obj.write_obj(nfs_config, self._get_user_conf_obj_name(cluster_id),
-                                    self._get_common_conf_obj_name(cluster_id))
+                rados_obj.write_obj(nfs_config, user_conf_obj_name(cluster_id),
+                                    conf_obj_name(cluster_id))
                 log.debug("Successfully saved %s's user config: \n %s", cluster_id, nfs_config)
                 restart_nfs_service(self.mgr, cluster_id)
                 return 0, "NFS-Ganesha Config Set Successfully", ""
@@ -246,8 +242,8 @@ class NFSCluster:
                 rados_obj = self._rados(cluster_id)
                 if not rados_obj.check_user_config():
                     return 0, "", "NFS-Ganesha User Config does not exist"
-                rados_obj.remove_obj(self._get_user_conf_obj_name(cluster_id),
-                                     self._get_common_conf_obj_name(cluster_id))
+                rados_obj.remove_obj(user_conf_obj_name(cluster_id),
+                                     conf_obj_name(cluster_id))
                 restart_nfs_service(self.mgr, cluster_id)
                 return 0, "NFS-Ganesha Config Reset Successfully", ""
             raise ClusterNotFound()
index 8d51225f9ad1b6300159dd8ac129fefb3e109ee9..13cc1a83bf7b45a2bcc4925387b9ef361c56017b 100644 (file)
@@ -11,7 +11,14 @@ from mgr_module import NFS_POOL_NAME as POOL_NAME, NFS_GANESHA_SUPPORTED_FSALS
 from .export_utils import GaneshaConfParser, Export, RawBlock, CephFSFSAL, RGWFSAL
 from .exception import NFSException, NFSInvalidOperation, FSNotFound, \
     ClusterNotFound
-from .utils import available_clusters, check_fs, restart_nfs_service
+from .utils import (
+    EXPORT_PREFIX,
+    USER_CONF_PREFIX,
+    export_obj_name,
+    conf_obj_name,
+    available_clusters,
+    check_fs,
+    restart_nfs_service)
 
 if TYPE_CHECKING:
     from nfs.module import Module
@@ -118,7 +125,7 @@ class NFSRados:
         with self.rados.open_ioctx(self.pool) as ioctx:
             ioctx.set_namespace(self.namespace)
             for obj in ioctx.list_objects():
-                if obj.key.startswith("userconf-nfs"):
+                if obj.key.startswith(USER_CONF_PREFIX):
                     return True
         return False
 
@@ -244,7 +251,7 @@ class ExportMgr:
         with self.mgr.rados.open_ioctx(self.rados_pool) as ioctx:
             ioctx.set_namespace(rados_namespace)
             for obj in ioctx.list_objects():
-                if obj.key.startswith("export-"):
+                if obj.key.startswith(EXPORT_PREFIX):
                     size, _ = obj.stat()
                     raw_config = obj.read(size)
                     raw_config = raw_config.decode("utf-8")
@@ -258,8 +265,8 @@ class ExportMgr:
         self.exports[cluster_id].append(export)
         self._rados(cluster_id).write_obj(
             GaneshaConfParser.write_block(export.to_export_block()),
-            f'export-{export.export_id}',
-            f'conf-nfs.{export.cluster_id}'
+            export_obj_name(export.export_id),
+            conf_obj_name(export.cluster_id)
         )
 
     def _delete_export(
@@ -278,7 +285,7 @@ class ExportMgr:
             if export:
                 if pseudo_path:
                     self._rados(cluster_id).remove_obj(
-                        f'export-{export.export_id}', f'conf-nfs.{cluster_id}')
+                        export_obj_name(export.export_id), conf_obj_name(cluster_id))
                 self.exports[cluster_id].remove(export)
                 self._delete_export_user(export)
                 if not self.exports[cluster_id]:
@@ -295,7 +302,7 @@ class ExportMgr:
                 ioctx.set_namespace(cluster_id)
                 export = Export.from_export_block(
                     GaneshaConfParser(
-                        ioctx.read(f"export-{ex_id}").decode("utf-8")
+                        ioctx.read(export_obj_name(ex_id)).decode("utf-8")
                     ).parse()[0],
                     cluster_id
                 )
@@ -308,7 +315,7 @@ class ExportMgr:
         self.exports[cluster_id].append(export)
         self._rados(cluster_id).update_obj(
             GaneshaConfParser.write_block(export.to_export_block()),
-            f'export-{export.export_id}', f'conf-nfs.{export.cluster_id}')
+            export_obj_name(export.export_id), conf_obj_name(export.cluster_id))
 
     def format_path(self, path: str) -> str:
         if path:
index 00552dfc0de12c18f4e987047d187027b602d0af..ac857d6d920e7af5805ded89028f8ce4bd367643 100644 (file)
@@ -5,6 +5,25 @@ import orchestrator
 if TYPE_CHECKING:
     from nfs.module import Module
 
+EXPORT_PREFIX: str = "export-"
+CONF_PREFIX: str = "conf-nfs."
+USER_CONF_PREFIX: str = "userconf-nfs."
+
+
+def export_obj_name(export_id: int) -> str:
+    """Return a rados object name for the export."""
+    return f"{EXPORT_PREFIX}{export_id}"
+
+
+def conf_obj_name(cluster_id: str) -> str:
+    """Return a rados object name for the config."""
+    return f"{CONF_PREFIX}{cluster_id}"
+
+
+def user_conf_obj_name(cluster_id: str) -> str:
+    """Returna a rados object name for the user config."""
+    return f"{USER_CONF_PREFIX}{cluster_id}"
+
 
 def available_clusters(mgr: 'Module') -> List[str]:
     '''