Skip to content

Commit 959dd89

Browse files
fix: add support for minor versions (#132)
1 parent 2a0bc88 commit 959dd89

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

iam_groups_authn/sync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
InstanceConnectionName,
2929
)
3030
from iam_groups_authn.iam_admin import get_iam_users
31-
from iam_groups_authn.utils import DatabaseVersion
31+
from iam_groups_authn.utils import DatabaseVersion, strip_minor_version
3232
from iam_groups_authn.mysql import (
3333
init_mysql_connection_engine,
3434
MysqlRoleService,
@@ -74,7 +74,6 @@ async def groups_sync(
7474
async with ClientSession(
7575
headers={"Content-Type": "application/json"}
7676
) as client_session:
77-
7877
# create UserService object for API calls
7978
user_service = UserService(client_session, credentials)
8079

@@ -369,6 +368,8 @@ async def get_database_version(self, instance_connection_name):
369368
logging.debug(
370369
f"[{project}:{region}:{instance}] Database version found: {database_version}"
371370
)
371+
# if major version is supported, we support minor version
372+
database_version = strip_minor_version(database_version)
372373
return DatabaseVersion(database_version)
373374
except ValueError as e:
374375
raise ValueError(

iam_groups_authn/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,13 @@ def grant_group_role(self, role, users):
8787
@abstractmethod
8888
def revoke_group_role(self, role, users):
8989
pass
90+
91+
92+
def strip_minor_version(database_version: str) -> str:
93+
"""
94+
Helper method for stripping minor version suffix from database version.
95+
"""
96+
for version in DatabaseVersion.__members__.keys():
97+
if database_version.startswith(version):
98+
database_version = version
99+
return database_version
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
from iam_groups_authn.utils import strip_minor_version
18+
19+
test_data = [
20+
("MYSQL_8_0", "MYSQL_8_0"),
21+
("MYSQL_8_0_26", "MYSQL_8_0"),
22+
("MYSQL_8_0_35", "MYSQL_8_0"),
23+
("POSTGRES_15", "POSTGRES_15"),
24+
("POSTGRES_14", "POSTGRES_14"),
25+
("POSTGRES_13", "POSTGRES_13"),
26+
("POSTGRES_12", "POSTGRES_12"),
27+
("POSTGRES_11", "POSTGRES_11"),
28+
("POSTGRES_10", "POSTGRES_10"),
29+
("POSTGRES_9_6", "POSTGRES_9_6"),
30+
]
31+
32+
@pytest.mark.parametrize("database_version,expected", test_data)
33+
def test_strip_minor_version(database_version, expected):
34+
"""
35+
Test that strip_minor_version() works correctly.
36+
"""
37+
database_version = strip_minor_version(database_version)
38+
assert database_version == expected

0 commit comments

Comments
 (0)