-
Notifications
You must be signed in to change notification settings - Fork 0
API Usage
This page uses the new v2.0.0 features! Read this to convert from the old format!!
You can pass in your username and api_key when creating a SoftLayer client instance. However, you can set these in the environmental variables 'SL_USERNAME' and 'SL_API_KEY'
Creating a client instance by passing in the username/api_key:
import SoftLayer
client = SoftLayer.Client(username='YOUR_USERNAME', api_key='YOUR_API_KEY')Creating a client instance with environmental variables set:
# env variables
# SL_USERNAME = YOUR_USERNAME
# SL_API_KEY = YOUR_API_KEY
import SoftLayer
client = SoftLayer.Client()Blow is an example of creating a client instance with more options. This will create a client with the private API endpoint (only accessable from the SoftLayer network), a timeout of 2 minutes, and with verbose mode on (prints out more than you ever wanted to know about the HTTP requests to stdout).
client = SoftLayer.Client(
username='YOUR_USERNAME',
api_key='YOUR_API_KEY'
endpoint_url=SoftLayer.API_PRIVATE_ENDPOINT,
timeout=240,
verbose=True,
)The SoftLayer API client for python leverages SoftLayer's XML-RPC API. It supports authentication, object masks, object filters, limits, offsets, and retrieving objects by id. The following section assumes you have a initialized client named 'client'.
The best way to test our setup is to call the getObject method on the SoftLayer_Account service.
client['Account'].getObject()For a more complex example we'll retrieve a support ticket with id 123456 along with the ticket's updates, the user it's assigned to, the servers attached to it, and the datacenter those servers are in. To retrieve our extra information using an object mask.
Retreive a ticket using Object Masks.
ticket = client['Ticket'].getObject(
id=123456, mask="mask[updates, assignedUser, attachedHardware.datacenter]")Now add an update to the ticket.
update = client['Ticket'].addUpdate({'entry' : 'Hello!'}, id=123456)Let's get a listing of virtual guests using the domain example.com
client['Account'].getVirtualGuests(
filter={'virtualGuests': {'domain': 'example.com'}})SoftLayer's XML-RPC API also allows for pagination.
client['Account'].getVirtualGuests(limit=10, offset=0) # Page 1
client['Account'].getVirtualGuests(limit=10, offset=10) # Page 2Here's how to create a new Cloud Compute Instance using SoftLayer_Virtual_Guest.createObject. Be warned, this call actually creates an hourly CCI so this does have billing implications.
client['Virtual_Guest'].createObject({
'hostname': 'myhostname',
'domain': 'example.com',
'startCpus': 1,
'maxMemory': 1024,
'hourlyBillingFlag': 'true',
'operatingSystemReferenceCode': 'UBUNTU_LATEST',
'localDiskFlag': 'false'
})