diff --git a/barbicanclient/base.py b/barbicanclient/base.py index ca157a13..5e63f882 100644 --- a/barbicanclient/base.py +++ b/barbicanclient/base.py @@ -29,7 +29,7 @@ class BaseEntityManager(object): def total(self): """ - Returns the toatl number of entities stored in Barbican. + Returns the total number of entities stored in Barbican. """ href = '{0}/{1}'.format(self.api.base_url, self.entity) params = {'limit': 0, 'offset': 0} diff --git a/barbicanclient/client.py b/barbicanclient/client.py index 81d2c6f9..414c4252 100644 --- a/barbicanclient/client.py +++ b/barbicanclient/client.py @@ -21,6 +21,7 @@ from barbicanclient.openstack.common import log as logging from barbicanclient.openstack.common.gettextutils import _ from barbicanclient import orders from barbicanclient import secrets +from barbicanclient import verifications LOG = logging.getLogger(__name__) @@ -50,7 +51,8 @@ class HTTPAuthError(HTTPError): class Client(object): - def __init__(self, auth_plugin=None, endpoint=None, tenant_id=None): + def __init__(self, session=None, auth_plugin=None, + endpoint=None, tenant_id=None): """ Barbican client object used to interact with barbican service. @@ -65,7 +67,7 @@ class Client(object): """ LOG.debug(_("Creating Client object")) - self._session = requests.Session() + self._session = session or requests.Session() self.auth_plugin = auth_plugin if self.auth_plugin is not None: @@ -90,6 +92,7 @@ class Client(object): self.base_url = '{0}/{1}'.format(self._barbican_url, self._tenant_id) self.secrets = secrets.SecretManager(self) self.orders = orders.OrderManager(self) + self.verifications = verifications.VerificationManager(self) def get(self, href, params=None): headers = {'Accept': 'application/json'} diff --git a/barbicanclient/orders.py b/barbicanclient/orders.py index 27d52a59..11dc0090 100644 --- a/barbicanclient/orders.py +++ b/barbicanclient/orders.py @@ -28,6 +28,9 @@ class Order(object): Builds an order object from a dictionary. """ self.order_ref = order_dict['order_ref'] + + self.error_status_code = order_dict.get('error_status_code', None) + self.error_reason = order_dict.get('error_reason', None) self.status = order_dict.get('status') self.created = timeutils.parse_isotime(order_dict['created']) if order_dict.get('updated') is not None: @@ -37,13 +40,19 @@ class Order(object): self.secret_ref = order_dict.get('secret_ref') def __str__(self): - return ("Order - order href: {0}\n" + strg = ("Order - order href: {0}\n" " secret href: {1}\n" " created: {2}\n" " status: {3}\n" - .format(self.order_ref, self.secret_ref, - self.created, self.status) - ) + ).format(self.order_ref, self.secret_ref, + self.created, self.status) + + if self.error_status_code: + strg = ''.join([strg, (" error_status_code: {0}\n" + " error_reason: {1}\n" + ).format(self.error_status_code, + self.error_reason)]) + return strg def __repr__(self): return 'Order(order_ref={0})'.format(self.order_ref) @@ -120,7 +129,7 @@ class OrderManager(base.BaseEntityManager): :param offset: Offset orders to begin list :returns: list of Order objects """ - LOG.debug('Listing orders - offest {0} limit {1}'.format(offset, + LOG.debug('Listing orders - offset {0} limit {1}'.format(offset, limit)) href = '{0}/{1}'.format(self.api.base_url, self.entity) params = {'limit': limit, 'offset': offset} diff --git a/barbicanclient/test/test_client.py b/barbicanclient/test/test_client.py index c4645685..f3298de3 100644 --- a/barbicanclient/test/test_client.py +++ b/barbicanclient/test/test_client.py @@ -91,3 +91,15 @@ class WhenTestingClient(unittest.TestCase): c = client.Client(auth_plugin=self.fake_auth) with self.assertRaises(client.HTTPClientError): c._check_status_code(resp) + + +class WhenTestingSecretsResource(unittest.TestCase): + def setUp(self): + self.endpoint = 'https://localhost:9311/v1/' + self.tenant_id = 'tenant_id' + + self.session = mock.MagicMock() + self.session.read.return_value = self.json + + def test_should_create_secret + diff --git a/barbicanclient/verifications.py b/barbicanclient/verifications.py new file mode 100644 index 00000000..656a54f8 --- /dev/null +++ b/barbicanclient/verifications.py @@ -0,0 +1,145 @@ +# Copyright (c) 2013 Rackspace, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from barbicanclient import base +from barbicanclient.openstack.common.gettextutils import _ +from barbicanclient.openstack.common import log as logging +from barbicanclient.openstack.common import timeutils + + +LOG = logging.getLogger(__name__) + + +class Verification(object): + + def __init__(self, verif_dict): + """ + Builds a verification object from a dictionary. + """ + self.verif_ref = verif_dict['verification_ref'] + self.resource_type = verif_dict['resource_type'] + self.resource_ref = verif_dict['resource_ref'] + self.resource_action = verif_dict['resource_action'] + self.impersonation_allowed = verif_dict['impersonation_allowed'] + self.is_verified = verif_dict.get('is_verified', False) + + self.error_status_code = verif_dict.get('error_status_code', None) + self.error_reason = verif_dict.get('error_reason', None) + self.status = verif_dict.get('status') + self.created = timeutils.parse_isotime(verif_dict['created']) + if verif_dict.get('updated') is not None: + self.updated = timeutils.parse_isotime(verif_dict['updated']) + else: + self.updated = None + + def __str__(self): + strg = ("Verification - verification href: {0}\n" + " resource_type: {1}\n" + " resource_ref: {2}\n" + " resource_action: {3}\n" + " impersonation_allowed: {4}\n" + " is_verified: {5}\n" + " created: {6}\n" + " status: {7}\n" + ).format(self.verif_ref, + self.resource_type, + self.resource_ref, + self.resource_action, + self.impersonation_allowed, + self.is_verified, + self.created, + self.status) + + if self.error_status_code: + strg = ''.join([strg, (" error_status_code: {0}\n" + " error_reason: {1}\n" + ).format(self.error_status_code, + self.error_reason)]) + return strg + + def __repr__(self): + return 'Verification(verification_ref={0})'.format(self.verif_ref) + + +class VerificationManager(base.BaseEntityManager): + + def __init__(self, api): + super(VerificationManager, self).__init__(api, 'verifications') + + def create(self, + resource_type=None, + resource_ref=None, + resource_action=None, + impersonation_allowed=None): + """ + Creates a new Verification in Barbican + + :param resource_type: Type of resource to verify + :param resource_ref: Reference to resource + :param resource_action: Action to be performed on or with the resource + :param impersonation_allowed: True if users/projects interacting + : with resource can be impersonated + :returns: Verification href for the created verification + """ + LOG.debug(_("Creating verification")) + + verif_dict = {'resource_type': resource_type, + 'resource_ref': resource_ref, + 'resource_action': resource_action, + 'impersonation_allowed': impersonation_allowed} + self._remove_empty_keys(verif_dict) + + LOG.debug(_("Request body: {0}").format(verif_dict)) + + resp = self.api.post(self.entity, verif_dict) + return resp['verification_ref'] + + def get(self, verif_ref): + """ + Returns a verification object + + :param verif_ref: The href for the verification instance + """ + LOG.debug(_("Getting verification - " + "Verification href: {0}").format(verif_ref)) + if not verif_ref: + raise ValueError('verif_ref is required.') + resp = self.api.get(verif_ref) + return Verification(resp) + + def delete(self, verif_ref): + """ + Deletes a verification + + :param verif_ref: The href for the verification instance + """ + if not verif_ref: + raise ValueError('verif_ref is required.') + self.api.delete(verif_ref) + + def list(self, limit=10, offset=0): + """ + Lists all verifications for the tenant + + :param limit: Max number of verifications returned + :param offset: Offset verifications to begin list + :returns: list of Verification objects + """ + LOG.debug('Listing verifications - ' + '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(href, params) + + return [Verification(o) for o in resp['verifications']]