Archive for June, 2007

Time for some morning Java

(This post refers to SoftLayer API version 1. Check out API version 3 for our latest updates.)

We’ve put a Java SOAP example up on our downloads page. This was written in Eclipse 3.2.2, and for your convenience the example package contains our Eclipse project files. The code should run on JVM version 1.4 and above. We’d love to hear your feedback on this example. Reply here or post on our forums and let us know what you think. See ya’ll later!

No comments

API Hacking Fun With Python

(This post refers to SoftLayer API version 1. Check out API version 3 for our latest updates.)

Hello! I’m Shawn, one of new “Code Writing Professionals” at SoftLayer. When a call went out for examples using SoftLayer’s new API, I immediately signed up to write the INTERCAL, Prolog, and Apollo Guidance Computer modules. I was told to make those low priority projects, and to perhaps focus on hacking with Python instead. If you just want to see the finished code without all the Python evangelization, skip to the end of the post. For those of you who haven’t hacked with Python yet, follow along for the ride!

In case you don’t know, Python is an incredibly simple and clean looking programming language. Many people have picked it up in just a few minutes! If you already program in another programming language (like C, Java, PHP, SH, or Perl (shiver)), Python may look a bit strange. But it’s not too strange at all. In fact, the only “strange” bits in Python are it’s reliance on whitespace (you have to line up your code) and the lack of braces.

Any good FORTRAN coder will tell you that whitespace is good.

If you run a flavor of UNIX, Linux, or BSD, you most likely have Python already installed. If you are running Windows or if your distribution doesn’t come with a recent Python package installed you will need to install Python. You Linux guys can install it with your favorite package manager (it’s usually named python). Windows users (or UNIX haxxors who prefer tarballs), pick up the latest version of Python from ActiveState. (You will also want to check out ActiveState’s free(!) Komodo Edit)

Install Python and we are ready to go!

Open up a UNIX Terminal or the Windows command line (Start->Run… type CMD {enter}), then type “python”. This boots up the Interactive Python Interpreter. One of the cool things about Python is that you can whip up a Python Interpreter and just start typing in program fragments. It’s like the “immediate” mode in QBASIC for DOS, but neater. You should see something like this:
 

 
The first step is to import the XML-RPC library for Python. In keeping with Python’s “Batteries Included” approach, the most useful libraries come standard with any complete download of the Python interpreter, and this includes the standard XML-RPC library. To do this, type:
 

import xmlrpclib

 
Now Python has all the functionality needed to make and receive XML-RPC calls loaded into the interpreter. Let’s set up some variables to make hacking with the library much easier. In the interpreter, type:
 

url = "http://api.service.softlayer.com/xmlrpc/v1/SL-Service.html"
key = "YOUR SOFTLAYER API KEY"
usr = "YOUR SOFTLAYER API USERNAME"

 
Now we need to set up a Server Proxy. This is an object that we can make method calls against that does all the dirty work of compiling the XML and parsing the data… all the boring stuff. Using the proxy we get to make XML-RPC calls as if all the work was being done on the local machine. This is easy to do. Type this into the interpreter:
 

server = xmlrpclib.ServerProxy(url)

 
Now you have your proxy. If you print out the server variable (print server) you will see that server is an instance of the ServerProxy class made to communicate with the SoftLayer XML-RPC server. Now, let’s actually do something!
 

serverList = server.getServerList(key, usr)

 
This invokes the XML-RPC getServerList method, which retrieves your list of servers and stashes it into serverList. Let’s see what you got:
 

print serverList

 
The data is returned as a LIST of DICTIONARIES, with one DICTIONARY per server. Let’s loop through this list and display the servername of all your servers:
 

for thisServer in serverList:
  print thisServer["SERVERNAME"]

 
The two space indentation at the beginning of the second line are required or else the code won’t work. Hit enter twice, and Python will print out the Servername (hostname + domain name) for all servers in your account. You can pull more data out of the dictionary easily:
 

for thisServer in serverList:
  print thisServer["SERVERNAME"], thisServer["PUBLIC_IP_ADDRESS"]

 
You can make all the method calls listed in the SoftLayer API Documentation by calling them as methods of the server class:
 

server.getBandwidthList(key, usr, "192.168.1.1")
server.getServerNetworkDetails(key, usr, "192.168.1.1")
server.rebootServer(key, usr, "192.168.1.1", "Soft Reboot")

 
Python is easy to pick up and play with, and with it’s XML-RPC library, it’s probably the fastest way for people to hack with the SoftLayer API. The complete code listing to display all your servernames is below:
 

# Quickie Python XML-RPC Demo for the SoftLayer API
# Written by Shawn Boles, SoftLayer Development

