ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxm...@apache.org
Subject incubator-ariatosca git commit: 1. ModelApi completely delegates the calls to the driver 2. The driver handles any pointer related translating 3. The Model constructor builds the model recursively into a class based object 4. Tests are fixed [Forced Update!]
Date Mon, 21 Nov 2016 14:23:53 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/wip-storage 3753c9172 -> cc54639c6 (forced update)


1. ModelApi completely delegates the calls to the driver
2. The driver handles any pointer related translating
3. The Model constructor builds the model recursively into a class based object
4. Tests are fixed


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/cc54639c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/cc54639c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/cc54639c

Branch: refs/heads/wip-storage
Commit: cc54639c67159897ddfb187f66db582d022a335f
Parents: 24e8457
Author: mxmrlv <mxmrlv@gmail.com>
Authored: Mon Nov 21 15:45:59 2016 +0200
Committer: mxmrlv <mxmrlv@gmail.com>
Committed: Mon Nov 21 16:23:41 2016 +0200

----------------------------------------------------------------------
 aria/__init__.py                    |  2 ++
 aria/storage/__init__.py            |  6 ++--
 aria/storage/drivers.py             |  4 +--
 aria/storage/structures.py          | 38 +++++++++++++++++---
 tests/storage/__init__.py           | 62 ++++++++++++++++++--------------
 tests/storage/test_drivers.py       |  7 ++--
 tests/storage/test_model_storage.py |  5 +--
 tests/storage/test_models.py        |  6 ++--
 tests/storage/test_models_api.py    |  4 +--
 9 files changed, 87 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/aria/__init__.py
