softlayer API:获取操作系统版本列表? [英] softlayer API : Get OS version list?

查看:20
本文介绍了softlayer API:获取操作系统版本列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Now I'm developing a project about softlayer api. I wan't to get the os list by softlayer api. Just like the portal site. Is there certain method to get correct os list ? regards~

推荐答案

遗憾的是,无法检索与 控制门户进行一次调用,但可以使用编程语言.

Unfortunately, it's not possible to retrieve the same result than Control Portal making a single call, but it's possible using a programming language.

要查看 SoftLayer 支持的编程语言:

To see programming languages supported by SoftLayer:

看看下面的python脚本:

Take a look the following python script:

 """
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 以获取特定数据中心的特定核心项目价格,也许这是必要的,但您可以根据自己的要求编辑此脚本.

I added core variable, because the OSs have restriction for capacity of cores. Also I added datecenter to get the specific core item price for a specifc datacenter, perhaps it's something innecesary, but you can edit this script according your requirements.

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

The same idea could be applied for others programming languges.

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

I hope it helps, please let me know any doubt, comments or if you need further assistance.

更新

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

I improved the script, I added the ability to check conflicts between items, in order to get the same result for each kind of Computing Instance

这篇关于softlayer API:获取操作系统版本列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