From 9a858fec19aca128a8356b72cd8a16fce9ee261f Mon Sep 17 00:00:00 2001
From: Anand Shanmugam <anand1712@gmail.com>
Date: Wed, 28 Oct 2015 13:47:15 -0700
Subject: [PATCH] Code for querying failed tests and list tests in the last x
 hours

Change-Id: I6b279ab2e909526606a9a6bc8cbfa1eea6d4ac30
---
 cloudpulseclient/v1/cloudpulseservices.py | 22 +++++++++++++++++++---
 cloudpulseclient/v1/shell.py              | 14 +++++++++++++-
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/cloudpulseclient/v1/cloudpulseservices.py b/cloudpulseclient/v1/cloudpulseservices.py
index 548801c..5bc0385 100644
--- a/cloudpulseclient/v1/cloudpulseservices.py
+++ b/cloudpulseclient/v1/cloudpulseservices.py
@@ -14,6 +14,8 @@
 # limitations under the License.
 
 from cloudpulseclient.common import base
+import six
+from six.moves.urllib import parse
 
 
 class HealthCheck(base.Resource):
@@ -28,9 +30,23 @@ class HealthCheckManager(base.Manager):
     def _path(id=None):
         return '/v1/cpulse/%s' % id if id else '/v1/cpulse'
 
-    def list(self, marker=None, limit=None, sort_key=None,
-             sort_dir=None, detail=False):
-        return self._list(self._path(''), "cpulses")
+    def list(self, search_opts=None, marker=None, limit=None,
+             sort_key=None, sort_dir=None, detail=False):
+        if search_opts is None:
+            search_opts = {}
+        qparams = {}
+        for opt, val in six.iteritems(search_opts):
+            if val:
+                if isinstance(val, six.text_type):
+                    val = val.encode('utf-8')
+                qparams[opt] = val
+        if qparams:
+            items = list(qparams.items())
+            new_qparams = sorted(items, key=lambda x: x[0])
+            query_string = "?%s" % parse.urlencode(new_qparams)
+        else:
+            query_string = ""
+        return self._list("%s%s" % (self._path(''), query_string), "cpulses")
 
     def create(self, **kwargs):
         new = {}
diff --git a/cloudpulseclient/v1/shell.py b/cloudpulseclient/v1/shell.py
index 15259c9..e707406 100644
--- a/cloudpulseclient/v1/shell.py
+++ b/cloudpulseclient/v1/shell.py
@@ -20,9 +20,21 @@ def _print_list_field(field):
     return lambda obj: ', '.join(getattr(obj, field))
 
 
+@utils.arg('--failed',
+           dest='failed',
+           action="store_true",
+           default=False,
+           help='Display only failed tests.')
+@utils.arg('--period',
+           metavar='<period>',
+           help='List all tests in the last x hours.')
 def do_result(cs, args):
     """List all the tests"""
-    healtchecks = cs.healthcheck.list()
+    search_opts = {
+        'failed': args.failed,
+        'period': args.period,
+    }
+    healtchecks = cs.healthcheck.list(search_opts=search_opts)
     columns = ('uuid', 'id', 'name', 'testtype', 'state')
     utils.print_list(healtchecks, columns,
                      {'versions': _print_list_field('versions')},