Skip to content

Commit b2532b4

Browse files
sgaistolevski
authored andcommitted
Notebook: refactor blueprint business logic to core (#429)
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.
1 parent 23205d3 commit b2532b4

15 files changed

Lines changed: 934 additions & 769 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,10 @@ docker-compose.override.yml
8080
result
8181
*.qcow2
8282

83+
# tests
8384
.k3d-config.yaml
85+
86+
# Misc
87+
*.pem
88+
*.gz
89+
*.tgz

CONTRIBUTING.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ When reporting a bug, please include the following information:
2222
If you do not already have a development environment set up, you will probably find the [developer documentation](DEVELOPING.md) helpful.
2323

2424
* 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)
25-
* 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
25+
* 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
2626
* Small improvements need not reference an issue, but PRs that introduce larger changes or add new functionality should refer to an issue
2727
* 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
2828
* Format commit messages using the [Conventional Commits](https://www.conventionalcommits.org/) style
29-

components/renku_data_services/base_api/auth.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ async def decorated_function(request: Request, *args: _P.args, **kwargs: _P.kwar
7171
return decorator
7272

7373

74+
def validate_path_project_id(
75+
f: Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]],
76+
) -> Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]]:
77+
"""Decorator for a Sanic handler that validates the project_id path parameter."""
78+
_path_project_id_regex = re.compile(r"^[A-Za-z0-9]{26}$")
79+
80+
@wraps(f)
81+
async def decorated_function(request: Request, *args: _P.args, **kwargs: _P.kwargs) -> _T:
82+
project_id = cast(str | None, kwargs.get("project_id"))
83+
if not project_id:
84+
raise errors.ProgrammingError(
85+
message="Could not find 'project_id' in the keyword arguments for the handler in order to validate it."
86+
)
87+
if not _path_project_id_regex.match(project_id):
88+
raise errors.ValidationError(
89+
message=f"The 'project_id' path parameter {project_id} does not match the required "
90+
f"regex {_path_project_id_regex}"
91+
)
92+
93+
return await f(request, *args, **kwargs)
94+
95+
return decorated_function
96+
97+
7498
def validate_path_user_id(
7599
f: Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]],
76100
) -> Callable[Concatenate[Request, _P], Coroutine[Any, Any, _T]]:

components/renku_data_services/notebooks/api/classes/k8s_client.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ async def get_server_logs(
448448
# NOTE: this get_server ensures the user has access to the server without it you could read someone elses logs
449449
server = await self.get_server(server_name, safe_username)
450450
if not server:
451-
raise MissingResourceError(
452-
f"Cannot find server {server_name} for user " f"{safe_username} to retrieve logs."
451+
raise errors.MissingResourceError(
452+
message=f"Cannot find server {server_name} for user " f"{safe_username} to retrieve logs."
453453
)
454454
pod_name = f"{server_name}-0"
455455
return await self.renku_ns_client.get_pod_logs(pod_name, max_log_lines)
@@ -490,9 +490,10 @@ async def delete_server(self, server_name: str, safe_username: str) -> None:
490490
"""Delete the server."""
491491
server = await self.get_server(server_name, safe_username)
492492
if not server:
493-
return None
494-
await self.renku_ns_client.delete_server(server_name)
495-
return None
493+
raise errors.MissingResourceError(
494+
message=f"Cannot find server {server_name} for user " f"{safe_username} in order to delete it."
495+
)
496+
return await self.renku_ns_client.delete_server(server_name)
496497

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

components/renku_data_services/notebooks/blueprints.py

Lines changed: 34 additions & 582 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)