Deckhand initial ORM implementation

This commit adds the initial implementation for the Deckhand ORM.
It is fashioned after Drydock's ORM, which in turn follows
OpenStack conventions.

This commit specifically:
  - creates the deckhand.objects package
  - includes base ORM classes
  - includes document ORM class which consists of the following
    fields:
      - blob: the document payload itself stored as an object
              that adheres to the bare minimum schema of
              schemaVersion, kind, metadata and data.
      - name: the name of the document, nullable
      - revision_index: the revision number for the document
      - status: the status of the document
  - includes basic fields for documents, i.e. what status
    the document is in
This commit is contained in:
Felipe Monteiro 2017-07-13 16:44:50 +01:00
parent 5d4be4cddd
commit caf52cbe63
4 changed files with 192 additions and 0 deletions

View File

85
deckhand/objects/base.py Normal file
View File

@ -0,0 +1,85 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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.
import datetime
from oslo_versionedobjects import base
from oslo_versionedobjects import fields as obj_fields
import Deckhand_provisioner.objects as objects
class DeckhandObjectRegistry(base.VersionedObjectRegistry):
# Steal this from Cinder to bring all registered objects
# into the Deckhand_provisioner.objects namespace.
def registration_hook(self, cls, index):
setattr(objects, cls.obj_name(), cls)
class DeckhandObject(base.VersionedObject):
# Version 1.0: Initial version
VERSION = '1.0'
OBJ_PROJECT_NAMESPACE = 'deckhand.objects'
def obj_load_attr(self, attrname):
if attrname in self.fields.keys():
setattr(self, attrname, None)
else:
raise ValueError("Unknown field %s." % (attrname))
def obj_to_simple(self):
"""
Create a simple primitive representation of this object excluding
all the versioning stuff. Used to serialize an object for public
consumption, not intended to be deserialized by OVO.
"""
primitive = dict()
primitive['model_type'] = self.__class__.__name__
primitive['model_version'] = self.VERSION
for name, field in self.fields.items():
if self.obj_attr_is_set(name):
value = getattr(self, name)
if (hasattr(value, 'obj_to_simple') and
callable(value.obj_to_simple)):
primitive[name] = value.obj_to_simple()
else:
value = field.to_primitive(self, name, value)
if value is not None:
primitive[name] = value
return primitive
class DeckhandPersistentObject(base.VersionedObject):
fields = {
'created_at': obj_fields.DateTimeField(nullable=False),
'created_by': obj_fields.StringField(nullable=False),
'updated_at': obj_fields.DateTimeField(nullable=True),
'updated_by': obj_fields.StringField(nullable=True),
}
def set_create_fields(self, context):
self.created_at = datetime.datetime.now()
self.created_by = context.user
def set_update_fields(self, context):
self.updated_at = datetime.datetime.now()
self.updated_by = context.user

View File

@ -0,0 +1,74 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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.
#
# Models for drydock_provisioner
#
import oslo_versionedobjects.fields as ovo_fields
import deckhand.objects as objects
import deckhand.objects.base as base
import deckhand.objects.fields as fields
class DocumentPayload(base.NotificationPayloadBase):
SCHEMA = {
'schema_version': ('document', 'schema_version'),
'kind': ('document', 'uuid'),
'metadata': ('document', 'name'),
'data': ('document', 'hosts')
}
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'schema_version': fields.StringField(nullable=False),
'kind': fields.StringField(nullable=False),
'metadata': fields.DictOfStringsField(nullable=False),
'data': fields.DictOfStringsField(nullable=False)
}
def __init__(self, document):
super(DocumentPayload, self).__init__()
self.populate_schema(document=document)
@base.DeckhandObjectRegistry.register
class Document(base.DeckhandPersistentObject, base.DeckhandObject):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'blob': ovo_fields.ObjectField('DocumentPayload', nullable=False),
'name': ovo_fields.StringField(nullable=True),
'revision_index': ovo_fields.NonNegativeIntegerField(nullable=False),
'status': fields.DocumentField(nullable=False)
}
def __init__(self, **kwargs):
super(Document, self).__init__(**kwargs)
@property
def name(self):
return self.name
@property
def revision(self):
return self.revision_index
@property
def status(self):
return self.status

View File

@ -0,0 +1,33 @@
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
#
# 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 oslo_versionedobjects import fields
class BaseDeckhandEnum(fields.Enum):
def __init__(self):
super(BaseDeckhandEnum, self).__init__(valid_values=self.__class__.ALL)
class DocumentField(BaseDeckhandEnum):
# A list of potentially valid statuses for a Document to have. This list
# will evolve over time.
active = 'ACTIVE'
conflict_error = 'CONFLICT_ERROR'
merge_error = 'MERGE_ERROR'
substitute_error = 'SUBSTITUTE_ERROR'
warning = 'WARNING'
ALL = (active, conflict_error, merge_error, substitute_error, warning)