From: John Mulligan Date: Fri, 4 Mar 2022 20:04:56 +0000 (-0500) Subject: mgr/nfs: move write_block func to file level format_block X-Git-Tag: v17.2.1~48^2~47 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=2f3bec9a42fa7c777214ee63b977408971721ef9;p=ceph.git mgr/nfs: move write_block func to file level format_block The method was an unnecessary use of staticmethod as well as a confusingly named function. Move the file and rename it for clarity. Signed-off-by: John Mulligan (cherry picked from commit 42e0a9ad9f813f64978f60dda25984ec2760d066) --- diff --git a/src/pybind/mgr/nfs/export.py b/src/pybind/mgr/nfs/export.py index cd2c7fa5d66f7..6f129e55c5282 100644 --- a/src/pybind/mgr/nfs/export.py +++ b/src/pybind/mgr/nfs/export.py @@ -24,7 +24,8 @@ from .ganesha_conf import ( Export, GaneshaConfParser, RGWFSAL, - RawBlock) + RawBlock, + format_block) from .exception import NFSException, NFSInvalidOperation, FSNotFound from .utils import ( CONF_PREFIX, @@ -119,7 +120,7 @@ class NFSRados: self.pool, self.namespace, obj) # Add created obj url to common config obj - ioctx.append(config_obj, GaneshaConfParser.write_block( + ioctx.append(config_obj, format_block( self._create_url_block(obj)).encode('utf-8')) _check_rados_notify(ioctx, config_obj) log.debug("Added %s url to %s", obj, config_obj) @@ -310,7 +311,7 @@ class ExportMgr: def _save_export(self, cluster_id: str, export: Export) -> None: self.exports[cluster_id].append(export) self._rados(cluster_id).write_obj( - GaneshaConfParser.write_block(export.to_export_block()), + format_block(export.to_export_block()), export_obj_name(export.export_id), conf_obj_name(export.cluster_id) ) @@ -361,7 +362,7 @@ class ExportMgr: need_nfs_service_restart: bool) -> None: self.exports[cluster_id].append(export) self._rados(cluster_id).update_obj( - GaneshaConfParser.write_block(export.to_export_block()), + format_block(export.to_export_block()), export_obj_name(export.export_id), conf_obj_name(export.cluster_id), should_notify=not need_nfs_service_restart) if need_nfs_service_restart: diff --git a/src/pybind/mgr/nfs/ganesha_conf.py b/src/pybind/mgr/nfs/ganesha_conf.py index 67bee5b44148a..0db9559b53856 100644 --- a/src/pybind/mgr/nfs/ganesha_conf.py +++ b/src/pybind/mgr/nfs/ganesha_conf.py @@ -149,33 +149,6 @@ class GaneshaConfParser: blocks.append(self.parse_block_or_section()) return blocks - @staticmethod - def write_block_body(block: RawBlock, depth: int = 0) -> str: - conf_str = "" - for blo in block.blocks: - conf_str += GaneshaConfParser.write_block(blo, depth) - - for key, val in block.values.items(): - if val is not None: - conf_str += _indentation(depth) - fval = _format_val(block.block_name, key, val) - conf_str += '{} = {};\n'.format(key, fval) - return conf_str - - @staticmethod - def write_block(block: RawBlock, depth: int = 0) -> str: - if block.block_name == "%url": - return '%url "{}"\n\n'.format(block.values['value']) - - conf_str = "" - conf_str += _indentation(depth) - conf_str += format(block.block_name) - conf_str += " {\n" - conf_str += GaneshaConfParser.write_block_body(block, depth + 1) - conf_str += _indentation(depth) - conf_str += "}\n" - return conf_str - class FSAL(object): def __init__(self, name: str) -> None: @@ -518,3 +491,33 @@ class Export: if not isinstance(other, Export): return False return self.to_dict() == other.to_dict() + + +def _format_block_body(block: RawBlock, depth: int = 0) -> str: + conf_str = "" + for blo in block.blocks: + conf_str += format_block(blo, depth) + + for key, val in block.values.items(): + if val is not None: + conf_str += _indentation(depth) + fval = _format_val(block.block_name, key, val) + conf_str += '{} = {};\n'.format(key, fval) + return conf_str + + +def format_block(block: RawBlock, depth: int = 0) -> str: + """Format a raw block object into text suitable as a ganesha configuration + block. + """ + if block.block_name == "%url": + return '%url "{}"\n\n'.format(block.values['value']) + + conf_str = "" + conf_str += _indentation(depth) + conf_str += format(block.block_name) + conf_str += " {\n" + conf_str += _format_block_body(block, depth + 1) + conf_str += _indentation(depth) + conf_str += "}\n" + return conf_str