Skip to content

Commit 4932cac

Browse files
joshuakwansudorandom
authored andcommitted
Enhance the client to support uploading an attachment to a ticket (softlayer#735)
* add support to ticket.addAttachedFile as slcli ticket upload * fix the doc * fix the issues in softlayer#735 * move the build of file objects into ticket.py * add tests to the attachment upload function
1 parent 6e1548c commit 4932cac

6 files changed

Lines changed: 112 additions & 0 deletions

File tree

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
('ticket:detail', 'SoftLayer.CLI.ticket.detail:cli'),
200200
('ticket:list', 'SoftLayer.CLI.ticket.list:cli'),
201201
('ticket:update', 'SoftLayer.CLI.ticket.update:cli'),
202+
('ticket:upload', 'SoftLayer.CLI.ticket.upload:cli'),
202203
('ticket:subjects', 'SoftLayer.CLI.ticket.subjects:cli'),
203204
('ticket:summary', 'SoftLayer.CLI.ticket.summary:cli'),
204205
('ticket:attach', 'SoftLayer.CLI.ticket.attach:cli'),

SoftLayer/CLI/ticket/upload.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Adds an attachment to an existing ticket."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import os
5+
6+
import click
7+
8+
import SoftLayer
9+
from SoftLayer.CLI import environment
10+
from SoftLayer.CLI import exceptions
11+
from SoftLayer.CLI import helpers
12+
13+
14+
@click.command()
15+
@click.argument('identifier')
16+
@click.option('--path', help="The path of the attachment to be uploaded")
17+
@click.option('--name', help="The name of the attachment shown in the ticket")
18+
@environment.pass_env
19+
def cli(env, identifier, path, name):
20+
"""Adds an attachment to an existing ticket."""
21+
mgr = SoftLayer.TicketManager(env.client)
22+
23+
ticket_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'ticket')
24+
25+
if path is None:
26+
raise exceptions.ArgumentError("Missing argument --path")
27+
28+
if not os.path.exists(path):
29+
raise exceptions.ArgumentError("%s not exist" % path)
30+
31+
if name is None:
32+
name = os.path.basename(path)
33+
34+
attached_file = mgr.upload_attachment(ticket_id=ticket_id,
35+
file_path=path,
36+
file_name=name)
37+
env.fout("File attached: \n%s" % attached_file)

SoftLayer/fixtures/SoftLayer_Ticket.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,17 @@
4848
"ticketId": 100
4949
}
5050

51+
addAttachedFile = {
52+
"id": 123,
53+
"fileName": "a_file_name",
54+
"fileSize": "100",
55+
"ticketId": 100,
56+
"updateId": 200,
57+
"createDate": "2013-08-01T14:14:04-07:00",
58+
"modifyDate": "2013-08-01T14:14:04-07:00",
59+
"uploaderType": "USER",
60+
"uploaderId": "300"
61+
}
62+
5163
removeAttachedHardware = True
5264
removeAttachedVirtualGuest = True

SoftLayer/managers/ticket.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ def update_ticket(self, ticket_id=None, body=None):
8080
"""
8181
return self.ticket.addUpdate({'entry': body}, id=ticket_id)
8282

83+
def upload_attachment(self, ticket_id=None, file_path=None,
84+
file_name=None):
85+
"""Upload an attachment to a ticket.
86+
87+
:param integer ticket_id: the id of the ticket to
88+
upload the attachment to
89+
:param string file_path:
90+
The path of the attachment to be uploaded
91+
:param string file_name:
92+
The name of the attachment shown
93+
in the ticket
94+
:returns The uploaded attachment
95+
"""
96+
file_content = None
97+
with open(file_path, 'rb') as attached_file:
98+
file_content = attached_file.read()
99+
100+
file_object = {
101+
"filename": file_name,
102+
"data": file_content
103+
}
104+
105+
return self.ticket.addAttachedFile(file_object, id=ticket_id)
106+
83107
def attach_hardware(self, ticket_id=None, hardware_id=None):
84108
"""Attach hardware to a ticket.
85109

tests/CLI/modules/ticket_tests.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,40 @@ def test_ticket_detach_virtual_server(self):
167167
'removeAttachedVirtualGuest',
168168
args=(100,),
169169
identifier=1)
170+
171+
def test_ticket_upload_no_path(self):
172+
result = self.run_command(['ticket', 'upload', '1'])
173+
174+
self.assertEqual(result.exit_code, 2)
175+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
176+
177+
def test_ticket_upload_invalid_path(self):
178+
result = self.run_command(['ticket', 'upload', '1',
179+
'--path=tests/resources/nonexistent_file',
180+
'--name=a_file_name'])
181+
182+
self.assertEqual(result.exit_code, 2)
183+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
184+
185+
def test_ticket_upload_no_name(self):
186+
result = self.run_command(['ticket', 'upload', '1',
187+
'--path=tests/resources/attachment_upload'])
188+
189+
self.assert_no_fail(result)
190+
self.assert_called_with('SoftLayer_Ticket',
191+
'addAttachedFile',
192+
args=({"filename": "attachment_upload",
193+
"data": b"ticket attached data"},),
194+
identifier=1)
195+
196+
def test_ticket_upload(self):
197+
result = self.run_command(['ticket', 'upload', '1',
198+
'--path=tests/resources/attachment_upload',
199+
'--name=a_file_name'])
200+
201+
self.assert_no_fail(result)
202+
self.assert_called_with('SoftLayer_Ticket',
203+
'addAttachedFile',
204+
args=({"filename": "a_file_name",
205+
"data": b"ticket attached data"},),
206+
identifier=1)

tests/resources/attachment_upload

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ticket attached data

0 commit comments

Comments
 (0)