diff --git a/barbican/api/app.py b/barbican/api/app.py index f7436cda2..398ea905d 100644 --- a/barbican/api/app.py +++ b/barbican/api/app.py @@ -21,6 +21,8 @@ import falcon from barbican.api.resources import VersionResource from barbican.api.resources import TenantsResource, TenantResource +from barbican.api.resources import CSRsResource, CSRResource +from barbican.api.resources import CertificatesResource, CertificateResource # TBD: from barbican.api.resources import SecretsResource, SecretResource # Resources @@ -31,7 +33,7 @@ TENANT = TenantResource() # TBD: SECRET = SecretResource() CSRS = CSRsResource() CSR = CSRResource() -CERTS = CertifcatesResource() +CERTS = CertificatesResource() CERT = CertificateResource() # Routing diff --git a/barbican/api/resources.py b/barbican/api/resources.py index e9093e28d..02eadc592 100644 --- a/barbican/api/resources.py +++ b/barbican/api/resources.py @@ -4,8 +4,8 @@ import logging from barbican.version import __version__ from barbican.api import ApiResource, load_body, abort -from barbican.model.models import Tenant, States -from barbican.model.repositories import TenantRepo +from barbican.model.models import Tenant, States, CSR, Certificate +from barbican.model.repositories import TenantRepo, CSRRepo, CertificateRepo from barbican.queue.resources import QueueResource, StartCSRMessage from barbican.common import config diff --git a/barbican/model/repositories.py b/barbican/model/repositories.py index b93725f67..960bebc68 100644 --- a/barbican/model/repositories.py +++ b/barbican/model/repositories.py @@ -415,3 +415,51 @@ class TenantRepo(BaseRepo): def _do_validate(self, values): """Sub-class hook: validate values.""" pass + + +class CSRRepo(BaseRepo): + """Repository for the CSR entity.""" + + def _do_entity_name(self): + """Sub-class hook: return entity name, such as for debugging.""" + return "CSR" + + def _do_create_instance(self): + return models.CSR() + + def _do_build_query_by_name(self, name, session): + """Sub-class hook: find entity by name.""" + raise TypeError(_("No support for retrieving by 'name' a CSR record.")) + + def _do_build_get_query(self, entity_id, session): + """Sub-class hook: build a retrieve query.""" + return session.query(models.CSR)\ + .filter_by(id=entity_id) + + def _do_validate(self, values): + """Sub-class hook: validate values.""" + pass + + +class CertificateRepo(BaseRepo): + """Repository for the Certificate entity.""" + + def _do_entity_name(self): + """Sub-class hook: return entity name, such as for debugging.""" + return "Certificate" + + def _do_create_instance(self): + return models.Certificate() + + def _do_build_query_by_name(self, name, session): + """Sub-class hook: find entity by name.""" + raise TypeError(_("No support for retrieving by 'name' a Certificate record.")) + + def _do_build_get_query(self, entity_id, session): + """Sub-class hook: build a retrieve query.""" + return session.query(models.Certificate)\ + .filter_by(id=entity_id) + + def _do_validate(self, values): + """Sub-class hook: validate values.""" + pass diff --git a/barbican/queue/__init__.py b/barbican/queue/__init__.py index 5955c4df0..86502a556 100644 --- a/barbican/queue/__init__.py +++ b/barbican/queue/__init__.py @@ -19,6 +19,7 @@ Queue objects for Cloudkeep's Barbican from oslo.config import cfg from barbican.common import config +from barbican.openstack.common.gettextutils import _ queue_opts = [ cfg.StrOpt('queue_api', default='barbican.queue.simple', @@ -26,4 +27,4 @@ queue_opts = [ ] CONF = cfg.CONF -CONF.register_opts(queue_opts, group='queue') +CONF.register_opts(queue_opts, group='queue') \ No newline at end of file diff --git a/barbican/queue/resources.py b/barbican/queue/resources.py index 513d724ef..86cfafa8f 100644 --- a/barbican/queue/resources.py +++ b/barbican/queue/resources.py @@ -17,12 +17,13 @@ Queue Resources related objects and functions. """ from oslo.config import cfg +from barbican.openstack.common import importutils CONF = cfg.CONF def get_queue_api(): - return importutils.import_module(CONF.queue_api) + return importutils.import_module(CONF.queue.queue_api) class StartCSRMessage(object): diff --git a/barbican/tests/api/resources_test.py b/barbican/tests/api/resources_test.py index 38d5b4e24..51d4aede3 100644 --- a/barbican/tests/api/resources_test.py +++ b/barbican/tests/api/resources_test.py @@ -1,6 +1,6 @@ from datetime import datetime from barbican.api.resources import * -from barbican.model.models import Tenant +from barbican.model.models import * from barbican.common import config from mock import MagicMock @@ -59,7 +59,8 @@ class WhenCreatingTenantsUsingTenantsResource(unittest.TestCase): self.resource.on_post(self.req, self.resp) self.tenant_repo.find_by_name.assert_called_once_with(name=self.username, suppress_exception=True) - # TBD: Make this work: self.tenant_repo.create_from.assert_called_once_with(unittest.mock.ANY) + args, kwargs = self.tenant_repo.create_from.call_args + assert isinstance(args[0], Tenant) def test_should_throw_exception_for_tenants_that_exist(self): self.tenant_repo.find_by_name.return_value = Tenant() @@ -110,5 +111,74 @@ class WhenGettingOrDeletingTenantUsingTenantResource(unittest.TestCase): self.resource.on_delete(self.req, self.resp, self.tenant.id) +class WhenCreatingCSRsUsingTenantsResource(unittest.TestCase): + + def setUp(self): + self.requestor = 'requestor1234' + self.tenant_id = 'tenant1234' + + self.csr_repo = MagicMock() + self.csr_repo.create_from.return_value = None + + self.queue_resource = MagicMock() + self.queue_resource.send.return_value = None + + self.stream = MagicMock() + self.stream.read.return_value = u'{ "requestor" : "%s" }' % self.requestor + + self.req = MagicMock() + self.req.stream = self.stream + + self.resp = MagicMock() + self.resource = CSRsResource(self.csr_repo, self.queue_resource) + + def test_should_add_new_csr(self): + self.resource.on_post(self.req, self.resp, self.tenant_id) + + args, kwargs = self.csr_repo.create_from.call_args + assert isinstance(args[0], CSR) + + +class WhenGettingOrDeletingCSRUsingCSRResource(unittest.TestCase): + + def setUp(self): + self.tenant_id = 'tenant1234' + self.requestor = 'requestor1234' + self.csr = CSR() + self.csr.id = "id1" + self.csr.requestor = self.requestor + + self.csr_repo = MagicMock() + self.csr_repo.get.return_value = self.csr + self.csr_repo.delete_entity.return_value = None + + self.req = MagicMock() + self.resp = MagicMock() + self.resource = CSRResource(self.csr_repo) + + def test_should_get_csr(self): + self.resource.on_get(self.req, self.resp, self.tenant_id, self.csr.id) + + self.csr_repo.get.assert_called_once_with(entity_id=self.csr.id) + + def test_should_delete_csr(self): + self.resource.on_delete(self.req, self.resp, self.tenant_id, self.csr.id) + + self.csr_repo.get.assert_called_once_with(entity_id=self.csr.id) + self.csr_repo.delete_entity.assert_called_once_with(self.csr) + + def test_should_throw_exception_for_get_when_csr_not_found(self): + self.csr_repo.get.side_effect = exception.NotFound("Test not found exception") + + with self.assertRaises(exception.NotFound): + self.resource.on_get(self.req, self.resp, self.tenant_id, self.csr.id) + + def test_should_throw_exception_for_delete_when_csr_not_found(self): + self.csr_repo.get.side_effect = exception.NotFound("Test not found exception") + + with self.assertRaises(exception.NotFound): + self.resource.on_delete(self.req, self.resp, self.tenant_id, self.csr.id) + + if __name__ == '__main__': unittest.main() diff --git a/barbican/worker/__init__.py b/barbican/worker/__init__.py index fd5b12de5..26775aa08 100644 --- a/barbican/worker/__init__.py +++ b/barbican/worker/__init__.py @@ -19,6 +19,7 @@ Worker objects for Cloudkeep's Barbican from oslo.config import cfg from barbican.common import config +from barbican.openstack.common.gettextutils import _ worker_opts = [ cfg.StrOpt('worker_api', default='barbican.worker.simple', diff --git a/barbican/worker/resources.py b/barbican/worker/resources.py index 5e73515ae..196f2187d 100644 --- a/barbican/worker/resources.py +++ b/barbican/worker/resources.py @@ -17,12 +17,13 @@ Queue Resources related objects and functions. """ from oslo.config import cfg +from barbican.openstack.common import importutils CONF = cfg.CONF def get_worker_api(): - return importutils.import_module(CONF.worker_api) + return importutils.import_module(CONF.worker.worker_api) class WorkerResource(object): """Handles Queue related requests""" diff --git a/barbican/worker/simple.py b/barbican/worker/simple.py index 79f11ff92..8133f13e9 100644 --- a/barbican/worker/simple.py +++ b/barbican/worker/simple.py @@ -18,8 +18,13 @@ Simple Worker API implementation. """ from barbican.queue.resources import StartCSRMessage + class StartCSRProcessor(object): - print "Processing CSR with ID = ", csr_id + """Process the start of CSR processing.""" + + def process(self, message): + print "Processing CSR with ID = ", message.csr_id + PROCESSES = {StartCSRMessage : StartCSRProcessor()} diff --git a/openstack-common.conf b/openstack-common.conf index eb05774a0..ef03426c3 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=gettextutils,jsonutils,log,local,notifier,timeutils,uuidutils +modules=gettextutils,jsonutils,log,local,notifier,timeutils,uuidutils,importutils # The base module to hold the copy of openstack.common base=barbican