Repository: softlayer/softlayer-python
Pull request #1246 (2020-03-24 13:17:45)

fix the issue 887
https://github.com/softlayer/softlayer-python/issues/887
- fixed too many newlins in tickets


Patch:
diff --git a/SoftLayer/CLI/ticket/__init__.py b/SoftLayer/CLI/ticket/__init__.py
index 9886aa68..81c24832 100644
--- a/SoftLayer/CLI/ticket/__init__.py
+++ b/SoftLayer/CLI/ticket/__init__.py
@@ -1,10 +1,10 @@
 """Support tickets."""
 
 import click
+import re
 
 from SoftLayer.CLI import formatting
 
-
 TEMPLATE_MSG = "***** SoftLayer Ticket Content ******"
 
 # https://softlayer.github.io/reference/services/SoftLayer_Ticket_Priority/getPriorities/
@@ -17,7 +17,7 @@
 ]
 
 
-def get_ticket_results(mgr, ticket_id, update_count=1):
+def get_ticket_results(mgr, ticket_id, is_json = False, update_count=1):
     """Get output about a ticket.
 
     :param integer id: the ticket ID
@@ -64,6 +64,7 @@ def get_ticket_results(mgr, ticket_id, update_count=1):
 
         # NOTE(kmcdonald): Windows new-line characters need to be stripped out
         wrapped_entry += click.wrap_text(update['entry'].replace('\r', ''))
+        if is_json and '\n' in wrapped_entry:
+            wrapped_entry = re.sub(r"(?<!\\)\n", " ", wrapped_entry)
         table.add_row(['update %s' % (count_offset + i,), wrapped_entry])
-
     return table
diff --git a/SoftLayer/CLI/ticket/create.py b/SoftLayer/CLI/ticket/create.py
index 75c34b79..5ebad8cf 100644
--- a/SoftLayer/CLI/ticket/create.py
+++ b/SoftLayer/CLI/ticket/create.py
@@ -45,4 +45,4 @@ def cli(env, title, subject_id, body, hardware_identifier, virtual_identifier, p
         vs_id = helpers.resolve_id(vs_mgr.resolve_ids, virtual_identifier, 'VS')
         ticket_mgr.attach_virtual_server(created_ticket['id'], vs_id)
 
-    env.fout(ticket.get_ticket_results(ticket_mgr, created_ticket['id']))
+    env.fout(ticket.get_ticket_results(ticket_mgr, False, created_ticket['id']))
diff --git a/SoftLayer/CLI/ticket/detail.py b/SoftLayer/CLI/ticket/detail.py
index b31c7e0c..a7c59416 100644
--- a/SoftLayer/CLI/ticket/detail.py
+++ b/SoftLayer/CLI/ticket/detail.py
@@ -20,7 +20,10 @@
 def cli(env, identifier, count):
     """Get details for a ticket."""
 
+    is_json = False
+    if (env.format == 'json'):
+        is_json = True
     mgr = SoftLayer.TicketManager(env.client)
 
     ticket_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'ticket')
-    env.fout(ticket.get_ticket_results(mgr, ticket_id, update_count=count))
+    env.fout(ticket.get_ticket_results(mgr, ticket_id, is_json, update_count=count))
diff --git a/docs/cli/hardware.rst b/docs/cli/hardware.rst
index cd97c9d0..0cce2304 100644
--- a/docs/cli/hardware.rst
+++ b/docs/cli/hardware.rst
@@ -37,7 +37,7 @@ Provides some basic functionality to order a server. `slcli order` has a more fu
    :show-nested:
 
 .. click:: SoftLayer.CLI.hardware.billing:cli
-   :prog: hw billing
+   :prog: hardware billing
    :show-nested:
 
 
@@ -111,5 +111,5 @@ This function updates the firmware of a server. If already at the latest version
    :show-nested:
 
 .. click:: SoftLayer.CLI.hardware.storage:cli
-   :prog: hw storage
+   :prog: hardware storage
    :show-nested:
diff --git a/docs/cli/users.rst b/docs/cli/users.rst
index 21ff1b7b..feb94e35 100644
--- a/docs/cli/users.rst
+++ b/docs/cli/users.rst
@@ -32,11 +32,11 @@ Version 5.6.0 introduces the ability to interact with user accounts from the cli
     :prog: user delete
     :show-nested:
 
-.. click:: SoftLayer.CLI.user.vpn-manual:cli
+.. click:: SoftLayer.CLI.user.vpn_manual:cli
     :prog: user vpn-manual
     :show-nested:
 
-.. click:: SoftLayer.CLI.user.vpn-subnet:cli
+.. click:: SoftLayer.CLI.user.vpn_subnet:cli
     :prog: user vpn-subnet
     :show-nested:
 


Test patch:
diff --git a/tests/CLI/modules/ticket_tests.py b/tests/CLI/modules/ticket_tests.py
index 2a47e782..4f8db62a 100644
--- a/tests/CLI/modules/ticket_tests.py
+++ b/tests/CLI/modules/ticket_tests.py
@@ -1,7 +1,6 @@
 """
     SoftLayer.tests.CLI.modules.ticket_tests
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
     :license: MIT, see LICENSE for more details.
 """
 import json
@@ -43,8 +42,8 @@ def test_detail(self):
             'status': 'Closed',
             'title': 'Cloud Instance Cancellation - 08/01/13',
             'update 1': 'a bot says something',
-            'update 2': 'By John Smith\nuser says something',
-            'update 3': 'By emp1 (Employee)\nemployee says something',
+            'update 2': 'By John Smith user says something',
+            'update 3': 'By emp1 (Employee) employee says something',
         }
         self.assert_no_fail(result)
         self.assertEqual(json.loads(result.output), expected)
@@ -300,3 +299,18 @@ def test_ticket_update_no_body(self, edit_mock):
         result = self.run_command(['ticket', 'update', '100'])
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Ticket', 'addUpdate', args=({'entry': 'Testing1'},), identifier=100)
+
+    def test_ticket_json(self):
+        result = self.run_command(['--format=json', 'ticket', 'detail', '1'])
+        expected = {'Case_Number': 'CS123456',
+                    'created': '2013-08-01T14:14:04-07:00',
+                    'edited': '2013-08-01T14:16:47-07:00',
+                    'id': 100,
+                    'priority': 'No Priority',
+                    'status': 'Closed',
+                    'title': 'Cloud Instance Cancellation - 08/01/13',
+                    'update 1': 'a bot says something',
+                    'update 2': 'By John Smith user says something',
+                    'update 3': 'By emp1 (Employee) employee says something'}
+        self.assert_no_fail(result)
+        self.assertEqual(json.loads(result.output), expected)