Github user mxmrlv commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/20#discussion_r87781904
  
    --- Diff: aria/context/toolbelt.py ---
    @@ -0,0 +1,102 @@
    +# 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.
    +"""
    +provides with different tools when working with the operation.
    +"""
    +
    +from contextlib import contextmanager
    +
    +from .. import exceptions
    +from . import operation
    +
    +
    +class _BaseToolbelt(object):
    +    """
    +    Base tool belt
    +    """
    +    def __init__(self):
    +        self._op_context = None
    +        self._workflow_context = None
    +
    +    @contextmanager
    +    def use(self, operation_context):
    +        """
    +        Context manager which switches the current toolbelt with the 
supplied ctx
    +        :param operation_context:
    +        :return:
    +        """
    +        assert isinstance(operation_context, 
operation.BaseOperationContext)
    +        _op_context = self._op_context
    +        _workflow_context = self._workflow_context
    +
    +        self._op_context = operation_context
    +        self._workflow_context = operation_context._workflow_context
    +        try:
    +            yield self
    +        finally:
    +            self._op_context = _op_context
    +            self._workflow_context = _workflow_context
    +
    +
    +class _NodeToolBelt(_BaseToolbelt):
    +    """
    +    Node operation related tool belt
    +    """
    +    @property
    +    def relationships_to_me(self):
    +        """
    +        Any relationship which the current node is the target of
    +        :return:
    +        """
    +        assert isinstance(self._op_context, operation.NodeOperationContext)
    +        for node_instance in self._workflow_context.node_instances:
    +            for relationship_instance in 
node_instance.relationship_instances:
    +                if relationship_instance.target_id == 
self._op_context.node_instance.id:
    +                    yield relationship_instance
    +
    +    @property
    +    def host_ip(self):
    +        """
    +        The host ip of the current node
    +        :return:
    +        """
    +        assert isinstance(self._op_context, operation.NodeOperationContext)
    +        host_id = self._op_context._actor.host_id
    +        host_instance = 
self._workflow_context.model.node_instance.get(host_id)
    +        return host_instance.runtime_properties.get('ip')
    +
    +
    +class _RelationshipToolBelt(_BaseToolbelt):
    +    """
    +    Relationship operation related tool belt
    +    """
    +    pass
    +
    +_operation_toolbelt = _NodeToolBelt()
    +_relationship_toolbelt = _RelationshipToolBelt()
    +
    +
    +def toolbelt(operation_context):
    +    """
    +    Get a toolbelt according to the current operation executor
    +    :param operation_context:
    +    :return:
    +    """
    +    if isinstance(operation_context, operation.NodeOperationContext):
    +        return _operation_toolbelt.use(operation_context)
    +    elif isinstance(operation_context, 
operation.RelationshipOperationContext):
    +        return _relationship_toolbelt.use(operation_context)
    +    else:
    +        raise exceptions.TaskException("Operation context not supported")
    --- End diff --
    
    raise RunTimeError


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to