client retrieval of freezer api endpoint

The api client queries keystone to obtain the freezer api endpoint,
provided that the freezer api service and endpoint have been registered in
keystone.

An optional parameter to specify the api endpoint is supported

Change-Id: I6626a60d1fd5d18a59376165e94c789832865ae0
Implements: blueprint freezer-apiclient-endpoint
This commit is contained in:
Fabrizio Vanni 2015-04-16 12:00:36 +01:00
parent 2d1323f51f
commit 1e101ddf1a
3 changed files with 81 additions and 47 deletions

View File

@ -29,10 +29,13 @@ class BackupsManager(object):
def __init__(self, client):
self.client = client
self.endpoint = self.client.endpoint + 'backups/'
self.headers = {'X-Auth-Token': client.token}
self.endpoint = self.client.api_endpoint + '/v1/backups/'
def create(self, backup_metadata, username=None, tenant_name=None):
@property
def headers(self):
return {'X-Auth-Token': self.client.auth_token}
def create(self, backup_metadata):
r = requests.post(self.endpoint,
data=json.dumps(backup_metadata),
headers=self.headers)
@ -42,14 +45,14 @@ class BackupsManager(object):
backup_id = r.json()['backup_id']
return backup_id
def delete(self, backup_id, username=None, tenant_name=None):
def delete(self, backup_id):
endpoint = self.endpoint + backup_id
r = requests.delete(endpoint, headers=self.headers)
if r.status_code != 204:
raise exceptions.MetadataDeleteFailure(
"[*] Error {0}".format(r.status_code))
def list(self, username=None, tenant_name=None):
def list(self):
r = requests.get(self.endpoint, headers=self.headers)
if r.status_code != 200:
raise exceptions.MetadataGetFailure(
@ -57,7 +60,7 @@ class BackupsManager(object):
return r.json()['backups']
def get(self, backup_id, username=None, tenant_name=None):
def get(self, backup_id):
endpoint = self.endpoint + backup_id
r = requests.get(endpoint, headers=self.headers)
if r.status_code == 200:

View File

@ -22,14 +22,15 @@ Hudson (tjh@cryptsoft.com).
import os
import sys
from openstackclient.identity import client as os_client
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir, os.pardir, os.pardir))
if os.path.exists(os.path.join(possible_topdir, 'freezer', '__init__.py')):
sys.path.insert(0, possible_topdir)
import keystoneclient
from freezer.apiclient.backups import BackupsManager
import exceptions
class Client(object):
@ -40,26 +41,66 @@ class Client(object):
password=None,
tenant_name=None,
auth_url=None,
endpoint=None,
session=None):
if endpoint is None:
raise Exception('Missing endpoint information')
self.endpoint = endpoint
if token is not None:
# validate the token ?
self.token = token
elif session is not None:
pass
# TODO: handle session auth
# assert isinstance(session, keystoneclient.session.Session)
else:
self.username = username
self.tenant_name = tenant_name
kc = keystoneclient.v2_0.client.Client(
username=username,
password=password,
tenant_name=tenant_name,
auth_url=auth_url)
self.token = kc.auth_token
session=None,
api_endpoint=None):
self.version = version
self.token = token
self.username = username
self.tenant_name = tenant_name
self.password = password
self.auth_url = auth_url
self._api_endpoint = api_endpoint
self.session = session
self._auth = None
self.backups = BackupsManager(self)
def _update_api_endpoint(self):
services = self.auth.services.list()
try:
freezer_service = next(x for x in services if x.name == 'freezer')
except:
raise exceptions.AuthFailure(
'freezer service not found in services list')
endpoints = self.auth.endpoints.list()
try:
freezer_endpoint =\
next(x for x in endpoints
if x.service_id == freezer_service.id)
except:
raise exceptions.AuthFailure(
'freezer endpoint not found in endpoint list')
self._api_endpoint = freezer_endpoint.publicurl
@property
def auth(self):
if self._auth is None:
if self.username and self.password:
self._auth = os_client.IdentityClientv2(
auth_url=self.auth_url,
username=self.username,
password=self.password,
tenant_name=self.tenant_name)
elif self.token:
self._auth = os_client.IdentityClientv2(
endpoint=self.auth_url,
token=self.token)
else:
raise exceptions.AuthFailure("Missing auth credentials")
return self._auth
@property
def auth_token(self):
return self.auth.auth_token
@property
def api_endpoint(self):
if self._api_endpoint is None:
self._update_api_endpoint()
return self._api_endpoint
def api_exists(self):
try:
if self.api_endpoint is not None:
return True
except:
return False

View File

@ -20,27 +20,17 @@ Hudson (tjh@cryptsoft.com).
"""
class FreezerClientException(Exception):
"""
Base Freezer API Exception
"""
message = ("Unknown exception occurred")
def __init__(self, message=None, *args, **kwargs):
if not message:
message = self.message
message = message % kwargs
Exception.__init__(self, message)
class MetadataCreationFailure(FreezerClientException):
class MetadataCreationFailure(Exception):
message = "Metadata creation failed: %reason"
class MetadataGetFailure(FreezerClientException):
class MetadataGetFailure(Exception):
message = "Metadata read failed: %reason"
class MetadataDeleteFailure(FreezerClientException):
class MetadataDeleteFailure(Exception):
message = "Metadata deletion failed: %reason"
class AuthFailure(Exception):
message = "Authentication Error: %reason"