From bf830be44ca5a5c137626ff0e6d3f08202799c0f Mon Sep 17 00:00:00 2001 From: mulhern Date: Thu, 3 Aug 2023 16:15:18 -0400 Subject: [PATCH] Add stub function to stratis for pool report subcommand Signed-off-by: mulhern --- src/stratis_cli/_actions/_pool.py | 16 +++ src/stratis_cli/_actions/_report_pool.py | 158 +++++++++++++++++++++++ src/stratis_cli/_parser/_pool.py | 39 ++++++ 3 files changed, 213 insertions(+) create mode 100644 src/stratis_cli/_actions/_report_pool.py diff --git a/src/stratis_cli/_actions/_pool.py b/src/stratis_cli/_actions/_pool.py index 1c108b032..3e4a323df 100644 --- a/src/stratis_cli/_actions/_pool.py +++ b/src/stratis_cli/_actions/_pool.py @@ -46,6 +46,7 @@ from ._constants import TOP_OBJECT from ._formatting import get_property, get_uuid_formatter from ._list_pool import list_pools +from ._report_pool import report_pool from ._utils import get_clevis_info @@ -710,3 +711,18 @@ def explain_code(namespace): Print an explanation of pool error code. """ print(PoolErrorCode.explain(namespace.code)) + + @staticmethod + def report_pool(namespace): + """ + Report information about a pool. + """ + uuid_formatter = get_uuid_formatter(namespace.unhyphenated_uuids) + + selection = ( + (PoolIdType.NAME, namespace.name) + if namespace.uuid is None + else (PoolIdType.UUID, namespace.uuid) + ) + + return report_pool(uuid_formatter, selection, stopped=namespace.stopped) diff --git a/src/stratis_cli/_actions/_report_pool.py b/src/stratis_cli/_actions/_report_pool.py new file mode 100644 index 000000000..0b9fd01b4 --- /dev/null +++ b/src/stratis_cli/_actions/_report_pool.py @@ -0,0 +1,158 @@ +# Copyright 2023 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Pool report. +""" + +# isort: STDLIB +from abc import ABC, abstractmethod + +from .._errors import StratisCliResourceNotFoundError +from .._stratisd_constants import PoolIdType +from ._connection import get_object +from ._constants import TOP_OBJECT + + +def _fetch_stopped_pools_property(proxy): + """ + Fetch the StoppedPools property from stratisd. + :param proxy: proxy to the top object in stratisd + :return: a representation of stopped devices + :rtype: dict + :raises StratisCliEngineError: + """ + + # pylint: disable=import-outside-toplevel + from ._data import Manager + + return Manager.Properties.StoppedPools.Get(proxy) + + +def report_pool(uuid_formatter, selection, *, stopped=False): + """ + List the specified information about pools. + """ + klass = ( + Stopped(uuid_formatter, selection) + if stopped + else Default(uuid_formatter, selection) + ) + klass.report_pool() + + +class Report(ABC): # pylint: disable=too-few-public-methods + """ + Handle reporting on a pool or pools. + """ + + def __init__(self, uuid_formatter, selection): + """ + Initialize a List object. + :param uuid_formatter: function to format a UUID str or UUID + :param uuid_formatter: str or UUID -> str + :param selection: how to select the pool to display + :type selection: pair of str * object + """ + self.uuid_formatter = uuid_formatter + self.selection = selection + + @abstractmethod + def report_pool(self): + """ + List the pools. + """ + + +class Default(Report): # pylint: disable=too-few-public-methods + """ + Report on pools that stratis list by default. + """ + + def report_pool(self): + """ + Do pool reports. + """ + # pylint: disable=import-outside-toplevel + from ._data import MODev, ObjectManager, devs, pools + + proxy = get_object(TOP_OBJECT) + + managed_objects = ObjectManager.Methods.GetManagedObjects(proxy, {}) + + (selection_type, selection_value) = self.selection + (selection_key, selection_value) = ( + ("Uuid", selection_value.hex) + if selection_type == PoolIdType.UUID + else ("Name", selection_value) + ) + + (pool_object_path, _) = next( + pools(props={selection_key: selection_value}) + .require_unique_match(True) + .search(managed_objects) + ) + + modevs = ( + MODev(info) + for objpath, info in devs(props={"Pool": pool_object_path}).search( + managed_objects + ) + ) + + # replace the lines below with the correct call to lsblk + for modev in modevs: + print(modev.Devnode()) + + +class Stopped(Report): # pylint: disable=too-few-public-methods + """ + Support for reporting on stopped pools. + """ + + def report_pool(self): + """ + Report on stopped pool. + """ + proxy = get_object(TOP_OBJECT) + + stopped_pools = _fetch_stopped_pools_property(proxy) + + (selection_type, selection_value) = self.selection + + if selection_type == PoolIdType.UUID: + selection_value = selection_value.hex + + def selection_func(uuid, _info): + return uuid == selection_value + + else: + + def selection_func(_uuid, info): + return info.get("name") == selection_value + + stopped_pool = next( + ( + info + for (uuid, info) in stopped_pools.items() + if selection_func(uuid, info) + ), + None, + ) + + if stopped_pool is None: + raise StratisCliResourceNotFoundError("list", selection_value) + + # Substitute lsblk call here + for dev in stopped_pool["devs"]: + print(dev) diff --git a/src/stratis_cli/_parser/_pool.py b/src/stratis_cli/_parser/_pool.py index e1ec72710..e07bb9f6e 100644 --- a/src/stratis_cli/_parser/_pool.py +++ b/src/stratis_cli/_parser/_pool.py @@ -442,4 +442,43 @@ def _ensure_nat(arg): "subcmds": POOL_DEBUG_SUBCMDS, }, ), + ( + "report", + { + "help": "Report information about pools", + "description": "Generate report for pools", + "mut_ex_args": [ + ( + True, + [ + ( + "--uuid", + { + "action": "store", + "type": UUID, + "help": "UUID of pool", + }, + ), + ( + "--name", + { + "action": "store", + "help": "name of pool", + }, + ), + ], + ), + ], + "args": [ + ( + "--stopped", + { + "action": "store_true", + "help": "Display information about stopped pools only.", + }, + ), + ], + "func": PoolActions.report_pool, + }, + ), ]