softlayer API : 获取 OS 版本列表?

softlayer API : Get OS version list?

现在正在开发一个关于softlayer的项目api。我不想通过 softlayer api 获取 os 列表。就像门户网站一样。是否有某种方法可以获取正确的 os 列表?问候~

是否有您要查找的特定语言示例?如果您使用 SoftLayer CLI,您可以使用以下命令执行此操作

slcli vs create-options # For Virtual Guests
slcli server create-options # For Bare Metal Servers 

遗憾的是,检索与 Control Portal 进行一次调用相同的结果是不可能的,但使用编程语言是可能的。

查看 SoftLayer 支持的编程语言:

看看下面的 python 脚本:

 """
List OSs for VSI similar than Portal

See below references for more details.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices
http://sldn.softlayer.com/article/object-filters
http://sldn.softlayer.com/article/object-Masks

@License: http://sldn.softlayer.com/article/License
@Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import datetime
import time

# Your SoftLayer's username and api Key
USERNAME = 'set me'
API_KEY = 'set me'

# Package id
packageId = 46
# Datacenter
datacenter = 'wdc04'
# Computing INstance
core = '1 x 2.0 GHz Core'

# Creating service
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
packageService = client['SoftLayer_Product_Package']

# Declaring filters and mask to get additional information for items
filterDatacenter = {"itemPrices": {"pricingLocationGroup": {"locations": {"name": {"operation": datacenter}}}}}
objectMaskDatacenter = 'mask[pricingLocationGroup[locations]]'

objectMask = 'mask[pricingLocationGroup[locations],categories,item[id, description, capacity,softwareDescription[manufacturer],availabilityAttributeCount, availabilityAttributes[attributeType]]]'
filterInstance = {
    'itemPrices': {
        'categories': {
            'categoryCode': {
                'operation': 'os'
            }
        }
    }
}

# Define a variable to get capacity
coreCapacity = 0
# To get item id information
itemId = 0
flag = False
# Define the manufacturers from which you like to get information
manufacturers = ["CentOS", "CloudLinux", "CoreOS", "Debian", "Microsoft", "Redhat", "Ubuntu"]

# Declare time to avoid list OS expired
now = time.strftime("%m/%d/%Y")
nowTime = time.mktime(datetime.datetime.strptime(now, "%m/%d/%Y").timetuple())

try:
    conflicts = packageService.getItemConflicts(id=packageId)
    itemPrices = packageService.getItemPrices(id=packageId, filter=filterDatacenter, mask=objectMask)
    if len(itemPrices) == 0:
        filterDatacenter = {"itemPrices":{"locationGroupId":{"operation":"is null"}}}
        itemPrices = packageService.getItemPrices(id=packageId, filter=filterDatacenter, mask=objectMask)
    for itemPrice in itemPrices:
        if itemPrice['item']['description'] == core:
            itemId = itemPrice['item']['id']
            coreCapacity = itemPrice['item']['capacity']
    result = packageService.getItemPrices(id=packageId, mask=objectMask, filter=filterInstance)
    filtered_os = []
    for item in result:
        for attribute in item['item']['availabilityAttributes']:
            expireTime = time.mktime(datetime.datetime.strptime(attribute['value'], "%m/%d/%Y").timetuple())
            if ((attribute['attributeType']['keyName'] == 'UNAVAILABLE_AFTER_DATE_NEW_ORDERS') and (expireTime >= nowTime)):
                filtered_os.append(item)
        if item['item']['availabilityAttributeCount'] == 0:
            filtered_os.append(item)
    for manufacturer in manufacturers:
        print(manufacturer)
        for itemOs in filtered_os:
            for conflict in conflicts:
                if (((itemOs['item']['id'] == conflict['itemId']) and (itemId == conflict['resourceTableId'])) or ((itemId == conflict['itemId']) and (itemOs['item']['id'] == conflict['resourceTableId']))):
                    flag = False
                    break
                else:
                    flag = True
            if flag:
                if itemOs['item']['softwareDescription']['manufacturer'] == manufacturer:
                    if 'capacityRestrictionMinimum' in itemOs:
                        if((itemOs['capacityRestrictionMinimum'] <= coreCapacity) and (coreCapacity <= itemOs['capacityRestrictionMaximum'])):
                                print("%s    Price Id: %s   Item Id: %s" % (itemOs['item']['description'], itemOs['id'], itemOs['item']['id']))
                    else:
                        print("%s    Price Id: %s   Item Id: %s" % (itemOs['item']['description'], itemOs['id'], itemOs['item']['id']))
        print("---------------------------------------------------")
except SoftLayer.SoftLayerAPIError as e:
    print('Unable to get Item Prices faultCode=%s, faultString=%s' 
    % (e.faultCode, e.faultString))

我添加了core变量,因为操作系统对内核容量有限制。我还添加了 datecenter 以获取特定数据中心的特定核心项目价格,也许这是必要的,但您可以根据需要编辑此脚本。

同样的想法可以应用于其他编程语言。

希望对您有所帮助,如有任何疑问、意见或需要进一步帮助,请告诉我。


Updated

我改进了脚本,添加了检查项目之间冲突的功能,以便为每种 计算实例

获得相同的结果