Skip to content

Instantly share code, notes, and snippets.

@hammer
Created December 18, 2008 09:07
Show Gist options
  • Save hammer/37445 to your computer and use it in GitHub Desktop.
Save hammer/37445 to your computer and use it in GitHub Desktop.
A more usable version of my Python wrapper for the SoftLayer API
import xmlrpclib
"""
A simple Python wrapper for the SoftLayer API as exposed via XML-RPC
"""
# TODO: Make this object-based for services, data types, and methods instead of dictionary-based
# TODO: Read authentication information from a secure location
# TODO: Demonstrate how to call a method that requires a parameter
# TODO: Work with exceptions
# Get this information from https://manage.softlayer.com/Administrative/apiKeychain
USERNAME = ''
API_KEY = ''
# see http://sldn.softlayer.com/wiki/index.php/Category:API_Services
# for the complete list; would be nice to fetch via WSDL or somesuch
services = { 'account': 'Account',
'billing': 'Billing_Invoice',
'dns': 'Dns_Domain',
'hardware': 'Hardware',
'software': 'Software_Component',
'backbone': 'Network_Backbone',
'bandwidth': 'Network_Bandwidth_Version1_Allotment',
'firewall': 'Network_Component_Firewall',
'lb': 'Network_LoadBalancer_VirtualIpAddress',
'monitor': 'Network_Monitor_Version1_Query_Host',
'storage': 'Network_Storage',
'subnet': 'Network_Subnet',
'vlan': 'Network_Vlan',
'ticket': 'Ticket',
'customer': 'User_Customer',
'utility': 'Utility_Network',
}
# see http://sldn.softlayer.com/wiki/index.php/Category:API_Data_Types
data_types = {}
# see http://sldn.softlayer.com/wiki/index.php/Category:API_Methods
methods = {}
#
# Service acquisition
#
ENDPOINT_BASE = 'http://api.service.softlayer.com/xmlrpc/v3/SoftLayer_'
def make_endpoint(service_name):
return ENDPOINT_BASE + service_name
def get_service(service):
service_endpoint = make_endpoint(services[service])
return xmlrpclib.ServerProxy(service_endpoint)
#
# Header construction
#
HEADERS_BASE = { 'headers': {} }
def add_header(new_header, current_header = HEADERS_BASE):
current_header['headers'].update(new_header)
return current_header
# see http://sldn.softlayer.com/wiki/index.php/Authenticating_to_the_SoftLayer_API
def make_auth_header():
header = {
'authenticate' : {
'username': USERNAME,
'apiKey': API_KEY
}
}
return header
# see http://sldn.softlayer.com/wiki/index.php/Using_Initialization_Parameters_in_the_SoftLayer_API
def make_init_header(service, id):
init_name = 'SoftLayer_' + services[service] + 'InitParameters'
header = {
init_name: {
'id': id
}
}
return header
# see http://sldn.softlayer.com/wiki/index.php/Using_Object_Masks_in_the_SoftLayer_API
def make_obj_mask_header(service, mask):
obj_mask_name = 'SoftLayer_' + services[service] + 'ObjectMask'
header = {
obj_mask_name: {
'mask': mask
}
}
return header
# see http://sldn.softlayer.com/wiki/index.php/Using_Result_Limits_in_the_SoftLayer_API
def make_limit_header(limit, offset = 0):
header = {
'resultLimit': {
'limit': limit,
'offset': offset
}
}
return header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment