Skip to content

Commit

Permalink
Skip database creation for mysqlrouter extra user role (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlcsaposs-canonical authored May 18, 2023
1 parent f9bb0a1 commit 66db346
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/charms/mysql/v0/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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(
Expand Down
19 changes: 14 additions & 5 deletions src/relations/mysql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,25 @@ def _on_database_requested(self, event: DatabaseRequestedEvent):
self.database.set_endpoints(relation_id, rw_endpoints)
self.database.set_version(relation_id, db_version)
self.database.set_read_only_endpoints(relation_id, ro_endpoints)
# TODO:
# add setup of tls, tls_ca and status
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
self.charm._mysql.create_application_database_and_scoped_user(
db_name, db_user, db_pass, "%"
)

logger.info(f"Created user for app {remote_app}")
except (
Expand Down

0 comments on commit 66db346

Please sign in to comment.