Skip to content

Commit

Permalink
Notebook: refactor blueprint business logic to core (#429)
Browse files Browse the repository at this point in the history
The move of business logic out of the blueprint allows the code to
be reusable without having to go through the web API. It also allows
for tests to be added without the web complexity.

Tests have been refactored so that test clusters will be created only
when testing endpoints that actually require a cluster. This has the
advantage of speeding up test time. This is especially useful when
doing development.

Finally, a new test option has been added so one can use a local
cluster in place of re-creating one each time tests are run.
  • Loading branch information
sgaist authored and olevski committed Oct 30, 2024
1 parent d235458 commit 3157a0f
Show file tree
Hide file tree
Showing 18 changed files with 941 additions and 776 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ docker-compose.override.yml
result
*.qcow2

# tests
.k3d-config.yaml

# Misc
*.pem
*.gz
*.tgz
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ When reporting a bug, please include the following information:
If you do not already have a development environment set up, you will probably find the [developer documentation](DEVELOPING.md) helpful.

* Before submitting a pull request, please make sure you agree to the license and have submitted a signed [contributor license agreement](https://github.com/SwissDataScienceCenter/renku/wiki/Legal)
* PRs should include a short, descriptive title. The titles will be used to compile changelogs for releases, so think about the title in that context. The title should be formatted using the [Conventional Commits](https://www.conventionalcommits.org/) style
* PRs should include a short, descriptive title. The titles will be used to compile changelogs for releases, so think about the title in that context. The title should be formatted using the [Conventional Commits](https://www.conventionalcommits.org/) style
* Small improvements need not reference an issue, but PRs that introduce larger changes or add new functionality should refer to an issue
* Structure your commits in meaningful units, each with an understandable purpose and coherent commit message. For example, if your proposed changes contain a refactoring and a new feature, make two PRs
* Format commit messages using the [Conventional Commits](https://www.conventionalcommits.org/) style

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ style_checks: ## Run linting and style checks
@echo "checking session apispec is up to date"
@$(call test_apispec_up_to_date,"session")
poetry run mypy
@echo "checking data connectors apispec is up to date"
@$(call test_apispec_up_to_date,"data_connectors")
#poetry run mypy
poetry run ruff format --check
poetry run ruff check .
poetry run bandit -c pyproject.toml -r .
Expand Down
2 changes: 1 addition & 1 deletion bases/renku_data_services/data_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def register_all_handlers(app: Sanic, config: Config) -> Sanic:
nb_config=config.nb_config,
project_repo=config.project_repo,
session_repo=config.session_repo,
storage_repo=config.storage_v2_repo,
storage_repo=config.storage_repo,
rp_repo=config.rp_repo,
internal_gitlab_authenticator=config.gitlab_authenticator,
)
Expand Down
24 changes: 24 additions & 0 deletions components/renku_data_services/base_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ async def decorated_function(request: Request, *args: _P.args, **kwargs: _P.kwar
return decorator


def validate_path_project_id(
f: Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]],
) -> Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]]:
"""Decorator for a Sanic handler that validates the project_id path parameter."""
_path_project_id_regex = re.compile(r"^[A-Za-z0-9]{26}$")

@wraps(f)
async def decorated_function(request: Request, *args: _P.args, **kwargs: _P.kwargs) -> _T:
project_id = cast(str | None, kwargs.get("project_id"))
if not project_id:
raise errors.ProgrammingError(
message="Could not find 'project_id' in the keyword arguments for the handler in order to validate it."
)
if not _path_project_id_regex.match(project_id):
raise errors.ValidationError(
message=f"The 'project_id' path parameter {project_id} does not match the required "
f"regex {_path_project_id_regex}"
)

return await f(request, *args, **kwargs)

return decorated_function


def validate_path_user_id(
f: Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]],
) -> Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ async def get_server_logs(
# NOTE: this get_server ensures the user has access to the server without it you could read someone elses logs
server = await self.get_server(server_name, safe_username)
if not server:
raise MissingResourceError(
f"Cannot find server {server_name} for user " f"{safe_username} to retrieve logs."
raise errors.MissingResourceError(
message=f"Cannot find server {server_name} for user " f"{safe_username} to retrieve logs."
)
pod_name = f"{server_name}-0"
return await self.renku_ns_client.get_pod_logs(pod_name, max_log_lines)
Expand Down Expand Up @@ -490,9 +490,10 @@ async def delete_server(self, server_name: str, safe_username: str) -> None:
"""Delete the server."""
server = await self.get_server(server_name, safe_username)
if not server:
return None
await self.renku_ns_client.delete_server(server_name)
return None
raise errors.MissingResourceError(
message=f"Cannot find server {server_name} for user " f"{safe_username} in order to delete it."
)
return await self.renku_ns_client.delete_server(server_name)

async def patch_tokens(self, server_name: str, renku_tokens: RenkuTokens, gitlab_token: GitlabToken) -> None:
"""Patch the Renku and Gitlab access tokens used in a session."""
Expand Down
Loading

0 comments on commit 3157a0f

Please sign in to comment.