You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by mx...@apache.org on 2017/01/08 09:45:43 UTC

[1/4] incubator-ariatosca git commit: ARIA-55 Implement task retry and abort mechanism [Forced Update!]

Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-52-Support-order-management-in-relationships b707b2365 -> c7c0e3351 (forced update)


ARIA-55 Implement task retry and abort mechanism


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

Branch: refs/heads/ARIA-52-Support-order-management-in-relationships
Commit: 8a00b5fce2c737058f10c1e3d3a92bdcfe53e882
Parents: 12e175b
Author: Dan Kilman <da...@gigaspaces.com>
Authored: Thu Jan 5 13:00:01 2017 +0200
Committer: Dan Kilman <da...@gigaspaces.com>
Committed: Thu Jan 5 13:21:35 2017 +0200

----------------------------------------------------------------------
 aria/orchestrator/exceptions.py                 | 16 ++++
 .../workflows/core/events_handler.py            | 25 ++++--
 aria/storage/base_model.py                      | 10 ++-
 .../orchestrator/workflows/core/test_engine.py  | 94 +++++++++++++++++++-
 .../workflows/executor/test_executor.py         |  1 +
 .../workflows/executor/test_process_executor.py |  1 +
 6 files changed, 136 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/aria/orchestrator/exceptions.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/exceptions.py b/aria/orchestrator/exceptions.py
