Last of the changes to Client

This commit is contained in:
Douglas Mendizabal
2013-09-04 11:50:10 -05:00
parent a8ba12f673
commit 8df8f80338
4 changed files with 51 additions and 23 deletions

View File

@@ -16,6 +16,7 @@
Base utilites to build API operation managers.
"""
class BaseEntityManager(object):
def __init__(self, api, entity):
self.api = api

View File

@@ -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):

View File

@@ -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']]

View File

@@ -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']]