SoftLayer Python API

 

The following is a brief introduction to using the SoftLayer API Python client library to automate many of the functions provided in the portal. Once you can grasp the concepts of how to use the API it is often a quicker and more efficient way of interacting with SoftLayer than relying on the portal.

 

This guide will cover the Python API library but the concepts can applied to any of the other API libraries provided by SoftLayer such as C# and VB.NET.

 

This is by no means exhaustive. Further information can be found in the links provided and on the SoftLayer Developers Network Website.

 

Lets begin. The first thing you will need to do is install the Python API client library.

 

 

Installing the Python API Library

 

To install the library simply issue the following command:

 

PIP Install softlayer

 

Once installed all you need to do to use the library is import it into your scripts:

 

Import SoftLayer

 

Case is very important when using any of the commands. Always ensure the S and L are in caps when referencing SoftLayer in your scripts.

 

Authenticating with SoftLayer

 

The first thing you need to do to start making API calls is to create a client object. This authenticates the API with your SoftLayer account.

 

Client = SoftLayer.Client(username=’YOUR_ACCOUNT_ID’,                 api_key=’YOUR_API_KEY’)

 

Your API key is obtained from the portal under ACCOUNT – USERS.

 

Alternatively you can store your username and key in variables and create the client object from these variables:

 

 

 

 

Myuser = ‘YOUR_ACCOUNT_ID’

Mykey = ‘YOUR_API_KEY’

 

Client = SoftLayer.create_client_from_env(username=Myuser, api_key=MyKey)

 

 

 

Making API Calls

 

There are 2 different ways to make API calls to SoftLayer, via either the Services or the Managers. The Managers are a little easier to use but do not provide all the data and functionality provided by the Services.

 

I will cover off using both of these ways here.

 

Managers

 

There are many managers provided for interacting with different components in SoftLayer such as network, images, tickets and hardware and virtual instances. A complete list of the managers and how to use them is available here.

 

Here is an example of creating a Virtual Instance by using the SoftLayer.vs manager.

 

First create your manager reference and pass in your client authentication object created above:

 

Mgr = SoftLayer.VSManager(Client)

 

Then to create a new Virtual Server Instance we use the create_instance method. First we will define a variable with the required parameters such as the hostname, datacentre, hourly billing etc.

 

new_vsi = {

‘domain’: u’test01.labs.sftlyr.ws’,

‘hostname’: u’minion05′,

‘datacenter’: u’mel01′,

‘dedicated’: False,

‘private’: False,

‘cpus’: 1,

‘os_code’ : u’UBUNTU_LATEST’,

‘hourly’: True,

‘ssh_keys’: [1234],

‘disks’: (‘100′,’25’),

‘local_disk’: True,

‘memory’: 1024,

‘tags’: ‘test, pleaseCancel’

}

 

Then we pass this to the create_instance method on our VS Manager:

 

vsi = mgr.create_instance(**new_vsi)

# vsi will have the newly created vsi details if done properly.

print vsi

 

 

Finally we print the details of the newly created virtual instance.

 

The create_instances method allows you to create multiple virtual instances. You simply populate an array with copies of the variable defining the parameters and then change the required parameters inside each array element. For example you would need to provide different hostnames for each instance. This can be done as follows:

 

# using .copy() we make copies of the defined instance and place them in an array

instances = [new_vsi.copy(), new_vsi.copy(), new_vsi.copy()]

 

# give each its own hostname, not required.

instances[0][‘hostname’] = “multi-test01”

instances[1][‘hostname’] = “multi-test02”

instances[2][‘hostname’] = “multi-test03”

 

vsi = mgr.create_instances(config_list=instances)

#vsi will be a dictionary of all the new virtual servers

print vsi

 

 

Services

 

The SoftLayer Services website provides a list of all the Services that can be called from the API. The list of services on the left is quite extensive and covers everything from Accounts and Billing to Users and Virtual Guests. Clicking on a service will bring up all the methods able to be called against that service. For example the Accounts service has a method called getVirtualGuests. This method will retrieve an accounts associated virtual guest objects. Once again just create your service object depending on the service you wish to use:

 

Mgr = client[‘Account’]

 

You can omit the preceeding ‘SoftLayer_’ when creating the object.

Now you can call your method:

 

VirtualGuests = mgr.getVirtualGuests()

 

And then iterate through the array that is returned and pull out the information you need:

 

For VM in VirtualGuests:

Print VM[‘id’]

Print VM[‘hostname’]

 

Each object in SoftLayer (i.e. computing instance or network instance) has an ID. Most service methods require this ID to be passed in as a parameter when being called to identify the object you want to perform the method on.

 

For example to extract the username and password for a newly created instance we would need to call the method getSoftwareComponents from the Virtual_Guest service. This method requires the ID of the Virtual Guest so following on from our example above:

 

For VM in VIrtualGuests:

If VM[‘hostname’]==’Multi-test01’

hostID = VM[‘id’]

# Extract software component associated with hardware id

software = client[‘Virtual_Guest’].getSoftwareComponent(id=hostid)

# Extract OS Software ID

softwareid = software[0][‘id’]

 

The first [0] element in the array is always the Operating System. Now the Software_Component service has a method called getPassords which will extract the user names and passwords:

 

Password = client[‘Software_Component’].getPasswords[id=softwareid’]

# Extract usernames and passwords

for user in password:

print user[‘username’]

print user[‘password’]

 

 

 

 

I hope from these examples you can see just how powerful interacting via the API can be. Information regarding all the SoftLayer API client libraries available and how to use them can be found on the SoftLayer Developer Network site.

 

 

 

 

Leave a comment