softlayer API:获取操作系统版本列表?

时间:2016-11-30 11:09:18

标签: list api operating-system ibm-cloud-infrastructure

现在我正在开发一个关于softlayer api的项目。我不想通过softlayer api获得os列表。就像门户网站一样。有没有一定的方法来获得正确的os列表?问候〜

enter image description here

2 个答案:

答案 0 :(得分:1)

您正在寻找特定的语言示例吗?如果使用SoftLayer CLI,则可以使用以下命令

执行此操作
slcli vs create-options # For Virtual Guests
slcli server create-options # For Bare Metal Servers 

答案 1 :(得分:0)

不幸的是,与单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))

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

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

我希望它有所帮助,请让我知道任何疑问,评论或是否需要进一步的帮助。

  

<强>更新

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

获得相同的结果