Skip to content

Commit

Permalink
fix: add support for minor versions (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Dec 6, 2023
1 parent 2a0bc88 commit 959dd89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions iam_groups_authn/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
InstanceConnectionName,
)
from iam_groups_authn.iam_admin import get_iam_users
from iam_groups_authn.utils import DatabaseVersion
from iam_groups_authn.utils import DatabaseVersion, strip_minor_version
from iam_groups_authn.mysql import (
init_mysql_connection_engine,
MysqlRoleService,
Expand Down Expand Up @@ -74,7 +74,6 @@ async def groups_sync(
async with ClientSession(
headers={"Content-Type": "application/json"}
) as client_session:

# create UserService object for API calls
user_service = UserService(client_session, credentials)

Expand Down Expand Up @@ -369,6 +368,8 @@ async def get_database_version(self, instance_connection_name):
logging.debug(
f"[{project}:{region}:{instance}] Database version found: {database_version}"
)
# if major version is supported, we support minor version
database_version = strip_minor_version(database_version)
return DatabaseVersion(database_version)
except ValueError as e:
raise ValueError(
Expand Down
10 changes: 10 additions & 0 deletions iam_groups_authn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ def grant_group_role(self, role, users):
@abstractmethod
def revoke_group_role(self, role, users):
pass


def strip_minor_version(database_version: str) -> str:
"""
Helper method for stripping minor version suffix from database version.
"""
for version in DatabaseVersion.__members__.keys():
if database_version.startswith(version):
database_version = version
return database_version
38 changes: 38 additions & 0 deletions tests/unit/test_database_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from iam_groups_authn.utils import strip_minor_version

test_data = [
("MYSQL_8_0", "MYSQL_8_0"),
("MYSQL_8_0_26", "MYSQL_8_0"),
("MYSQL_8_0_35", "MYSQL_8_0"),
("POSTGRES_15", "POSTGRES_15"),
("POSTGRES_14", "POSTGRES_14"),
("POSTGRES_13", "POSTGRES_13"),
("POSTGRES_12", "POSTGRES_12"),
("POSTGRES_11", "POSTGRES_11"),
("POSTGRES_10", "POSTGRES_10"),
("POSTGRES_9_6", "POSTGRES_9_6"),
]

@pytest.mark.parametrize("database_version,expected", test_data)
def test_strip_minor_version(database_version, expected):
"""
Test that strip_minor_version() works correctly.
"""
database_version = strip_minor_version(database_version)
assert database_version == expected

0 comments on commit 959dd89

Please sign in to comment.