Cleaned up some logic, added exception handling to document creation.
This commit is contained in:
parent
2fd01fd9b4
commit
8cf795b7a1
|
@ -23,6 +23,7 @@ from deckhand.control import base as api_base
|
|||
from deckhand.control import documents
|
||||
from deckhand.control import secrets
|
||||
from deckhand.db.sqlalchemy import api as db_api
|
||||
from deckhand.db.sqlalchemy import models as db_models
|
||||
|
||||
CONF = cfg.CONF
|
||||
LOG = None
|
||||
|
@ -52,12 +53,17 @@ def __setup_logging():
|
|||
LOG.debug('Initiated Deckhand logging.')
|
||||
|
||||
|
||||
def __setup_db():
|
||||
db_models.register_models(db_api.get_engine())
|
||||
|
||||
|
||||
def start_api(state_manager=None):
|
||||
"""Main entry point for initializing the Deckhand API service.
|
||||
|
||||
Create routes for the v1.0 API and sets up logging.
|
||||
"""
|
||||
__setup_logging()
|
||||
__setup_db()
|
||||
|
||||
control_api = falcon.API(request_type=api_base.DeckhandRequest)
|
||||
|
||||
|
|
|
@ -97,8 +97,6 @@ def clear_db_env():
|
|||
|
||||
def document_create(context, values, session=None):
|
||||
"""Create a document."""
|
||||
models.register_models(get_engine())
|
||||
|
||||
values = values.copy()
|
||||
session = session or get_session()
|
||||
with session.begin():
|
||||
|
|
|
@ -106,7 +106,8 @@ class Document(BASE, DeckhandBase):
|
|||
|
||||
id = Column(String(36), primary_key=True,
|
||||
default=lambda: str(uuid.uuid4()))
|
||||
revision_index = Column(Integer, nullable=False)
|
||||
revision_index = Column(String(36), nullable=False,
|
||||
default=lambda: str(uuid.uuid4()))
|
||||
schema_version = Column(String(64), nullable=False)
|
||||
kind = Column(String(64), nullable=False)
|
||||
# NOTE: Do not define a maximum length for these JSON data below. However,
|
||||
|
|
|
@ -13,9 +13,46 @@
|
|||
# limitations under the License.
|
||||
|
||||
|
||||
class DeckhandException(Exception):
|
||||
"""Base Nova Exception
|
||||
To correctly use this class, inherit from it and define
|
||||
a 'msg_fmt' property. That msg_fmt will get printf'd
|
||||
with the keyword arguments provided to the constructor.
|
||||
"""
|
||||
msg_fmt = "An unknown exception occurred."
|
||||
code = 500
|
||||
|
||||
def __init__(self, message=None, **kwargs):
|
||||
self.kwargs = kwargs
|
||||
|
||||
if 'code' not in self.kwargs:
|
||||
try:
|
||||
self.kwargs['code'] = self.code
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if not message:
|
||||
try:
|
||||
message = self.msg_fmt % kwargs
|
||||
|
||||
except Exception:
|
||||
message = self.msg_fmt
|
||||
|
||||
self.message = message
|
||||
super(DeckhandException, self).__init__(message)
|
||||
|
||||
def format_message(self):
|
||||
return self.args[0]
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidFormat(ApiError):
|
||||
"""The YAML file is incorrectly formatted and cannot be read."""
|
||||
|
||||
|
||||
class DocumentExists(DeckhandException):
|
||||
msg_fmt = ("Document with kind %(kind)s and schemaVersion "
|
||||
"%(schema_version)s already exists.")
|
||||
|
|
|
@ -16,10 +16,12 @@
|
|||
# Models for deckhand
|
||||
#
|
||||
|
||||
from oslo_db import exception as db_exc
|
||||
from oslo_log import log as logging
|
||||
from oslo_versionedobjects import fields as ovo_fields
|
||||
|
||||
from deckhand.db.sqlalchemy import api as db_api
|
||||
from deckhand import errors
|
||||
from deckhand import objects
|
||||
from deckhand.objects import base
|
||||
from deckhand.objects import fields as deckhand_fields
|
||||
|
@ -48,7 +50,6 @@ class DocumentPayload(base.DeckhandPayloadBase):
|
|||
|
||||
def __init__(self, document):
|
||||
super(DocumentPayload, self).__init__()
|
||||
LOG.debug(document)
|
||||
self.populate_schema(document=document)
|
||||
|
||||
|
||||
|
@ -61,8 +62,7 @@ class Document(base.DeckhandPersistentObject, base.DeckhandObject):
|
|||
fields = {
|
||||
'id': ovo_fields.IntegerField(nullable=False, read_only=True),
|
||||
'document': ovo_fields.ObjectField('DocumentPayload', nullable=False),
|
||||
'revision_index': ovo_fields.NonNegativeIntegerField(nullable=False),
|
||||
'status': ovo_fields.StringField(nullable=False)
|
||||
'revision_index': ovo_fields.NonNegativeIntegerField(nullable=False)
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -71,18 +71,19 @@ class Document(base.DeckhandPersistentObject, base.DeckhandObject):
|
|||
self.obj_reset_changes()
|
||||
|
||||
def create(self, document):
|
||||
LOG.debug(document)
|
||||
self.document = DocumentPayload(document)
|
||||
|
||||
if not self.document.populated:
|
||||
return
|
||||
document_obj = DocumentPayload(document)
|
||||
|
||||
values = {
|
||||
'revision_index': 0,
|
||||
'schema_version': self.document.schema_version,
|
||||
'kind': self.document.kind,
|
||||
'doc_metadata': self.document.metadata,
|
||||
'data': self.document.data
|
||||
'schema_version': document_obj.schema_version,
|
||||
'kind': document_obj.kind,
|
||||
'doc_metadata': document_obj.metadata,
|
||||
'data': document_obj.data
|
||||
}
|
||||
|
||||
db_api.document_create(None, values)
|
||||
|
||||
try:
|
||||
db_api.document_create(None, values)
|
||||
except db_exc.DBDuplicateEntry as e:
|
||||
raise errors.DocumentExists(
|
||||
kind=values['kind'], schema_version=values['schema_version'])
|
||||
except Exception as e:
|
||||
raise db_exc.DBError(e)
|
||||
|
|
|
@ -22,12 +22,14 @@ from deckhand.control import base as api_base
|
|||
|
||||
class TestApi(testtools.TestCase):
|
||||
|
||||
@mock.patch.object(api, 'db_api', autospec=True)
|
||||
@mock.patch.object(api, 'db_models', autospec=True)
|
||||
@mock.patch.object(api, 'config', autospec=True)
|
||||
@mock.patch.object(api, 'secrets', autospec=True)
|
||||
@mock.patch.object(api, 'documents', autospec=True)
|
||||
@mock.patch.object(api, 'falcon', autospec=True)
|
||||
def test_start_api(self, mock_falcon, mock_documents, mock_secrets,
|
||||
mock_config):
|
||||
mock_config, mock_db_models, mock_db_api):
|
||||
mock_falcon_api = mock_falcon.API.return_value
|
||||
|
||||
result = api.start_api()
|
||||
|
@ -41,3 +43,5 @@ class TestApi(testtools.TestCase):
|
|||
mock.call('/api/v1.0/secrets', mock_secrets.SecretsResource())
|
||||
])
|
||||
mock_config.parse_args.assert_called_once_with()
|
||||
mock_db_models.register_models.assert_called_once_with(
|
||||
mock_db_api.get_engine())
|
||||
|
|
Loading…
Reference in New Issue