Implement _map_to_user_inputs helper

It converts plain configs to the list of UserInput objects

* simple unit tests added
* __eq__ implemented for UserInput

Implements blueprint convert-user-input

Change-Id: I12c21ecf8b5ff8c8c4e4e6f01e1fc28118da0ab5
This commit is contained in:
Sergey Lukjanov 2013-06-24 23:14:59 +04:00
parent 0fd8fcc4c0
commit 57e83e0e1e
2 changed files with 123 additions and 0 deletions

View File

@ -68,6 +68,38 @@ class ProvisioningPluginBase(plugins_base.PluginInterface):
res['versions'] = self.get_versions()
return res
# Some helpers for plugins
def _map_to_user_inputs(self, hadoop_version, configs):
config_objs = self.get_configs(hadoop_version)
# convert config objects to applicable_target -> config_name -> obj
config_objs_map = {}
for config_obj in config_objs:
applicable_target = config_obj.applicable_target
confs = config_objs_map.get(applicable_target, {})
confs[config_obj.name] = config_obj
config_objs_map[applicable_target] = confs
# iterate over all configs and append UserInputs to result list
result = []
for applicable_target in configs:
for config_name in configs[applicable_target]:
confs = config_objs_map.get(applicable_target)
if not confs:
# TODO(slukjanov): raise specific exception
raise RuntimeError("Can't find applicable target '%s'"
% applicable_target)
conf = confs.get(config_name)
if not conf:
# TODO(slukjanov): raise specific exception
raise RuntimeError("Can't find config '%s' in '%s'"
% (config_name, applicable_target))
result.append(UserInput(
conf, configs[applicable_target][config_name]))
return result
class Config(resources.BaseResource):
"""Describes a single config parameter.
@ -114,6 +146,9 @@ class UserInput(object):
self.config = config
self.value = value
def __eq__(self, other):
return self.config == other.config and self.value == other.value
def __repr__(self):
return '<UserInput %s = %s>' % (self.config.name, self.value)

View File

@ -0,0 +1,88 @@
# Copyright (c) 2013 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 unittest2
from savanna.plugins import provisioning as p
class ProvisioningPluginBaseTest(unittest2.TestCase):
def test__map_to_user_inputs_success(self):
c1, c2, c3, plugin = _build_configs_and_plugin()
user_inputs = plugin._map_to_user_inputs(None, {
'at-1': {
'n-1': 'v-1',
'n-3': 'v-3',
},
'at-2': {
'n-2': 'v-2',
},
})
self.assertEqual(user_inputs, [
p.UserInput(c3, 'v-3'),
p.UserInput(c1, 'v-1'),
p.UserInput(c2, 'v-2'),
])
def test__map_to_user_inputs_failure(self):
c1, c2, c3, plugin = _build_configs_and_plugin()
with self.assertRaises(RuntimeError):
plugin._map_to_user_inputs(None, {
'at-X': {
'n-1': 'v-1',
},
})
with self.assertRaises(RuntimeError):
plugin._map_to_user_inputs(None, {
'at-1': {
'n-X': 'v-1',
},
})
def _build_configs_and_plugin():
c1 = p.Config('n-1', 'at-1', 'cluster')
c2 = p.Config('n-2', 'at-2', 'cluster')
c3 = p.Config('n-3', 'at-1', 'node')
class TestPlugin(TestEmptyPlugin):
def get_configs(self, hadoop_version):
return [c1, c2, c3]
return c1, c2, c3, TestPlugin()
class TestEmptyPlugin(p.ProvisioningPluginBase):
def get_title(self):
pass
def get_versions(self):
pass
def get_configs(self, hadoop_version):
pass
def get_node_processes(self, hadoop_version):
pass
def configure_cluster(self, cluster):
pass
def start_cluster(self, cluster):
pass