index 74e9002..bd5238e 100644
--- a/aria/orchestrator/exceptions.py
+++ b/aria/orchestrator/exceptions.py
@@ -30,3 +30,19 @@ class PluginAlreadyExistsError(AriaError):
     Raised when a plugin with the same package name and package version already exists
     """
     pass
+
+
+class TaskRetryException(RuntimeError):
+    """
+    Used internally when ctx.task.retry is called
+    """
+    def __init__(self, message, retry_interval):
+        super(TaskRetryException, self).__init__(message)
+        self.retry_interval = retry_interval
+
+
+class TaskAbortException(RuntimeError):
+    """
+    Used internally when ctx.task.abort is called
+    """
+    pass

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/aria/orchestrator/workflows/core/events_handler.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/workflows/core/events_handler.py b/aria/orchestrator/workflows/core/events_handler.py
index d05cbcb..c973ad9 100644
--- a/aria/orchestrator/workflows/core/events_handler.py
+++ b/aria/orchestrator/workflows/core/events_handler.py
@@ -27,7 +27,7 @@ from datetime import (
 )
 
 from ... import events
-
+from ... import exceptions
 
 @events.sent_task_signal.connect
 def _task_sent(task, *args, **kwargs):
@@ -43,18 +43,25 @@ def _task_started(task, *args, **kwargs):
 
 
 @events.on_failure_task_signal.connect
-def _task_failed(task, *args, **kwargs):
+def _task_failed(task, exception, *args, **kwargs):
     with task._update():
-        should_retry = (
-            (task.retry_count < task.max_attempts - 1 or
-             task.max_attempts == task.INFINITE_RETRIES) and
-            # ignore_failure check here means the task will not be retries and it will be marked as
-            # failed. The engine will also look at ignore_failure so it won't fail the workflow.
-            not task.ignore_failure)
+        should_retry = all([
+            not isinstance(exception, exceptions.TaskAbortException),
+            task.retry_count < task.max_attempts - 1 or task.max_attempts == task.INFINITE_RETRIES,
+            # ignore_failure check here means the task will not be retries and it will be marked
+            # as failed. The engine will also look at ignore_failure so it won't fail the
+            # workflow.
+            not task.ignore_failure
+        ])
         if should_retry:
+            retry_interval = None
+            if isinstance(exception, exceptions.TaskRetryException):
+                retry_interval = exception.retry_interval
+            if retry_interval is None:
+                retry_interval = task.retry_interval
             task.status = task.RETRYING
             task.retry_count += 1
-            task.due_at = datetime.utcnow() + timedelta(seconds=task.retry_interval)
+            task.due_at = datetime.utcnow() + timedelta(seconds=retry_interval)
         else:
             task.ended_at = datetime.utcnow()
             task.status = task.FAILED

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index d1aebf2..97c541c 100644
--- a/aria/storage/base_model.py
+++ b/aria/storage/base_model.py
@@ -52,8 +52,8 @@ from sqlalchemy import (
     orm,
 )
 
+from ..orchestrator.exceptions import TaskAbortException, TaskRetryException
 from .structure import ModelMixin
-
 from .type import (
     List,
     Dict
@@ -675,3 +675,11 @@ class TaskBase(ModelMixin):
     @classmethod
     def as_relationship_instance(cls, instance, **kwargs):
         return cls(relationship_instance=instance, **kwargs)
+
+    @staticmethod
+    def abort(message=None):
+        raise TaskAbortException(message)
+
+    @staticmethod
+    def retry(message=None, retry_interval=None):
+        raise TaskRetryException(message, retry_interval=retry_interval)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/tests/orchestrator/workflows/core/test_engine.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/workflows/core/test_engine.py b/tests/orchestrator/workflows/core/test_engine.py
index a6b55ba..d9b50a9 100644
--- a/tests/orchestrator/workflows/core/test_engine.py
+++ b/tests/orchestrator/workflows/core/test_engine.py
@@ -382,6 +382,78 @@ class TestRetries(BaseTest):
         assert global_test_holder.get('sent_task_signal_calls') == 1
 
 
+class TestTaskRetryAndAbort(BaseTest):
+    message = 'EXPECTED_ERROR'
+
+    def test_task_retry_default_interval(self, workflow_context, executor):
+        default_retry_interval = 0.1
+
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_task_retry, ctx,
+                          inputs={'message': self.message},
+                          retry_interval=default_retry_interval,
+                          max_attempts=2)
+            graph.add_tasks(op)
+        with pytest.raises(exceptions.ExecutorException):
+            self._execute(
+                workflow_func=mock_workflow,
+                workflow_context=workflow_context,
+                executor=executor)
+        assert workflow_context.states == ['start', 'failure']
+        assert isinstance(workflow_context.exception, exceptions.ExecutorException)
+        invocations = global_test_holder.get('invocations', [])
+        assert len(invocations) == 2
+        invocation1, invocation2 = invocations
+        assert invocation2 - invocation1 >= default_retry_interval
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+    def test_task_retry_custom_interval(self, workflow_context, executor):
+        default_retry_interval = 100
+        custom_retry_interval = 0.1
+
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_task_retry, ctx,
+                          inputs={'message': self.message,
+                                  'retry_interval': custom_retry_interval},
+                          retry_interval=default_retry_interval,
+                          max_attempts=2)
+            graph.add_tasks(op)
+        execution_start = time.time()
+        with pytest.raises(exceptions.ExecutorException):
+            self._execute(
+                workflow_func=mock_workflow,
+                workflow_context=workflow_context,
+                executor=executor)
+        execution_end = time.time()
+        assert workflow_context.states == ['start', 'failure']
+        assert isinstance(workflow_context.exception, exceptions.ExecutorException)
+        invocations = global_test_holder.get('invocations', [])
+        assert len(invocations) == 2
+        assert (execution_end - execution_start) < default_retry_interval
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+    def test_task_abort(self, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_task_abort, ctx,
+                          inputs={'message': self.message},
+                          retry_interval=100,
+                          max_attempts=100)
+            graph.add_tasks(op)
+        with pytest.raises(exceptions.ExecutorException):
+            self._execute(
+                workflow_func=mock_workflow,
+                workflow_context=workflow_context,
+                executor=executor)
+        assert workflow_context.states == ['start', 'failure']
+        assert isinstance(workflow_context.exception, exceptions.ExecutorException)
+        invocations = global_test_holder.get('invocations', [])
+        assert len(invocations) == 1
+        assert global_test_holder.get('sent_task_signal_calls') == 1
+
+
 @operation
 def mock_success_task(**_):
     pass
@@ -408,7 +480,27 @@ def mock_conditional_failure_task(failure_count, **_):
         invocations.append(time.time())
 
 
+@operation
 def mock_sleep_task(seconds, **_):
+    _add_invocation_timestamp()
+    time.sleep(seconds)
+
+
+@operation
+def mock_task_retry(ctx, message, retry_interval=None, **_):
+    _add_invocation_timestamp()
+    retry_kwargs = {}
+    if retry_interval is not None:
+        retry_kwargs['retry_interval'] = retry_interval
+    ctx.task.retry(message, **retry_kwargs)
+
+
+@operation
+def mock_task_abort(ctx, message, **_):
+    _add_invocation_timestamp()
+    ctx.task.abort(message)
+
+
+def _add_invocation_timestamp():
     invocations = global_test_holder.setdefault('invocations', [])
     invocations.append(time.time())
-    time.sleep(seconds)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/tests/orchestrator/workflows/executor/test_executor.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/workflows/executor/test_executor.py b/tests/orchestrator/workflows/executor/test_executor.py
index cd00cd5..2486a1e 100644
--- a/tests/orchestrator/workflows/executor/test_executor.py
+++ b/tests/orchestrator/workflows/executor/test_executor.py
@@ -107,6 +107,7 @@ class MockTask(object):
         self.retry_count = 0
         self.max_attempts = 1
         self.plugin_fk = None
+        self.ignore_failure = False
 
         for state in model.Task.STATES:
             setattr(self, state.upper(), state)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/8a00b5fc/tests/orchestrator/workflows/executor/test_process_executor.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/workflows/executor/test_process_executor.py b/tests/orchestrator/workflows/executor/test_process_executor.py
index e321388..687e245 100644
--- a/tests/orchestrator/workflows/executor/test_process_executor.py
+++ b/tests/orchestrator/workflows/executor/test_process_executor.py
@@ -130,6 +130,7 @@ class MockTask(object):
         self.max_attempts = 1
         self.plugin_fk = plugin.id
         self.plugin = plugin
+        self.ignore_failure = False
 
         for state in aria_model.Task.STATES:
             setattr(self, state.upper(), state)


[3/4] incubator-ariatosca git commit: ARIA-56 Implement ip property on node instance model

Posted by mx...@apache.org.
ARIA-56 Implement ip property on node instance model


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

Branch: refs/heads/ARIA-52-Support-order-management-in-relationships
Commit: 3caf17717a70041d878ad11b4e4234d362a7f000
Parents: fa221f7
Author: Dan Kilman <da...@gigaspaces.com>
Authored: Thu Jan 5 15:01:16 2017 +0200
Committer: Dan Kilman <da...@gigaspaces.com>
Committed: Thu Jan 5 16:51:25 2017 +0200

----------------------------------------------------------------------
 aria/.pylintrc               |  2 +-
 aria/storage/base_model.py   | 12 ++++++
 tests/storage/test_models.py | 80 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3caf1771/aria/.pylintrc
----------------------------------------------------------------------
diff --git a/aria/.pylintrc b/aria/.pylintrc
index 13a84fc..ee4d0ef 100644
--- a/aria/.pylintrc
+++ b/aria/.pylintrc
@@ -162,7 +162,7 @@ logging-modules=logging
 [BASIC]
 
 # Good variable names which should always be accepted, separated by a comma
-good-names=i,j,k,v,f,ex,e,_,id
+good-names=i,j,k,v,f,ex,e,_,id,ip
 
 # Bad variable names which should always be refused, separated by a comma
 bad-names=foo,bar,baz,toto,tutu,tata

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3caf1771/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index 97c541c..418d3b6 100644
--- a/aria/storage/base_model.py
+++ b/aria/storage/base_model.py
@@ -495,6 +495,18 @@ class NodeInstanceBase(ModelMixin):
     def node_name(cls):
         return association_proxy('node', cls.name_column_name())
 
+    @property
+    def ip(self):
+        if not self.host_fk:
+            return None
+        host_node_instance = self.host
+        if 'ip' in host_node_instance.runtime_properties:  # pylint: disable=no-member
+            return host_node_instance.runtime_properties['ip']  # pylint: disable=no-member
+        host_node = host_node_instance.node  # pylint: disable=no-member
+        if 'ip' in host_node.properties:
+            return host_node.properties['ip']
+        return None
+
 
 class RelationshipInstanceBase(ModelMixin):
     """

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3caf1771/tests/storage/test_models.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_models.py b/tests/storage/test_models.py
index df81f95..2088676 100644
--- a/tests/storage/test_models.py
+++ b/tests/storage/test_models.py
@@ -664,6 +664,86 @@ class TestNodeInstance(object):
             assert node_instance.deployment == node_storage.deployment.list()[0]
 
 
