Skip to content

Commit

Permalink
Refactor the configcache command line interface.
Browse files Browse the repository at this point in the history
The CLI now follows the pattern of specifying a context followed by
a command consistently.  E.g. `configcache device list` instead of
`configcache list device`.

ZEN-35059
  • Loading branch information
jpeacock-zenoss committed Sep 12, 2024
1 parent 1e2da2e commit 5b651af
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 238 deletions.
10 changes: 3 additions & 7 deletions Products/ZenCollector/configcache/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

from __future__ import absolute_import

from .device import Device
from .oidmap import OidMap

from .expire import Expire
from .list import List_
from .remove import Remove
from .show import Show
from .stats import Stats


__all__ = ("Expire", "List_", "Remove", "Show", "Stats")
__all__ = ("Device", "OidMap")
18 changes: 9 additions & 9 deletions Products/ZenCollector/configcache/cli/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,35 @@ def __iter__(self):
return iter(self._choices)


_common_parser = None
_devargs_parser = None


def get_common_parser():
global _common_parser
if _common_parser is None:
_common_parser = argparse.ArgumentParser(add_help=False)
_common_parser.add_argument(
def get_devargs_parser():
global _devargs_parser
if _devargs_parser is None:
_devargs_parser = argparse.ArgumentParser(add_help=False)
_devargs_parser.add_argument(
"-m",
"--collector",
type=str,
default="*",
help="Name of the performance collector. Supports simple '*' "
"wildcard comparisons. A lone '*' selects all collectors.",
)
_common_parser.add_argument(
_devargs_parser.add_argument(
"-s",
"--service",
type=str,
default="*",
help="Name of the configuration service. Supports simple '*' "
"wildcard comparisons. A lone '*' selects all services.",
)
_common_parser.add_argument(
_devargs_parser.add_argument(
"device",
nargs="*",
default=argparse.SUPPRESS,
help="Name of the device. Multiple devices may be specified. "
"Supports simple '*' wildcard comparisons. Not specifying a "
"device will select all devices.",
)
return _common_parser
return _devargs_parser
36 changes: 36 additions & 0 deletions Products/ZenCollector/configcache/cli/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2024, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################

from __future__ import absolute_import, print_function

from ..app.args import get_subparser

from .expire import ExpireDevice
from .list import ListDevice
from .remove import RemoveDevice
from .show import ShowDevice
from .stats import StatsDevice


class Device(object):
description = "Manage the device configuration cache"

@staticmethod
def add_arguments(parser, subparsers):
devicep = get_subparser(
subparsers,
"device",
description=Device.description,
)
device_subparsers = devicep.add_subparsers(title="Device Subcommands")
ExpireDevice.add_arguments(devicep, device_subparsers)
ListDevice.add_arguments(devicep, device_subparsers)
RemoveDevice.add_arguments(devicep, device_subparsers)
ShowDevice.add_arguments(devicep, device_subparsers)
StatsDevice.add_arguments(devicep, device_subparsers)
37 changes: 12 additions & 25 deletions Products/ZenCollector/configcache/cli/expire.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,20 @@
from ..app.args import get_subparser
from ..cache import ConfigStatus, DeviceQuery

from .args import get_common_parser
from .args import get_devargs_parser
from ._selection import get_message, confirm


class Expire(object):
description = "Mark configurations as expired"

@staticmethod
def add_arguments(parser, subparsers):
listp = get_subparser(
subparsers,
"expire",
description=Expire.description,
)
expire_subparsers = listp.add_subparsers(title="Expire Subcommands")
ExpireDevices.add_arguments(listp, expire_subparsers)
ExpireOidMap.add_arguments(listp, expire_subparsers)


class ExpireOidMap(object):
configs = (("expire.zcml", __name__),)
description = "Mark OID Map as expired"
configs = (("store.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
subp = get_subparser(
subparsers,
"oidmap",
description="Expire oidmap configuration",
"expire",
description=ExpireOidMap.description,
)
subp.set_defaults(factory=ExpireOidMap)

Expand All @@ -66,18 +52,19 @@ def run(self):
print("Oidmap configuration already expired")


class ExpireDevices(object):
configs = (("expire.zcml", __name__),)
class ExpireDevice(object):
description = "Mark device configurations as expired"
configs = (("store.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
subp = get_subparser(
subparsers,
"device",
description="Expire device configurations",
parent=get_common_parser(),
"expire",
description=ExpireDevice.description,
parent=get_devargs_parser(),
)
subp.set_defaults(factory=ExpireDevices)
subp.set_defaults(factory=ExpireDevice)

def __init__(self, args):
self._monitor = args.collector
Expand Down
77 changes: 8 additions & 69 deletions Products/ZenCollector/configcache/cli/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,91 +24,30 @@

from ..app import initialize_environment
from ..app.args import get_subparser
from ..cache import DeviceQuery, ConfigStatus
from ..cache import ConfigStatus, DeviceQuery

from .args import get_common_parser, MultiChoice
from .args import get_devargs_parser, MultiChoice


class List_(object):
description = "List configurations"
class ListDevice(object):
configs = (("store.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
listp = get_subparser(
subparsers,
"list",
description=List_.description,
)
list_subparsers = listp.add_subparsers(title="List Subcommands")
ListDevices.add_arguments(listp, list_subparsers)
ListOidMap.add_arguments(listp, list_subparsers)


class ListOidMap(object):
description = "List the oidmap configuration"
configs = (("list.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
devicep = get_subparser(
subparsers,
"oidmap",
description=ListOidMap.description,
)
devicep.set_defaults(factory=ListOidMap)

def __init__(self, args):
pass

def run(self):
initialize_environment(configs=self.configs, useZope=False)
client = getRedisClient(url=getRedisUrl())
store = createObject("oidmapcache-store", client)
status = store.get_status()
if status is None:
print("No oidmap found in the cache.")
return

hdr_tmplt = "{0:{3}} {1:^{4}} {2:^{5}}"
row_tmplt = "{0:{3}} {1:{4}} {2:>{5}}"

headings = ("STATUS", "LAST CHANGE", "AGE")
status_text = _format_status(status)
ts = attr.astuple(status)[-1]
ts_text = _format_date(ts)
now = time.time()
age_text = _format_timedelta(now - ts)
row = (status_text, ts_text, age_text)

maxs, maxt, maxa = 1, 1, 1
maxs = max(maxs, len(status_text))
maxt = max(maxt, len(ts_text))
maxa = max(maxa, len(age_text))
widths = (maxs, maxt, maxa)

print(hdr_tmplt.format(*chain(headings, widths)))
print(row_tmplt.format(*chain(row, widths)))


class ListDevices(object):
configs = (("list.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
devicep = get_subparser(
subparsers,
"device",
description="List device configurations",
parent=get_common_parser(),
parent=get_devargs_parser(),
)
devicep.add_argument(
listp.add_argument(
"-u",
dest="show_uid",
default=False,
action="store_true",
help="Display ZODB path for device",
)
devicep.add_argument(
listp.add_argument(
"-f",
dest="states",
action=MultiChoice,
Expand All @@ -117,7 +56,7 @@ def add_arguments(parser, subparsers):
help="Only list configurations having these states. One or "
"more states may be specified, separated by commas.",
)
devicep.set_defaults(factory=ListDevices)
listp.set_defaults(factory=ListDevice)

def __init__(self, args):
self._monitor = "*{}*".format(args.collector).replace("***", "*")
Expand Down
13 changes: 0 additions & 13 deletions Products/ZenCollector/configcache/cli/list.zcml

This file was deleted.

32 changes: 32 additions & 0 deletions Products/ZenCollector/configcache/cli/oidmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2024, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################

from __future__ import absolute_import, print_function

from ..app.args import get_subparser

from .expire import ExpireOidMap
from .show import ShowOidMap
from .stats import StatsOidMap


class OidMap(object):
description = "Manage the OID Map cache"

@staticmethod
def add_arguments(parser, subparsers):
oidmapp = get_subparser(
subparsers,
"oidmap",
description=OidMap.description,
)
oidmap_subparsers = oidmapp.add_subparsers(title="OidMap Subcommands")
ExpireOidMap.add_arguments(oidmapp, oidmap_subparsers)
ShowOidMap.add_arguments(oidmapp, oidmap_subparsers)
StatsOidMap.add_arguments(oidmapp, oidmap_subparsers)
31 changes: 8 additions & 23 deletions Products/ZenCollector/configcache/cli/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,14 @@
from ..app.args import get_subparser
from ..cache import DeviceQuery

from .args import get_common_parser
from .args import get_devargs_parser
from ._selection import get_message, confirm


class Remove(object):
description = "Mark configurations as expired"

@staticmethod
def add_arguments(parser, subparsers):
removep = get_subparser(
subparsers,
"remove",
description=Remove.description,
)
remove_subparsers = removep.add_subparsers(title="Remove Subcommands")
RemoveDevices.add_arguments(removep, remove_subparsers)
RemoveOidMap.add_arguments(removep, remove_subparsers)


class RemoveOidMap(object):

description = "Remove oidmap configuration from the cache"
configs = (("remove.zcml", __name__),)
configs = (("store.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
Expand All @@ -67,20 +52,20 @@ def run(self):
print("Oidmap configuration removed from the cache")


class RemoveDevices(object):
class RemoveDevice(object):

description = "Delete device configurations from the cache"
configs = (("remove.zcml", __name__),)
configs = (("store.zcml", __name__),)

@staticmethod
def add_arguments(parser, subparsers):
subp = get_subparser(
subparsers,
"device",
description=RemoveDevices.description,
parent=get_common_parser(),
"remove",
description=RemoveDevice.description,
parent=get_devargs_parser(),
)
subp.set_defaults(factory=RemoveDevices)
subp.set_defaults(factory=RemoveDevice)

def __init__(self, args):
self._monitor = args.collector
Expand Down
13 changes: 0 additions & 13 deletions Products/ZenCollector/configcache/cli/remove.zcml

This file was deleted.

Loading

0 comments on commit 5b651af

Please sign in to comment.