Removing pydantic from POL charm 63/10163/2
authorsousaedu <eduardo.sousa@canonical.com>
Tue, 5 Jan 2021 15:25:10 +0000 (15:25 +0000)
committerbeierlm <mark.beierl@canonical.com>
Tue, 5 Jan 2021 20:23:36 +0000 (21:23 +0100)
Change-Id: I0c8a7cac50f7f81f61fb151d8166846cf4c4fd18
Signed-off-by: sousaedu <eduardo.sousa@canonical.com>
installers/charm/pol/requirements.txt
installers/charm/pol/src/charm.py
installers/charm/pol/src/pod_spec.py
installers/charm/pol/tests/test_charm.py
installers/charm/pol/tests/test_pod_spec.py
installers/charm/pol/tox.ini

index 24f1672..a26601f 100644 (file)
@@ -20,5 +20,4 @@
 ##
 
 ops
-pydantic
 git+https://github.com/juju-solutions/resource-oci-image/@c5778285d332edf3d9a538f9d0c06154b7ec1b0b#egg=oci-image
index 649e15b..6d15c57 100755 (executable)
@@ -21,7 +21,6 @@
 ##
 
 import logging
-from pydantic import ValidationError
 from typing import Any, Dict, NoReturn
 
 from ops.charm import CharmBase, CharmEvents
@@ -99,10 +98,8 @@ class PolCharm(CharmBase):
         Args:
             event (EventBase): Kafka relation event.
         """
-        data_loc = event.unit if event.unit else event.app
-
-        message_host = event.relation.data[data_loc].get("host")
-        message_port = event.relation.data[data_loc].get("port")
+        message_host = event.relation.data[event.unit].get("host")
+        message_port = event.relation.data[event.unit].get("port")
 
         if (
             message_host
@@ -132,9 +129,7 @@ class PolCharm(CharmBase):
         Args:
             event (EventBase): DB relation event.
         """
-        data_loc = event.unit if event.unit else event.app
-
-        database_uri = event.relation.data[data_loc].get("connection_string")
+        database_uri = event.relation.data[event.unit].get("connection_string")
 
         if database_uri and self.state.database_uri != database_uri:
             self.state.database_uri = database_uri
@@ -215,7 +210,7 @@ class PolCharm(CharmBase):
                 self.relation_state,
                 self.model.app.name,
             )
-        except ValidationError as exc:
+        except ValueError as exc:
             logger.exception("Config/Relation data validation error")
             self.unit.status = BlockedStatus(str(exc))
             return
index 2caafbd..ec7f2d9 100644 (file)
 ##
 
 import logging
-from pydantic import BaseModel, constr, PositiveInt
-from typing import Any, Dict, List
+from typing import Any, Dict, List, NoReturn
 
 logger = logging.getLogger(__name__)
 
 
-class ConfigData(BaseModel):
-    """Configuration data model."""
+def _validate_data(
+    config_data: Dict[str, Any], relation_data: Dict[str, Any]
+) -> NoReturn:
+    """Validate input data.
 
-    log_level: constr(regex=r"^(INFO|DEBUG)$")
+    Args:
+        config_data (Dict[str, Any]): configuration data.
+        relation_data (Dict[str, Any]): relation data.
+    """
+    config_validators = {
+        "log_level": lambda value, _: isinstance(value, str)
+        and value in ("INFO", "DEBUG"),
+    }
+    relation_validators = {
+        "message_host": lambda value, _: isinstance(value, str) and len(value) > 0,
+        "message_port": lambda value, _: isinstance(value, int) and value > 0,
+        "database_uri": lambda value, _: isinstance(value, str)
+        and value.startswith("mongodb://"),
+    }
+    problems = []
+
+    for key, validator in config_validators.items():
+        valid = validator(config_data.get(key), config_data)
+
+        if not valid:
+            problems.append(key)
 
+    for key, validator in relation_validators.items():
+        valid = validator(relation_data.get(key), relation_data)
 
-class RelationData(BaseModel):
-    """Relation data model."""
+        if not valid:
+            problems.append(key)
 
