Skip to content
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: View and create Storage methods using SDK #206

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/formats/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ The RedBrick SDK will export a list of :class:`redbrick.types.task.OutputTask` o

annotations
taxonomy
storage_method
4 changes: 4 additions & 0 deletions docs/formats/storage_method.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Storage Method Formats
----------------------
.. automodule:: redbrick.types.storage_method
:members:
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires-python = ">=3.9,<3.14"
keywords = ["redbrick"]
dependencies = [
"aiohttp<4,>=3.10.5",
"altadb>=0.0.5",
"altadb>=0.0.6",
"dicom2nifti<3",
"inquirerpy<1",
"natsort<9,>=8.0.2",
Expand Down
6 changes: 5 additions & 1 deletion redbrick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from redbrick.common.context import RBContext
from redbrick.common.enums import (
StorageMethod,
StorageProvider,
ImportTypes,
TaskEventTypes,
TaskFilters,
Expand All @@ -30,7 +31,7 @@
from .version_check import version_check


__version__ = "2.20.0"
__version__ = "2.20.1"

# windows event loop close bug https://github.com/encode/httpx/issues/914#issuecomment-622586610
try:
Expand Down Expand Up @@ -78,6 +79,7 @@ def _populate_context(context: RBContext) -> RBContext:
SettingsRepo,
ProjectRepo,
WorkspaceRepo,
StorageMethodRepo,
)

if context.config.debug:
Expand All @@ -89,6 +91,7 @@ def _populate_context(context: RBContext) -> RBContext:
context.settings = SettingsRepo(context.client)
context.project = ProjectRepo(context.client)
context.workspace = WorkspaceRepo(context.client)
context.storage_method = StorageMethodRepo(context.client)
return context


Expand Down Expand Up @@ -236,6 +239,7 @@ def get_project_from_profile(
"version",
"RBContext",
"StorageMethod",
"StorageProvider",
"ImportTypes",
"TaxonomyTypes",
"TaskTypes",
Expand Down
2 changes: 2 additions & 0 deletions redbrick/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, api_key: str, url: str) -> None:
from .settings import SettingsControllerInterface
from .project import ProjectRepoInterface
from .workspace import WorkspaceRepoInterface
from .storage_method import StorageMethodRepoInterface

self.config = config
self.client = RBClient(api_key=api_key, url=url)
Expand All @@ -28,6 +29,7 @@ def __init__(self, api_key: str, url: str) -> None:
self.settings: SettingsControllerInterface
self.project: ProjectRepoInterface
self.workspace: WorkspaceRepoInterface
self.storage_method: StorageMethodRepoInterface

self._key_id: Optional[str] = None

Expand Down
11 changes: 11 additions & 0 deletions redbrick/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class StorageMethod:
REDBRICK = "22222222-2222-2222-2222-222222222222"


class StorageProvider(Enum):
"""Storage Provider options."""

GCS = "GCS"
PUBLIC = "PUBLIC"
AWS_S3 = "AWS_S3"
AZURE_BLOB = "AZURE_BLOB"
REDBRICK = "REDBRICK"
ALTA_DB = "ALTA_DB"


class TaskStates(str, Enum):
"""Task Status.

Expand Down
25 changes: 25 additions & 0 deletions redbrick/common/storage_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Interdace for getting information about storage methods."""

from abc import ABC, abstractmethod
from typing import Dict, List

from redbrick.common.enums import StorageProvider
from redbrick.types.storage_method import StorageMethodDetails


class StorageMethodRepoInterface(ABC):
"""Abstract interface to Storage Method APIs."""

@abstractmethod
def get_storage_methods(self, org_id: str) -> List[Dict]:
"""Get storage methods."""

@abstractmethod
def create_storage_method(
self,
org_id: str,
name: str,
provider: StorageProvider,
details: StorageMethodDetails,
) -> bool:
"""Create a storage method."""
1 change: 1 addition & 0 deletions redbrick/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .settings import SettingsRepo
from .project import ProjectRepo
from .workspace import WorkspaceRepo
from .storage_method import StorageMethodRepo
46 changes: 46 additions & 0 deletions redbrick/repo/shards.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@
}
"""

STORAGE_METHOD_SHARD = """
orgId
storageId
name
provider
details{
__typename
... on S3BucketStorageDetails {
bucket
region
duration
access
roleArn
endpoint
accelerate
}
... on GCSBucketStorageDetails {
bucket
}
... on AzureBlobStorageDetails {
_
}
... on PublicStorageDetails {
_
}
... on RedBrickStorageDetails {
_
}
... on AltaDBStorageDetails {
access
host
}
}
createdBy{
userType
userId
email
givenName
familyName
loggedInUser
idProvider
}
createdAt
deleted
"""

OLD_ATTRIBUTE_SHARD = """
name
attrType
Expand Down
61 changes: 61 additions & 0 deletions redbrick/repo/storage_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Handlers to access APIs for storage methods."""

from typing import Dict, List
from redbrick.common.client import RBClient
from redbrick.common.enums import StorageProvider
from redbrick.common.storage_method import StorageMethodRepoInterface
from redbrick.repo.shards import STORAGE_METHOD_SHARD
from redbrick.types.storage_method import StorageMethodDetails


class StorageMethodRepo(StorageMethodRepoInterface):
"""StorageMethodRepo class."""

def __init__(self, client: RBClient):
"""Construct StorageMethodRepo."""
self.client = client

def get_storage_methods(self, org_id: str) -> List[Dict]:
"""Get storage methods."""
query = f"""

