2016-11-30 93 views

回答

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. <[email protected]> 
""" 
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以獲取特定數據中心的特定核心項目價格,這可能是不必要的,但您可以根據您的要求編輯此腳本。

同樣的想法可以應用於其他編程語言。

我希望它有幫助,請讓我知道任何疑問,意見或如果您需要進一步的幫助。


更新

我提高了劇本,我加入到檢查項目之間的衝突,爲了得到相同的結果爲每一種能力計算實例

+0

我明白了。非常感謝〜 –

+0

我改進了腳本。在我的答案中查看**更新的**部分 –

0

有沒有你正在尋找一個特定語言的例子嗎?如果您使用的SoftLayer CLI可以做到這一點使用下面的命令

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

我使用python語言編程,非常感謝。 –