Repository: softlayer/softlayer-python
Pull request #2072 (2023-08-08 12:24:11)

slcli ticket list command with user limit option #2049
Hi Chris,

Please review this PR.
Fixes: #2049

Changes:
 - slcli ticket list command with user limit option.

<img width="1203" alt="Screenshot 2023-08-08 at 5 35 49 PM" src="https://github.com/softlayer/softlayer-python/assets/134699156/2be26dbd-11b9-48b5-9c05-461c6ace7956">


Thanks,
Jayasilan


Patch:
diff --git a/SoftLayer/CLI/ticket/list.py b/SoftLayer/CLI/ticket/list.py
index ea0febfe..186c15f2 100644
--- a/SoftLayer/CLI/ticket/list.py
+++ b/SoftLayer/CLI/ticket/list.py
@@ -12,15 +12,19 @@
 @click.command(cls=SoftLayer.CLI.command.SLCommand, )
 @click.option('--open / --closed', 'is_open', default=True,
               help="Display only open or closed tickets")
+@click.option('--limit', default=100, show_default=True, type=click.INT, help="Result limit")
+@click.option("--all", "-a", "all_tickets", is_flag=True, default=False, help="Return all tickets")
 @environment.pass_env
-def cli(env, is_open):
+def cli(env, is_open, limit, all_tickets):
     """List tickets."""
     ticket_mgr = SoftLayer.TicketManager(env.client)
     table = formatting.Table([
         'id', 'Case_Number', 'assigned_user', 'title', 'last_edited', 'status', 'updates', 'priority'
     ])
 
-    tickets = ticket_mgr.list_tickets(open_status=is_open, closed_status=not is_open)
+    tickets = ticket_mgr.list_tickets(open_status=is_open,
+                                      closed_status=not is_open, limit=limit, all_tickets=all_tickets)
+
     for ticket in tickets:
         user = formatting.blank()
         if ticket.get('assignedUser'):
diff --git a/SoftLayer/managers/ticket.py b/SoftLayer/managers/ticket.py
index 006ebb8a..558a8a9a 100644
--- a/SoftLayer/managers/ticket.py
+++ b/SoftLayer/managers/ticket.py
@@ -22,11 +22,12 @@ def __init__(self, client):
         self.account = self.client['Account']
         self.ticket = self.client['Ticket']
 
-    def list_tickets(self, open_status=True, closed_status=True):
+    def list_tickets(self, open_status=True, closed_status=True, limit=None, all_tickets=False):
         """List all tickets.
 
         :param boolean open_status: include open tickets
         :param boolean closed_status: include closed tickets
+        :param int limit: result limit
         """
         mask = """mask[id, serviceProviderResourceId, title, assignedUser[firstName, lastName], priority,
                   createDate, lastEditDate, accountId, status, updateCount]"""
@@ -39,7 +40,8 @@ def list_tickets(self, open_status=True, closed_status=True):
                 call = 'getClosedTickets'
             else:
                 raise ValueError("open_status and closed_status cannot both be False")
-        return self.client.call('Account', call, mask=mask, iter=False, limit=100)
+
+        return self.client.call('Account', call, mask=mask, iter=all_tickets, limit=limit)
 
     def list_subjects(self):
         """List all ticket subjects."""


Test patch:
diff --git a/tests/CLI/modules/ticket_tests.py b/tests/CLI/modules/ticket_tests.py
index fb5ff8ec..91f64dcb 100644
--- a/tests/CLI/modules/ticket_tests.py
+++ b/tests/CLI/modules/ticket_tests.py
@@ -358,3 +358,33 @@ def test_ticket_json(self):
                     'update 3': 'By emp1 (Employee) employee says something'}
         self.assert_no_fail(result)
         self.assertEqual(json.loads(result.output), expected)
+
+    def test_list_limit(self):
+        result = self.run_command(['ticket', 'list', '--limit', '1'])
+
+        expected = [{
+            'assigned_user': 'John Smith',
+            'Case_Number': 'CS123456',
+            'id': 102,
+            'last_edited': '2013-08-01',
+            'priority': 0,
+            'status': 'Open',
+            'title': 'Cloud Instance Cancellation - 08/01/13',
+            'updates': 0}]
+        self.assert_no_fail(result)
+        self.assertEqual(expected, json.loads(result.output))
+
+    def test_list_all_open(self):
+        result = self.run_command(['ticket', 'list', '--all'])
+
+        expected = [{
+            'assigned_user': 'John Smith',
+            'Case_Number': 'CS123456',
+            'id': 102,
+            'last_edited': '2013-08-01',
+            'priority': 0,
+            'status': 'Open',
+            'title': 'Cloud Instance Cancellation - 08/01/13',
+            'updates': 0}]
+        self.assert_no_fail(result)
+        self.assertEqual(expected, json.loads(result.output))