----------------------------------------------------------------------
diff --git a/aria/__init__.py b/aria/__init__.py
index f88fa69..ad84c37 100644
--- a/aria/__init__.py
+++ b/aria/__init__.py
@@ -67,7 +67,9 @@ def application_model_storage(driver):
     if driver not in _model_storage:
         _model_storage[driver] = ModelStorage(
             driver, model_classes=[
+                models.Relationship,
                 models.Node,
+                models.RelationshipInstance,
                 models.NodeInstance,
                 models.Plugin,
                 models.Blueprint,

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/aria/storage/__init__.py
----------------------------------------------------------------------
diff --git a/aria/storage/__init__.py b/aria/storage/__init__.py
index 245c6c3..9b17ffb 100644
--- a/aria/storage/__init__.py
+++ b/aria/storage/__init__.py
@@ -105,7 +105,7 @@ class ModelStorage(Storage):
         Registers the model type in the resource storage manager.
         :param model_cls: the model to register.
         """
-        model_name = generate_lower_name(model_cls)
+        model_name = _generate_lower_name(model_cls)
         model_api = _ModelApi(model_name, self.driver, model_cls)
         self.registered[model_name] = model_api
 
@@ -168,7 +168,7 @@ class _ModelApi(object):
             connection.store(
                 name=self.name,
                 entry_id=entry.id,
-                entry=entry.fields_dict,
+                entry=entry,
                 **kwargs)
 
     def delete(self, entry_id, **kwargs):
@@ -286,7 +286,7 @@ class ResourceApi(object):
                 **kwargs)
 
 
-def generate_lower_name(model_cls):
+def _generate_lower_name(model_cls):
     """
     Generates the name of the class from the class object. e.g. SomeClass -> some_class
     :param model_cls: the class to evaluate.

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/aria/storage/drivers.py
----------------------------------------------------------------------
diff --git a/aria/storage/drivers.py b/aria/storage/drivers.py
index 27c80fa..fc76fe0 100644
--- a/aria/storage/drivers.py
+++ b/aria/storage/drivers.py
@@ -257,7 +257,7 @@ class FileSystemModelDriver(ModelDriver, BaseFileSystemDriver):
         return '{cls.__name__}(directory={self.directory})'.format(
             cls=self.__class__, self=self)
 
-    def create(self, name):
+    def create(self, name, **kwargs):
         """
         Create directory in storage by path.
         tries to create the root directory as well.
@@ -345,7 +345,7 @@ class FileSystemResourceDriver(ResourceDriver, BaseFileSystemDriver):
         return '{cls.__name__}(directory={self.directory})'.format(
             cls=self.__class__, self=self)
 
-    def create(self, name):
+    def create(self, name, **kwargs):
         """
         Create directory in storage by path.
         tries to create the root directory as well.

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/aria/storage/structures.py
----------------------------------------------------------------------
diff --git a/aria/storage/structures.py b/aria/storage/structures.py
index b02366e..7396d00 100644
--- a/aria/storage/structures.py
+++ b/aria/storage/structures.py
@@ -29,6 +29,10 @@ classes:
 import json
 from itertools import count
 from uuid import uuid4
+try:
+    from collections import OrderedDict
+except ImportError:
+    from ordereddict import OrderedDict
 
 from .exceptions import StorageError
 from ..logger import LoggerMixin
@@ -236,7 +240,18 @@ class Model(object):
         :return: all fields in dict format.
         :rtype dict
         """
-        return dict((name, getattr(self, name)) for name in self.fields)
+        return self._fields_dict(self)
+
+    def _fields_dict(self, obj):
+        dict_to_return = {}
+        for name in obj.fields:
+            value = getattr(obj, name)
+            if isinstance(value, Model) and isinstance(getattr(self.__class__, name), PointerField):
+                dict_to_return[name] = self._fields_dict(value)
+            else:
+                dict_to_return[name] = value
+
+        return dict_to_return.copy()
 
     @property
     def json(self):
@@ -260,8 +275,21 @@ class Model(object):
         missing = []
         for field_name in self.fields:
             try:
-                field_obj = input_fields.pop(field_name)
-                setattr(self, field_name, field_obj)
+                field_value = input_fields.pop(field_name)
+                field = getattr(self.__class__, field_name)
+                field_obj = None
+                if isinstance(field, IterPointerField):
+                    if all(isinstance(item, Model) for item in field_value):
+                        field_obj = field_value
+                    else:
+                        field_obj = [field.type(**field_dict) for field_dict in field_value]
+                elif isinstance(field, PointerField):
+                    if isinstance(field_value, Model):
+                        field_obj = field_value
+                    else:
+                        field_obj = field.type(**field_value)
+
+                setattr(self, field_name, field_obj or field_value)
             except KeyError:
                 field = getattr(self.__class__, field_name)
                 if field.default == field.NO_DEFAULT:
@@ -278,7 +306,7 @@ class Storage(LoggerMixin):
     def __init__(self, driver, items=(), **kwargs):
         super(Storage, self).__init__(**kwargs)
         self.driver = driver
-        self.registered = {}
+        self.registered = OrderedDict()
         for item in items:
             self.register(item)
         self.logger.debug('{name} object is ready: {0!r}'.format(
@@ -298,7 +326,7 @@ class Storage(LoggerMixin):
         """
         Setup and create all storage items
         """
-        for name, api in self.registered.iteritems():
+        for name, api in self.registered.items():
             try:
                 api.create()
                 self.logger.debug(

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/tests/storage/__init__.py
----------------------------------------------------------------------
diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py
index d9c25f7..9380707 100644
--- a/tests/storage/__init__.py
+++ b/tests/storage/__init__.py
@@ -16,7 +16,7 @@ from collections import namedtuple
 from tempfile import mkdtemp
 from shutil import rmtree
 
-from aria.storage import ModelDriver, PointerField, IterField, generate_lower_name, exceptions
+from aria.storage import ModelDriver, PointerField, IterField, _generate_lower_name, exceptions
 
 
 _Pointer = namedtuple('_Pointer', 'name, is_iter')
@@ -26,11 +26,11 @@ class InMemoryModelDriver(ModelDriver):
     def __init__(self, **kwargs):
         super(InMemoryModelDriver, self).__init__(**kwargs)
         self.storage = {}
-        self.pointer_mapping = {}
+        self.pointers = {}
 
     def create(self, name, model_cls, *args, **kwargs):
         self.storage[name] = {}
-        self.pointer_mapping[name] = {}
+        self.pointers[name] = {}
         self._create_pointer_mapping(name=name, model_cls=model_cls)
 
     def get(self, name, entry_id, **kwargs):
@@ -40,43 +40,51 @@ class InMemoryModelDriver(ModelDriver):
 
     def _get_pointers(self, name, base_entry, **kwargs):
         pointers = {}
-        for field, model_cls in self.pointer_mapping[name].items():
+        for field, field_cls in self.pointers[name].items():
+            pointer_storage_name = _generate_lower_name(field_cls.type)
             if field.is_iter:
-                pointers[field.name] = [
-                    model_cls(**self.storage[field.name][pointer_id])
-                    for pointer_id in base_entry[field.name]
-                    if pointer_id]
+                pointers[field.name] = [self.storage[pointer_storage_name][pointer_id]
+                                        for pointer_id in base_entry[field.name]
+                                        if pointer_id]
             elif base_entry[field.name]:
-                pointers[field.name] = model_cls(**base_entry[field.name])
+                pointers[field.name] = self.storage[pointer_storage_name][base_entry[field.name]]
         return pointers
 
     def store(self, name, entry_id, entry, **kwargs):
-        base_entry = entry
+        pointers = self._store_pointers(name, entry)
+        dict_entry = entry.fields_dict
+        dict_entry.update(pointers)
+        self.storage[name][entry_id] = dict_entry
+
+    def _store_pointers(self, name, base_entry, **kwargs):
         pointers = {}
-        for field, model_cls in self.pointer_mapping[name].items():
-            self.storage.setdefault(field.name, {})
+        for field, field_cls in self.pointers[name].items():
+            pointer_storage_name = _generate_lower_name(field_cls.type)
+            entry_field_value = getattr(base_entry, field.name)
             if field.is_iter:
                 pointers[field.name] = []
-                for iter_entity in base_entry[field.name]:
-                    pointers[field.name].append(iter_entity.id)
-                    self.storage[field.name][iter_entity.id] = iter_entity.fields_dict
+                for iter_entry_field in entry_field_value:
+                    pointers[field.name].append(iter_entry_field.id)
+                    self.storage[pointer_storage_name][iter_entry_field.id] = \
+                        iter_entry_field.fields_dict
             else:
-                pointers[field.name] = base_entry[field.name].id
-                self.storage[field.name] = base_entry[field.name]
-        base_entry.update(pointers)
-        self.storage[name][entry_id] = base_entry
+                pointers[field.name] = entry_field_value.id
+                self.storage[pointer_storage_name][entry_field_value.id] = \
+                    entry_field_value.fields_dict
+        return pointers.copy()
 
     def delete(self, name, entry_id, **kwargs):
         base_entry = self.storage[name].pop(entry_id)
         self._delete_pointers(name, base_entry)
 
     def _delete_pointers(self, name, base_entry, **kwargs):
-        for field, model_cls in self.pointer_mapping[name].items():
+        for field, field_cls in self.pointers[name].items():
+            pointer_storage_name = _generate_lower_name(field_cls.type)
             if field.is_iter:
                 for iter_entry in base_entry[field.name]:
-                    del(self.storage[field.name][iter_entry]['id'])
+                    del self.storage[pointer_storage_name][iter_entry]['id']
             else:
-                del(self.storage[field.name])
+                del self.storage[pointer_storage_name]
 
     def iter(self, name, **kwargs):
         for base_entry in self.storage[name].values():
@@ -92,11 +100,11 @@ class InMemoryModelDriver(ModelDriver):
             if not(isinstance(field_cls, PointerField) and field_cls.type):
                 continue
             pointer_name = _Pointer(field_name, is_iter=isinstance(field_cls, IterField))
-            pointer_storage_name = generate_lower_name(field_cls.type)
-            if pointer_storage_name not in self.storage:
-                raise exceptions.StorageError("{0} is not yet in storage".format(pointer_storage_name))
-            self.storage[pointer_storage_name] = {}
-            self.pointer_mapping[name][pointer_name] = field_cls.type
+            pointer_storage_name = _generate_lower_name(field_cls.type)
+            if pointer_storage_name in self.storage:
+                raise exceptions.StorageError("{0} is not yet in storage"
+                                              .format(pointer_storage_name))
+            self.pointers[name][pointer_name] = field_cls
 
 
 class TestFileSystem(object):

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/tests/storage/test_drivers.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_drivers.py b/tests/storage/test_drivers.py
index 804936f..5bb091c 100644
--- a/tests/storage/test_drivers.py
+++ b/tests/storage/test_drivers.py
@@ -77,14 +77,15 @@ def test_custom_driver():
     with InMemoryModelDriver() as driver:
         driver.create('entry', Entry)
         assert driver.storage['entry'] == {}
+        entry = Entry(**entry_dict)
 
-        driver.store(name='entry', entry=entry_dict, entry_id=entry_dict['id'])
+        driver.store(name='entry', entry=entry, entry_id=entry.id)
         assert driver.get(name='entry', entry_id='entry_id') == entry_dict
 
         assert list(node for node in driver.iter('entry')) == [entry_dict]
 
-        driver.update(name='entry', entry_id=entry_dict['id'], entry_value='new_value')
-        assert driver.get(name='entry', entry_id='entry_id') == entry_dict
+        driver.update(name='entry', entry_id=entry.id, entry_value='new_value')
+        assert driver.get(name='entry', entry_id='entry_id')['entry_value'] == 'new_value'
 
         driver.delete(name='entry', entry_id='entry_id')
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/tests/storage/test_model_storage.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_model_storage.py b/tests/storage/test_model_storage.py
index b2d6c8e..1798a20 100644
--- a/tests/storage/test_model_storage.py
+++ b/tests/storage/test_model_storage.py
@@ -64,7 +64,7 @@ def test_storage_driver():
     storage.register(models.ProviderContext)
     storage.setup()
     pc = models.ProviderContext(context={}, name='context_name', id='id2')
-    storage.driver.store(name='provider_context', entry=pc.fields_dict, entry_id=pc.id)
+    storage.driver.store(name='provider_context', entry=pc, entry_id=pc.id)
 
     assert storage.driver.get(
         name='provider_context',
@@ -136,7 +136,8 @@ def test_storage_iter_pointers():
         id = models.Field()
         pointing_field = structures.IterPointerField(type=PointedIterModel)
 
-    storage = ModelStorage(InMemoryModelDriver(), model_classes=[PointingIterModel, PointedIterModel])
+    storage = ModelStorage(InMemoryModelDriver(), model_classes=[PointingIterModel,
+                                                                 PointedIterModel])
     storage.setup()
 
     assert storage.pointed_iter_model

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/tests/storage/test_models.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_models.py b/tests/storage/test_models.py
index 7e289e6..d8a3a39 100644
--- a/tests/storage/test_models.py
+++ b/tests/storage/test_models.py
@@ -206,7 +206,7 @@ def _relationship(id=''):
 
 
 def test_relationships():
-    relationships = [_relationship(index) for index in xrange(3)]
+    relationships = [_relationship(index).fields_dict for index in xrange(3)]
 
     node = Node(
         blueprint_id='blueprint_id',
@@ -222,8 +222,8 @@ def test_relationships():
         max_number_of_instances=1)
 
     for index in xrange(3):
-        assert relationships[index] is \
-               next(node.relationships_by_target('target{0}'.format(index)))
+        assert relationships[index] == \
+               next(node.relationships_by_target('target{0}'.format(index))).fields_dict
 
     relationship = _relationship()
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/cc54639c/tests/storage/test_models_api.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_models_api.py b/tests/storage/test_models_api.py
index 261665b..4c54ab2 100644
--- a/tests/storage/test_models_api.py
+++ b/tests/storage/test_models_api.py
@@ -44,8 +44,8 @@ def test_models_api_base():
 
 def test_iterable_model_api():
     driver = InMemoryModelDriver()
-    driver.create('deployment_update')
-    driver.create('deployment_update_step')
+    driver.create('deployment_update', model_cls=models.DeploymentUpdate)
+    driver.create('deployment_update_step', model_cls=models.DeploymentUpdateStep)
     model_api = _ModelApi('deployment_update', driver, models.DeploymentUpdate)
     deployment_update = models.DeploymentUpdate(
         id='id',


Mime
View raw message