2016-03-02 95 views
0

我正嘗試創建REST call,它將根據選定的塊存儲類型(endurance, performance, portable storage)和選定的位置爲選定的storage type提取數據。然後試圖收集我看到0.25 IOPS/GB, 2 IOPS/GB, and 4 IOPS/GB等選項的"storage package"列表。存儲包(iops/gb)與存儲類型和位置之間的關係?

考慮到我有存儲類型和位置信息,動態獲取可用「存儲包」列表的最佳方式是什麼?

我應該使用什麼方法以及過濾器是什麼?

請分享該過濾器的示例。

+0

您是否要求提供有關網址可能的外觀的建議;例如/ storage-packages?location = EU&storageType = endurance – morsor

+0

@morsor是的,有什麼方法和對象過濾器存在相同..? –

+0

我不確定你的意思。我給了你一個我將使用的URL的例子 - 並且QueryParams作爲一個過濾器 – morsor

回答

0

你的問題類似於這樣一個Filter parameters to POST verify and place order request for Performance storage

我建議你使用標準的價格,因爲它們很容易獲取,他們在任何數據中心工作。另一方面,如果您想要確定某個位置的價格,請記住價格不存在的可能性,這是因爲價格只在與標準價格不同時才存在。欲瞭解更多信息,請參閱http://sldn.softlayer.com/blog/cmporter/Location-based-Pricing-and-You

另外,我建議您閱讀文檔以瞭解訂單的工作方式以及如何獲取訂單所需的數據。

這裏是一個例子,使用Python對位置進行排序(注意:靜態過濾器是相同的),查看目前他們正在尋找特定值的篩選器,如存儲庫大小存儲大小隻是從過濾器中刪除該部分。

