Skip to content

Commit

Permalink
refactored tklbam to use pycurl-wrapper's API class
Browse files Browse the repository at this point in the history
Conflicts:

	debian/control
	hub.py
  • Loading branch information
lirazsiri committed Dec 11, 2012
1 parent 34ed78c commit 5e77fe8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion debian/control
Expand Up @@ -7,6 +7,6 @@ Standards-Version: 3.6.1

Package: tklbam
Architecture: all
Depends: python (>= 2.4), tklbam-squid, tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.3), turnkey-version, python-crypto, pycurl-wrapper, python-simplejson, ca-certificates, ntpdate
Depends: python (>= 2.4), tklbam-squid, tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.3), turnkey-version, python-crypto, pycurl-wrapper (>= 1.1), python-simplejson, ca-certificates, ntpdate
Section: misc
Description: TurnKey Linux Backup and Migration agent
53 changes: 19 additions & 34 deletions hub.py
Expand Up @@ -82,11 +82,10 @@

import base64
import tempfile
import simplejson as json
from datetime import datetime

import executil
from pycurl_wrapper import Curl
from pycurl_wrapper import API as _API
from utils import AttrDict

class Error(Exception):
Expand All @@ -103,30 +102,19 @@ def __init__(self, desc=DESC):
class InvalidBackupError(Error):
pass

class API:
ALL_OK = 200
CREATED = 201
DELETED = 204
class API(_API):
def request(self, method, url, attrs={}, headers={}):
try:
return _API.request(self, method, url, attrs, headers)
except self.Error, e:
if e.name == "BackupRecord.NotFound":
raise InvalidBackupError(e.description)

@classmethod
def request(cls, method, url, attrs={}, headers={}):
c = Curl(url, headers)
func = getattr(c, method.lower())
func(attrs)

if not c.response_code in (cls.ALL_OK, cls.CREATED, cls.DELETED):
name, description = c.response_data.split(":", 1)

if name == "BackupRecord.NotFound":
raise InvalidBackupError(description)

if name in ("BackupAccount.NotSubscribed",
"BackupAccount.NotFound"):
if e.name in ("BackupAccount.NotSubscribed",
"BackupAccount.NotFound"):
raise NotSubscribedError()

raise Error(c.response_code, name, description)

return json.loads(c.response_data)
raise

class BackupRecord(AttrDict):
@staticmethod
Expand All @@ -153,38 +141,35 @@ def __init__(self, response):
# no interface for this in tklbam, so not returned from hub
self.sessions = []

AttrDict.__init__(self)

class Credentials(AttrDict):
def __init__(self, response):
self.accesskey = response['accesskey']
self.secretkey = response['secretkey']
self.usertoken = response['usertoken']
self.producttoken = response['producttoken']

AttrDict.__init__(self)

class Backups:
API_URL = os.getenv('TKLBAM_APIURL', 'https://hub.turnkeylinux.org/api/backup/')
API_HEADERS = {'Accept': 'application/json'}

Error = Error

def __init__(self, subkey=None):
if subkey is None:
raise Error("no APIKEY - tklbam not initialized")

self.subkey = subkey
self.api = API()

def _api(self, method, uri, attrs={}):
headers = self.API_HEADERS.copy()
headers['subkey'] = str(self.subkey)

# workaround: http://redmine.lighttpd.net/issues/1017
if method == "PUT":
headers['Expect'] = ''

return API.request(method, self.API_URL + uri, attrs, headers)
headers = { 'subkey': str(self.subkey) }
return self.api.request(method, self.API_URL + uri, attrs, headers)

@classmethod
def get_sub_apikey(cls, apikey):
response = API.request('GET', cls.API_URL + 'subkey/', {'apikey': apikey}, cls.API_HEADERS)
response = API().request('GET', cls.API_URL + 'subkey/', {'apikey': apikey})
return response['subkey']

def get_credentials(self):
Expand Down

0 comments on commit 5e77fe8

Please sign in to comment.