diff --git a/src/stratis_cli/_actions/_debug.py b/src/stratis_cli/_actions/_debug.py index 372263338..9040e22f3 100644 --- a/src/stratis_cli/_actions/_debug.py +++ b/src/stratis_cli/_actions/_debug.py @@ -153,6 +153,46 @@ def get_object_path(namespace): ) print(fs_object_path) + @staticmethod + def get_metadata(namespace): + """ + Get filesystem medatada. If a specific filesystem is not specified, + get metadata for all filesystems in the pool. + + :raises StratisCliEngineError: + """ + + # pylint: disable=import-outside-toplevel + from ._data import ObjectManager, Pool, pools + + proxy = get_object(TOP_OBJECT) + managed_objects = ObjectManager.Methods.GetManagedObjects(proxy, {}) + + props = ( + {"Uuid": namespace.uuid.hex} + if namespace.name is None + else {"Name": namespace.name} + ) + (pool_object_path, _) = next( + pools(props=props).require_unique_match(True).search(managed_objects) + ) + + (metadata, return_code, message) = Pool.Methods.FilesystemMetadata( + get_object(pool_object_path), {} + ) + + if return_code != StratisdErrors.OK: # pragma: no cover + raise StratisCliEngineError(return_code, message) + + json_format_data = [json.loads(chunk) for chunk in metadata.split("\n")] + + if namespace.pretty: + for js in json_format_data: + print(json.dumps(js, sort_keys=True, indent=4)) + else: + for js in json_format_data: + print(json.dumps(js)) + class BlockdevDebugActions: # pylint: disable=too-few-public-methods """ diff --git a/src/stratis_cli/_parser/_debug.py b/src/stratis_cli/_parser/_debug.py index 5f6d69078..d151d357c 100644 --- a/src/stratis_cli/_parser/_debug.py +++ b/src/stratis_cli/_parser/_debug.py @@ -144,6 +144,45 @@ "func": FilesystemDebugActions.get_object_path, }, ), + ( + "get-metadata", + { + "help": ( + "Get the filesystem metadata for all filesystems belonging to " + "the specified pool" + ), + "args": [ + ( + "--pretty", + { + "action": "store_true", + "help": "Format output string prettily", + }, + ), + ], + "mut_ex_args": [ + ( + False, + [ + ( + "--name", + { + "help": "Name of pool", + }, + ), + ( + "--uuid", + { + "help": "UUID of pool", + "type": UUID, + }, + ), + ], + ) + ], + "func": FilesystemDebugActions.get_metadata, + }, + ), ] BLOCKDEV_DEBUG_SUBCMDS = [ diff --git a/tests/whitebox/integration/logical/test_debug.py b/tests/whitebox/integration/logical/test_debug.py index 0d93324d6..17cb39582 100644 --- a/tests/whitebox/integration/logical/test_debug.py +++ b/tests/whitebox/integration/logical/test_debug.py @@ -73,3 +73,32 @@ def test_lookup_name(self): self._FSNAME, ] TEST_RUNNER(command_line) + + def test_metadata_name(self): + """ + Test getting filesystem metadata. + """ + command_line = ["filesystem", "create", self._POOLNAME, "newname"] + RUNNER(command_line) + + command_line = self._MENU + [ + "get-metadata", + "--name", + self._POOLNAME, + ] + TEST_RUNNER(command_line) + + def test_metadata_name_pretty(self): + """ + Test getting filesystem metadata and pretty printing it. + """ + command_line = ["filesystem", "create", self._POOLNAME, "newname"] + RUNNER(command_line) + + command_line = self._MENU + [ + "get-metadata", + "--pretty", + "--name", + self._POOLNAME, + ] + TEST_RUNNER(command_line)