如何使用 SoftLayer 订购便携式存储 API

How to order portable storage using SoftLayer API

给定输入数据中心(例如 WDC06)和 500 GB 大小,是否有一种简单的方法来订购便携式存储设备。

目前我所知道的方法是痛苦的、复杂的和手动的,如果我在一个新的数据中心这样做的话。首先通过 Product_Package 获取配置,然后通过长长的项目列表找到正确的产品 ID、itemId ... 等。此调用还要求我事先知道 pkgid

categories = client['Product_Package'].getConfiguration(id=pkgId, mask='isRequired, itemCategory.id, itemCategory.name, itemCategory.categoryCode')

如果可以简化此订购过程,请分享一些代码示例。

我不知道您是如何订购便携式存储设备的,但您需要使用 placeOrder 方法并为您要订购的磁盘大小获取合适的价格,这篇文献可以帮助您了解如何下订单:

https://sldn.softlayer.com/blog/cmporter/location-based-pricing-and-you https://sldn.softlayer.com/blog/bpotter/going-further-softlayer-api-python-client-part-3

选择正确价格的过程很困难,但您可以使用对象过滤器来获取它们:

https://sldn.softlayer.com/article/object-filters

这里是一个使用 softlayer Python client:

的示例
import SoftLayer

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

datacenter = "wdc06" # lower case
size = "500" # the size of the disk
diskDescription = "optional value"

client = SoftLayer.Client(username=API_USERNAME, api_key=API_KEY)
package = 198 # this package is always the same
# Using a filter to get the price for an especific disk size
# into an specific datacenter
filter = {
    "itemPrices": {
        "pricingLocationGroup": {
            "locations": {
                "name": {
                    "operation": datacenter
                }
            }
        },
        "item": {
            "capacity": {
                "operation": size
            }
        }
    }
}
price = client['SoftLayer_Product_Package'].getItemPrices(id=package, filter=filter)
# In case the request do not return any price we will look for the standard price
if not price:
   filter = {
    "itemPrices": {
        "locationGroupId": {
            "operation": "is null"
            },
        "item": {
            "capacity": {
                "operation": size
                }
            }
        }
    }
   price = client['SoftLayer_Product_Package'].getItemPrices(id=package, filter=filter)
if not price:
    print ("there is no a price for the selected datacenter %s and disk size %s" % (datacenter, size))
    sys.exit(0)

# getting the locationId for the order template
filter = {
    "regions": {
        "location": {
            "location": {
                "name": {
                    "operation": datacenter
                }
            }
        }
    }
}
location = client['SoftLayer_Product_Package'].getRegions(id=package, filter=filter)
# now we are creating the ordertemplate
orderTemplate = {
    "complexType": "SoftLayer_Container_Product_Order_Virtual_Disk_Image",
    "packageId": package,
    "location": location[0]["location"]["location"]["id"],
    "prices": [{"id": price[0]["id"]}],
    "diskDescription": diskDescription
}

#When you are ready to order change "verifyOrder" by "placeOrder"
order = client['SoftLayer_Product_Order'].verifyOrder(orderTemplate)
print order