Last active
August 29, 2015 13:59
-
-
Save cameron/10822484 to your computer and use it in GitHub Desktop.
Digital Ocean API — Python Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from triton import triton | |
def get_droplet(): | |
droplets = triton.droplets() | |
if len(droplets): | |
return droplets[0] | |
return triton.droplets.new() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import requests | |
import os | |
import re | |
class GetAPIClient(object): | |
def __init__(self, base_url): | |
self.base_url = base_url | |
self.url = '' | |
self.kwargs = {} | |
self.kwargXforms = {} | |
self.defaultKwargs = {} | |
def setDefaultKwargs(self, *endpoint, **defaultKwargs): | |
endpoint = len(endpoint) == 0 and '.*' or endpoint[0] | |
self.defaultKwargs[re.compile(endpoint)] = defaultKwargs | |
def applyDefaultKwargs(self): | |
for endpoint, defaults in self.defaultKwargs.iteritems(): | |
if endpoint.match(self.url): | |
self.kwargs.update(defaults) | |
def setKwargXform(self, *endpoint, **kwargXforms): | |
endpoint = len(endpoint) == 0 and '.*' or endpoint[0] | |
self.kwargXforms[re.compile(endpoint)] = kwargXforms | |
def applyKwargXforms(self): | |
for endpoint, xforms in self.kwargXforms.iteritems(): | |
if endpoint.match(self.url): | |
for k, xform in xforms.iteritems(): | |
xform(self.kwargs[k], self.kwargs) | |
def get(self, key=None): | |
self.applyDefaultKwargs() | |
self.applyKwargXforms() | |
res = requests.get(self.base_url + | |
self.url + | |
'?' + urllib.urlencode(self.kwargs)).json() | |
if res['status'] == 'ERROR': | |
raise Exception('GETAPI Error for url %s\n%s' % (self.url, res['message'])) | |
self.kwargs = {} | |
if not key: | |
key = self.url.split('/')[1] | |
if key not in res and key.endswith('s'): | |
key = key[:-1] | |
self.url = '' | |
return res[key] | |
def addSegment(self, segment): | |
if segment not in self.url: # nasty, dangerous hack to avoid weird second call to getattr | |
self.url += '/' + segment | |
def __getattr__(self, name): | |
self.addSegment(name) | |
return self | |
def __call__(self, *args, **kwargs): | |
if len(args) == len(kwargs) == 0: | |
return self.get() | |
for arg in args: | |
self.addSegment(arg) | |
kwargs and self.kwargs.update(kwargs) | |
return self | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests=2.2.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from getapi import GetAPIClient | |
import uuid | |
triton = GetAPIClient('https://api.digitalocean.com') | |
triton.setDefaultKwargs(client_id=os.environ['DO_CLIENT_ID'], | |
api_key=os.environ['DO_API_KEY']) | |
slugToSizeId = dict((s['slug'], s['id']) for s in triton.sizes()) | |
def toSizeId(size, kwargs): | |
global slugToSizeId | |
kwargs['size_id'] = slugToSizeId.get(size.lower()) | |
print kwargs['size_id'] | |
del kwargs['size'] | |
OCEAN_SSH_KEY_NAME = 'set-sail' | |
keys = [key for key in triton.ssh_keys() if OCEAN_SSH_KEY_NAME in key['name']] | |
OCEAN_SSH_KEY_ID = len(keys) and str(keys[0]['id']) or None | |
if not OCEAN_SSH_KEY_ID: | |
sshKey = open(os.path.expanduser('~/.ssh/id_rsa.pub')).read() | |
sshKeyId = str(triton.ssh_keys.new(name=OCEAN_SSH_KEY_NAME, | |
ssh_pub_key=sshKey)['id']) | |
def toSSHKeyIds(keys, kwargs): | |
global OCEAN_SSH_KEY_ID | |
for k in keys: | |
keys.append(triton.ssh_keys.add(name=str(uuid.uuid4()), ssh_pub_key=k)()['id']) | |
keys.append(OCEAN_SSH_KEY_ID) | |
kwargs['ssh_key_ids'] = ','.join(keys) | |
del kwargs['ssh_keys'] | |
triton.setDefaultKwargs('/droplets/new', | |
name='antelope', | |
size='512mb', | |
image_id='3104894', # docker on ubuntu | |
region_id=3, # sf | |
ssh_keys=[]) | |
triton.setKwargXform('/droplets/new', | |
ssh_keys=toSSHKeyIds, | |
size=toSizeId) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment