Skip to content
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
3 changes: 2 additions & 1 deletion lib/charms/postgresql_k8s/v1/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ def create_database(
raise PostgreSQLCreateDatabaseError() from e

# Enable preset extensions
self.enable_disable_extensions(dict.fromkeys(plugins, True), database)
if plugins:
self.enable_disable_extensions(dict.fromkeys(plugins, True), database)
Comment on lines +333 to +334
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lib change for PGB.


def create_user(
self,
Expand Down
18 changes: 12 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2497,15 +2497,21 @@ def relations_user_databases_map(self) -> dict:
})
return user_database_map
try:
for user in self.postgresql.list_users_from_relation(
current_host=self.is_connectivity_enabled
):
databases = ",".join(
for user in self.postgresql.list_users(current_host=self.is_connectivity_enabled):
if user in (
"backup",
"monitoring",
"operator",
"postgres",
"replication",
"rewind",
):
continue
Comment on lines +2501 to +2509
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied from the trigger.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please create a dedicated Jira ticket on Mohamed to decide the behavior here, as we are adding here backward compatibility to the current behavior which is not the best way to do in modernly hardened charm.

Thank you!

if databases := ",".join(
self.postgresql.list_accessible_databases_for_user(
user, current_host=self.is_connectivity_enabled
)
)
if databases:
):
user_database_map[user] = databases
else:
logger.debug(f"User {user} has no databases to connect to")
Comment on lines 2516 to 2517
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The structure of relations_user_databases_map changed with logical replication. Should it be ported back to 14/edge?

Copy link
Member

Choose a reason for hiding this comment

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

Done: #1075

Expand Down
56 changes: 56 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2678,3 +2678,59 @@ def test_generate_user_hash(harness):
assert harness.charm.generate_user_hash == sentinel.hash

_shake_128.assert_called_once_with(b"{'relation_id_2': 'test_db'}")


def test_relations_user_databases_map(harness):
with (
patch("charm.PostgresqlOperatorCharm.postgresql") as _postgresql,
patch("charm.Patroni.member_started", new_callable=PropertyMock) as _member_started,
patch(
"charm.PostgresqlOperatorCharm.is_cluster_initialised", new_callable=PropertyMock
) as _is_cluster_initialised,
):
# Initial empty results from the functions used in the property that's being tested.
_postgresql.list_users_from_relation.return_value = set()
_postgresql.list_accessible_databases_for_user.return_value = set()
_postgresql.list_access_groups.return_value = {
"identity_access",
"internal_access",
"relation_access",
}

# Test when the cluster isn't initialised yet.
_is_cluster_initialised.return_value = False
_member_started.return_value = True
assert harness.charm.relations_user_databases_map == {
"operator": "all",
"replication": "all",
"rewind": "all",
}

# Test when the cluster is initialised but the cluster member hasn't started yet.
_is_cluster_initialised.return_value = True
_member_started.return_value = False
assert harness.charm.relations_user_databases_map == {
"operator": "all",
"replication": "all",
"rewind": "all",
}

# Test when there are no relation users in the database.
_member_started.return_value = True
assert harness.charm.relations_user_databases_map == {}

# Test when there are relation users in the database.
_postgresql.list_users.return_value = ["user1", "user2"]
_postgresql.list_accessible_databases_for_user.side_effect = [["db1", "db2"], ["db3"]]
assert harness.charm.relations_user_databases_map == {"user1": "db1,db2", "user2": "db3"}

# Test when the access groups where not created yet.
_postgresql.list_accessible_databases_for_user.side_effect = [["db1", "db2"], ["db3"]]
_postgresql.list_access_groups.return_value = set()
assert harness.charm.relations_user_databases_map == {
"user1": "db1,db2",
"user2": "db3",
"operator": "all",
"replication": "all",
"rewind": "all",
}
Loading