Skip to content
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
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
('ticket:detail', 'SoftLayer.CLI.ticket.detail:cli'),
('ticket:list', 'SoftLayer.CLI.ticket.list:cli'),
('ticket:update', 'SoftLayer.CLI.ticket.update:cli'),
('ticket:upload', 'SoftLayer.CLI.ticket.upload:cli'),
('ticket:subjects', 'SoftLayer.CLI.ticket.subjects:cli'),
('ticket:summary', 'SoftLayer.CLI.ticket.summary:cli'),
('ticket:attach', 'SoftLayer.CLI.ticket.attach:cli'),
Expand Down
37 changes: 37 additions & 0 deletions SoftLayer/CLI/ticket/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Adds an attachment to an existing ticket."""
# :license: MIT, see LICENSE for more details.

import os

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import helpers


@click.command()
@click.argument('identifier')
@click.option('--path', help="The path of the attachment to be uploaded")
@click.option('--name', help="The name of the attachment shown in the ticket")
@environment.pass_env
def cli(env, identifier, path, name):
"""Adds an attachment to an existing ticket."""
mgr = SoftLayer.TicketManager(env.client)

ticket_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'ticket')

if path is None:
raise exceptions.ArgumentError("Missing argument --path")

if not os.path.exists(path):
raise exceptions.ArgumentError("%s not exist" % path)

if name is None:
name = os.path.basename(path)

attached_file = mgr.upload_attachment(ticket_id=ticket_id,
file_path=path,
file_name=name)
env.fout("File attached: \n%s" % attached_file)
12 changes: 12 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,17 @@
"ticketId": 100
}

addAttachedFile = {
"id": 123,
"fileName": "a_file_name",
"fileSize": "100",
"ticketId": 100,
"updateId": 200,
"createDate": "2013-08-01T14:14:04-07:00",
"modifyDate": "2013-08-01T14:14:04-07:00",
"uploaderType": "USER",
"uploaderId": "300"
}

removeAttachedHardware = True
removeAttachedVirtualGuest = True
24 changes: 24 additions & 0 deletions SoftLayer/managers/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ def update_ticket(self, ticket_id=None, body=None):
"""
return self.ticket.addUpdate({'entry': body}, id=ticket_id)

def upload_attachment(self, ticket_id=None, file_path=None,
file_name=None):
"""Upload an attachment to a ticket.

:param integer ticket_id: the id of the ticket to
upload the attachment to
:param string file_path:
The path of the attachment to be uploaded
:param string file_name:
The name of the attachment shown
in the ticket
:returns The uploaded attachment
"""
file_content = None
with open(file_path, 'rb') as attached_file:
file_content = attached_file.read()

file_object = {
"filename": file_name,
"data": file_content
}

return self.ticket.addAttachedFile(file_object, id=ticket_id)

def attach_hardware(self, ticket_id=None, hardware_id=None):
"""Attach hardware to a ticket.

Expand Down
37 changes: 37 additions & 0 deletions tests/CLI/modules/ticket_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,40 @@ def test_ticket_detach_virtual_server(self):
'removeAttachedVirtualGuest',
args=(100,),
identifier=1)

def test_ticket_upload_no_path(self):
result = self.run_command(['ticket', 'upload', '1'])

self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.ArgumentError)

def test_ticket_upload_invalid_path(self):
result = self.run_command(['ticket', 'upload', '1',
'--path=tests/resources/nonexistent_file',
'--name=a_file_name'])

self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.ArgumentError)

def test_ticket_upload_no_name(self):
result = self.run_command(['ticket', 'upload', '1',
'--path=tests/resources/attachment_upload'])

self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Ticket',
'addAttachedFile',
args=({"filename": "attachment_upload",
"data": b"ticket attached data"},),
identifier=1)

def test_ticket_upload(self):
result = self.run_command(['ticket', 'upload', '1',
'--path=tests/resources/attachment_upload',
'--name=a_file_name'])

self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Ticket',
'addAttachedFile',
args=({"filename": "a_file_name",
"data": b"ticket attached data"},),
identifier=1)
1 change: 1 addition & 0 deletions tests/resources/attachment_upload
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ticket attached data