Blob Blame History Raw
From 0cfbd1bd87d4484eca054d41aea1d8ac9b55e93c Mon Sep 17 00:00:00 2001
From: Tomas Jelinek <tojeline@redhat.com>
Date: Mon, 8 Aug 2016 13:32:07 +0200
Subject: [PATCH] add possibility to hide inactive resources in "pcs resource
 show"

---
 .pylintrc                 |  2 +-
 pcs/pcs.8                 | 12 ++++++------
 pcs/resource.py           | 33 +++++++++++++++++++++++++++------
 pcs/test/test_resource.py | 21 ++++++++++++++-------
 pcs/usage.py              | 16 ++++++++++------
 5 files changed, 58 insertions(+), 26 deletions(-)

diff --git a/.pylintrc b/.pylintrc
index e378e6a..1dd6d5d 100644
--- a/.pylintrc
+++ b/.pylintrc
@@ -92,7 +92,7 @@ dummy-variables-rgx=_$|dummy
 
 [FORMAT]
 # Maximum number of lines in a module
-max-module-lines=4577
+max-module-lines=4584
 # Maximum number of characters on a single line.
 max-line-length=1291
 
diff --git a/pcs/pcs.8 b/pcs/pcs.8
index 52497a0..9064054 100644
--- a/pcs/pcs.8
+++ b/pcs/pcs.8
@@ -64,8 +64,8 @@ alert
 Manage pacemaker alerts.
 .SS "resource"
 .TP
-[show [resource id]] [\fB\-\-full\fR] [\fB\-\-groups\fR]
-Show all currently configured resources or if a resource is specified show the options for the configured resource.  If \fB\-\-full\fR is specified all configured resource options will be displayed.  If \fB\-\-groups\fR is specified, only show groups (and their resources).
+[show [<resource id>] | \fB\-\-full\fR | \fB\-\-groups\fR | \fB\-\-hide\-inactive\fR]
+Show all currently configured resources or if a resource is specified show the options for the configured resource.  If \fB\-\-full\fR is specified, all configured resource options will be displayed.  If \fB\-\-groups\fR is specified, only show groups (and their resources).  If \fB\-\-hide\-inactive\fR is specified, only show active resources.
 .TP
 list [<standard|provider|type>] [\fB\-\-nodesc\fR]
 Show list of all available resources, optionally filtered by specified type, standard or provider. If \fB\-\-nodesc\fR is used then descriptions of resources are not printed.
@@ -627,11 +627,11 @@ stop
 Stop booth arbitrator service.
 .SS "status"
 .TP
-[status] [\fB\-\-full\fR | \fB\-\-hide-inactive\fR]
-View all information about the cluster and resources (\fB\-\-full\fR provides more details, \fB\-\-hide-inactive\fR hides inactive resources).
+[status] [\fB\-\-full\fR | \fB\-\-hide\-inactive\fR]
+View all information about the cluster and resources (\fB\-\-full\fR provides more details, \fB\-\-hide\-inactive\fR hides inactive resources).
 .TP
-resources
-View current status of cluster resources.
+resources [<resource id> | \fB\-\-full\fR | \fB\-\-groups\fR | \fB\-\-hide\-inactive\fR]
+Show all currently configured resources or if a resource is specified show the options for the configured resource.  If \fB\-\-full\fR is specified, all configured resource options will be displayed.  If \fB\-\-groups\fR is specified, only show groups (and their resources).  If \fB\-\-hide\-inactive\fR is specified, only show active resources.
 .TP
 groups
 View currently configured groups and their resources.
diff --git a/pcs/resource.py b/pcs/resource.py
index 66c743c..74adac6 100644
--- a/pcs/resource.py
+++ b/pcs/resource.py
@@ -1993,6 +1993,17 @@ def resource_group_list(argv):
         print(" ".join(line_parts))
 
 def resource_show(argv, stonith=False):
+    mutually_exclusive_opts = ("--full", "--groups", "--hide-inactive")
+    modifiers = [
+        key for key in utils.pcs_options if key in mutually_exclusive_opts
+    ]
+    if (len(modifiers) > 1) or (argv and modifiers):
+        utils.err(
+            "you can specify only one of resource id, {0}".format(
+                ", ".join(mutually_exclusive_opts)
+            )
+        )
+
     if "--groups" in utils.pcs_options:
         resource_group_list(argv)
         return
@@ -2009,15 +2020,28 @@ def resource_show(argv, stonith=False):
         return
 
     if len(argv) == 0:
-        output, retval = utils.run(["crm_mon", "-1", "-r"])
+        monitor_command = ["crm_mon", "--one-shot"]
+        if "--hide-inactive" not in utils.pcs_options:
+            monitor_command.append('--inactive')
+        output, retval = utils.run(monitor_command)
         if retval != 0:
             utils.err("unable to get cluster status from crm_mon\n"+output.rstrip())
         preg = re.compile(r'.*(stonith:.*)')
         resources_header = False
         in_resources = False
         has_resources = False