-    message_host: constr(min_length=1)
-    message_port: PositiveInt
-    database_uri: constr(regex=r"^(mongodb://)")
+    if len(problems) > 0:
+        raise ValueError("Errors found in: {}".format(", ".join(problems)))
 
 
 def _make_pod_ports(port: int) -> List[Dict[str, Any]]:
@@ -152,8 +174,7 @@ def make_pod_spec(
     if not image_info:
         return None
 
-    ConfigData(**(config))
-    RelationData(**(relation_state))
+    _validate_data(config, relation_state)
 
     ports = _make_pod_ports(port)
     env_config = _make_pod_envconfig(config, relation_state)
index 68130fc..a2e81cc 100644 (file)
@@ -123,34 +123,6 @@ class TestCharm(unittest.TestCase):
 
         self.assertDictEqual(expected_result, pod_spec)
 
-    def test_on_kafka_app_relation_changed(self) -> NoReturn:
-        """Test to see if kafka relation is updated."""
-        self.harness.charm.on.start.emit()
-
-        self.assertIsNone(self.harness.charm.state.message_host)
-        self.assertIsNone(self.harness.charm.state.message_port)
-
-        relation_id = self.harness.add_relation("kafka", "kafka")
-        self.harness.add_relation_unit(relation_id, "kafka/0")
-        self.harness.update_relation_data(
-            relation_id, "kafka", {"host": "kafka", "port": 9092}
-        )
-
-        self.assertEqual(self.harness.charm.state.message_host, "kafka")
-        self.assertEqual(self.harness.charm.state.message_port, 9092)
-
-        # Verifying status
-        self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
-        # Verifying status message
-        self.assertGreater(len(self.harness.charm.unit.status.message), 0)
-        self.assertTrue(
-            self.harness.charm.unit.status.message.startswith("Waiting for ")
-        )
-        self.assertNotIn("kafka", self.harness.charm.unit.status.message)
-        self.assertIn("mongodb", self.harness.charm.unit.status.message)
-        self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
-
     def test_on_kafka_unit_relation_changed(self) -> NoReturn:
         """Test to see if kafka relation is updated."""
         self.harness.charm.on.start.emit()
@@ -179,32 +151,6 @@ class TestCharm(unittest.TestCase):
         self.assertIn("mongodb", self.harness.charm.unit.status.message)
         self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
 
-    def test_on_mongodb_app_relation_changed(self) -> NoReturn:
-        """Test to see if mongodb relation is updated."""
-        self.harness.charm.on.start.emit()
-
-        self.assertIsNone(self.harness.charm.state.database_uri)
-
-        relation_id = self.harness.add_relation("mongodb", "mongodb")
-        self.harness.add_relation_unit(relation_id, "mongodb/0")
-        self.harness.update_relation_data(
-            relation_id, "mongodb", {"connection_string": "mongodb://mongo:27017"}
-        )
-
-        self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
-
-        # Verifying status
-        self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
-        # Verifying status message
-        self.assertGreater(len(self.harness.charm.unit.status.message), 0)
-        self.assertTrue(
-            self.harness.charm.unit.status.message.startswith("Waiting for ")
-        )
-        self.assertIn("kafka", self.harness.charm.unit.status.message)
-        self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
-        self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
-
     def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
         """Test to see if mongodb relation is updated."""
         self.harness.charm.on.start.emit()
index 5ca5a93..eb5f5cf 100644 (file)
@@ -20,7 +20,6 @@
 # osm-charmers@lists.launchpad.net
 ##
 
-from pydantic import ValidationError
 from typing import NoReturn
 import unittest
 
@@ -196,7 +195,7 @@ class TestPodSpec(unittest.TestCase):
         app_name = "pol"
         port = 80
 
-        with self.assertRaises(ValidationError):
+        with self.assertRaises(ValueError):
             pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
 
     def test_make_pod_spec_without_relation_state(self) -> NoReturn:
@@ -209,7 +208,7 @@ class TestPodSpec(unittest.TestCase):
         app_name = "pol"
         port = 80
 
-        with self.assertRaises(ValidationError):
+        with self.assertRaises(ValueError):
             pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
 
 
index 4bc50b7..25bf830 100644 (file)
@@ -56,7 +56,6 @@ deps =
   stestr
   mock
   ops
-  pydantic
 setenv =
   {[testenv]setenv}
   PYTHON=coverage run