import logging
+import re
from typing import NoReturn, Optional
from ops.main import main
from opslib.osm.charm import CharmedOsmBase, RelationsMissing
from opslib.osm.interfaces.kafka import KafkaClient
from opslib.osm.interfaces.mongo import MongoClient
+from opslib.osm.interfaces.mysql import MysqlClient
from opslib.osm.pod import (
ContainerV3Builder,
PodSpecV3Builder,
logger = logging.getLogger(__name__)
PORT = 9999
+DEFAULT_MYSQL_DATABASE = "pol"
class ConfigModel(ModelValidator):
log_level: str
mongodb_uri: Optional[str]
+ mysql_uri: Optional[str]
@validator("log_level")
def validate_log_level(cls, v):
raise ValueError("mongodb_uri is not properly formed")
return v
+ @validator("mysql_uri")
+ def validate_mysql_uri(cls, v):
+ pattern = re.compile("^mysql:\/\/.*:.*@.*:\d+\/.*$") # noqa: W605
+ if v and not pattern.search(v):
+ raise ValueError("mysql_uri is not properly formed")
+ return v
+
class PolCharm(CharmedOsmBase):
def __init__(self, *args) -> NoReturn:
self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
+ self.mysql_client = MysqlClient(self, "mysql")
+ self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
+ self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
+
def _check_missing_dependencies(self, config: ConfigModel):
missing_relations = []
missing_relations.append("kafka")
if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
missing_relations.append("mongodb")
-
+ if not config.mysql_uri and self.mysql_client.is_missing_data_in_unit():
+ missing_relations.append("mysql")
if missing_relations:
raise RelationsMissing(missing_relations)
if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
raise Exception("Mongodb data cannot be provided via config and relation")
+ if config.mysql_uri and not self.mysql_client.is_missing_data_in_unit():
+ raise Exception("Mysql data cannot be provided via config and relation")
# Check relations
self._check_missing_dependencies(config)
"OSMPOL_DATABASE_DRIVER": "mongo",
"OSMPOL_DATABASE_URI": config.mongodb_uri
or self.mongodb_client.connection_string,
+ "OSMPOL_SQL_DATABASE_URI": config.mysql_uri
+ or self.mysql_client.get_root_uri(DEFAULT_MYSQL_DATABASE),
}
)
container = container_builder.build()
self,
) -> NoReturn:
"Test with relations and mongodb config (internal)"
+ self.initialize_mysql_relation()
self.initialize_kafka_relation()
self.initialize_mongo_config()
# Verifying status
"Test with relations (internal)"
self.initialize_kafka_relation()
self.initialize_mongo_relation()
+ self.initialize_mysql_relation()
# Verifying status
self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
# Verifying status
self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+ def test_mysql_config_success(self):
+ self.initialize_kafka_relation()
+ self.initialize_mongo_relation()
+ self.initialize_mysql_config()
+ # Verifying status
+ self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
+
+ def test_mysql_config_wrong_value(self):
+ self.initialize_kafka_relation()
+ self.initialize_mongo_relation()
+ self.initialize_mysql_config(uri="wrong_uri")
+ # Verifying status
+ self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+ self.assertIn(
+ "mysql_uri is not properly formed",
+ self.harness.charm.unit.status.message,
+ )
+
+ def test_mysql_config_and_relation(self):
+ self.initialize_mysql_relation()
+ self.initialize_mysql_config()
+ # Verifying status
+ self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+ # import pdb; pdb.set_trace()
+ self.assertIn(
+ "Mysql data cannot be provided via config and relation",
+ self.harness.charm.unit.status.message,
+ )
+
def initialize_kafka_relation(self):
kafka_relation_id = self.harness.add_relation("kafka", "kafka")
self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
{"connection_string": "mongodb://mongo:27017"},
)
+ def initialize_mysql_config(self, uri=None):
+ self.harness.update_config(
+ {"mysql_uri": uri or "mysql://user:pass@mysql-host:3306/database"}
+ )
+
+ def initialize_mysql_relation(self):
+ mongodb_relation_id = self.harness.add_relation("mysql", "mysql")
+ self.harness.add_relation_unit(mongodb_relation_id, "mysql/0")
+ self.harness.update_relation_data(
+ mongodb_relation_id,
+ "mysql/0",
+ {
+ "user": "user",
+ "password": "pass",
+ "host": "host",
+ "port": "1234",
+ "database": "pol",
+ "root_password": "root_password",
+ },
+ )
+
if __name__ == "__main__":
unittest.main()