Skip to content

Commit

Permalink
SDK-285: Add list functionality to Command to view command history (#258
Browse files Browse the repository at this point in the history
)
  • Loading branch information
akaranjkar-qu authored and msumit committed Jan 24, 2019
1 parent fbfdc11 commit 962a0e4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/qds.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ def cancelaction(cmdclass, args):
sys.stderr.write("Cancel failed with reason '%s'\n" % r.get('result'))
return 12

def listaction(cmdclass, args):
args = cmdclass.listparse(args)
if args is not None:
return json.dumps(cmdclass.list(**args), indent=4)


def getresultaction(cmdclass, args):
if len(args) > 2:
Expand Down Expand Up @@ -206,7 +211,7 @@ def getjobsaction(cmdclass, args):
def cmdmain(cmd, args):
cmdclass = CommandClasses[cmd]

actionset = set(["submit", "run", "check", "cancel", "getresult", "getlog", "getjobs"])
actionset = set(["list", "submit", "run", "check", "cancel", "getresult", "getlog", "getjobs"])
if len(args) < 1:
sys.stderr.write("missing argument containing action\n")
usage()
Expand Down
40 changes: 40 additions & 0 deletions qds_sdk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ class Command(Resource):
"""all commands use the /commands endpoint"""
rest_entity_path = "commands"

listusage = "<subcommand> list [options]"
listparser = GentleOptionParser(usage=listusage)
listparser.add_option("-p", "--page", dest="page", type="int",
help="page number")
listparser.add_option("-r", "--per-page", dest="per_page", type="int",
help="number of commands to be retrieved per page")
listparser.add_option("-a", "--all-users", dest="all_users", type="int", default=0,
help="get the command history of all users. default: 0")
listparser.add_option("-i", "--include-query-properties", action="store_true", dest="include_query_properties",
default=False, help="displays query properties such as tags and query history comments. "
"default: False")
listparser.add_option("-s", "--start-date", dest="start_date",
help="the date from which you want the command history")
listparser.add_option("-e", "--end-date", dest="end_date",
help="the date until which you want the command history")

@staticmethod
def is_done(status):
"""
Expand All @@ -56,6 +72,30 @@ def is_done(status):
def is_success(status):
return status == "done"

@classmethod
def list(cls, **kwargs):
"""
List a command by issuing a GET request to the /command endpoint
"""
conn = Qubole.agent()
params = {}
for k in kwargs:
if kwargs[k]:
params[k] = kwargs[k]
params = None if not params else params
return conn.get(cls.rest_entity_path, params=params)

@classmethod
def listparse(cls, args):
try:
(options, args) = cls.listparser.parse_args(args)
except OptionParsingError as e:
raise ParseError(e.msg, cls.listparser.format_help())
except OptionParsingExit as e:
return None

return vars(options)

@classmethod
def create(cls, **kwargs):
"""
Expand Down
59 changes: 59 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,65 @@
from test_base import print_command
from test_base import QdsCliTestCase


class TestCommandList(QdsCliTestCase):

def test_list_minimal(self):
sys.argv = ['qds.py', 'shellcmd', 'list']
print_command()
Connection._api_call = Mock(return_value={})
params = None
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_page(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--page', '2']
print_command()
Connection._api_call = Mock(return_value={})
params = {'page': 2}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_per_page(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--per-page', '5']
print_command()
Connection._api_call = Mock(return_value={})
params = {'per_page': 5}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_all_users(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--all-users', '1']
print_command()
Connection._api_call = Mock(return_value={})
params = {'all_users': 1}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_include_query_properties(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--include-query-properties']
print_command()
Connection._api_call = Mock(return_value={})
params = {'include_query_properties': True}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_start_date(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--start-date', '2019-01-22T15:11:00Z']
print_command()
Connection._api_call = Mock(return_value={})
params = {'start_date': '2019-01-22T15:11:00Z'}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

def test_list_end_date(self):
sys.argv = ['qds.py', 'shellcmd', 'list', '--end-date', '2019-01-22T15:11:00Z']
print_command()
Connection._api_call = Mock(return_value={})
params = {'end_date': '2019-01-22T15:11:00Z'}
qds.main()
Connection._api_call.assert_called_with("GET", "commands", params=params)

class TestCommandCheck(QdsCliTestCase):

@patch("qds.print",create=True)
Expand Down

0 comments on commit 962a0e4

Please sign in to comment.