Finding VSi's or Bare metals in certain pods

2019-09-13 11:37发布

Is there a way to search pods in a datacenter then grab the servers that are in those pods? I read somewhere that it's somewhat possible but the last question was regarding REST. Currently utitlizing the Python API

1条回答
Melony?
2楼-- · 2019-09-13 12:02

Try this python script:

"""
List servers according pods from a specific datacenter

Important manual pages
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getVirtualGuests
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware
http://sldn.softlayer.com/reference/datatypes/SoftLayer_VirtualGuests

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import pprint
from prettytable import PrettyTable

# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)

# Define datacenter e.g: dal05, dal09, ams01, sao01, etc
datacenter = 'ams01'

# Declare a new API service objects for SoftLayer_Account
accountClient = client['SoftLayer_Account']

# Define objectMask and objectFilters to get additional information
objectMask = 'mask[primaryNetworkComponent[router[hostname]]]'
objectFilterBms = {"hardware": {"datacenter": {"name": {"operation": datacenter}}}}
objectFilterVsi = {"virtualGuests": {"datacenter": {"name": {"operation": datacenter}}}}

# Define array to add pods
pods = []

try:
    # Getting servers and vsis
    servers = accountClient.getHardware(mask=objectMask, filter=objectFilterBms)
    vsis = accountClient.getVirtualGuests(mask=objectMask, filter=objectFilterVsi)

    # Getting pods from servers 
    for server in servers:
        if server['primaryNetworkComponent']['router']['hostname'] not in pods:
            pods.append(server['primaryNetworkComponent']['router']['hostname']) 
    for vsi in vsis:
        if vsi['primaryNetworkComponent']['router']['hostname'] not in pods:
            pods.append(vsi['primaryNetworkComponent']['router']['hostname'])
    # Printing servers according pods
    for pod in pods:
        table = []
        tableDetails = PrettyTable(['POD', 'Server Id', 'Hostname', 'Domain', 'Type'])
        print("POD: %s " % pod)
        for server in servers:
            if pod in server['primaryNetworkComponent']['router']['hostname']:
                tableDetails.add_row([pod, server['id'], server['hostname'], server['domain'], 'Hardware'])
                #print("   Server Id: %s   Hostname: %s   Domain: %s" % (server['id'], server['hostname'], server['domain']))
        for vsi in vsis:
            if pod in vsi['primaryNetworkComponent']['router']['hostname']:
                tableDetails.add_row([pod, vsi['id'], vsi['hostname'], vsi['domain'], 'Virtual Guest'])
                #print("   Virtual Server Id: %s   Hostname: %s   Domain: %s" % (vsi['id'], vsi['hostname'], vsi['domain']))
        print(tableDetails)

except SoftLayer.SoftLayerAPIError as e:
    print("Error. " % (e.faultCode, e.faultString))

It shows bare metal and virtual servers from each pod from a specific datacenter, try it and let me know if you need further assistance or any doubt

查看更多
登录 后发表回答