Skip to content

Commit

Permalink
Merge pull request #239 from aiven/carobme-unpinned-pools
Browse files Browse the repository at this point in the history
add support for unpinned connection pools [BF-552]

#239
  • Loading branch information
kmichel-aiven authored Nov 12, 2021
2 parents a1beb77 + f1ecbca commit bd39210
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
12 changes: 8 additions & 4 deletions aiven/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions aiven/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
except ImportError:
__version__ = "UNKNOWN"

UNCHANGED = object() # used as a sentinel value


class Error(Exception):
"""Request error"""
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
92 changes: 92 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit bd39210

Please sign in to comment.