Add add_k8s, add_cloud, and remove_cloud commands to libjuju.py and unit tests 17/9717/1
authorDavid Garcia <david.garcia@canonical.com>
Thu, 17 Sep 2020 14:01:48 +0000 (16:01 +0200)
committerDavid Garcia <david.garcia@canonical.com>
Thu, 17 Sep 2020 14:01:48 +0000 (16:01 +0200)
- add_k8s: Generates Cloud and CloudCredential objects for adding a Kubernetes cloud to the VCA
- add_cloud: Takes Cloud and CloudCredential as arguments for adding the cloud to the VCA
- remove_cloud: Remove cloud from VCA

Change-Id: Ia6b4c0cbd06f38df6fe4c52414f5bcb8ffb9a5a8
Signed-off-by: David Garcia <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/tests/unit/test_libjuju.py

index e73e552..12730fd 100644 (file)
@@ -22,7 +22,12 @@ from juju.errors import JujuAPIError
 from juju.model import Model
 from juju.machine import Machine
 from juju.application import Application
 from juju.model import Model
 from juju.machine import Machine
 from juju.application import Application
-from juju.client._definitions import FullStatus, QueryApplicationOffersResults
+from juju.client._definitions import (
+    FullStatus,
+    QueryApplicationOffersResults,
+    Cloud,
+    CloudCredential,
+)
 from n2vc.juju_watcher import JujuModelWatcher
 from n2vc.provisioner import AsyncSSHProvisioner
 from n2vc.n2vc_conn import N2VCConnector
 from n2vc.juju_watcher import JujuModelWatcher
 from n2vc.provisioner import AsyncSSHProvisioner
 from n2vc.n2vc_conn import N2VCConnector
@@ -994,3 +999,91 @@ class Libjuju:
             return await controller.list_offers(model_name)
         finally:
             await self.disconnect_controller(controller)
             return await controller.list_offers(model_name)
         finally:
             await self.disconnect_controller(controller)
+
+    async def add_k8s(self, name: str, auth_data: dict, storage_class: str):
+        """
+        Add a Kubernetes cloud to the controller
+
+        Similar to the `juju add-k8s` command in the CLI
+
+        :param: name:           Name for the K8s cloud
+        :param: auth_data:      Dictionary with needed credentials. Format:
+        {
+            "server": "192.168.0.21:16443",
+            "cacert": "-----BEGIN CERTIFI...",
+            "token": "clhkRExRem5Xd1dCdnFEVXdvRGt...",
+
+        }
+        :param: storage_class:  Storage Class to use in the cloud
+        """
+
+        required_auth_data_keys = ["server", "cacert", "token"]
+        missing_keys = []
+        for k in required_auth_data_keys:
+            if k not in auth_data:
+                missing_keys.append(k)
+        if missing_keys:
+            raise Exception(
+                "missing keys in auth_data: {}".format(",".join(missing_keys))
+            )
+        if not storage_class:
+            raise Exception("storage_class must be a non-empty string")
+        if not name:
+            raise Exception("name must be a non-empty string")
+
+        endpoint = auth_data["server"]
+        cacert = auth_data["cacert"]
+        token = auth_data["token"]
+        region_name = "{}-region".format(name)
+
+        cloud = client.Cloud(
+            auth_types=["certificate"],
+            ca_certificates=[cacert],
+            endpoint=endpoint,
+            config={
+                "operator-storage": storage_class,
+                "workload-storage": storage_class,
+            },
+            regions=[client.CloudRegion(endpoint=endpoint, name=region_name)],
+            type_="kubernetes",
+        )
+
+        cred = client.CloudCredential(
+            auth_type="certificate",
+            attrs={"ClientCertificateData": cacert, "Token": token},
+        )
+        return await self.add_cloud(name, cloud, cred)
+
+    async def add_cloud(
+        self, name: str, cloud: Cloud, credential: CloudCredential = None
+    ) -> Cloud:
+        """
+        Add cloud to the controller
+
+        :param: name:   Name of the cloud to be added
+        :param: cloud:  Cloud object
+        :param: credential:   CloudCredentials object for the cloud
+        """
+        controller = await self.get_controller()
+        try:
+            _ = await controller.add_cloud(name, cloud)
+            if credential:
+                await controller.add_credential(name, credential=credential, cloud=name)
+            # Need to return the object returned by the controller.add_cloud() function
+            # I'm returning the original value now until this bug is fixed:
+            #   https://github.com/juju/python-libjuju/issues/443
+            return cloud
+        finally:
+            await self.disconnect_controller(controller)
+
+    async def remove_cloud(self, name: str):
+        """
+        Remove cloud
+
+        :param: name:   Name of the cloud to be removed
+        """
+        controller = await self.get_controller()
+        try:
+            await controller.remove_cloud(name)
+        finally:
+            await self.disconnect_controller(controller)
index 41b32d1..ae39078 100644 (file)
@@ -96,10 +96,11 @@ class GetControllerTest(LibjujuTestCase):
     ):
         self.libjuju.endpoints = []
         mock__update_api_endpoints_db.side_effect = Exception()
     ):
         self.libjuju.endpoints = []
         mock__update_api_endpoints_db.side_effect = Exception()
+        controller = None
         with self.assertRaises(JujuControllerFailedConnecting):
             controller = self.loop.run_until_complete(self.libjuju.get_controller())
         with self.assertRaises(JujuControllerFailedConnecting):
             controller = self.loop.run_until_complete(self.libjuju.get_controller())
-            self.assertIsNone(controller)
-            mock_disconnect_controller.assert_called_once()
+        self.assertIsNone(controller)
+        mock_disconnect_controller.assert_called_once()
 
     def test_same_endpoint_get_controller(
         self, mock__update_api_endpoints_db, mock_api_endpoints, mock_connect
 
     def test_same_endpoint_get_controller(
         self, mock__update_api_endpoints_db, mock_api_endpoints, mock_connect
@@ -151,7 +152,7 @@ class AddModelTest(LibjujuTestCase):
                 self.libjuju.add_model("existing_model", "cloud")
             )
 
                 self.libjuju.add_model("existing_model", "cloud")
             )
 
-            mock_disconnect_controller.assert_called()
+        mock_disconnect_controller.assert_called()
 
     # TODO Check two job executing at the same time and one returning without doing anything.
 
 
     # TODO Check two job executing at the same time and one returning without doing anything.
 
@@ -254,7 +255,7 @@ class GetModelStatusTest(LibjujuTestCase):
 
         self.assertEqual(status, {"status"})
 
 
         self.assertEqual(status, {"status"})
 
-    def test_excpetion(
+    def test_exception(
         self,
         mock_get_status,
         mock_disconnect_controller,
         self,
         mock_get_status,
         mock_disconnect_controller,
@@ -264,16 +265,16 @@ class GetModelStatusTest(LibjujuTestCase):
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_get_status.side_effect = Exception()
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_get_status.side_effect = Exception()
-
+        status = None
         with self.assertRaises(Exception):
             status = self.loop.run_until_complete(
                 self.libjuju.get_model_status("model")
             )
 
         with self.assertRaises(Exception):
             status = self.loop.run_until_complete(
                 self.libjuju.get_model_status("model")
             )
 
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
 
-            self.assertIsNone(status)
+        self.assertIsNone(status)
 
 
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
 
 
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
@@ -319,16 +320,18 @@ class CreateMachineTest(LibjujuTestCase):
         mock_get_model,
         mock_get_controller,
     ):
         mock_get_model,
         mock_get_controller,
     ):
+        machine = None
+        bool_res = None
         mock_get_model.return_value = juju.model.Model()
         with self.assertRaises(JujuMachineNotFound):
             machine, bool_res = self.loop.run_until_complete(
                 self.libjuju.create_machine("model", "non_existing_machine")
             )
         mock_get_model.return_value = juju.model.Model()
         with self.assertRaises(JujuMachineNotFound):
             machine, bool_res = self.loop.run_until_complete(
                 self.libjuju.create_machine("model", "non_existing_machine")
             )