# API Connection Details
url = "http://api.service.softlayer.com/xmlrpc/v1/SL-Service.html"
key = "PUT YOUR SOFTLAYER API KEY HERE..."
usr = "PUT YOUR API USERNAME HERE..."

# This is the XML-RPC Library for Python.  It comes standard!
import xmlrpclib

# Set up the proxy object for the SoftLayer server.  You can make
# calls against this object as if the processing is local.
server = xmlrpclib.ServerProxy(url)

# Make a method call on getServerList from the API.
# Don't forget to pass the key and usr variables as the first
# two parameters, in that order!
serverList = server.getServerList(key, usr)

# Loop through all servers, displaying their servernames.
for myserver in serverList:
    print myserver["SERVERNAME"]

 
Only 8 lines of code, ignoring comments and empty lines. Hacking indeed! When a new API method comes out you can pop open a command line and slap together a test case in a minute or two to try it out!

Extra Credit: Let’s do something a little more useful with our new |33+ Python API Hacking Skilz:
 

# Display all my servers and IP addresses, using the SoftLayer API
# Written by Shawn Boles, SoftLayer Development

# API Connection Details
url = "http://api.service.softlayer.com/xmlrpc/v1/SL-Service.html"
key = "PUT YOUR SOFTLAYER API KEY HERE..."
usr = "PUT YOUR API USERNAME HERE..."

# This is the XML-RPC Library for Python.  It comes standard!
import xmlrpclib

# Set up the proxy object for the SoftLayer server.  You can make
# calls against this object as if the processing is local.
server = xmlrpclib.ServerProxy(url)

# Make a method call on getServerList from the API.
# Don't forget to pass the key and usr variables as the first
# two parameters, in that order!
serverList = server.getServerList(key, usr)

# Loop through all servers, displaying their servernames.
for myserver in serverList:
    # Get the list of Server Details from the API for each server:
    serverDetail = server.getServerDetails(key, usr, myserver["ID"])

    # "Unfold" the Server Details data structure and give us access
    # to only the IP information...
    prettyServerDetail = serverDetail[2][0]

    try:
        print myserver["SERVERNAME"], \
            "\n\tPublic IP:", prettyServerDetail["PRIMARY_PUBLIC_IP"], \
            "(", prettyServerDetail["PUBLIC_PORT_SPEED"], "),", \
            "\n\tPrivate IP:", prettyServerDetail["PRIMARY_PRIVATE_IP"], \
            "(", prettyServerDetail["PRIVATE_PORT_SPEED"], "),", \
            "\n\tManage IP:", prettyServerDetail["MGMT_IP"], "\n"
    except KeyError:
        print "NONE\n"
        pass

 
Here’s a direct link to the sample file for your editing pleasure.

In no time at all you will be hacking apps for your API no sweat! And you aren’t locked down to command line tools, either. Python comes with just about every GUI kit, can be used to make your own Python-powered control panel…

Have fun hacking!

No comments

Let’s get this party started.

(This post refers to SoftLayer API version 1. Check out API version 3 for our latest updates.)

Howdy, everyone. The SoftLayer dev team has been growing a bit lately to handle new portal additions and adding fancy new methods to the API for ya’ll. So far we’ve gotten some great feedback on the API and how to make our application servers work better for you and your businesses. Now that we’ve got the API out and some extra head count what say we kick into high gear?

Here’s our game plan. We know how smart our customers are. We know how they think (we hope). They’re geeks who find perverse joy in taking things apart, reassembling them, and building cool new toys out of the leftover parts. We want you to give that a try with our API. Our Implementations Forum has a good start, but we’re looking for something to really knock our socks off. So far the coolest thing we’ve seen is a Yahoo! widget. Who can top that?

Email us, post to the forum, or leave a comment here with sweet implementation goodness. Surprise us. Show us what wacky stuff you can do with your server and our API. If you don’t code then that’s fine. Send us your ideas. Maybe someone will pick up on it. To get the ball rolling we’ll write an app or two of our own. Even better we’ll release the source of our app to get ya’ll started. Let’s see what kind of cool programs we can write.

2 comments

API Addition - Backbone Graphs

(This post refers to SoftLayer API version 1. Check out API version 3 for our latest updates.)

Due to popular demand, we rolled out a change today which allows querying of our backbone connection graphs. For the uninitiated, backbone graphs show the network usage between the SoftLayer datacenter and the Internet. This functionality was accomplished by adding two new methods to the SoftLayer API.

getBackboneList
Retrieves an array of id and name pairs for all SoftLayer’s Backbone providers

getBackboneImage(intBackboneId)
Retrieves a PNG image for display or local storage

Both of the methods are available via the SOAP and XML-RPC interfaces. We will be updating the documentation and examples for these methods and a few other additions (Top Secret) over the next few weeks.

1 comment