-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add resource class id in session launcher #249
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce4d761
feat: add resource class id in session launcher
andre-code c4f5808
feat: add validation for resource_class_id access in launcher insert/…
andre-code 55575c6
fixes
leafty cfe55ec
update migration file to don't include index for resource_class_id in…
andre-code b8b8464
remove unnecessary noqa: E501
andre-code bab430e
remove unnecessary comment and fix style_check
andre-code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
..._data_services/migrations/versions/57dfd69ea814_add_resource_class_in_session_launcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""add resource class in session launcher | ||
|
||
Revision ID: 57dfd69ea814 | ||
Revises: c0631477aea4 | ||
Create Date: 2024-06-10 17:05:39.618418 | ||
|
||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "57dfd69ea814" | ||
down_revision = "c0631477aea4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("launchers", sa.Column("resource_class_id", sa.Integer(), nullable=True), schema="sessions") | ||
|
||
op.create_foreign_key( | ||
"fk_sessions_launchers_resource_class_id", | ||
"launchers", | ||
"resource_classes", | ||
["resource_class_id"], | ||
["id"], | ||
source_schema="sessions", | ||
referent_schema="resource_pools", | ||
ondelete="SET NULL", | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint("fk_sessions_launchers_resource_class_id", "launchers", schema="sessions", type_="foreignkey") | ||
op.drop_column("launchers", "resource_class_id", schema="sessions") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
from collections.abc import Callable | ||
from datetime import UTC, datetime | ||
from typing import Any | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
@@ -12,6 +13,7 @@ | |
from renku_data_services import errors | ||
from renku_data_services.authz.authz import Authz, ResourceType | ||
from renku_data_services.authz.models import Scope | ||
from renku_data_services.crc.db import ResourcePoolRepository | ||
from renku_data_services.session import apispec, models | ||
from renku_data_services.session import orm as schemas | ||
from renku_data_services.session.apispec import EnvironmentKind | ||
|
@@ -20,9 +22,12 @@ | |
class SessionRepository: | ||
"""Repository for sessions.""" | ||
|
||
def __init__(self, session_maker: Callable[..., AsyncSession], project_authz: Authz) -> None: | ||
def __init__( | ||
self, session_maker: Callable[..., AsyncSession], project_authz: Authz, resource_pools: ResourcePoolRepository | ||
) -> None: | ||
self.session_maker = session_maker | ||
self.project_authz: Authz = project_authz | ||
self.resource_pools: ResourcePoolRepository = resource_pools | ||
|
||
async def get_environments(self) -> list[models.Environment]: | ||
"""Get all session environments from the database.""" | ||
|
@@ -181,6 +186,7 @@ async def insert_launcher( | |
description=new_launcher.description, | ||
environment_kind=new_launcher.environment_kind, | ||
environment_id=new_launcher.environment_id, | ||
resource_class_id=new_launcher.resource_class_id, | ||
container_image=new_launcher.container_image, | ||
default_url=new_launcher.default_url, | ||
created_by=models.Member(id=user.id), | ||
|
@@ -208,12 +214,30 @@ async def insert_launcher( | |
message=f"Session environment with id '{environment_id}' does not exist or you do not have access to it." # noqa: E501 | ||
) | ||
|
||
resource_class_id = new_launcher.resource_class_id | ||
if resource_class_id is not None: | ||
res = await session.scalars( | ||
select(schemas.ResourceClassORM).where(schemas.ResourceClassORM.id == resource_class_id) | ||
) | ||
resource_class = res.one_or_none() | ||
if resource_class is None: | ||
raise errors.MissingResourceError( | ||
message=f"Resource class with id '{resource_class_id}' does not exist." | ||
) | ||
|
||
res_classes = await self.resource_pools.get_classes(api_user=user, id=resource_class_id) | ||
resource_class_by_user = next((rc for rc in res_classes if rc.id == resource_class_id), None) | ||
if resource_class_by_user is None: | ||
raise errors.Unauthorized( | ||
message=f"Resource class with id '{resource_class_id}' you do not have access to it." | ||
) | ||
|
||
launcher = schemas.SessionLauncherORM.load(launcher_model) | ||
session.add(launcher) | ||
return launcher.dump() | ||
|
||
async def update_launcher( | ||
self, user: base_models.APIUser, launcher_id: str, **kwargs: dict | ||
self, user: base_models.APIUser, launcher_id: str, **kwargs: Any | ||
) -> models.SessionLauncher: | ||
"""Update a session launcher entry.""" | ||
if not user.is_authenticated or user.id is None: | ||
|
@@ -249,14 +273,34 @@ async def update_launcher( | |
message=f"Session environment with id '{environment_id}' does not exist or you do not have access to it." # noqa: E501 | ||
) | ||
|
||
resource_class_id = kwargs.get("resource_class_id") | ||
if resource_class_id is not None: | ||
res = await session.scalars( | ||
select(schemas.ResourceClassORM).where(schemas.ResourceClassORM.id == resource_class_id) | ||
) | ||
resource_class = res.one_or_none() | ||
if resource_class is None: | ||
raise errors.MissingResourceError( | ||
message=f"Resource class with id '{resource_class_id}' does not exist." | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
res_classes = await self.resource_pools.get_classes(api_user=user, id=resource_class_id) | ||
resource_class_by_user = next((rc for rc in res_classes if rc.id == resource_class_id), None) | ||
if resource_class_by_user is None: | ||
raise errors.Unauthorized( | ||
message=f"Resource class with id '{resource_class_id}' you do not have access to it." | ||
) | ||
|
||
for key, value in kwargs.items(): | ||
# NOTE: Only ``name``, ``description``, ``environment_kind``, | ||
# ``environment_id``, ``container_image`` and ``default_url`` can be edited. | ||
# ``environment_id``, ``resource_class_id``, ``container_image`` and | ||
# ``default_url`` can be edited. | ||
if key in [ | ||
"name", | ||
"description", | ||
"environment_kind", | ||
"environment_id", | ||
"resource_class_id", | ||
"container_image", | ||
"default_url", | ||
]: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: if the user is not used in this method at all, you can just remove the
@authenticate
decorator aboveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, included in the latest commit.