Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support for STDIN on creating and updating tickets. #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions SoftLayer/CLI/ticket/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
29 changes: 24 additions & 5 deletions SoftLayer/CLI/ticket/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
6 changes: 6 additions & 0 deletions docs/cli/tickets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://cloud.ibm.com/apidocs/case-management#introduction>`_

.. 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:
Expand Down
55 changes: 49 additions & 6 deletions tests/CLI/modules/ticket_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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',
Expand Down