""" 
Order a block storage (endurance) with snapshot storage space. 

Important manual pages: 
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order 
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder 
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder 
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package 
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItems 
http://sldn.softlayer.com/reference/services/SoftLayer_Location_Group_Pricing 
http://sldn.softlayer.com/reference/services/SoftLayer_Location_Group_Pricing/getAllObjects 
http://sldn.softlayer.com/reference/services/SoftLayer_Location 
http://sldn.softlayer.com/reference/services/SoftLayer_Location/getDatacenters 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Location_Group_Pricing 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Location 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Network_Storage_Enterprise 
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Price 
http://sldn.softlayer.com/blog/cmporter/Location-based-Pricing-and-You 
http://sldn.softlayer.com/blog/bpotter/Going-Further-SoftLayer-API-Python-Client-Part-3 
http://sldn.softlayer.com/node/274081 
http://sldn.softlayer.com/article/Python 
http://sldn.softlayer.com/article/Object-Masks 

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

import SoftLayer 
import json 

USERNAME = 'set me' 
API_KEY = 'set me' 

# Values "AMS01", "AMS03", "CHE01", "DAL05", "DAL06" "FRA02", "HKG02", "LON02", etc. 
location = "set me" 

# Values: "0.25", "2", "4" 
iops = "set me" 

# Values "20", "40", "80", "100", etc. 
storageSize = "set me" 

# Values "0", "5", "10", "20", "40", etc. 
# Remember that the values must not exceed the size of the configured storage size. 
snapshotSize = "set me" 

# Values "Hyper-V", "Linux", "VMWare", "Windows 2008+", "Windows GPT", "Windows 2003", "Xen" 
os = "set me" 

PACKAGE_ID = 240 

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY) 
productOrderService = client['SoftLayer_Product_Order'] 
packageService = client['SoftLayer_Product_Package'] 
locationGroupService = client['SoftLayer_Location_Group_Pricing'] 
locationService = client['SoftLayer_Location'] 
osService = client['SoftLayer_Network_Storage_Iscsi_OS_Type'] 

objectFilterDatacenter = {"name": {"operation": location.lower()}} 
objectFilterStorageEnterprise = {"items": {"categories": {"categoryCode": {"operation": "storage_service_enterprise"}}}} 
objectFilterFileStorage = {"items": {"categories": {"categoryCode": {"operation": "storage_block"}}}} 
objectFilterOsType = {"name": {"operation": os}} 
objectMaskLocation = "mask[locations]" 
objectMaskIops = "mask[attributes]" 


try: 
    # Getting the datacenter. 
    datacenter = locationService.getDatacenters(filter=objectFilterDatacenter) 
    # Getting file storage prices. 
    itemsFileStorage = packageService.getItems(id=PACKAGE_ID, filter=objectFilterFileStorage) 
    # Getting the item prices whose category is storage_service_enterprise. 
    itemsStorageEnterprise = packageService.getItems(id=PACKAGE_ID, filter=objectFilterStorageEnterprise) 
    # Getting the SoftLayer_Location_Group_Pricing which contains the configured location. 
    locations = locationGroupService.getAllObjects(mask=objectMaskLocation) 
    for item in locations: 
     for loc in item['locations']: 
      if location.lower() == loc['name'].lower(): 
       location = item 
       break 
     if 'id' in location: 
      break 
    # Getting the IOPS item prices which are equals to the configured IOPS, and are valid for the configured location. 
    # In case we did not get a location group for the configured location 
    # we are going to search for standard prices. 
    if 'id' in location: 
     objectFilterLocation = {"items": {"description": {"operation": iops + " IOPS per GB"}, "categories": {"categoryCode": {"operation": "storage_tier_level"}}, "locationGroupId": {"operation": "in", "options": [{"name": "data", "value": [location['id']]}]}}} 
     objectFilter = objectFilterLocation 
    else: 
     objectFilterNoLocation = {"items": {"description": {"operation": iops + " IOPS per GB"}, "categories": {"categoryCode": {"operation": "storage_tier_level"}}, "locationGroupId": {"operation": "is null"}}} 
     objectFilter = objectFilterNoLocation 
    itemsIops = packageService.getItems(id=PACKAGE_ID, filter=objectFilter, mask=objectMaskIops) 
    if len(itemsIops) == 0: 
     # In case we got an empty list we try getting the standard prices 
     objectFilter = {"items": {"description": {"operation": iops + " IOPS per GB"}, "categories": {"categoryCode": {"operation": "storage_tier_level"}}, "locationGroupId": {"operation": "is null"}}} 
     itemsIops = packageService.getItems(id=PACKAGE_ID, filter=objectFilter, mask=objectMaskIops) 
    # Getting the storage space which are equals to the configured storage space, meet the requirements for the configured IOPS, and are valid for the configured location 
    # In case we did not get a location group for the configured location 
    # we are going to search for standard prices. 
    if 'id' in location: 
     objectFilterLocation = {"items": {"capacity": {"operation": storageSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "performance_storage_space"}}, "locationGroupId": {"operation": "in", "options": [{"name": "data", "value": [location['id']]}]}}}} 
     objectFilter = objectFilterLocation 
    else: 
     objectFilterNoLocation = {"items": {"capacity": {"operation": storageSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "performance_storage_space"}}, "locationGroupId": {"operation": "is null"}}}} 
     objectFilter = objectFilterNoLocation 
    itemsStorage = packageService.getItems(id=PACKAGE_ID, filter=objectFilter) 
    if len(itemsStorage) == 0: 
     # In case we got an empty list we try getting the standard prices 
     objectFilter = {"items": {"capacity": {"operation": storageSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "performance_storage_space"}}, "locationGroupId": {"operation": "is null"}}}} 
     itemsStorage = packageService.getItems(id=PACKAGE_ID, filter=objectFilter) 
    # Getting the snapshot storage space which are equals to the configured snapshot storage space, meet the requirements for the configured IOPS, and are valid for the configured location 
    # In case we did not get a location group for the configured location 
    # we are going to search for standard prices. 
    if 'id' in location: 
     objectFilterLocation = {"items": {"capacity": {"operation": snapshotSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "storage_snapshot_space"}}, "locationGroupId": {"operation": "in", "options": [{"name": "data", "value": [location['id']]}]}}}} 
     objectFilter = objectFilterLocation 
    else: 
     objectFilterNoLocation = {"items": {"capacity": {"operation": snapshotSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "storage_snapshot_space"}}, "locationGroupId": {"operation": "is null"}}}} 
     objectFilter = objectFilterNoLocation 
    itemsSnapshot = packageService.getItems(id=PACKAGE_ID, filter=objectFilter) 
    if len(itemsStorage) == 0: 
     # In case we got an empty list we try getting the standard prices 
     objectFilter = {"items": {"capacity": {"operation": snapshotSize}, "prices": {"capacityRestrictionMaximum": {"operation": itemsIops[0]['attributes'][0]['value']}, "categories": {"categoryCode": {"operation": "storage_snapshot_space"}}, "locationGroupId": {"operation": "is null"}}}} 
     itemsSnapshot = packageService.getItems(id=PACKAGE_ID, filter=objectFilter) 
    # Getting the OS 
    os = osService.getAllObjects(filter=objectFilterOsType) 
    # Building the order template 
    orderData = { 
     "complexType": "SoftLayer_Container_Product_Order_Network_Storage_Enterprise", 
     "packageId": PACKAGE_ID, 
     "location": datacenter[0]['id'], 
     "quantity": 1, 
     "prices": [ 
      { 
       "id": itemsFileStorage[0]['prices'][0]['id'] 
      }, 
      { 
       "id": itemsStorageEnterprise[0]['prices'][0]['id'] 
      }, 
      { 
       "id": itemsIops[0]['prices'][0]['id'] 
      }, 
      { 
       "id": itemsStorage[0]['prices'][0]['id'] 
      }, 
      { 
       "id": itemsSnapshot[0]['prices'][0]['id'] 
      } 
     ], 
     "osFormatType": os[0] 
    } 
    # verifyOrder() will check your order for errors. Replace this with a call to 
    # placeOrder() when you're ready to order. Both calls return a receipt object 
    # that you can use for your records. 
    response = productOrderService.verifyOrder(orderData) 
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': '))) 
except SoftLayer.SoftLayerAPIError as e: 
    print("Unable to place the order. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))