Skip to content

Commit

Permalink
[API] Fix leader format in list projects endpoint (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedingber committed Sep 28, 2021
1 parent 9660289 commit e6b0000
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
20 changes: 10 additions & 10 deletions mlrun/api/api/endpoints/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ def list_projects(
mlrun.api.api.deps.get_db_session
),
):
projects_output = get_project_member().list_projects(
db_session,
owner,
mlrun.api.schemas.ProjectsFormat.name_only,
labels,
state,
auth_info.projects_role,
auth_info.session,
)
allowed_project_names = projects_output.projects
allowed_project_names = None
# skip permission check if it's the leader
if not _is_request_from_leader(auth_info.projects_role):
projects_output = get_project_member().list_projects(
db_session,
owner,
mlrun.api.schemas.ProjectsFormat.name_only,
labels,
state,
auth_info.projects_role,
auth_info.session,
)
allowed_project_names = mlrun.api.utils.auth.verifier.AuthVerifier().filter_projects_by_permissions(
projects_output.projects, auth_info,
)
Expand Down
2 changes: 1 addition & 1 deletion mlrun/api/db/sqldb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ def list_projects(
query = self._query(session, Project, owner=owner, state=state)
if labels:
query = self._add_labels_filter(session, query, Project, labels)
if names:
if names is not None:
query = query.filter(Project.name.in_(names))
project_records = query.all()
projects = []
Expand Down
2 changes: 1 addition & 1 deletion mlrun/api/utils/projects/follower.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _filter_projects(
state: mlrun.api.schemas.ProjectState = None,
names: typing.Optional[typing.List[str]] = None,
) -> typing.List[mlrun.api.schemas.Project]:
if names:
if names is not None:
projects = [
project for project in projects if project.metadata.name in names
]
Expand Down
35 changes: 35 additions & 0 deletions tests/api/api/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,41 @@ def test_delete_project_deletion_strategy_check(
assert response.status_code == HTTPStatus.PRECONDITION_FAILED.value


# leader format is only relevant to follower mode
@pytest.mark.parametrize("project_member_mode", ["follower"], indirect=True)
def test_list_projects_leader_format(
db: Session, client: TestClient, project_member_mode: str
) -> None:
"""
See list_projects in follower.py for explanation on the rationality behind the leader format
"""
# create some projects in the db (mocking projects left there from before when leader format was used)
project_names = []
for _ in range(5):
project_name = f"prj-{uuid4().hex}"
project = mlrun.api.schemas.Project(
metadata=mlrun.api.schemas.ProjectMetadata(name=project_name),
)
mlrun.api.utils.singletons.db.get_db().create_project(db, project)
project_names.append(project_name)

# list in leader format
response = client.get(
"/api/projects",
params={"format": mlrun.api.schemas.ProjectsFormat.leader},
headers={
mlrun.api.schemas.HeaderNames.projects_role: mlrun.mlconf.httpdb.projects.leader
},
)
returned_project_names = [
project["data"]["metadata"]["name"] for project in response.json()["projects"]
]
assert (
deepdiff.DeepDiff(project_names, returned_project_names, ignore_order=True,)
== {}
)


def test_projects_crud(
db: Session, client: TestClient, project_member_mode: str
) -> None:
Expand Down
6 changes: 6 additions & 0 deletions tests/api/db/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def test_list_project_names_filter(
== {}
)

projects_output = db.list_projects(
db_session, format_=mlrun.api.schemas.ProjectsFormat.name_only, names=[],
)

assert projects_output.projects == []


# running only on sqldb cause filedb is not really a thing anymore, will be removed soon
@pytest.mark.parametrize(
Expand Down
5 changes: 5 additions & 0 deletions tests/api/utils/projects/test_follower_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ def test_list_project(
names=[archived_project.metadata.name, labeled_project.metadata.name],
)

# list no valid names
_assert_list_projects(
projects_follower, [], names=[],
)

# list labeled - key existence
_assert_list_projects(
projects_follower,
Expand Down

0 comments on commit e6b0000

Please sign in to comment.