2016-04-14 61 views
0

我有SoftLayer_Ticket一些問題:的SoftLayer API:如何創建一個機票和附加文件到票

  1. SoftLayer_Ticket :: createStandardTicket

    我被弄得參數:http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/createStandardTicket

    這個API的一些參數(attachmentId,rootPassword ...)是可選的,但我找不到一種方法來忽略它們。 我認爲我可以使用「paramName = XXX」來指定我需要的參數,並且像這樣調用: client['Ticket'].createStandardTicket(templateObject=templateObject,contents=contents, attachedFiles=attachedFiles)

    但它失敗了。 ?如果我要附加一些文件,而不是一臺服務器,我應該怎麼稱呼此功能,請給我一個例子,謝謝〜

  2. SoftLayer_Ticket :: addAttachedFile

    我想這個API附加一些文件:

    的test.txt(文本文件)和test.png(圖片)

    我成功了附加的test.txt,但未能連接test.png。

代碼:

import SoftLayer 

from SoftLayer import utils 

class Ticket_Manager(object): 
    def __init__(self, client): 
     self.sl_account = client['Account'] 
     self.sl_ticket = client['Ticket'] 
     self.sl_ticket_subject = client['Ticket_Subject'] 

    def test_attach_files(self, ticket_config): 
     if ticket_config.has_key('attachedFiles'): 
      for file in ticket_config['attachedFiles']: 
       print self.sl_ticket.addAttachedFile(file, id=27333259) 

    def test_create_ticket(self, ticket_config): 
     pass 

if __name__ == '__main__': 
    client = SoftLayer.create_client_from_env(username=API_USERNAME,api_key=API_KEY) 

    ticket_mgt = Ticket_Manager(client) 
    ticket_contents = 'SoftLayer API(SoftLayer_Ticket) test, thanks!' 
    with open('test.txt', 'rb') as file1: 
     bytes_stream1 = file1.read() 
     file1.close() 

    with open('test.png', 'rb') as file2: 
     bytes_stream2 = file2.read() 
     file2.close() 

    ticket_config = { 
     'subjectId':1522, 
     'title':'SoftLayer API test(no need to reply)', 
     'contents':ticket_contents, 
     'serverId':0, 
     'serverRootPassword':'', 
     'accessPort':'', 
     'serverType':'virtual', 
     'type':'standard', 
     'attachedFiles':[ 
      { 
       'filename':'test.txt', 
       'data':bytes_stream1 
      }, 
      { 
       'filename':'test.png', 
       'data':bytes_stream2 
      } 
     ], 
     'attachedAdditionalEmails':[ 
     ] 
    } 

    ticket_mgt.test_attach_files(ticket_config) 

我在linux下運行此腳本,並會見了此異常:

SoftLayer.exceptions.SoftLayerAPIError:SoftLayerAPIError(SoftLayer_Ticket):未找到有效的認證頭。

回答

0

問題1:

有必要通過所有參數,以便你能避免任何問題,但你可以將它們設置爲空。

嘗試這個例子中附加文件,它工作正常:

""" 
Create Standard Ticket 

This script creates a standard support ticket with file attached 

Important manual pages: 
http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/createStandardTicket 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket/createStandardTicket_File_Attachment 

License: http://sldn.softlayer.com/article/License 
Author: SoftLayer Technologies, Inc. <[email protected]> 
""" 
import SoftLayer 

# Define your SoftLayer username and apiKey 
USERNAME = 'set me' 
API_KEY = 'set me' 

# Declare the API client 
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY) 

# Build a SoftLayer_Ticket object 
templateObject = { 
        "assignedUserId": 123123, 
        "subjectId": 1522, 
        "title": "ticket example" 
        } 
# Define contents 
contents = 'this the ticket content' 

# Define the name of a file that is uploaded to the SoftLayer API. 
filename = "TestPicture.jpg" 

# Reading file 
with open("C:/Users/Ruber Cuellar/Pictures/allow.jpg", "rb") as f: 
    byte = f.read() 
    f.close() 

# Build SoftLayer_Container_Utility_File_Attachment object 
attachedFiles = [ 
        { 
        "filename": filename, 
        "data": byte 
        } 
       ] 

# Define additional parameters 
attachId = 0 
rootPassword = "" 
controlPanelPassword = "" 
accesPort = "" 

try: 
    result = client['SoftLayer_Ticket'].createStandardTicket(templateObject, contents, attachId, rootPassword, controlPanelPassword, accesPort, attachedFiles) 
    print (result) 
except SoftLayer.SoftLayerAPIError as e: 
    print("Unable to create standard ticket. " 
      % (e.faultCode, e.faultString)) 

問題2:

依我之見,你正在使用這個方法:addAttachedAdditionalEmails,你需要使用:SoftLayer_Ticket::addAttachedFile方法如果要附加文件,則只允許附加單個文件,如果要在一次調用中附加多個文件,請嘗試使用此方法:SoftLayer_Ticket::addUpdate

Al所以,異常:

SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(SoftLayer_Ticket): No valid authentication headers found.

它的東西與認證相關的,如果你能提供您正在嘗試的代碼,這將有助於確定哪些可以是問題

Updated

我回顧並驗證您的腳本能夠成功運行。然而,存在報告關於它的問題:

https://github.com/softlayer/softlayer-python/issues/506

看一看的解決方案,這:

https://github.com/softlayer/softlayer-python/pull/507

+0

謝謝您的回答!這對我有用。 我重新編輯了我的問題,並在那裏粘貼了我的源代碼。 我仍然陷在問題2中。 – ChrisM

+0

ChrisM,請參閱關於您的問題2的答案(更新部分)。 –