Search code examples
python-2.7ibm-cloud-infrastructure

Softlayer Server_ID


I am trying to implement the following:

import SoftLayer.API
username = 'set me!'
apiKey = 'set me too!'
serverId = 1234       
client = SoftLayer.API.Client('SoftLayer_Hardware_Server', serverId, username, apiKey)

Here, I don't really know how to retrieve the serverId. How could I know my server ID for a specific server. Please help.


Solution

  • The SoftLayer_Account::getHardware retrieves information about your hardware objects, in which you can find the serverIds from your servers.

    Try this python script:

    """
    This script retrieves an account's associated hardware objects
    
    Important manual pages:
    http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    """
    # So we can talk to the SoftLayer API:
    import SoftLayer.API
    
    # For nice debug output:
    from pprint import pprint as pp
    
    # Your SoftLayer username and api key
    API_USERNAME = 'set me'
    API_KEY = 'set me'
    
    # Creates a new connection to the API service.
    client = SoftLayer.API.Client(username=API_USERNAME,api_key=API_KEY)
    
    try:
        hardwareObjects = client['SoftLayer_Account'].getHardware()
        pp(hardwareObjects)
    
    except SoftLayer.SoftLayerAPIError as e:
            pp('Unable to get hardware objects faultCode=%s, faultString=%s'
                % (e.faultCode, e.faultString))
    

    This script will return the information from your servers, in which the "id" property refers to serverId from the server that you need.

    However, if you wish to retrieve the information for an specific server, it can be done using Object Filters, here an example:

    """
    This script retrieves a hardware information for an specific hardware object.
    It is only necessary to specify the hostname from the server.
    
    Important manual pages:
    http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
    http://sldn.softlayer.com/article/object-filters
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    """
    # So we can talk to the SoftLayer API:
    import SoftLayer.API
    
    # For nice debug output:
    from pprint import pprint as pp
    
    # Your SoftLayer username and api key
    API_USERNAME = 'set me'
    API_KEY = 'set me'
    # Define the hostname from the hardware object
    hostname = 'hostnametest'
    
    # Declare an object filter to get an specific hardware object
    filterHardware = {
        'hardware': {
            'hostname': {
                'operation': hostname
            }
        }
    }
    
    # Creates a new connection to the API service.
    client = SoftLayer.API.Client(username=API_USERNAME,api_key=API_KEY)
    
    try:
        hardwareObjects = client['SoftLayer_Account'].getHardware(filter=filterHardware)
        pp(hardwareObjects)
    
    except SoftLayer.SoftLayerAPIError as e:
            pp('Unable to get the hardware object faultCode=%s, faultString=%s'
                % (e.faultCode, e.faultString))
    

    You need to specify the "hostname" from your server. The "id" in the response refers to serverId.

    Some references: