diff --git a/deckhand/objects/__init__.py b/deckhand/objects/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/deckhand/objects/base.py b/deckhand/objects/base.py new file mode 100644 index 00000000..5b175b9a --- /dev/null +++ b/deckhand/objects/base.py @@ -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 diff --git a/deckhand/objects/document.py b/deckhand/objects/document.py new file mode 100644 index 00000000..9f526113 --- /dev/null +++ b/deckhand/objects/document.py @@ -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 diff --git a/deckhand/objects/fields.py b/deckhand/objects/fields.py new file mode 100644 index 00000000..0f99f27c --- /dev/null +++ b/deckhand/objects/fields.py @@ -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)