From 8df8f8033806e7d1004667a10d388bed6f9b9483 Mon Sep 17 00:00:00 2001 From: Douglas Mendizabal Date: Wed, 4 Sep 2013 11:50:10 -0500 Subject: [PATCH] Last of the changes to Client --- barbicanclient/base.py | 1 + barbicanclient/client.py | 31 +++++++++++++++++++++---------- barbicanclient/orders.py | 16 +++++++++++----- barbicanclient/secrets.py | 26 ++++++++++++++++++-------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/barbicanclient/base.py b/barbicanclient/base.py index 449b07a9..0736e5f9 100644 --- a/barbicanclient/base.py +++ b/barbicanclient/base.py @@ -16,6 +16,7 @@ Base utilites to build API operation managers. """ + class BaseEntityManager(object): def __init__(self, api, entity): self.api = api diff --git a/barbicanclient/client.py b/barbicanclient/client.py index c7159809..7e77eb32 100644 --- a/barbicanclient/client.py +++ b/barbicanclient/client.py @@ -102,6 +102,10 @@ class Client(object): self._check_status_code(resp) return resp.content + def delete(self, href): + resp = self._session.delete(href) + self._check_status_code(resp) + def post(self, path, data): url = '{0}/{1}/'.format(self.base_url, path) headers = {'content-type': 'application/json'} @@ -109,22 +113,29 @@ class Client(object): self._check_status_code(resp) return resp.json() - def delete(self, href): - resp = self._session.delete(href) - self._check_status_code(resp) - def _check_status_code(self, resp): status = resp.status_code LOG.debug('Response status {0}'.format(status)) if status == 401: - LOG.error('Auth error: {0}'.format(resp.content)) - raise HTTPAuthError('{0}'.format(resp.content)) + LOG.error('Auth error: {0}'.format(self._get_error_message(resp))) + raise HTTPAuthError('{0}'.format(self._get_error_message(resp))) if status >= 500: - LOG.error('5xx Server error: {0}'.format(resp.content)) - raise HTTPServerError('{0}'.format(resp.content)) + LOG.error('5xx Server error: {0}'.format( + self._get_error_message(resp) + )) + raise HTTPServerError('{0}'.format(self._get_error_message(resp))) if status >= 400: - LOG.error('4xx Client error: {0}'.format(resp.content)) - raise HTTPClientError('{0}'.format(resp.content)) + LOG.error('4xx Client error: {0}'.format( + self._get_error_message(resp) + )) + raise HTTPClientError('{0}'.format(self._get_error_message(resp))) + + def _get_error_message(self, resp): + try: + message = resp.json()['title'] + except ValueError: + message = resp.content + return message def env(*vars, **kwargs): diff --git a/barbicanclient/orders.py b/barbicanclient/orders.py index fb9bef8e..34ed191e 100644 --- a/barbicanclient/orders.py +++ b/barbicanclient/orders.py @@ -12,13 +12,10 @@ # implied. # See the License for the specific language governing permissions and # limitations under the License. -import urlparse - from barbicanclient import base from barbicanclient.openstack.common.gettextutils import _ from barbicanclient.openstack.common import log as logging from barbicanclient.openstack.common import timeutils -from barbicanclient import secrets LOG = logging.getLogger(__name__) @@ -99,7 +96,7 @@ class OrderManager(base.BaseEntityManager): :param order_ref: The href for the order """ - LOG.debug(_("Getting order - Order href: {0}").format(secret_ref)) + LOG.debug(_("Getting order - Order href: {0}").format(order_ref)) if not order_ref: raise ValueError('order_ref is required.') resp = self.api.get(order_ref) @@ -116,7 +113,16 @@ class OrderManager(base.BaseEntityManager): self.api.delete(order_ref) def list(self, limit=10, offset=0): + """ + Lists all orders for the tenant + + :param limit: Max number of orders returned + :param offset: Offset orders to begin list + :returns: list of Order objects + """ + LOG.debug('Listing orders - offest {0} limit {1}').format(offset, limit) + href = '{0}/{1}'.format(self.api.base_url, self.entity) params = {'limit': limit, 'offset': offset} - resp = self.api.get(self.entity, params) + resp = self.api.get(href, params) return [Order(o) for o in resp['orders']] diff --git a/barbicanclient/secrets.py b/barbicanclient/secrets.py index 4bdada65..f4dda80e 100644 --- a/barbicanclient/secrets.py +++ b/barbicanclient/secrets.py @@ -12,8 +12,6 @@ # implied. # See the License for the specific language governing permissions and # limitations under the License. -import urlparse - from barbicanclient import base from barbicanclient.openstack.common import log as logging from barbicanclient.openstack.common.timeutils import parse_isotime @@ -118,7 +116,7 @@ class SecretManager(base.BaseEntityManager): def get(self, secret_ref): """ - Returns a Secret object with information about the secret. + Returns a Secret object with metadata about the secret. :param secret_ref: The href for the secret """ @@ -127,16 +125,21 @@ class SecretManager(base.BaseEntityManager): resp = self.api.get(secret_ref) return Secret(resp) - def decrypt(self, secret_ref, content_type): + def decrypt(self, secret_ref, content_type=None): """ Returns the actual secret data stored in Barbican. :param secret_ref: The href for the secret - :param content_type: The content_type of the secret + :param content_type: The content_type of the secret, if not + provided, the client will fetch the secret meta and use the + default content_type to decrypt the secret :returns: secret data """ - if not all([secret_ref, content_type]): - raise ValueError('secret_ref and content_type are required.') + if not secret_ref: + raise ValueError('secret_ref is required.') + if not content_type: + secret = self.get(secret_ref) + content_type = secret.content_types['default'] headers = {'Accept': content_type} return self.api.get_raw(secret_ref, headers) @@ -151,10 +154,17 @@ class SecretManager(base.BaseEntityManager): self.api.delete(secret_ref) def list(self, limit=10, offset=0): + """ + List all secrets for the tenant + :param limit: Max number of secrets returned + :param offset: Offset secrets to begin list + :returns: list of Secret metadata objects + """ LOG.debug('Listing secrets - offset {0} limit {1}'.format(offset, limit)) + href = '{0}/{1}'.format(self.api.base_url, self.entity) params = {'limit': limit, 'offset': offset} - resp = self.api.get(self.entity, params) + resp = self.api.get(href, params) return [Secret(s) for s in resp['secrets']]