-            self.assertIsNone(machine)
-            self.assertIsNone(bool_res)
+        self.assertIsNone(machine)
+        self.assertIsNone(bool_res)
 
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_no_machine(
         self,
 
     def test_no_machine(
         self,
@@ -390,14 +393,15 @@ class DeployCharmTest(LibjujuTestCase):
         mock_get_model.return_value = juju.model.Model()
         mock_applications.return_value = {"existing_app"}
 
         mock_get_model.return_value = juju.model.Model()
         mock_applications.return_value = {"existing_app"}
 
+        application = None
         with self.assertRaises(JujuApplicationExists):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("existing_app", "path", "model", "machine",)
             )
         with self.assertRaises(JujuApplicationExists):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("existing_app", "path", "model", "machine",)
             )
-            self.assertIsNone(application)
+        self.assertIsNone(application)
 
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_non_existing_machine(
         self,
 
     def test_non_existing_machine(
         self,
@@ -413,15 +417,16 @@ class DeployCharmTest(LibjujuTestCase):
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_machines.return_value = {"existing_machine": FakeMachine()}
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_machines.return_value = {"existing_machine": FakeMachine()}
+        application = None
         with self.assertRaises(JujuMachineNotFound):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("app", "path", "model", "machine",)
             )
 
         with self.assertRaises(JujuMachineNotFound):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("app", "path", "model", "machine",)
             )
 
-            self.assertIsNone(application)
+        self.assertIsNone(application)
 
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_2_units(
         self,
 
     def test_2_units(
         self,
@@ -532,16 +537,17 @@ class ExecuteActionTest(LibjujuTestCase):
     ):
         mock__get_application.return_value = None
         mock_get_model.return_value = juju.model.Model()
     ):
         mock__get_application.return_value = None
         mock_get_model.return_value = juju.model.Model()