+class TestNodeInstanceIP(object):
+
+    ip = '1.1.1.1'
+
+    def test_ip_on_none_hosted_node_instance(self, deployment_storage):
+        node = self._node(deployment_storage, ip='not considered')
+        node_instance = self._node_instance(deployment_storage, node,
+                                            is_host=False,
+                                            ip='not considered')
+        assert node_instance.ip is None
+
+    def test_property_ip_on_host_node_instance(self, deployment_storage):
+        node = self._node(deployment_storage, ip=self.ip)
+        node_instance = self._node_instance(deployment_storage, node,
+                                            is_host=True,
+                                            ip=None)
+        assert node_instance.ip == self.ip
+
+    def test_runtime_property_ip_on_host_node_instance(self, deployment_storage):
+        node = self._node(deployment_storage, ip='not considered')
+        node_instance = self._node_instance(deployment_storage, node,
+                                            is_host=True,
+                                            ip=self.ip)
+        assert node_instance.ip == self.ip
+
+    def test_no_ip_configured_on_host_node_instance(self, deployment_storage):
+        node = self._node(deployment_storage, ip=None)
+        node_instance = self._node_instance(deployment_storage, node,
+                                            is_host=True,
+                                            ip=None)
+        assert node_instance.ip is None
+
+    def test_runtime_property_on_hosted_node_instance(self, deployment_storage):
+        host_node = self._node(deployment_storage, ip=None)
+        host_node_instance = self._node_instance(deployment_storage, host_node,
+                                                 is_host=True,
+                                                 ip=self.ip)
+        node = self._node(deployment_storage, ip=None)
+        node_instance = self._node_instance(deployment_storage, node,
+                                            is_host=False,
+                                            ip=None,
+                                            host_fk=host_node_instance.id)
+        assert node_instance.ip == self.ip
+
+    def _node(self, storage, ip):
+        kwargs = dict(
+            name='node',
+            deploy_number_of_instances=1,
+            max_number_of_instances=1,
+            min_number_of_instances=1,
+            number_of_instances=1,
+            planned_number_of_instances=1,
+            properties={},
+            type='',
+            deployment=storage.deployment.list()[0]
+        )
+        if ip:
+            kwargs['properties']['ip'] = ip
+        node = Node(**kwargs)
+        storage.node.put(node)
+        return node
+
+    def _node_instance(self, storage, node, is_host, ip, host_fk=None):
+        kwargs = dict(
+            name='node_instance',
+            node=node,
+            runtime_properties={},
+            state=''
+        )
+        if ip:
+            kwargs['runtime_properties']['ip'] = ip
+        if is_host:
+            kwargs['host_fk'] = 1
+        elif host_fk:
+            kwargs['host_fk'] = host_fk
+        node_instance = NodeInstance(**kwargs)
+        storage.node_instance.put(node_instance)
+        return node_instance
+
+
 class TestRelationshipInstance(object):
     def test_relatiship_instance_model_creation(self, node_instances_storage):
         relationship = mock.models.get_relationship(


[2/4] incubator-ariatosca git commit: ARIA-34 Adding get/download resource and render API (as jinja template)

Posted by mx...@apache.org.
ARIA-34 Adding get/download resource and render API (as jinja template)


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

Branch: refs/heads/ARIA-52-Support-order-management-in-relationships
Commit: fa221f7fa654c8957a7a2bad1790f73ffe1de78c
Parents: 8a00b5f
Author: Dan Kilman <da...@gigaspaces.com>
Authored: Thu Jan 5 14:15:40 2017 +0200
Committer: Dan Kilman <da...@gigaspaces.com>
Committed: Thu Jan 5 16:49:40 2017 +0200

----------------------------------------------------------------------
 aria/orchestrator/context/common.py             | 48 ++++++++++---
 .../context/test_resource_render.py             | 73 ++++++++++++++++++++
 2 files changed, 113 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/fa221f7f/aria/orchestrator/context/common.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/common.py b/aria/orchestrator/context/common.py
index fdbe152..53844e8 100644
--- a/aria/orchestrator/context/common.py
+++ b/aria/orchestrator/context/common.py
@@ -17,6 +17,8 @@ A common context for both workflow and operation
 """
 from uuid import uuid4
 
+import jinja2
+
 from aria import logger
 from aria.storage import exceptions
 
@@ -97,19 +99,49 @@ class BaseContext(logger.LoggerMixin):
         Download a blueprint resource from the resource storage
         """
         try:
-            return self.resource.deployment.download(entry_id=self.deployment.id,
-                                                     destination=destination,
-                                                     path=path)
+            self.resource.deployment.download(entry_id=str(self.deployment.id),
+                                              destination=destination,
+                                              path=path)
         except exceptions.StorageError:
-            return self.resource.blueprint.download(entry_id=self.blueprint.id,
-                                                    destination=destination,
-                                                    path=path)
+            self.resource.blueprint.download(entry_id=str(self.blueprint.id),
+                                             destination=destination,
+                                             path=path)
+
+    def download_resource_and_render(self, destination, path=None, variables=None):
+        """
+        Download a blueprint resource from the resource storage render its content as a jinja
+        template using the provided variables. ctx is available to the template without providing it
+        explicitly.
+        """
+        self.download_resource(destination=destination, path=path)
+        with open(destination, 'rb') as f:
+            resource_content = f.read()
+        resource_content = self._render_resource(resource_content=resource_content,
+                                                 variables=variables)
+        with open(destination, 'wb') as f:
+            f.write(resource_content)
 
     def get_resource(self, path=None):
         """
         Read a deployment resource as string from the resource storage
         """
         try:
-            return self.resource.deployment.read(entry_id=self.deployment.id, path=path)
+            return self.resource.deployment.read(entry_id=str(self.deployment.id), path=path)
         except exceptions.StorageError:
-            return self.resource.blueprint.read(entry_id=self.blueprint.id, path=path)
+            return self.resource.blueprint.read(entry_id=str(self.blueprint.id), path=path)
+
+    def get_resource_and_render(self, path=None, variables=None):
+        """
+        Read a deployment resource as string from the resource storage and render it as a jinja
+        template using the provided variables. ctx is available to the template without providing it
+        explicitly.
+        """
+        resource_content = self.get_resource(path=path)
+        return self._render_resource(resource_content=resource_content, variables=variables)
+
+    def _render_resource(self, resource_content, variables):
+        variables = variables or {}
+        if 'ctx' not in variables:
+            variables['ctx'] = self
+        resource_template = jinja2.Template(resource_content)
+        return resource_template.render(variables)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/fa221f7f/tests/orchestrator/context/test_resource_render.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/context/test_resource_render.py b/tests/orchestrator/context/test_resource_render.py
new file mode 100644
index 0000000..ca2ef42
--- /dev/null
+++ b/tests/orchestrator/context/test_resource_render.py
@@ -0,0 +1,73 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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 pytest
+
+from tests import mock, storage
+
+_IMPLICIT_CTX_TEMPLATE = '{{ctx.deployment.name}}'
+_IMPLICIT_CTX_TEMPLATE_PATH = 'implicit-ctx.template'
+_VARIABLES_TEMPLATE = '{{variable}}'
+_VARIABLES_TEMPLATE_PATH = 'variables.template'
+
+
+def test_get_resource_and_render_implicit_ctx_no_variables(ctx):
+    content = ctx.get_resource_and_render(_IMPLICIT_CTX_TEMPLATE_PATH)
+    assert content == mock.models.DEPLOYMENT_NAME
+
+
+def test_get_resource_and_render_provided_variables(ctx):
+    variable = 'VARIABLE'
+    content = ctx.get_resource_and_render(_VARIABLES_TEMPLATE_PATH,
+                                          variables={'variable': variable})
+    assert content == variable
+
+
+def test_download_resource_and_render_implicit_ctx_no_variables(tmpdir, ctx):
+    destination = tmpdir.join('destination')
+    ctx.download_resource_and_render(destination=str(destination),
+                                     path=_IMPLICIT_CTX_TEMPLATE_PATH)
+    assert destination.read() == mock.models.DEPLOYMENT_NAME
+
+
+def test_download_resource_and_render_provided_variables(tmpdir, ctx):
+    destination = tmpdir.join('destination')
+    variable = 'VARIABLE'
+    ctx.download_resource_and_render(destination=str(destination),
+                                     path=_VARIABLES_TEMPLATE_PATH,
+                                     variables={'variable': variable})
+    assert destination.read() == variable
+
+
+@pytest.fixture
+def ctx(tmpdir):
+    context = mock.context.simple(storage.get_sqlite_api_kwargs(),
+                                  resources_dir=str(tmpdir.join('resources')))
+    yield context
+    storage.release_sqlite_storage(context.model)
+
+
+@pytest.fixture(autouse=True)
+def resources(tmpdir, ctx):
+    implicit_ctx_template_path = tmpdir.join(_IMPLICIT_CTX_TEMPLATE_PATH)
+    implicit_ctx_template_path.write(_IMPLICIT_CTX_TEMPLATE)
+    variables_template_path = tmpdir.join(_VARIABLES_TEMPLATE_PATH)
+    variables_template_path.write(_VARIABLES_TEMPLATE)
+    ctx.resource.deployment.upload(entry_id='1',
+                                   source=str(implicit_ctx_template_path),
+                                   path=_IMPLICIT_CTX_TEMPLATE_PATH)
+    ctx.resource.deployment.upload(entry_id='1',
+                                   source=str(variables_template_path),
+                                   path=_VARIABLES_TEMPLATE_PATH)


[4/4] incubator-ariatosca git commit: ARIA-52-Support-order-management-in-relationships

Posted by mx...@apache.org.
ARIA-52-Support-order-management-in-relationships


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

Branch: refs/heads/ARIA-52-Support-order-management-in-relationships
Commit: c7c0e3351dd00880acd6f0097289cdff25d7283a
Parents: 3caf177
Author: mxmrlv <mx...@gmail.com>
Authored: Tue Dec 27 16:42:57 2016 +0200
Committer: mxmrlv <mx...@gmail.com>
Committed: Sun Jan 8 11:45:33 2017 +0200

----------------------------------------------------------------------
 aria/storage/base_model.py | 70 +++++++++++++++++++++++++++++++++--------
 aria/storage/sql_mapi.py   |  2 +-
 aria/storage/structure.py  | 10 ++++--
 3 files changed, 66 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c7c0e335/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index 418d3b6..a534db4 100644
--- a/aria/storage/base_model.py
+++ b/aria/storage/base_model.py
@@ -51,6 +51,7 @@ from sqlalchemy import (
     Float,
     orm,
 )
+from sqlalchemy.ext.orderinglist import ordering_list
 
 from ..orchestrator.exceptions import TaskAbortException, TaskRetryException
 from .structure import ModelMixin
@@ -415,7 +416,13 @@ class RelationshipBase(ModelMixin):
     """
     __tablename__ = 'relationships'
 
-    _private_fields = ['source_node_fk', 'target_node_fk']
+    _private_fields = ['source_node_fk', 'target_node_fk', 'position']
+
+    position = Column(Integer)
+
+    @declared_attr
+    def deployment_id(self):
+        return association_proxy('source_node', 'deployment_id')
 
     @declared_attr
     def source_node_fk(cls):
@@ -423,8 +430,14 @@ class RelationshipBase(ModelMixin):
 
     @declared_attr
     def source_node(cls):
-        return cls.one_to_many_relationship('source_node_fk',
-                                            backreference='outbound_relationships')
+        return cls.one_to_many_relationship(
+            'source_node_fk',
+            backreference='outbound_relationships',
+            backref_kwargs=dict(
+                order_by=cls.position,
+                collection_class=ordering_list('position', count_from=0)
+            )
+        )
 
     @declared_attr
     def source_name(cls):
@@ -432,11 +445,18 @@ class RelationshipBase(ModelMixin):
 
     @declared_attr
     def target_node_fk(cls):
-        return cls.foreign_key(NodeBase)
+        return cls.foreign_key(NodeBase, nullable=True)
 
     @declared_attr
     def target_node(cls):
-        return cls.one_to_many_relationship('target_node_fk', backreference='inbound_relationships')
+        return cls.one_to_many_relationship(
+            'target_node_fk',
+            backreference='inbound_relationships',
+            backref_kwargs=dict(
+                order_by=cls.position,
+                collection_class=ordering_list('position', count_from=0)
+            )
+        )
 
     @declared_attr
     def target_name(cls):
@@ -515,35 +535,58 @@ class RelationshipInstanceBase(ModelMixin):
     __tablename__ = 'relationship_instances'
     _private_fields = ['relationship_storage_fk',
                        'source_node_instance_fk',
-                       'target_node_instance_fk']
+                       'target_node_instance_fk',
+                       'position']
+
+    position = Column(Integer)
 
     @declared_attr
     def source_node_instance_fk(cls):
-        return cls.foreign_key(NodeInstanceBase)
+        return cls.foreign_key(NodeInstanceBase, nullable=True)
 
     @declared_attr
     def source_node_instance(cls):
-        return cls.one_to_many_relationship('source_node_instance_fk',
-                                            backreference='outbound_relationship_instances')
+        return cls.one_to_many_relationship(
+            'source_node_instance_fk',
+            backreference='outbound_relationship_instances',
+            backref_kwargs=dict(
+                order_by=cls.position,
+                collection_class=ordering_list('position', count_from=0)
+            )
+        )
 
     @declared_attr
     def source_node_instance_name(cls):
+        return association_proxy('source_node_instance', 'node_{0}'.format(cls.name_column_name()))
+
+    @declared_attr
+    def source_node_name(cls):
         return association_proxy('source_node_instance', cls.name_column_name())
 
     @declared_attr
     def target_node_instance_fk(cls):
-        return cls.foreign_key(NodeInstanceBase)
+        return cls.foreign_key(NodeInstanceBase, nullable=True)
 
     @declared_attr
     def target_node_instance(cls):
-        return cls.one_to_many_relationship('target_node_instance_fk',
-                                            backreference='inbound_relationship_instances')
+        return cls.one_to_many_relationship(
+            'target_node_instance_fk',
+            backreference='inbound_relationship_instances',
+            backref_kwargs=dict(
+                order_by=cls.position,
+                collection_class=ordering_list('position', count_from=0)
+            )
+        )
 
     @declared_attr
     def target_node_instance_name(cls):
         return association_proxy('target_node_instance', cls.name_column_name())
 
     @declared_attr
+    def target_node_name(cls):
+        return association_proxy('target_node_instance', 'node_{0}'.format(cls.name_column_name()))
+
+    @declared_attr
     def relationship_fk(cls):
         return cls.foreign_key(RelationshipBase)
 
@@ -556,6 +599,7 @@ class RelationshipInstanceBase(ModelMixin):
         return association_proxy('relationship', cls.name_column_name())
 
 
+
 class PluginBase(ModelMixin):
     """
     Plugin model representation.
@@ -658,7 +702,7 @@ class TaskBase(ModelMixin):
 
     INFINITE_RETRIES = -1
 
-    status = Column(Enum(*STATES), name='status', default=PENDING)
+    status = Column(Enum(*STATES, name='status'), default=PENDING)
 
     due_at = Column(DateTime, default=datetime.utcnow)
     started_at = Column(DateTime, default=None)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c7c0e335/aria/storage/sql_mapi.py
----------------------------------------------------------------------
diff --git a/aria/storage/sql_mapi.py b/aria/storage/sql_mapi.py
index cde40c2..809f677 100644
--- a/aria/storage/sql_mapi.py
+++ b/aria/storage/sql_mapi.py
@@ -46,7 +46,7 @@ class SQLAlchemyModelAPI(api.ModelAPI):
 
         if not result:
             raise exceptions.StorageError(
-                'Requested {0} with ID `{1}` was not found'
+                'Requested `{0}` with ID `{1}` was not found'
                 .format(self.model_cls.__name__, entry_id)
             )
         return result

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c7c0e335/aria/storage/structure.py
----------------------------------------------------------------------
diff --git a/aria/storage/structure.py b/aria/storage/structure.py
index 431633b..fa592ac 100644
--- a/aria/storage/structure.py
+++ b/aria/storage/structure.py
@@ -80,7 +80,9 @@ class ModelMixin(object):
     @classmethod
     def one_to_many_relationship(cls,
                                  foreign_key_column,
-                                 backreference=None):
+                                 backreference=None,
+                                 backref_kwargs=None,
+                                 **kwargs):
         """Return a one-to-many SQL relationship object
         Meant to be used from inside the *child* object
 
@@ -89,6 +91,7 @@ class ModelMixin(object):
         :param foreign_key_column: The column of the foreign key (from the child table)
         :param backreference: The name to give to the reference to the child (on the parent table)
         """
+        backref_kwargs = backref_kwargs or {}
         parent_table = cls._get_cls_by_tablename(
             getattr(cls, foreign_key_column).__remote_table_name)
         primaryjoin_str = '{parent_class_name}.{parent_unique_id} == ' \
@@ -105,7 +108,8 @@ class ModelMixin(object):
             foreign_keys=[getattr(cls, foreign_key_column)],
             # The following line make sure that when the *parent* is
             # deleted, all its connected children are deleted as well
-            backref=backref(backreference or cls.__tablename__, cascade='all'),
+            backref=backref(backreference or cls.__tablename__, cascade='all', **backref_kwargs),
+            **kwargs
         )
 
     @classmethod
@@ -145,6 +149,8 @@ class ModelMixin(object):
                 field_value = list(field_value)
             elif isinstance(field_value, dict):
                 field_value = dict(field_value)
+            elif isinstance(field_value, ModelMixin):
+                field_value = field_value.to_dict()
             res[field] = field_value
 
         return res