Change how actions are stored in Object Model
Example: { ... "?": { ... "_actions": { "action_id": { "action_name": "name", "enabled": true } } ... } ... } Partially-Implements: blueprint application-actions Change-Id: I9b6a35a4eb236f18e03b9e14b6943ee8dee7748f
This commit is contained in:
parent
5369506800
commit
1551de70bd
@ -63,28 +63,24 @@ def _serialize_available_action(obj):
|
||||
actions = {}
|
||||
for name, method in obj_type.methods.iteritems():
|
||||
if method.usage == murano_method.MethodUsages.Action:
|
||||
actions[name] = {
|
||||
'id': '%s_%s' % (obj.object_id, name),
|
||||
action_id = '{0}_{1}'.format(obj.object_id, name)
|
||||
actions[action_id] = {
|
||||
'name': name,
|
||||
'enabled': True,
|
||||
'enabled': True
|
||||
}
|
||||
for parent in obj_type.parents:
|
||||
parent_actions = _serialize(parent)
|
||||
actions = helpers.merge_dicts(parent_actions, actions)
|
||||
return actions
|
||||
return _serialize(obj.type).values()
|
||||
return _serialize(obj.type)
|
||||
|
||||
|
||||
def _merge_actions(list1, list2):
|
||||
dict1 = dict([(action['id'], action) for action in list1])
|
||||
dict2 = dict([(action['id'], action) for action in list2])
|
||||
|
||||
def _merge_actions(dict1, dict2):
|
||||
result = helpers.merge_dicts(dict1, dict2)
|
||||
for action_id in dict1:
|
||||
if action_id not in dict2:
|
||||
del result[action_id]
|
||||
|
||||
return result.values()
|
||||
return result
|
||||
|
||||
|
||||
def _pass1_serialize(value, parent, serialized_objects,
|
||||
@ -103,7 +99,7 @@ def _pass1_serialize(value, parent, serialized_objects,
|
||||
#deserialize and merge list of actions
|
||||
actions = _serialize_available_action(value)
|
||||
result['?']['_actions'] = _merge_actions(
|
||||
result['?'].get('_actions', []), actions)
|
||||
result['?'].get('_actions', {}), actions)
|
||||
serialized_objects.add(value.object_id)
|
||||
return _pass1_serialize(
|
||||
result, value, serialized_objects, designer_attributes_getter)
|
||||
|
96
murano/tests/test_actions.py
Normal file
96
murano/tests/test_actions.py
Normal file
@ -0,0 +1,96 @@
|
||||
# Copyright (c) 2014 Mirantis, Inc.
|
||||
#
|
||||
# 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 mock
|
||||
|
||||
from murano.dsl import murano_method
|
||||
from murano.dsl import results_serializer
|
||||
from murano.tests import base
|
||||
|
||||
|
||||
class TestActionsSerializer(base.MuranoTestCase):
|
||||
def setUp(self):
|
||||
super(TestActionsSerializer, self).setUp()
|
||||
|
||||
def test_old_actions_deletion(self):
|
||||
old = {
|
||||
'action1': {'name': 'name1', 'enabled': True},
|
||||
'action2': {'name': 'name2', 'enabled': True},
|
||||
'action3': {'name': 'name3', 'enabled': True},
|
||||
}
|
||||
new = {
|
||||
'action2': {'name': 'name2', 'enabled': False},
|
||||
'action3': {'name': 'name3', 'enabled': True},
|
||||
}
|
||||
|
||||
result = results_serializer._merge_actions(old, new)
|
||||
|
||||
self.assertEqual(2, len(result))
|
||||
self.assertNotIn('action1', result)
|
||||
|
||||
def test_actions_state_update(self):
|
||||
old = {
|
||||
'action1': {'name': 'name1', 'enabled': True},
|
||||
'action2': {'name': 'name2', 'enabled': True},
|
||||
}
|
||||
new = {
|
||||
'action1': {'name': 'name2', 'enabled': False},
|
||||
'action2': {'name': 'name3', 'enabled': True},
|
||||
}
|
||||
|
||||
result = results_serializer._merge_actions(old, new)
|
||||
|
||||
self.assertFalse(result['action1']['enabled'])
|
||||
|
||||
def _get_mocked_obj(self):
|
||||
method1 = mock.Mock()
|
||||
method1.usage = murano_method.MethodUsages.Action
|
||||
method2 = mock.Mock()
|
||||
method2.usage = murano_method.MethodUsages.Runtime
|
||||
method3 = mock.Mock()
|
||||
method3.usage = murano_method.MethodUsages.Action
|
||||
|
||||
obj2_type = mock.Mock()
|
||||
obj2_type.parents = []
|
||||
obj2_type.methods = {'method3': method3}
|
||||
|
||||
obj = mock.Mock()
|
||||
obj.object_id = 'id1'
|
||||
obj.type.parents = [obj2_type]
|
||||
obj.type.methods = {'method1': method1, 'method2': method2}
|
||||
|
||||
return obj
|
||||
|
||||
def test_object_actions_serialization(self):
|
||||
obj = self._get_mocked_obj()
|
||||
|
||||
obj_actions = results_serializer._serialize_available_action(obj)
|
||||
|
||||
expected_result = {'name': 'method1', 'enabled': True}
|
||||
self.assertIn('id1_method1', obj_actions)
|
||||
self.assertEqual(expected_result, obj_actions['id1_method1'])
|
||||
|
||||
def test_that_only_actions_are_serialized(self):
|
||||
obj = self._get_mocked_obj()
|
||||
obj_actions = results_serializer._serialize_available_action(obj)
|
||||
self.assertNotIn('id1_method2', obj_actions)
|
||||
|
||||
def test_parent_actions_are_serialized(self):
|
||||
obj = self._get_mocked_obj()
|
||||
|
||||
obj_actions = results_serializer._serialize_available_action(obj)
|
||||
|
||||
expected_result = {'name': 'method3', 'enabled': True}
|
||||
self.assertIn('id1_method3', obj_actions)
|
||||
self.assertEqual(expected_result, obj_actions['id1_method3'])
|
Loading…
x
Reference in New Issue
Block a user