Fix values for root plugin attributes
Currently root plugin attributes values inconsistent with specific plugin version attributes provided by client. This patch fills plugin attributes with proper values for specific plugin version. Change-Id: I1c85d6e080f8fd16d5b65c1bf670fdfb3ba0ff1b Closes-Bug: #1573440
This commit is contained in:
parent
7f88af6014
commit
cea2d5cc55
|
@ -360,6 +360,7 @@ class Cluster(NailgunObject):
|
|||
# Merge plugins attributes into editable ones
|
||||
plugin_attrs = PluginManager.get_plugins_attributes(
|
||||
instance, all_versions=all_plugins_versions)
|
||||
|
||||
plugin_attrs = traverse(
|
||||
plugin_attrs,
|
||||
formatter_context={'cluster': instance, 'settings': settings},
|
||||
|
@ -450,11 +451,18 @@ class Cluster(NailgunObject):
|
|||
:param data: dict
|
||||
:returns: dict
|
||||
"""
|
||||
return {'editable': dict_merge(
|
||||
|
||||
attributes = {'editable': dict_merge(
|
||||
cls.get_editable_attributes(instance),
|
||||
data.get('editable', {})
|
||||
)}
|
||||
|
||||
# plugin attributes should be updated with values to provide
|
||||
# consitency between new data and plugin attributes data from DB
|
||||
PluginManager.inject_plugin_attribute_values(attributes['editable'])
|
||||
|
||||
return attributes
|
||||
|
||||
@classmethod
|
||||
def get_network_manager(cls, instance=None):
|
||||
"""Get network manager for Cluster instance.
|
||||
|
|
|
@ -22,6 +22,7 @@ from nailgun.objects.plugin import ClusterPlugins
|
|||
from nailgun.objects.plugin import Plugin
|
||||
from nailgun.objects.plugin import PluginCollection
|
||||
from nailgun.plugins.adapters import wrap_plugin
|
||||
from nailgun.utils import dict_update
|
||||
|
||||
|
||||
class PluginManager(object):
|
||||
|
@ -114,6 +115,25 @@ class PluginManager(object):
|
|||
|
||||
return plugins_attributes
|
||||
|
||||
@classmethod
|
||||
def inject_plugin_attribute_values(cls, attributes):
|
||||
"""Inject given attributes with plugin attributes values.
|
||||
|
||||
:param attributes: Cluster attributes
|
||||
:type attributes: dict
|
||||
"""
|
||||
for k, attrs in six.iteritems(attributes):
|
||||
if (not cls.is_plugin_data(attrs) or
|
||||
not attrs['metadata']['enabled']):
|
||||
continue
|
||||
metadata = attrs['metadata']
|
||||
selected_plugin_attrs = cls._get_specific_version(
|
||||
metadata.get('versions', []),
|
||||
metadata.get('chosen_id'))
|
||||
selected_plugin_attrs.pop('metadata', None)
|
||||
|
||||
dict_update(attrs, selected_plugin_attrs, 1)
|
||||
|
||||
@classmethod
|
||||
def is_plugin_data(cls, attributes):
|
||||
"""Looking for a plugins hallmark.
|
||||
|
@ -382,3 +402,15 @@ class PluginManager(object):
|
|||
for plugin in cls.get_enabled_plugins(cluster):
|
||||
tasks.extend(plugin.tasks)
|
||||
return tasks
|
||||
|
||||
@classmethod
|
||||
def _get_specific_version(cls, versions, plugin_id):
|
||||
"""Return plugin attributes for specific version.
|
||||
|
||||
:returns: dict -- plugin attributes
|
||||
"""
|
||||
for version in versions:
|
||||
if version['metadata']['plugin_id'] == plugin_id:
|
||||
return version
|
||||
|
||||
return {}
|
||||
|
|
|
@ -437,6 +437,63 @@ class TestPluginManager(base.BaseIntegrationTest):
|
|||
attributes['plugin_b']['metadata']['chosen_id']
|
||||
)
|
||||
|
||||
def test_merge_plugin_values(self):
|
||||
attributes = {
|
||||
'test_plugin': {
|
||||
'metadata': {
|
||||
'class': 'plugin',
|
||||
'chosen_id': 1,
|
||||
'enabled': True,
|
||||
'versions': [
|
||||
{
|
||||
'metadata': {
|
||||
'plugin_id': 1
|
||||
},
|
||||
'attribute_a': {
|
||||
'value': 'test_a'
|
||||
},
|
||||
'attribute_b': {
|
||||
'value': 'test_b'
|
||||
}
|
||||
},
|
||||
{
|
||||
'metadata': {
|
||||
'plugin_id': 2
|
||||
},
|
||||
'attribute_a': {
|
||||
'value': 'test_a'
|
||||
},
|
||||
'attribute_c': {
|
||||
'value': 'test_c'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
'attribute_a': {'value': ''},
|
||||
'attribute_b': {'value': ''}
|
||||
}
|
||||
}
|
||||
|
||||
PluginManager.inject_plugin_attribute_values(attributes)
|
||||
|
||||
self.assertEqual(
|
||||
'test_a', attributes['test_plugin']['attribute_a']['value'])
|
||||
self.assertEqual(
|
||||
'test_b', attributes['test_plugin']['attribute_b']['value'])
|
||||
|
||||
def test_get_specific_version(self):
|
||||
versions = [
|
||||
{'metadata': {'plugin_id': '1'}},
|
||||
{'metadata': {'plugin_id': '2'}}
|
||||
]
|
||||
|
||||
plugin_version_attrs = PluginManager._get_specific_version(
|
||||
versions, '1')
|
||||
self.assertEqual(versions[0], plugin_version_attrs)
|
||||
not_existed_plugin_version_attrs = PluginManager._get_specific_version(
|
||||
versions, '3')
|
||||
self.assertEqual({}, not_existed_plugin_version_attrs)
|
||||
|
||||
|
||||
class TestClusterPluginIntegration(base.BaseTestCase):
|
||||
|
||||
|
|
|
@ -1616,6 +1616,34 @@ class TestClusterObject(BaseTestCase):
|
|||
for ip in node.ip_addrs:
|
||||
self.assertEquals(admin_ng_id, ip.network)
|
||||
|
||||
def test_get_updated_editable_attributes_with_plugin(self):
|
||||
cluster = self.env.create_cluster(api=False)
|
||||
self.env.create_plugin(
|
||||
name='test_plugin',
|
||||
version='1.0.0',
|
||||
package_version='4.0.0',
|
||||
cluster=cluster,
|
||||
attributes_metadata=self.env.get_default_plugin_env_config()
|
||||
)
|
||||
self.env.create_plugin(
|
||||
name='test_plugin',
|
||||
version='3.0.0',
|
||||
package_version='4.0.0',
|
||||
cluster=cluster,
|
||||
attributes_metadata=self.env.get_default_plugin_env_config()
|
||||
)
|
||||
|
||||
attributes = objects.Cluster.get_editable_attributes(cluster, True)
|
||||
plugin_attrs = attributes['test_plugin']['metadata']['versions'][1]
|
||||
plugin_attrs['plugin_name_text']['value'] = 'test_value_a'
|
||||
updated_attributes = objects.Cluster.get_updated_editable_attributes(
|
||||
cluster, {'editable': attributes})
|
||||
|
||||
self.assertEqual(
|
||||
'test_value_a',
|
||||
updated_attributes['editable']['test_plugin']
|
||||
['plugin_name_text']['value'])
|
||||
|
||||
|
||||
class TestClusterObjectVirtRoles(BaseTestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue