简体   繁体   English

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

[英]softlayer API : Get OS version list?

Now I'm developing a project about softlayer api. 现在,我正在开发一个有关softlayer api的项目。 I wan't to get the os list by softlayer api. 我不会通过softlayer api获取操作系统列表。 Just like the portal site. 就像门户网站一样。 Is there certain method to get correct os list ? 有某种方法可以获取正确的操作系统列表? regards~ 问候〜

在此处输入图片说明

Is there a specific language example you are looking for? 您是否在寻找特定的语言示例? If you use the SoftLayer CLI you can do this with the following command 如果使用SoftLayer CLI,则可以使用以下命令执行此操作

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

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. 不幸的是,不可能获得与Control Portal进行一次调用相同的结果,但是可以使用编程语言来获得相同的结果。

To see programming languages supported by SoftLayer: 要查看SoftLayer支持的编程语言:

Take a look the following python script: 看一下以下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))

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. 另外,我还添加了datecenter以获得特定数据中心的特定核心项目价格,这也许是不必要的,但是您可以根据需要编辑此脚本。

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. 希望对您有所帮助,如有任何疑问,意见或需要进一步帮助,请告诉我。


Updated 更新

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 我改进了脚本,添加了检查项目之间冲突的功能,以便为每种计算实例获得相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM