How to create a priceConflicts hash like on store

2019-03-03 17:21发布

问题:

Context:

On softlayer configure page for a virtual guest ( https://www.softlayer.com/Store/orderComputingInstance/1640,1644,2202 ), the JavaScript do a lot of show / hide on price items based on some restrictions like:

  • MySQL for Linux is hidden when you choose Windows as Operating System (price to price restriction)
  • Private Node are unavailable on Dallas (location to price restriction)

My Problem:

Building a web interface to configure a virtual guest, I need to build a hash exactly like priceConflicts that is shown on configure page.

calling SoftLayer_Product_Package.getItemLocationConflicts I can get the location to price restrictions, but when I call SoftLayer_Product_Package.getItemConflicts is returned an array of SoftLayer_Product_Item_Resource_Conflict_Item with 4 attributes itemId, packageId, resourceTableId and message, that is exactly what is describe for http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Resource_Conflict_Item

Some things that are weird:

  • According to documentation: http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemConflicts return values was supposed to be an array of SoftLayer_Product_Item_Resource_Conflict, not an array of SoftLayer_Product_Item_Resource_Conflict_Item
  • According to documenation: http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Resource_Conflict_Item there's a resource relational property, but when I call with a mask mask[resource] the following error is returned: Property 'resource' not valid for 'SoftLayer_Product_Item_Resource_Conflict'.

So, how do I get the information needed to create a struct like priceConflicts hash?

Thank you

回答1:

For both cases it returns an array of SoftLayer_Product_Item_Resource_Conflict

You need to assume

  • For SoftLayer_Product_Package::getItemLocationConflicts method, the "itemId" has a "location conflict" with the location which has the same identifier as "resourceTableId".

    You can retrieve location identifiers with the following method: SoftLayer_Location_Datacenter::getDatacenters. So you will need to match the identifier, to get the location

  • For SoftLayer_Product_Package::getItemConflicts method, the "itemId" has conflicts with "resourceTableId" which corresponds another "itemId". You should match these values with the result from SoftLayer_Product_Package::getItems

As your said, JavaScript does a lot of show / hide on price items based on some restrictions.

I can provide a Python script which will help you to get all of this information

Updated

"""
Get item prices information

This script retrieves information of prices from a package. It retrieves the item description,
location conflicts, pricing location group and item conflicts

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getRegions
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Package/getItemPrices
http://sldn.softlayer.com/article/object-masks

@License: http://sldn.softlayer.com/article/License
@Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
# So we can talk to the SoftLayer API:
import SoftLayer
from prettytable import PrettyTable
# Your SoftLayer API username and key.
API_USERNAME = 'set me'
API_KEY = 'set me'
# Declare the image template id
packageId = 46
# Create a client instance
client = SoftLayer.Client(username=API_USERNAME, api_key=API_KEY)
# Declare an object mask to get location conflicts
objectMask = 'mask[pricingLocationGroup[locations],item[locationConflicts, conflicts]]'

try:
    locations = client['SoftLayer_Product_Package'].getRegions(id=packageId)
    items = client['SoftLayer_Product_Package'].getItems(id=packageId)
    print('*****  AVAILABLE LOCATIONS  *****')
    for location in locations:
        print('Id: %s,  Location: %s' % (location['location']['location']['id'], location['location']['location']['longName']))
    itemPrices = client['SoftLayer_Product_Package'].getItemPrices(id=packageId, mask=objectMask)
    items = client['SoftLayer_Product_Package'].getItems(id=packageId, mask='mask[prices]')
    x = PrettyTable(["Price Id", "Item Id", "Description", "Datacenter conflicts", "Pricing Location", 'Price conflicts', 'Item conflicts'])
    x.align["Price Id"] = "l"  # Left align city names
    x.padding_width = 1
    for price in itemPrices:
        dcConflicts = ''
        pricingLocation = ''
        conflictItems = ''
        conflictPrices = ''
        # Get location conflicts
        if  len(price['item']['locationConflicts']) > 0:
            for locationConflicts in price['item']['locationConflicts']:
                for location in locations:
                    if locationConflicts['resourceTableId'] == location['location']['location']['id']:
                        dcConflicts = dcConflicts + ' ' + location['location']['location']['longName']
        else:
            dcConflicts = "None"
        # Get Pricing location
        if 'pricingLocationGroup' in price:
            for priceLocation in price['pricingLocationGroup']['locations']:
                pricingLocation = pricingLocation + ' ' + priceLocation['longName']
        else:
            pricingLocation = 'Standard price'
        # Get item conflicts
        if len(price['item']['conflicts']) > 0:
            for conflict in price['item']['conflicts']:
                for item in items:
                    if conflict['resourceTableId'] == item['id']:
                        conflictItems = conflictItems + ' ' + str(conflict['resourceTableId'])
                        for priceConf in item['prices']:
                            conflictPrices = conflictPrices + ' ' + str(priceConf['id']) 
        if conflictItems == '':
            conflictItems = 'None'
            conflictPrices = 'None'
        x.add_row([price['id'], price['item']['id'], price['item']['description'], dcConflicts, pricingLocation, conflictPrices, conflictItems])
    print(x)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to get item prices faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
    exit(1)