过滤器如何在python中使用softlayer API
问题描述:
我试图通过使用过滤器的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'。但我仍然困惑它如何真正起作用。
答
试试下面的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是如何工作的:
- Object Filters
- Going Further with the SoftLayer API Python Client - Part 1
- Going Further with the SoftLayer API Python Client - Part 2
我希望它有帮助,请让我知道任何疑问或意见