-
+        output = None
+        status = None
         with self.assertRaises(JujuApplicationNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
         with self.assertRaises(JujuApplicationNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
-            self.assertIsNone(output)
-            self.assertIsNone(status)
+        self.assertIsNone(output)
+        self.assertIsNone(status)
 
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_no_action(
         self,
 
     def test_no_action(
         self,
@@ -557,15 +563,17 @@ class ExecuteActionTest(LibjujuTestCase):
 
         mock_get_model.return_value = juju.model.Model()
         mock__get_application.return_value = FakeApplication()
 
         mock_get_model.return_value = juju.model.Model()
         mock__get_application.return_value = FakeApplication()
+        output = None
+        status = None
         with self.assertRaises(JujuActionNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
         with self.assertRaises(JujuActionNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
-            self.assertIsNone(output)
-            self.assertIsNone(status)
+        self.assertIsNone(output)
+        self.assertIsNone(status)
 
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     # TODO no leader unit found exception
 
 
     # TODO no leader unit found exception
 
@@ -614,15 +622,15 @@ class GetActionTest(LibjujuTestCase):
         mock_get_controller,
     ):
         mock_get_application.side_effect = Exception()
         mock_get_controller,
     ):
         mock_get_application.side_effect = Exception()
-
+        actions = None
         with self.assertRaises(Exception):
             actions = self.loop.run_until_complete(
                 self.libjuju.get_actions("app", "model")
             )
 
         with self.assertRaises(Exception):
             actions = self.loop.run_until_complete(
                 self.libjuju.get_actions("app", "model")
             )
 
-            self.assertIsNone(actions)
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        self.assertIsNone(actions)
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
     def test_success(
         self,
 
     def test_success(
         self,
@@ -718,8 +726,8 @@ class AddRelationTest(LibjujuTestCase):
                 self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
             )
 
                 self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
             )
 
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
     def test_success(
         self,
 
     def test_success(
         self,
@@ -735,9 +743,7 @@ class AddRelationTest(LibjujuTestCase):
             self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
         )
 
             self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
         )
 
-        mock_add_relation.assert_called_with(
-            "app1:relation1", "app2:relation2"
-        )
+        mock_add_relation.assert_called_with("app1:relation1", "app2:relation2")
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
@@ -755,9 +761,7 @@ class AddRelationTest(LibjujuTestCase):
             self.libjuju.add_relation("model", "app1:relation1", "saas_name",)
         )
 
             self.libjuju.add_relation("model", "app1:relation1", "saas_name",)
         )
 
-        mock_add_relation.assert_called_with(
-            "app1:relation1", "saas_name"
-        )
+        mock_add_relation.assert_called_with("app1:relation1", "saas_name")
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
@@ -840,8 +844,8 @@ class ConfigureApplicationTest(LibjujuTestCase):
             self.loop.run_until_complete(
                 self.libjuju.configure_application("model", "app", {"config"},)
             )
             self.loop.run_until_complete(
                 self.libjuju.configure_application("model", "app", {"config"},)
             )
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
 
 # TODO _get_api_endpoints_db test case
 
 
 # TODO _get_api_endpoints_db test case
@@ -976,9 +980,7 @@ class ConsumeTest(LibjujuTestCase):
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
 
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
 
-        self.loop.run_until_complete(
-            self.libjuju.consume("offer_url", "model_name")
-        )
+        self.loop.run_until_complete(self.libjuju.consume("offer_url", "model_name"))
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
@@ -1033,12 +1035,9 @@ class ConsumeTest(LibjujuTestCase):
     ):
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
     ):
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
-        mock_consume.side_effect = juju.errors.JujuAPIError({
-            "error": "",
-            "response": "",
-            "request-id": "",
-
-        })
+        mock_consume.side_effect = juju.errors.JujuAPIError(
+            {"error": "", "response": "", "request-id": ""}
+        )
 
         with self.assertRaises(juju.errors.JujuAPIError):
             self.loop.run_until_complete(
 
         with self.assertRaises(juju.errors.JujuAPIError):
             self.loop.run_until_complete(
@@ -1047,3 +1046,162 @@ class ConsumeTest(LibjujuTestCase):
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.add_cloud")
+class AddK8sTest(LibjujuTestCase):
+    def setUp(self):
+        super(AddK8sTest, self).setUp()
+        self.auth_data = {
+            "server": "https://192.168.0.21:16443",
+            "token": "1234",
+            "cacert": "cacert",
+        }
+
+    def test_add_k8s(self, mock_add_cloud):
+        self.loop.run_until_complete(
+            self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+        )
+        mock_add_cloud.assert_called_once()
+
+    def test_add_k8s_exception(self, mock_add_cloud):
+        mock_add_cloud.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+            )
+        mock_add_cloud.assert_called_once()
+
+    def test_add_k8s_missing_name(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("", self.auth_data, "storage_class")
+            )
+        mock_add_cloud.assert_not_called()
+
+    def test_add_k8s_missing_storage_name(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("cloud", self.auth_data, "")
+            )
+        mock_add_cloud.assert_not_called()
+
+    def test_add_k8s_missing_auth_data_keys(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("cloud", {}, "")
+            )
+        mock_add_cloud.assert_not_called()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.add_cloud")
+@asynctest.mock.patch("juju.controller.Controller.add_credential")
+class AddCloudTest(LibjujuTestCase):
+    def setUp(self):
+        super(AddCloudTest, self).setUp()
+        self.cloud = juju.client.client.Cloud()
+        self.credential = juju.client.client.CloudCredential()
+
+    def test_add_cloud_with_credential(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        cloud = self.loop.run_until_complete(
+            self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+        )
+        self.assertEqual(cloud, self.cloud)
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_called_once_with(
+            "cloud", credential=self.credential, cloud="cloud"
+        )
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_cloud_no_credential(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        cloud = self.loop.run_until_complete(
+            self.libjuju.add_cloud("cloud", self.cloud)
+        )
+        self.assertEqual(cloud, self.cloud)
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_not_called()
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_cloud_exception(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_add_cloud.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+            )
+
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_not_called()
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_credential_exception(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_add_credential.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+            )
+
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_called_once_with(
+            "cloud", credential=self.credential, cloud="cloud"
+        )
+        mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.remove_cloud")
+class RemoveCloudTest(LibjujuTestCase):
+    def setUp(self):
+        super(RemoveCloudTest, self).setUp()
+
+    def test_remove_cloud(
+        self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+        mock_remove_cloud.assert_called_once_with("cloud")
+        mock_disconnect_controller.assert_called_once()
+
+    def test_remove_cloud_exception(
+        self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_remove_cloud.side_effect = Exception()
+
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+        mock_remove_cloud.assert_called_once_with("cloud")
+        mock_disconnect_controller.assert_called_once()