From 15ac7bec2d0ed88a76aadcf8e76192d2358fdf79 Mon Sep 17 00:00:00 2001 From: Tetiana Lashchova Date: Mon, 7 Sep 2015 13:39:42 +0300 Subject: [PATCH] Add hadoop_version validation to sahara resources Change-Id: I55cc3f049a78528143cb8eb6a3cfbf297806a2ac --- heat/engine/clients/os/sahara.py | 11 +++++++++++ .../openstack/sahara/sahara_cluster.py | 5 +++++ .../openstack/sahara/sahara_templates.py | 10 ++++++++++ heat/tests/clients/test_sahara_client.py | 19 +++++++++++++++++++ heat/tests/test_sahara_cluster.py | 2 ++ heat/tests/test_sahara_templates.py | 4 ++++ 6 files changed, 51 insertions(+) diff --git a/heat/engine/clients/os/sahara.py b/heat/engine/clients/os/sahara.py index b1ba173a0..0f48e477a 100644 --- a/heat/engine/clients/os/sahara.py +++ b/heat/engine/clients/os/sahara.py @@ -52,6 +52,17 @@ class SaharaClientPlugin(client_plugin.ClientPlugin): client = sahara_client.Client('1.1', **args) return client + def validate_hadoop_version(self, plugin_name, hadoop_version): + plugin = self.client().plugins.get(plugin_name) + allowed_versions = plugin.versions + if hadoop_version not in allowed_versions: + msg = (_("Requested plugin '%(plugin)s' doesn\'t support version " + "'%(version)s'. Allowed versions are %(allowed)s") % + {'plugin': plugin_name, + 'version': hadoop_version, + 'allowed': ', '.join(allowed_versions)}) + raise exception.StackValidationFailed(message=msg) + def is_not_found(self, ex): return (isinstance(ex, sahara_base.APIException) and ex.error_code == 404) diff --git a/heat/engine/resources/openstack/sahara/sahara_cluster.py b/heat/engine/resources/openstack/sahara/sahara_cluster.py index bbb7fa2e9..cda31bc56 100644 --- a/heat/engine/resources/openstack/sahara/sahara_cluster.py +++ b/heat/engine/resources/openstack/sahara/sahara_cluster.py @@ -221,6 +221,11 @@ class SaharaCluster(resource.Resource): ) % self.MANAGEMENT_NETWORK raise exception.StackValidationFailed(message=msg) + self.client_plugin().validate_hadoop_version( + self.properties[self.PLUGIN_NAME], + self.properties[self.HADOOP_VERSION] + ) + def resource_mapping(): return { diff --git a/heat/engine/resources/openstack/sahara/sahara_templates.py b/heat/engine/resources/openstack/sahara/sahara_templates.py index 0cdc1781f..d45b72046 100644 --- a/heat/engine/resources/openstack/sahara/sahara_templates.py +++ b/heat/engine/resources/openstack/sahara/sahara_templates.py @@ -265,6 +265,11 @@ class SaharaNodeGroupTemplate(resource.Resource): message=ex.message) raise + self.client_plugin().validate_hadoop_version( + self.properties[self.PLUGIN_NAME], + self.properties[self.HADOOP_VERSION] + ) + class SaharaClusterTemplate(resource.Resource): @@ -422,6 +427,11 @@ class SaharaClusterTemplate(resource.Resource): ) % self.MANAGEMENT_NETWORK raise exception.StackValidationFailed(message=msg) + self.client_plugin().validate_hadoop_version( + self.properties[self.PLUGIN_NAME], + self.properties[self.HADOOP_VERSION] + ) + def resource_mapping(): return { diff --git a/heat/tests/clients/test_sahara_client.py b/heat/tests/clients/test_sahara_client.py index 52c13a5d5..10c19b29e 100644 --- a/heat/tests/clients/test_sahara_client.py +++ b/heat/tests/clients/test_sahara_client.py @@ -135,6 +135,25 @@ class SaharaUtilsTests(common.HeatTestCase): calls = [mock.call(plugin_name), mock.call('noplugin')] self.sahara_client.plugins.get.assert_has_calls(calls) + def test_validate_hadoop_version(self): + """Tests the validate_hadoop_version function.""" + versions = ['1.2.1', '2.6.0', '2.7.1'] + plugin_name = 'vanilla' + self.my_plugin.name = plugin_name + self.my_plugin.versions = versions + + self.sahara_client.plugins.get.return_value = self.my_plugin + self.assertIsNone(self.sahara_plugin.validate_hadoop_version( + plugin_name, '2.6.0')) + ex = self.assertRaises(exception.StackValidationFailed, + self.sahara_plugin.validate_hadoop_version, + plugin_name, '1.2.3') + self.assertEqual("Requested plugin 'vanilla' doesn't support version " + "'1.2.3'. Allowed versions are 1.2.1, 2.6.0, 2.7.1", + six.text_type(ex)) + calls = [mock.call(plugin_name), mock.call(plugin_name)] + self.sahara_client.plugins.get.assert_has_calls(calls) + class ImageConstraintTest(common.HeatTestCase): diff --git a/heat/tests/test_sahara_cluster.py b/heat/tests/test_sahara_cluster.py index a0a5fc577..eb50e2956 100644 --- a/heat/tests/test_sahara_cluster.py +++ b/heat/tests/test_sahara_cluster.py @@ -68,6 +68,8 @@ class SaharaClusterTest(common.HeatTestCase): self.sahara_mock = mock.MagicMock() self.patchobject(sahara.SaharaClientPlugin, '_create' ).return_value = self.sahara_mock + self.patchobject(sahara.SaharaClientPlugin, 'validate_hadoop_version' + ).return_value = None self.cl_mgr = self.sahara_mock.clusters self.fake_cl = FakeCluster() diff --git a/heat/tests/test_sahara_templates.py b/heat/tests/test_sahara_templates.py index 31337f3b7..a85e58120 100644 --- a/heat/tests/test_sahara_templates.py +++ b/heat/tests/test_sahara_templates.py @@ -114,6 +114,8 @@ class SaharaNodeGroupTemplateTest(common.HeatTestCase): self.ngt_mgr = sahara_mock.node_group_templates self.patchobject(sahara.SaharaClientPlugin, '_create').return_value = sahara_mock + self.patchobject(sahara.SaharaClientPlugin, 'validate_hadoop_version' + ).return_value = None self.fake_ngt = FakeNodeGroupTemplate() self.t = template_format.parse(node_group_template) @@ -256,6 +258,8 @@ class SaharaClusterTemplateTest(common.HeatTestCase): self.ct_mgr = sahara_mock.cluster_templates self.patchobject(sahara.SaharaClientPlugin, '_create').return_value = sahara_mock + self.patchobject(sahara.SaharaClientPlugin, 'validate_hadoop_version' + ).return_value = None self.fake_ct = FakeClusterTemplate() self.t = template_format.parse(cluster_template)