过滤器如何在python中使用softlayer API

时间:2016-09-07 13:35:55

标签: ibm-cloud-infrastructure

我尝试使用过滤器通过softlayer API获取storageID。但它给了我所有存储的列表,即使我用方法应用过滤器。

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'。但我仍然困惑它是如何运作的。

1 个答案:

答案 0 :(得分: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. <sldn@softlayer.com>
"""
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的工作原理:

我希望它有所帮助,请让我知道任何疑问或评论