From a6d3c562dbdc2b7e4ffcdd8af3c9cd3b5fc1a7d9 Mon Sep 17 00:00:00 2001 From: Lucas Gameiro Borges Date: Fri, 5 Jul 2024 11:43:46 -0300 Subject: [PATCH] add libpq connection string --- src/relations/postgresql_provider.py | 6 ++++++ tests/unit/test_postgresql_provider.py | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/relations/postgresql_provider.py b/src/relations/postgresql_provider.py index 6b105fa602..476e04d44b 100644 --- a/src/relations/postgresql_provider.py +++ b/src/relations/postgresql_provider.py @@ -115,6 +115,12 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None: # Set the database name self.database_provides.set_database(event.relation.id, database) + # Set connection string URI. + self.database_provides.set_uris( + event.relation.id, + f"postgresql://{user}:{password}@{self.charm.primary_endpoint}:{DATABASE_PORT}/{database}", + ) + self._update_unit_status(event.relation) except ( PostgreSQLCreateDatabaseError, diff --git a/tests/unit/test_postgresql_provider.py b/tests/unit/test_postgresql_provider.py index cb8e010943..a1ee3e9038 100644 --- a/tests/unit/test_postgresql_provider.py +++ b/tests/unit/test_postgresql_provider.py @@ -62,7 +62,7 @@ def request_database(_harness): _harness.update_relation_data( rel_id, _harness.charm.app.name, - {"data": "", "username": "", "password": "", "version": "", "database": ""}, + {"data": "", "username": "", "password": "", "uris": "", "version": "", "database": ""}, ) # Simulate the request of a new database plus extra user roles. @@ -94,10 +94,11 @@ def test_on_database_requested(harness): _member_started.side_effect = [False, True, True, True, True, True] _primary_endpoint.side_effect = [ None, - {"1.1.1.1"}, - {"1.1.1.1"}, - {"1.1.1.1"}, - {"1.1.1.1"}, + "1.1.1.1", + "1.1.1.1", + "1.1.1.1", + "1.1.1.1", + "1.1.1.1", ] postgresql_mock.create_user = PropertyMock( side_effect=[None, PostgreSQLCreateUserError, None, None] @@ -141,6 +142,7 @@ def test_on_database_requested(harness): "data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}', "username": user, "password": "test-password", + "uris": f"postgresql://{user}:test-password@1.1.1.1:5432/{DATABASE}", "version": POSTGRESQL_VERSION, "database": f"{DATABASE}", }