diff --git a/SoftLayer/CLI/ticket/create.py b/SoftLayer/CLI/ticket/create.py index 5ebad8cf4..17667d74f 100644 --- a/SoftLayer/CLI/ticket/create.py +++ b/SoftLayer/CLI/ticket/create.py @@ -12,8 +12,7 @@ @click.command() @click.option('--title', required=True, help="The title of the ticket") @click.option('--subject-id', type=int, required=True, - help="""The subject id to use for the ticket, - issue 'slcli ticket subjects' to get the list""") + help="""The subject id to use for the ticket, run 'slcli ticket subjects' to get the list""") @click.option('--body', help="The ticket body") @click.option('--hardware', 'hardware_identifier', help="The identifier for hardware to attach") @@ -24,11 +23,31 @@ Only settable with Advanced and Premium support. See https://www.ibm.com/cloud/support""") @environment.pass_env def cli(env, title, subject_id, body, hardware_identifier, virtual_identifier, priority): - """Create a support ticket.""" - ticket_mgr = SoftLayer.TicketManager(env.client) + """Create a Infrastructure support ticket. + + Example:: + + Will create the ticket with `Some text`. + + slcli ticket create --body="Some text" --subject-id 1522 --hardware 12345 --title "My New Ticket" + Will create the ticket with text from STDIN + + cat sometfile.txt | slcli ticket create --subject-id 1003 --virtual 111111 --title "Reboot Me" + + Will open the default text editor, and once closed, use that text to create the ticket + + slcli ticket create --subject-id 1482 --title "Vyatta Questions..." + """ + ticket_mgr = SoftLayer.TicketManager(env.client) if body is None: - body = click.edit('\n\n' + ticket.TEMPLATE_MSG) + stdin = click.get_text_stream('stdin') + # Means there is text on the STDIN buffer, read it and add to the ticket + if not stdin.isatty(): + body = stdin.read() + # This is an interactive terminal, open a text editor + else: + body = click.edit('\n\n' + ticket.TEMPLATE_MSG) created_ticket = ticket_mgr.create_ticket( title=title, body=body, diff --git a/SoftLayer/CLI/ticket/update.py b/SoftLayer/CLI/ticket/update.py index 9c10971ad..f04d36f94 100644 --- a/SoftLayer/CLI/ticket/update.py +++ b/SoftLayer/CLI/ticket/update.py @@ -11,16 +11,35 @@ @click.command() @click.argument('identifier') -@click.option('--body', help="The entry that will be appended to the ticket") +@click.option('--body', help="Text to add to the ticket. STDIN or the default text editor will be used otherwise.") @environment.pass_env def cli(env, identifier, body): - """Adds an update to an existing ticket.""" + """Adds an update to an existing ticket. + + Example:: + + Will update the ticket with `Some text`. + + slcli ticket update 123456 --body="Some text" + + Will update the ticket with text from STDIN + + cat sometfile.txt | slcli ticket update 123456 + + Will open the default text editor, and once closed, use that text to update the ticket + + slcli ticket update 123456 + """ mgr = SoftLayer.TicketManager(env.client) ticket_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'ticket') - if body is None: - body = click.edit('\n\n' + ticket.TEMPLATE_MSG) - + stdin = click.get_text_stream('stdin') + # Means there is text on the STDIN buffer, read it and add to the ticket + if not stdin.isatty(): + body = stdin.read() + # This is an interactive terminal, open a text editor + else: + body = click.edit('\n\n' + ticket.TEMPLATE_MSG) mgr.update_ticket(ticket_id=ticket_id, body=body) env.fout("Ticket Updated!") diff --git a/docs/cli/tickets.rst b/docs/cli/tickets.rst index ad5f23428..71ef3192c 100644 --- a/docs/cli/tickets.rst +++ b/docs/cli/tickets.rst @@ -3,6 +3,12 @@ Support Tickets =============== +The SoftLayer ticket API is used to create "classic" or Infrastructure Support cases. +These tickets will still show up in your web portal, but for the more unified case management API, +see the `Case Management API `_ + +.. note:: Windows Git-Bash users might run into issues with `ticket create` and `ticket update` if --body isn't used, as it doesn't report that it is a real TTY to python, so the default editor can not be launched. + .. click:: SoftLayer.CLI.ticket.create:cli :prog: ticket create :show-nested: diff --git a/tests/CLI/modules/ticket_tests.py b/tests/CLI/modules/ticket_tests.py index 4f8db62a8..7b2363eab 100644 --- a/tests/CLI/modules/ticket_tests.py +++ b/tests/CLI/modules/ticket_tests.py @@ -13,6 +13,22 @@ from SoftLayer import testing +class FakeTTY(): + """A fake object to fake STD input""" + def __init__(self, isatty=False, read="Default Output"): + """Sets isatty and read""" + self._isatty = isatty + self._read = read + + def isatty(self): + """returns self.isatty""" + return self._isatty + + def read(self): + """returns self.read""" + return self._read + + class TicketTests(testing.TestCase): def test_list(self): @@ -99,18 +115,33 @@ def test_create_and_attach(self): identifier=100) @mock.patch('click.edit') - def test_create_no_body(self, edit_mock): + @mock.patch('click.get_text_stream') + def test_create_no_body(self, isatty_mock, edit_mock): + fake_tty = FakeTTY(True, "TEST") + isatty_mock.return_value = fake_tty edit_mock.return_value = 'ticket body' - result = self.run_command(['ticket', 'create', '--title=Test', - '--subject-id=1000']) + result = self.run_command(['ticket', 'create', '--title=Test', '--subject-id=1000']) self.assert_no_fail(result) args = ({'subjectId': 1000, 'assignedUserId': 12345, 'title': 'Test'}, 'ticket body') - self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', - args=args) + self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args) + + @mock.patch('click.get_text_stream') + def test_create_no_body_stdin(self, isatty_mock): + fake_tty = FakeTTY(False, "TEST TICKET BODY") + isatty_mock.return_value = fake_tty + result = self.run_command(['ticket', 'create', '--title=Test', '--subject-id=1000']) + print(result.output) + self.assert_no_fail(result) + + args = ({'subjectId': 1000, + 'assignedUserId': 12345, + 'title': 'Test'}, 'TEST TICKET BODY') + + self.assert_called_with('SoftLayer_Ticket', 'createStandardTicket', args=args) def test_subjects(self): list_expected_ids = [1001, 1002, 1003, 1004, 1005] @@ -294,12 +325,24 @@ def test_ticket_update(self): self.assert_called_with('SoftLayer_Ticket', 'addUpdate', args=({'entry': 'Testing'},), identifier=100) @mock.patch('click.edit') - def test_ticket_update_no_body(self, edit_mock): + @mock.patch('click.get_text_stream') + def test_ticket_update_no_body(self, isatty_mock, edit_mock): + fake_tty = FakeTTY(True, "TEST TICKET BODY") + isatty_mock.return_value = fake_tty edit_mock.return_value = 'Testing1' result = self.run_command(['ticket', 'update', '100']) self.assert_no_fail(result) self.assert_called_with('SoftLayer_Ticket', 'addUpdate', args=({'entry': 'Testing1'},), identifier=100) + @mock.patch('click.get_text_stream') + def test_ticket_update_no_body_stdin(self, isatty_mock): + fake_tty = FakeTTY(False, "TEST TICKET BODY") + isatty_mock.return_value = fake_tty + result = self.run_command(['ticket', 'update', '100']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Ticket', 'addUpdate', + args=({'entry': 'TEST TICKET BODY'},), identifier=100) + def test_ticket_json(self): result = self.run_command(['--format=json', 'ticket', 'detail', '1']) expected = {'Case_Number': 'CS123456',