Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip database creation for mysqlrouter extra user role #227

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe this should be libpatch 27 (on the vm charm - which where the charm lib gets published)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
Comment on lines +182 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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