过滤器如何与 python 中的软层 API 一起工作

How filter is working with softlayer API in python

我试图使用过滤器通过软层 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'。但是我还是搞不懂它到底是怎么工作的。

尝试以下 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 的工作原理:

希望对您有所帮助,如有任何疑问或意见,请告诉我