2016-09-07 65 views
0

我試圖通過使用過濾器的softlayer API獲取storageID。但是,即使我使用過濾方法,它也會給我所有存儲列表。過濾器如何在python中使用softlayer API

import SoftLayer 

import json 

storagename = "ABC-123" 

client = SoftLayer.Client() 

accountservice = client['SoftLayer_Account'] 

objectFilterstorage = {"username": {"operation": storagename}} 

storageId = accountservice.getNetworkStorage(filter=objectFilterstorage) 

print storageId 

此外,如何在獲取特定產品的列表時弄清需要哪些屬性?

在這裏,我發現屬性&列表'objectFilterstorage'。但我仍然困惑它如何真正起作用。

回答

0

試試下面的python腳本:

""" 
This script retrieves storage identifier through name 

Important manual pages: 
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkStorage 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Storage 

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

# Your SoftLayer API username and key. 
USERNAME = 'set me' 
API_KEY = 'set me' 

# Define username name from the storage 
storageUsername = 'SL345234' 

# Declaring the API client 
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY) 
# Define an object mask to get additional information from servers 
objectFilter = {"networkStorage": {"username": {"operation": storageUsername}}} 

try: 
    storageResult = client['SoftLayer_Account'].getNetworkStorage(filter=objectFilter) 
    for storage in storageResult: 
     print("Storage Id: %s" % storage['id']) 

except SoftLayer.SoftLayerAPIError as e: 
    print(('Error faultCode=%s, faultString=%s' 
    % (e.faultCode, e.faultString))) 

另外,我可以推薦給查看以下鏈接摸清楚objectFilters是如何工作的:

我希望它有幫助,請讓我知道任何疑問或意見