Check warning on line 20 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L20

Added line #L20 was not covered by tests
query listStorageMethodsSDK($orgId: UUID!) {{
storageMethods(orgId: $orgId) {{
{STORAGE_METHOD_SHARD}
}}
}}
"""
variables = {"orgId": org_id}
response = self.client.execute_query(query, variables)
return response["storageMethods"]

Check warning on line 29 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L27-L29

Added lines #L27 - L29 were not covered by tests

def create_storage_method(
self,
org_id: str,
name: str,
provider: StorageProvider,
details: StorageMethodDetails,
) -> bool:
"""Create a storage method."""
query = """

Check warning on line 39 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L39

Added line #L39 was not covered by tests
mutation createStorageSDK($orgId: UUID!, $name: String!, $provider: PROVIDER!, $details: StorageDetailsInput){
createStorage(orgId: $orgId, name: $name, provider: $provider, details: $details){
ok
}
}
"""
provider_map = {

Check warning on line 46 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L46

Added line #L46 was not covered by tests
StorageProvider.ALTA_DB: "altaDb",
StorageProvider.AWS_S3: "s3Bucket",
StorageProvider.AZURE_BLOB: "azureBucket",
StorageProvider.GCS: "gcsBucket",
}
variables = {

Check warning on line 52 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L52

Added line #L52 was not covered by tests
"orgId": org_id,
"name": name,
"provider": provider.value,
"details": {
provider_map[provider]: details,
},
}
response = self.client.execute_query(query, variables)
return response["createStorage"]["ok"]

Check warning on line 61 in redbrick/repo/storage_method.py

View check run for this annotation

Codecov / codecov/patch

redbrick/repo/storage_method.py#L60-L61

Added lines #L60 - L61 were not covered by tests
81 changes: 81 additions & 0 deletions redbrick/types/storage_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""Storage Method Types"""

from typing import TypedDict, Union
from typing_extensions import NotRequired


class InputAWSS3StorageMethodDetails(TypedDict):
"""AWS S3 Storage Method Type.

Contains the necessary information to access a user's AWS S3 bucket.
"""

#: The name of the bucket
bucket: str

#: The region of the bucket
region: str

#: The duration of the session
duration: NotRequired[int]

#: The access key (AWS_ACCESS_KEY_ID)
access: NotRequired[str]

#: The secret key (AWS_SECRET_ACCESS_KEY)
secret: NotRequired[str]

# session creds

#: The role ARN
roleArn: NotRequired[str]

#: The role external ID
roleExternalId: NotRequired[str]

#: The endpoint
endpoint: NotRequired[str]

#: Whether to use S3 transfer acceleration
accelerate: NotRequired[bool]


class InputGCSStorageMethodDetails(TypedDict):
"""Storage information for DataPoints in a user's Google Cloud bucket."""

#: The name of the bucket
bucket: str

#: The service account JSON as a string
serviceAccount: str


class InputAzureBlobStorageMethodDetails(TypedDict):
"""Azure Blob Storage Method Type."""

#: The connection string
connectionString: NotRequired[str]

#: The SAS URL
sasUrl: NotRequired[str]


class InputAltaDBStorageMethodDetails(TypedDict):
"""AltaDB Storage Method Type."""

#: The access key
access: str

#: The secret key
secret: str

#: The host (backend URL)
host: NotRequired[str]


StorageMethodDetails = Union[
InputAWSS3StorageMethodDetails,
InputGCSStorageMethodDetails,
InputAzureBlobStorageMethodDetails,
InputAltaDBStorageMethodDetails,
]
Loading