Get VIM certificates from DB 68/12468/6
authorcalvinosanc1 <guillermo.calvino@canonical.com>
Fri, 19 Aug 2022 10:44:52 +0000 (10:44 +0000)
committercalvinosanc1 <guillermo.calvino@canonical.com>
Mon, 19 Sep 2022 15:24:08 +0000 (15:24 +0000)
Change-Id: Ic551004f5d2837cc2390092f330cc8c82722e9d5
Signed-off-by: calvinosanc1 <guillermo.calvino@canonical.com>
osmclient/scripts/osm.py
osmclient/scripts/tests/tests_vim.py [new file with mode: 0644]

index 02870e4..ab56a07 100755 (executable)
@@ -3082,6 +3082,20 @@ def pdu_delete(ctx, name, force):
 #################
 
 
+def _check_ca_cert(vim_config: dict) -> None:
+    """
+    Checks if the VIM has a CA certificate.
+    In that case, reads the content and add it to the config
+    : param vim_config: configuration provided with the VIM creation
+    : return: None
+    """
+
+    if vim_config.get("ca_cert"):
+        with open(vim_config["ca_cert"], "r") as cert_f:
+            vim_config["ca_cert_content"] = str(cert_f.read())
+            del vim_config["ca_cert"]
+
+
 @cli_osm.command(name="vim-create", short_help="creates a new VIM account")
 @click.option("--name", required=True, help="Name to create datacenter")
 @click.option("--user", default=None, help="VIM username")
@@ -3166,6 +3180,7 @@ def vim_create(
     if vca:
         vim["vca"] = vca
     vim_config = create_config(config_file, config)
+    _check_ca_cert(vim_config)
     if creds:
         with open(creds, "r") as cf:
             vim_config["credentials"] = yaml.safe_load(cf.read())
@@ -3263,6 +3278,7 @@ def vim_update(
     vim_config = None
     if config or config_file:
         vim_config = create_config(config_file, config)
+        _check_ca_cert(vim_config)
     if creds:
         with open(creds, "r") as cf:
             vim_config["credentials"] = yaml.safe_load(cf.read())
diff --git a/osmclient/scripts/tests/tests_vim.py b/osmclient/scripts/tests/tests_vim.py
new file mode 100644 (file)
index 0000000..32b229e
--- /dev/null
@@ -0,0 +1,281 @@
+# Copyright 2021 Canonical Ltd.
+#
+# 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 unittest
+import json
+from unittest.mock import Mock, patch, mock_open
+from click.testing import CliRunner
+from osmclient.scripts import osm
+
+
+@patch("osmclient.scripts.osm.check_client_version")
+@patch("osmclient.scripts.osm.client.Client")
+@patch("osmclient.scripts.osm.create_config")
+class TestVim(unittest.TestCase):
+    def setUp(self):
+        self.runner = CliRunner()
+        self.ctx_obj = Mock()
+
+    def test_vim_create_ca_cert(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+        mock_create_config.return_value = {"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}
+        vim_config = mock_create_config.return_value
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-create",
+                    "--name",
+                    "vim1",
+                    "--user",
+                    "user1",
+                    "--password",
+                    "pass",
+                    "--auth_url",
+                    "http://test",
+                    "--tenant",
+                    "tenant1",
+                    "--config",
+                    json.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
+                    "--account_type",
+                    "openstack",
+                ],
+            )
+
+        mock_create_config.assert_called()
+        assert vim_config["ca_cert_content"] == "test"
+
+        self.ctx_obj.vim.create.assert_called_with(
+            "vim1",
+            {
+                "vim-username": "user1",
+                "vim-password": "pass",
+                "vim-url": "http://test",
+                "vim-tenant-name": "tenant1",
+                "vim-type": "openstack",
+                "description": None,
+            },
+            {"ca_cert_content": "test"},
+            None,
+            None,
+            wait=False,
+        )
+
+    def test_vim_create_no_config(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+        mock_create_config.return_value = {}
+
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-create",
+                    "--name",
+                    "vim1",
+                    "--user",
+                    "user1",
+                    "--password",
+                    "pass",
+                    "--auth_url",
+                    "http://test",
+                    "--tenant",
+                    "tenant1",
+                    "--account_type",
+                    "openstack",
+                ],
+            )
+        mock_check_client_version.assert_not_called()
+        mock_create_config.assert_called()
+
+        self.ctx_obj.vim.create.assert_called_with(
+            "vim1",
+            {
+                "vim-username": "user1",
+                "vim-password": "pass",
+                "vim-url": "http://test",
+                "vim-tenant-name": "tenant1",
+                "vim-type": "openstack",
+                "description": None,
+            },
+            {},
+            None,
+            None,
+            wait=False,
+        )
+
+    def test_vim_create_sdn(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+        mock_create_config.return_value = {}
+
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-create",
+                    "--name",
+                    "vim1",
+                    "--user",
+                    "user1",
+                    "--password",
+                    "pass",
+                    "--auth_url",
+                    "http://test",
+                    "--tenant",
+                    "tenant1",
+                    "--account_type",
+                    "openstack",
+                    "--sdn_controller",
+                    "controller",
+                    "--sdn_port_mapping",
+                    "port-map",
+                ],
+            )
+        mock_check_client_version.call_count == 2
+        mock_create_config.assert_called()
+
+        self.ctx_obj.vim.create.assert_called_with(
+            "vim1",
+            {
+                "vim-username": "user1",
+                "vim-password": "pass",
+                "vim-url": "http://test",
+                "vim-tenant-name": "tenant1",
+                "vim-type": "openstack",
+                "description": None,
+            },
+            {},
+            "controller",
+            "port-map",
+            wait=False,
+        )
+
+    def test_vim_update_ca_cert(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+        mock_create_config.return_value = {"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}
+        vim_config = mock_create_config.return_value
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-update",
+                    "vim1",
+                    "--config",
+                    json.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
+                ],
+            )
+
+        mock_check_client_version.assert_called()
+        mock_create_config.assert_called()
+        assert vim_config["ca_cert_content"] == "test"
+
+        self.ctx_obj.vim.update.assert_called_with(
+            "vim1",
+            {},
+            {"ca_cert_content": "test"},
+            None,
+            None,
+            wait=False,
+        )
+
+    def test_vim_update_no_config(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-update",
+                    "vim1",
+                    "--password",
+                    "passwd",
+                ],
+            )
+        mock_check_client_version.assert_called()
+        mock_create_config.assert_not_called()
+
+        self.ctx_obj.vim.update.assert_called_with(
+            "vim1",
+            {
+                "vim_password": "passwd",
+            },
+            None,
+            None,
+            None,
+            wait=False,
+        )
+
+    def test_vim_update_sdn(
+        self,
+        mock_create_config,
+        mock_client,
+        mock_check_client_version,
+    ):
+        mock_client.return_value = self.ctx_obj
+        mock_create_config.return_value = {}
+
+        with patch("builtins.open", mock_open(read_data="test")):
+
+            self.runner.invoke(
+                osm.cli_osm,
+                [
+                    "vim-update",
+                    "vim1",
+                    "--sdn_controller",
+                    "controller",
+                    "--sdn_port_mapping",
+                    "port-map",
+                ],
+            )
+        mock_check_client_version.assert_called()
+        mock_create_config.assert_not_called()
+
+        self.ctx_obj.vim.update.assert_called_with(
+            "vim1",
+            {},
+            None,
+            "controller",
+            "port-map",
+            wait=False,
+        )