From c85d3af61dab8a00a7e988b82bd0433c9c9a6be5 Mon Sep 17 00:00:00 2001 From: Patricia Reinoso Date: Thu, 23 Mar 2023 09:27:08 +0000 Subject: [PATCH] Check VIM juju paas config It must contain : - paas_provider - ca_cert_content - cloud - cloud_credentials Change-Id: I8ce9918f81afd120c7a3d4e1e05fe27fac3508ad Signed-off-by: Patricia Reinoso Signed-off-by: Mark Beierl --- osm_nbi/admin_topics.py | 40 ++++++++---- osm_nbi/tests/test_admin_topics.py | 97 ++++++++++++++++++++---------- 2 files changed, 93 insertions(+), 44 deletions(-) diff --git a/osm_nbi/admin_topics.py b/osm_nbi/admin_topics.py index 6510d5e..f70c497 100644 --- a/osm_nbi/admin_topics.py +++ b/osm_nbi/admin_topics.py @@ -556,20 +556,34 @@ class VimAccountTopic(CommonVimWimSdn): return indata.get("vim_type") and indata["vim_type"] == "paas" def _check_paas_account(self, indata): - if self._is_paas_vim_type(indata): - self._check_paas_provider_is_valid(indata) + if not self._is_paas_vim_type(indata): + return + if not self._is_valid_paas_config(indata.get("config")): + raise EngineException( + "Invalid config for VIM account '{}'.".format(indata["name"]), + HTTPStatus.UNPROCESSABLE_ENTITY, + ) - def _check_paas_provider_is_valid(self, indata): - try: - paas_provider = indata["config"]["paas_provider"] - if paas_provider in self.valid_paas_providers: - return - except Exception: - pass - raise EngineException( - "Invalid paas_provider for VIM account '{}'.".format(indata["name"]), - HTTPStatus.UNPROCESSABLE_ENTITY, - ) + def _is_valid_paas_config(self, config) -> bool: + if not config: + return False + paas_provider = config.get("paas_provider") + is_valid_paas_provider = paas_provider in self.valid_paas_providers + if paas_provider == "juju": + return self._is_valid_juju_paas_config(config) + return is_valid_paas_provider + + def _is_valid_juju_paas_config(self, config) -> bool: + if not config: + return False + config_keys = [ + "paas_provider", + "ca_cert_content", + "cloud", + "cloud_credentials", + "authorized_keys", + ] + return all(key in config for key in config_keys) def check_conflict_on_del(self, session, _id, db_content): """ diff --git a/osm_nbi/tests/test_admin_topics.py b/osm_nbi/tests/test_admin_topics.py index 6c8083f..57551c7 100755 --- a/osm_nbi/tests/test_admin_topics.py +++ b/osm_nbi/tests/test_admin_topics.py @@ -1620,11 +1620,18 @@ class TestVimAccountTopic(TestCase): def check_invalid_indata_raises_exception(self, indata, mock_common_vim_wim_sdn): with self.assertRaises(EngineException) as error: self.topic.check_conflict_on_new(self.fake_session, indata) - mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata) - error_msg = "Invalid paas_provider for VIM account '{}'.".format(indata["name"]) + mock_common_vim_wim_sdn.assert_called_with(self.fake_session, indata) + error_msg = "Invalid config for VIM account '{}'.".format(indata["name"]) self.assertEqual(str(error.exception), error_msg) def test_check_conflict_on_new_vim_type_paas(self, mock_common_vim_wim_sdn): + valid_juju_paas_config = { + "paas_provider": "juju", + "ca_cert_content": "file_content", + "cloud": "microk8s", + "cloud_credentials": "cloud_credential_name", + "authorized_keys": "keys", + } indata = { "name": "juju_paas", "vim_type": "paas", @@ -1633,26 +1640,11 @@ class TestVimAccountTopic(TestCase): "vim_user": "some_user", "vim_password": "some_password", "vim_tenant_name": "null", - "config": {"paas_provider": "juju"}, + "config": valid_juju_paas_config, } self.topic.check_conflict_on_new(self.fake_session, indata) mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata) - def test_check_conflict_on_new_vim_type_paas_incorrect_provider( - self, mock_common_vim_wim_sdn - ): - indata = { - "name": "juju_paas", - "vim_type": "paas", - "description": None, - "vim_url": "http://0.0.0.0:0", - "vim_user": "some_user", - "vim_password": "some_password", - "vim_tenant_name": "null", - "config": {"paas_provider": "some_provider"}, - } - self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn) - def test_check_conflict_on_new_vim_type_paas_config_missing( self, mock_common_vim_wim_sdn ): @@ -1667,22 +1659,65 @@ class TestVimAccountTopic(TestCase): } self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn) - def test_check_conflict_on_new_vim_type_paas_provider_missing( + def test_check_conflict_on_new_vim_type_paas_invalid_config( self, mock_common_vim_wim_sdn ): - indata = { - "name": "juju_paas", - "vim_type": "paas", - "description": None, - "vim_url": "http://0.0.0.0:0", - "vim_user": "some_user", - "vim_password": "some_password", - "vim_tenant_name": "null", - "config": {"some_param": None}, - } - self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn) + invalid_configs = [ + { + "paas_provider": "some_paas_provider", + "ca_cert_content": "file_content", + "cloud": "microk8s", + "cloud_credentials": "cloud_credential_name", + }, + { + "ca_cert_content": "file_content", + "cloud": "microk8s", + "cloud_credentials": "cloud_credential_name", + }, + { + "paas_provider": "juju", + "cloud": "microk8s", + "cloud_credentials": "cloud_credential_name", + }, + { + "paas_provider": "juju", + "ca_cert_content": "file_content", + "cloud_credentials": "cloud_credential_name", + }, + { + "paas_provider": "juju", + "ca_cert_content": "file_content", + "cloud": "microk8s", + }, + { + "some_param": None, + }, + {}, + ] + for config in invalid_configs: + with self.subTest(): + indata = { + "name": "juju_paas", + "vim_type": "paas", + "description": None, + "vim_url": "http://0.0.0.0:0", + "vim_user": "some_user", + "vim_password": "some_password", + "vim_tenant_name": "null", + "config": config, + } + self.check_invalid_indata_raises_exception( + indata, mock_common_vim_wim_sdn + ) def test_kafka_message_is_not_sent_if_paas_vim(self, mock_common_vim_wim_sdn): + valid_juju_paas_config = { + "paas_provider": "juju", + "ca_cert_content": "file_content", + "cloud": "microk8s", + "cloud_credentials": "cloud_credential_name", + "authorized_keys": "keys", + } indata = { "name": "juju_paas", "vim_type": "paas", @@ -1691,7 +1726,7 @@ class TestVimAccountTopic(TestCase): "vim_user": "some_user", "vim_password": "some_password", "vim_tenant_name": "null", - "config": {"paas_provider": "juju"}, + "config": valid_juju_paas_config, } rollback = [] self.topic.temporal = Mock() -- 2.25.1