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):
"""
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",
"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
):
}
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",
"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()