Skip to content

Instantly share code, notes, and snippets.

@anzenehansen
Last active December 26, 2015 19:19
Show Gist options
  • Save anzenehansen/7200503 to your computer and use it in GitHub Desktop.
Save anzenehansen/7200503 to your computer and use it in GitHub Desktop.
./solusvm_api.py info ipaddr,hdd,hostname ipaddr=true,hdd=true
{'hdd': ['53687091200,274677760,53412413440,1'], 'ipaddr': ['192.xxx.xxx.158'], 'hostname': ['cs01']}
#! /usr/bin/env python
"""
Python method to make SolusVM API calls.
Takes the API call to be made ("info", "status", etc...) and a list of items to return ("ipaddress,hostname", "all", etc...),
and returns a dictionary of each.
The dictionary is formatted as:
{'item_1' : ['data list'], 'item_2' : ['data list']}
Example:
{'ipaddress' : ['ip 1', 'ip 2'], 'hostname' : ['hostname of instance']}
This was made out of boredom mostly but is also a fun project.
"""
class SolusVM(object):
def __init__(self, host, api_key, api_hash):
self.host = host
self.key = api_key
self.hash = api_hash
def api(self, *args):
ret = {}
try:
action = args[0]
except IndexError:
return ret
try:
url = "%s/api/client/command.php" % self.host
except IndexError:
return ret
import urllib
params = {'key' : self.key, 'hash' : self.hash, 'action' : action}
for key,val in kwargs.iteritems():
params[key] = val
f = urllib.urlopen(url, params)
# Python's XML library requires the string to be enclosed in XML, so we pick some random data to wrap it up in
data = "<data>%s</data>" % f.read()
import xml.etree.ElementTree as ET
tree = ET.fromstring(data)
for item in args[1:]:
for data in tree.findall(item):
if item not in ret:
ret[item] = []
ret[item].append(data.text)
return ret
s = SolusVM("https://cp.catalysthost.com", "xxx-1TC6S", "8d1xxx522")
try:
flags = dict(item.split("=") for item in sys.argv[3].split(","))
except IndexError:
flags = {}
print s.api(sys.argv[1], *sys.argv[2].split(","), **(flags) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment