Skip to content

Commit

Permalink
Add stub function to stratis for pool report subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Aug 3, 2023
1 parent 2e6645a commit 7c4f86c
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/stratis_cli/_actions/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
157 changes: 157 additions & 0 deletions src/stratis_cli/_actions/_report_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# 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=selection)
if stopped
else Default(uuid_formatter, selection=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
print(stopped_pool)
39 changes: 39 additions & 0 deletions src/stratis_cli/_parser/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
),
]

0 comments on commit 7c4f86c

Please sign in to comment.