Skip to content

Commit

Permalink
Skip database creation for mysqlrouter extra user role (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlcsaposs-canonical authored May 18, 2023
1 parent c2e396f commit 3b454dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 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
21 changes: 15 additions & 6 deletions src/relations/mysql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b454dd

Please sign in to comment.