Add Juju PaaS as a VIM 11/12911/5
authorPatricia Reinoso <patricia.reinoso@canonical.com>
Thu, 2 Feb 2023 15:00:40 +0000 (15:00 +0000)
committerbeierlm <mark.beierl@canonical.com>
Thu, 9 Feb 2023 20:52:35 +0000 (21:52 +0100)
The following fields are mandatory:

--account_type = paas
--config = '{paas_provider: juju}'

In case of PaaS VIM, Kafka message
is not sent to LCM.

Change-Id: Ib53a953706d05659aee4fdf0b147623cb5347cfd
Signed-off-by: Patricia Reinoso <patricia.reinoso@canonical.com>
osm_nbi/admin_topics.py
osm_nbi/tests/test_admin_topics.py

index 9f6f6d7..b2def67 100644 (file)
@@ -518,6 +518,30 @@ class VimAccountTopic(CommonVimWimSdn):
             "vrops_password",
         ),
     }
+    valid_paas_providers = ["juju"]
+
+    def check_conflict_on_new(self, session, indata):
+        super().check_conflict_on_new(session, indata)
+        self._check_paas_account(indata)
+
+    def _is_paas_vim_type(self, indata):
+        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)
+
+    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 check_conflict_on_del(self, session, _id, db_content):
         """
@@ -537,6 +561,11 @@ class VimAccountTopic(CommonVimWimSdn):
             )
         super().check_conflict_on_del(session, _id, db_content)
 
+    def _send_msg(self, action, content, not_send_msg=None):
+        if self._is_paas_vim_type(content):
+            return
+        super()._send_msg(action, content, not_send_msg)
+
 
 class WimAccountTopic(CommonVimWimSdn):
     topic = "wim_accounts"
index eb1cf32..ce6a21b 100755 (executable)
@@ -19,7 +19,7 @@ __date__ = "$2019-10-019"
 
 import unittest
 from unittest import TestCase
-from unittest.mock import Mock, patch, call
+from unittest.mock import Mock, patch, call, ANY
 from uuid import uuid4
 from http import HTTPStatus
 from time import time
@@ -32,6 +32,7 @@ from osm_nbi.admin_topics import (
     UserTopicAuth,
     CommonVimWimSdn,
     VcaTopic,
+    VimAccountTopic,
 )
 from osm_nbi.engine import EngineException
 from osm_nbi.authconn import AuthconnNotFoundException
@@ -1596,5 +1597,125 @@ class Test_CommonVimWimSdn(TestCase):
             )
 
 
+@patch("osm_nbi.admin_topics.CommonVimWimSdn.check_conflict_on_new")
+class TestVimAccountTopic(TestCase):
+    def setUp(self):
+        self.db = Mock(dbbase.DbBase())
+        self.fs = Mock(fsbase.FsBase())
+        self.msg = Mock(msgbase.MsgBase())
+        self.auth = Mock(authconn.Authconn(None, None, None))
+        self.topic = VimAccountTopic(self.db, self.fs, self.msg, self.auth)
+        self.topic.check_quota = Mock(return_value=None)  # skip quota
+
+        self.fake_session = {
+            "username": test_name,
+            "project_id": (test_pid,),
+            "method": None,
+            "admin": True,
+            "force": False,
+            "public": False,
+            "allow_show_user_project_role": True,
+        }
+
+    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"])
+        self.assertEqual(str(error.exception), error_msg)
+
+    def test_check_conflict_on_new_vim_type_paas(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": "juju"},
+        }
+        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
+    ):
+        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",
+        }
+        self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
+
+    def test_check_conflict_on_new_vim_type_paas_provider_missing(
+        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)
+
+    def test_kafka_message_is_not_sent_if_paas_vim(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": "juju"},
+        }
+        rollback = []
+
+        self.topic.new(rollback, self.fake_session, indata)
+        self.assertEqual(len(rollback), 1, "Wrong rollback length")
+        self.msg.write.assert_not_called()
+
+    def test_kafka_message_is_sent_if_not_paas_vim(self, mock_common_vim_wim_sdn):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "openstack",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+        }
+        rollback = []
+
+        self.topic.new(rollback, self.fake_session, indata)
+        self.assertEqual(len(rollback), 1, "Wrong rollback length")
+        mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata)
+        self.msg.write.assert_called_once_with("vim_account", "created", ANY)
+
+
 if __name__ == "__main__":
     unittest.main()