+        no_resources_line = (
+            "NO stonith devices configured" if stonith
+            else "NO resources configured"
+        )
         for line in output.split('\n'):
-            if line == "Full list of resources:":
+            if line == "No active resources":
+                print(line)
+                return
+            if line == "No resources":
+                print(no_resources_line)
+                return
+            if line in ("Full list of resources:", "Active resources:"):
                 resources_header = True
                 continue
             if line == "":
@@ -2026,10 +2050,7 @@ def resource_show(argv, stonith=False):
                     in_resources = True
                 elif in_resources:
                     if not has_resources:
-                        if not stonith:
-                            print("NO resources configured")
-                        else:
-                            print("NO stonith devices configured")
+                        print(no_resources_line)
                     return
                 continue
             if in_resources:
diff --git a/pcs/test/test_resource.py b/pcs/test/test_resource.py
index 2fa5088..614b895 100644
--- a/pcs/test/test_resource.py
+++ b/pcs/test/test_resource.py
@@ -213,8 +213,7 @@ the health of a system via IPMI.
  ClusterIP7\t(ocf::heartbeat:IPaddr2):\tStopped (disabled)
 """)
 
-        output, returnVal = pcs(temp_cib, "resource show ClusterIP6 --full")
-        assert returnVal == 0
+        output, returnVal = pcs(temp_cib, "resource show --full")
         ac(output, """\
  Resource: ClusterIP (class=ocf provider=heartbeat type=IPaddr2)
   Attributes: ip=192.168.0.99 cidr_netmask=32
@@ -241,6 +240,7 @@ the health of a system via IPMI.
   Meta Attrs: target-role=Stopped 
   Operations: monitor interval=30s (ClusterIP7-monitor-interval-30s)
 """)
+        self.assertEqual(0, returnVal)
 
         output, returnVal = pcs(
             temp_cib,
@@ -785,7 +785,7 @@ monitor interval=60s (state-monitor-interval-60s)
         assert returnVal == 0
         assert output == ""
 
-        line = 'resource show ClusterIP --full'
+        line = 'resource show ClusterIP'
         output, returnVal = pcs(temp_cib, line)
         ac(output, """\
  Resource: ClusterIP (class=ocf provider=heartbeat type=IPaddr2)
@@ -3463,16 +3463,23 @@ Error: Cannot remove more than one resource from cloned group
         ac(o,"")
         assert r == 0
 
-        o,r = pcs(temp_cib, "resource show D1 --full")
-        ac(o," Resource: D1 (class=ocf provider=heartbeat type=Dummy)\n  Meta Attrs: target-role=Stopped \n  Operations: monitor interval=60s (D1-monitor-interval-60s)\n")
+        o,r = pcs(temp_cib, "resource show D1")
+        ac(o, """\
+ Resource: D1 (class=ocf provider=heartbeat type=Dummy)
+  Meta Attrs: target-role=Stopped 
+  Operations: monitor interval=60s (D1-monitor-interval-60s)
+""")
         assert r == 0
 
         o,r = pcs(temp_cib, "resource enable D1")
         ac(o,"")
         assert r == 0
 
-        o,r = pcs(temp_cib, "resource show D1 --full")
-        ac(o," Resource: D1 (class=ocf provider=heartbeat type=Dummy)\n  Operations: monitor interval=60s (D1-monitor-interval-60s)\n")
+        o,r = pcs(temp_cib, "resource show D1")
+        ac(o, """\
+ Resource: D1 (class=ocf provider=heartbeat type=Dummy)
+  Operations: monitor interval=60s (D1-monitor-interval-60s)
+""")
         assert r == 0
 
         # bad resource name
diff --git a/pcs/usage.py b/pcs/usage.py
index baa70d0..b11a5fa 100644
--- a/pcs/usage.py
+++ b/pcs/usage.py
@@ -189,12 +189,12 @@ Usage: pcs resource [commands]...
 Manage pacemaker resources
 
 Commands:
-    [show [resource id]] [--full] [--groups]
+    [show [<resource id>] | --full | --groups | --hide-inactive]
         Show all currently configured resources or if a resource is specified
-        show the options for the configured resource.  If --full is specified
+        show the options for the configured resource.  If --full is specified,
         all configured resource options will be displayed.  If --groups is
-        specified, only show groups (and their resources).
-
+        specified, only show groups (and their resources).  If --hide-inactive
+        is specified, only show active resources.
 
     list [<standard|provider|type>] [--nodesc]
         Show list of all available resources, optionally filtered by specified
@@ -1108,8 +1108,12 @@ Commands:
         View all information about the cluster and resources (--full provides
         more details, --hide-inactive hides inactive resources).
 
-    resources
-        View current status of cluster resources.
+    resources [<resource id> | --full | --groups | --hide-inactive]
+        Show all currently configured resources or if a resource is specified
+        show the options for the configured resource.  If --full is specified,
+        all configured resource options will be displayed.  If --groups is
+        specified, only show groups (and their resources).  If --hide-inactive
+        is specified, only show active resources.
 
     groups
         View currently configured groups and their resources.
-- 
1.8.3.1