-
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 polybox storage #527
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
578240d
feat: add polybox and switchDrive storage
andre-code 1a97357
add access_level checks for required options
andre-code ebb0042
feat: add transform_public_access_config for test connection
andre-code 6fc7c96
feat: add polybox and switchDrive when launch a session
andre-code 24a2592
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
andre-code 1bdf0ce
fix: transform data connector config for polybox and switchDrive
andre-code 1405f06
fix: remove access_level
andre-code 74edcc3
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
andre-code dc7ce49
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
Panaetius f6cef33
apply code suggestions
andre-code addb28b
fix typo
andre-code 9a58d06
add provider for polybox and switchDrive
andre-code 176a359
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
andre-code 888891b
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
andre-code 8744a6b
fix: break in two lines long text
andre-code 6c463aa
Merge branch 'main' into andrea/3392-add-polybox-switchdrive-schema
Panaetius 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||
import json | ||||||
import tempfile | ||||||
from collections.abc import Generator | ||||||
from copy import deepcopy | ||||||
from pathlib import Path | ||||||
from typing import TYPE_CHECKING, Any, NamedTuple, Union, cast | ||||||
|
||||||
|
@@ -93,7 +94,7 @@ def __patch_schema_s3_endpoint_required(spec: list[dict[str, Any]]) -> None: | |||||
@staticmethod | ||||||
def __patch_schema_add_switch_provider(spec: list[dict[str, Any]]) -> None: | ||||||
"""Adds a fake provider to help with setting up switch storage.""" | ||||||
s3 = next(s for s in spec if s["Prefix"] == "s3") | ||||||
s3 = RCloneValidator.__find_storage(spec, "s3") | ||||||
providers = next(o for o in s3["Options"] if o["Name"] == "provider") | ||||||
providers["Examples"].append({"Value": "Switch", "Help": "Switch Object Storage", "Provider": ""}) | ||||||
s3["Options"].append( | ||||||
|
@@ -156,6 +157,75 @@ def __patch_schema_remove_oauth_propeties(spec: list[dict[str, Any]]) -> None: | |||||
options.append(option) | ||||||
storage["Options"] = options | ||||||
|
||||||
@staticmethod | ||||||
def __find_storage(spec: list[dict[str, Any]], prefix: str) -> dict[str, Any]: | ||||||
"""Find and return the WebDAV storage schema from the spec.""" | ||||||
storage = next((s for s in spec if s["Prefix"] == prefix), None) | ||||||
if not storage: | ||||||
raise errors.ValidationError(message=f"'{prefix}' storage not found in schema.") | ||||||
return deepcopy(storage) | ||||||
|
||||||
@staticmethod | ||||||
def __add_webdav_based_storage( | ||||||
spec: list[dict[str, Any]], | ||||||
prefix: str, | ||||||
name: str, | ||||||
description: str, | ||||||
url_value: str, | ||||||
public_link_help: str, | ||||||
) -> None: | ||||||
"""Create a modified copy of WebDAV storage and add it to the schema.""" | ||||||
# Find WebDAV storage schema and create a modified copy | ||||||
storage_copy = RCloneValidator.__find_storage(spec, "webDav") | ||||||
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.
Suggested change
I think this is a typo? at least it was lower case before. 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. yes, it is. thanks! |
||||||
storage_copy.update({"Prefix": prefix, "Name": name, "Description": description}) | ||||||
|
||||||
custom_option = [ | ||||||
{ | ||||||
"Name": "public_link", | ||||||
"Help": public_link_help, | ||||||
"Provider": "shared", | ||||||
"Default": "", | ||||||
"Value": None, | ||||||
"Examples": None, | ||||||
"ShortOpt": "", | ||||||
"Hide": 0, | ||||||
"Required": True, | ||||||
"IsPassword": False, | ||||||
"NoPrefix": False, | ||||||
"Advanced": False, | ||||||
"Exclusive": False, | ||||||
"Sensitive": False, | ||||||
"DefaultStr": "", | ||||||
"ValueStr": "", | ||||||
"Type": "string", | ||||||
}, | ||||||
] | ||||||
storage_copy["Options"].extend(custom_option) | ||||||
|
||||||
# use provider to indicate if the option is for an personal o shared storage | ||||||
for option in storage_copy["Options"]: | ||||||
if option["Name"] == "provider": | ||||||
option["example"] = [ | ||||||
{"Value": "personal", "Help": "Use Private to connect a folder that only you use", "Provider": ""}, | ||||||
{ | ||||||
"Value": "shared", | ||||||
"Help": "To connect a folder you share with others, both personal & shared folders.", | ||||||
"Provider": "", | ||||||
}, | ||||||
] | ||||||
option["Exclusive"] = True | ||||||
elif option["Name"] == "url": | ||||||
option.update({"Provider": "personal", "Default": url_value, "Required": False}) | ||||||
elif option["Name"] in ["bearer_token", "bearer_token_command", "headers", "user"]: | ||||||
option["Provider"] = "personal" | ||||||
|
||||||
# Remove obsolete options no longer applicable for Polybox or SwitchDrive | ||||||
storage_copy["Options"] = [ | ||||||
o for o in storage_copy["Options"] if o["Name"] not in ["vendor", "nextcloud_chunk_size"] | ||||||
] | ||||||
|
||||||
spec.append(storage_copy) | ||||||
|
||||||
def apply_patches(self, spec: list[dict[str, Any]]) -> None: | ||||||
"""Apply patches to RClone schema.""" | ||||||
patches = [ | ||||||
|
@@ -167,6 +237,25 @@ def apply_patches(self, spec: list[dict[str, Any]]) -> None: | |||||
for patch in patches: | ||||||
patch(spec) | ||||||
|
||||||
# Apply patches for PolyBox and SwitchDrive to the schema. | ||||||
self.__add_webdav_based_storage( | ||||||
spec, | ||||||
prefix="polybox", | ||||||
name="PolyBox", | ||||||
description="Polybox", | ||||||
url_value="https://polybox.ethz.ch/remote.php/webdav/", | ||||||
public_link_help="Shared folder link. E.g., https://polybox.ethz.ch/index.php/s/8NffJ3rFyHaVyyy", | ||||||
) | ||||||
|
||||||
self.__add_webdav_based_storage( | ||||||
spec, | ||||||
prefix="switchDrive", | ||||||
name="SwitchDrive", | ||||||
description="SwitchDrive", | ||||||
url_value="https://drive.switch.ch/remote.php/webdav/", | ||||||
public_link_help="Shared folder link. E.g., https://drive.switch.ch/index.php/s/OPSd72zrs5JG666", | ||||||
) | ||||||
|
||||||
def validate(self, configuration: Union["RCloneConfig", dict[str, Any]], keep_sensitive: bool = False) -> None: | ||||||
"""Validates an RClone config.""" | ||||||
provider = self.get_provider(configuration) | ||||||
|
@@ -182,10 +271,12 @@ async def test_connection( | |||||
except errors.ValidationError as e: | ||||||
return ConnectionResult(False, str(e)) | ||||||
|
||||||
# Obscure configuration and transform if needed | ||||||
obscured_config = await self.obscure_config(configuration) | ||||||
transformed_config = self.transform_polybox_switchdriver_config(obscured_config) | ||||||
|
||||||
with tempfile.NamedTemporaryFile(mode="w+", delete=False, encoding="utf-8") as f: | ||||||
config = "\n".join(f"{k}={v}" for k, v in obscured_config.items()) | ||||||
config = "\n".join(f"{k}={v}" for k, v in transformed_config.items()) | ||||||
f.write(f"[temp]\n{config}") | ||||||
f.close() | ||||||
proc = await asyncio.create_subprocess_exec( | ||||||
|
@@ -245,6 +336,45 @@ def get_private_fields( | |||||
provider = self.get_provider(configuration) | ||||||
return provider.get_private_fields(configuration) | ||||||
|
||||||
@staticmethod | ||||||
def transform_polybox_switchdriver_config( | ||||||
configuration: Union["RCloneConfig", dict[str, Any]], | ||||||
) -> Union["RCloneConfig", dict[str, Any]]: | ||||||
"""Transform the configuration for public access.""" | ||||||
storage_type = configuration.get("type") | ||||||
|
||||||
# Only process Polybox or SwitchDrive configurations | ||||||
if storage_type not in {"polybox", "switchDrive"}: | ||||||
return configuration | ||||||
|
||||||
configuration["type"] = "webdav" | ||||||
|
||||||
provider = configuration.get("provider") | ||||||
|
||||||
if provider == "personal": | ||||||
configuration["url"] = configuration.get("url") or ( | ||||||
"https://polybox.ethz.ch/remote.php/webdav/" | ||||||
if storage_type == "polybox" | ||||||
else "https://drive.switch.ch/remote.php/webdav/" | ||||||
) | ||||||
return configuration | ||||||
|
||||||
## Set url and username when is a shared configuration | ||||||
configuration["url"] = ( | ||||||
"https://polybox.ethz.ch/public.php/webdav/" | ||||||
if storage_type == "polybox" | ||||||
else "https://drive.switch.ch/public.php/webdav/" | ||||||
) | ||||||
public_link = configuration.get("public_link") | ||||||
|
||||||
if not public_link: | ||||||
raise ValueError("Missing 'public_link' for public access configuration.") | ||||||
|
||||||
# Extract the user from the public link | ||||||
configuration["user"] = public_link.split("/")[-1] | ||||||
|
||||||
return configuration | ||||||
|
||||||
|
||||||
class RCloneTriState(BaseModel): | ||||||
"""Represents a Tristate of true|false|unset.""" | ||||||
|
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.
just as a reminder, these changes also need to be backported to https://github.com/SwissDataScienceCenter/renku-notebooks/blob/master/renku_notebooks/api/schemas/cloud_storage.py#L155 to work with Renku v1.