diff --git a/lib/charms/mysql/v0/mysql.py b/lib/charms/mysql/v0/mysql.py index 3a860cad8..24f2e6390 100644 --- a/lib/charms/mysql/v0/mysql.py +++ b/lib/charms/mysql/v0/mysql.py @@ -91,7 +91,7 @@ def wait_until_mysql_connection(self) -> None: # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 27 +LIBPATCH = 28 UNIT_TEARDOWN_LOCKNAME = "unit-teardown" @@ -487,6 +487,7 @@ def create_application_database_and_scoped_user( hostname: str, *, unit_name: str = None, + create_database: bool = True, ) -> None: """Create an application database and a user scoped to the created database. @@ -496,6 +497,7 @@ def create_application_database_and_scoped_user( password: The password of the scoped user hostname: The hostname of the scoped user unit_name: The name of the unit from which the user will be accessed + create_database: Whether to create database Raises MySQLCreateApplicationDatabaseAndScopedUserError if there is an issue creating the application database or a user scoped to the database @@ -521,7 +523,8 @@ def create_application_database_and_scoped_user( f'session.run_sql("GRANT ALL PRIVILEGES ON `{database_name}`.* TO `{username}`@`{hostname}`;")', ) - self._run_mysqlsh_script("\n".join(create_database_commands)) + if create_database: + self._run_mysqlsh_script("\n".join(create_database_commands)) self._run_mysqlsh_script("\n".join(create_scoped_user_commands)) except MySQLClientError as e: logger.exception( diff --git a/src/relations/mysql_provider.py b/src/relations/mysql_provider.py index 49a1cc1c7..62c3a88c4 100644 --- a/src/relations/mysql_provider.py +++ b/src/relations/mysql_provider.py @@ -165,17 +165,26 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None: self.database.set_endpoints(relation_id, f"{primary_endpoint}:3306") replicas_endpoint = socket.getfqdn(f"{self.charm.app.name}-replicas") self.database.set_read_only_endpoints(relation_id, f"{replicas_endpoint}:3306") - # TODO: - # add setup of tls, tls_ca and status - # add extra roles parsing from relation data - self.charm._mysql.create_application_database_and_scoped_user( - db_name, db_user, db_pass, "%" - ) if "mysqlrouter" in extra_user_roles: + self.charm._mysql.create_application_database_and_scoped_user( + db_name, + db_user, + db_pass, + "%", + # MySQL Router charm does not need a new database + create_database=False, + ) self.charm._mysql.grant_privileges_to_user( db_user, "%", ["ALL PRIVILEGES"], with_grant_option=True ) + else: + # TODO: + # add setup of tls, tls_ca and status + # add extra roles parsing from relation data + self.charm._mysql.create_application_database_and_scoped_user( + db_name, db_user, db_pass, "%" + ) logger.info(f"Created user for app {remote_app}") return