diff --git a/aiven/client/cli.py b/aiven/client/cli.py index db096f8a..5f2c0627 100644 --- a/aiven/client/cli.py +++ b/aiven/client/cli.py @@ -1558,7 +1558,7 @@ def service__metrics(self): @arg.service_name @arg("--pool-name", help="Connection pool name", required=True) @arg("--dbname", help="Service database name", required=True) - @arg("--username", help="Service username", required=True) + @arg("--username", help="Service username") @arg("--pool-size", type=int, help="Connection pool size") @arg("--pool-mode", help="Connection pool mode") @arg.json @@ -1578,22 +1578,26 @@ def service__connection_pool_create(self): @arg.service_name @arg("--pool-name", help="Connection pool name", required=True) @arg("--dbname", help="Service database name") - @arg("--username", help="Service username") + @arg("--username", help="Service username (set to empty string to remove the pool username)") @arg("--pool-size", type=int, help="Connection pool size") @arg("--pool-mode", help="Connection pool mode") @arg.json def service__connection_pool_update(self): """Update a connection pool for a given PostgreSQL service""" - self.client.update_service_connection_pool( + kwargs = dict( project=self.get_project(), service=self.args.service_name, pool_name=self.args.pool_name, dbname=self.args.dbname, - username=self.args.username, pool_size=self.args.pool_size, pool_mode=self.args.pool_mode, ) + if self.args.username is not None: + kwargs["username"] = self.args.username if self.args.username != "" else None + + self.client.update_service_connection_pool(**kwargs) + @arg.project @arg.service_name @arg("--pool-name", help="Connection pool name", required=True) diff --git a/aiven/client/client.py b/aiven/client/client.py index cd1b1fe0..fa72ec13 100644 --- a/aiven/client/client.py +++ b/aiven/client/client.py @@ -18,6 +18,8 @@ except ImportError: __version__ = "UNKNOWN" +UNCHANGED = object() # used as a sentinel value + class Error(Exception): """Request error""" @@ -221,11 +223,13 @@ def create_service_connection_pool( service, pool_name, dbname, - username, + username=None, pool_size=None, pool_mode=None, ): - body = {"database": dbname, "username": username, "pool_name": pool_name} + body = {"database": dbname, "pool_name": pool_name} + if username: + body["username"] = username if pool_size: body["pool_size"] = pool_size if pool_mode: @@ -242,12 +246,12 @@ def update_service_connection_pool( service, pool_name, dbname=None, - username=None, + username=UNCHANGED, pool_size=None, pool_mode=None, ): body = {} - if username is not None: + if username is not UNCHANGED: body["username"] = username if dbname is not None: body["database"] = dbname diff --git a/tests/test_cli.py b/tests/test_cli.py index 5eb5ccf1..42edfea8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -203,3 +203,95 @@ def test_version_eol_check(): with mock.patch("aiven.client.cli.get_current_date", return_value=fake_time_soon): cli._do_version_eol_check(service_type, service_version) # pylint: disable=protected-access cli.confirm.assert_called() # Confirmation should have been asked + + +def test_create_service_connection_pool(): + aiven_client = mock.Mock(spec_set=AivenClient) + aiven_client.update_service_connection_pool.return_value = {"message": "created"} + + build_aiven_cli(aiven_client).run( + args=[ + "service", "connection-pool-create", "--project", "testproject", "--dbname", "defaultdb", "--pool-name", "foo", + "--pool-size=23", "--username", "avnadmin", "pg-foo-bar" + ] + ) + + aiven_client.create_service_connection_pool.assert_called_with( + project="testproject", + service="pg-foo-bar", + pool_name="foo", + dbname="defaultdb", + username="avnadmin", + pool_size=23, + pool_mode=None + ) + + build_aiven_cli(aiven_client).run( + args=[ + "service", "connection-pool-create", "--project", "testproject", "--dbname", "defaultdb", "--pool-name", "bar", + "pg-foo-bar" + ] + ) + + aiven_client.create_service_connection_pool.assert_called_with( + project="testproject", + service="pg-foo-bar", + pool_name="bar", + dbname="defaultdb", + username=None, + pool_size=None, + pool_mode=None + ) + + +def test_update_service_connection_pool(): + aiven_client = mock.Mock(spec_set=AivenClient) + aiven_client.update_service_connection_pool.return_value = {"message": "updated"} + + # pin + build_aiven_cli(aiven_client).run( + args=[ + "service", "connection-pool-update", "--project", "testproject", "--pool-name", "foo", "--username", "avnadmin", + "pg-foo-bar" + ] + ) + + aiven_client.update_service_connection_pool.assert_called_with( + project="testproject", + service="pg-foo-bar", + pool_name="foo", + dbname=None, + username="avnadmin", + pool_size=None, + pool_mode=None + ) + + # unpin + build_aiven_cli(aiven_client).run( + args=[ + "service", "connection-pool-update", "--project", "testproject", "--pool-name", "foo", "--username", "", + "pg-foo-bar" + ] + ) + + aiven_client.update_service_connection_pool.assert_called_with( + project="testproject", + service="pg-foo-bar", + pool_name="foo", + dbname=None, + username=None, + pool_size=None, + pool_mode=None + ) + + # leave username as is, change pool-size instead + build_aiven_cli(aiven_client).run( + args=[ + "service", "connection-pool-update", "--project", "testproject", "--pool-name", "foo", "--pool-size", "42", + "pg-foo-bar" + ] + ) + + aiven_client.update_service_connection_pool.assert_called_with( + project="testproject", service="pg-foo-bar", pool_name="foo", dbname=None, pool_size=42, pool_mode=None + )