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 #210

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.

should this libpatch be 27 instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

preemptively bumped it for #208

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect CI will fail if you try to release LIBPATCH = 27 after merging this LIBPATCH = 28 (also, AFAIK charmcraft will refuse to upload the library with gap in LIBPATCH). I could be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes my plan was to merge #208 first


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

Choose a reason for hiding this comment

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

JFMI, is it still topical? mysql-router-k8s supports TLS since the last sprint AFAIK, mysql-router is subordinate == no TLS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure—also this is the mysql charm, not the mysql router charm

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