Skip to content

Commit

Permalink
[CU-86b2kx0tp] updated storage command to require bucket for storage … (
Browse files Browse the repository at this point in the history
#43)

* [CU-86b2kx0tp] updated storage command to require bucket for storage commands.

* [CU-86b2kx0tp] changed return to exit(1)
  • Loading branch information
tianning-zhang authored Oct 28, 2024
1 parent 601c571 commit e3d4da3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
36 changes: 30 additions & 6 deletions dnastack/cli/workbench/storage_commands.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pickle import FALSE
import re
from typing import Optional, List

import click
from click import style

from dnastack.cli.workbench.utils import get_storage_client
from dnastack.client.workbench.storage.models import AwsStorageAccountCredentials, StorageAccount, Provider, \
StorageListOptions, Platform, PlatformListOptions
from dnastack.cli.helpers.command.decorator import command
from dnastack.cli.helpers.command.spec import ArgumentSpec
from dnastack.cli.helpers.exporter import to_json, normalize
from dnastack.cli.helpers.iterator_printer import OutputFormat, show_iterator
from dnastack.cli.workbench.utils import get_storage_client
from dnastack.client.workbench.storage.models import AwsStorageAccountCredentials, StorageAccount, Provider, \
StorageListOptions, Platform, PlatformListOptions


@click.group('storage')
Expand Down Expand Up @@ -71,6 +71,14 @@ def add_storage_command_group():
required=True,
default=None
),
ArgumentSpec(
name='bucket',
arg_names=['--bucket'],
help='The name of the bucket to use for the storage account',
as_option=True,
required=True,
default=None
),
]
)
def add_aws_storage_account(context: Optional[str],
Expand All @@ -80,14 +88,21 @@ def add_aws_storage_account(context: Optional[str],
name: str,
access_key_id: str,
secret_access_key: str,
region: str):
region: str,
bucket: str):
# Validate bucket format
if not re.match(r'^s3://', bucket):
click.echo(style("Error: Bucket name must start with 's3://'", fg='red'), err=True, color=True)
exit(1)

"""Create a new aws storage account"""
client = get_storage_client(context, endpoint_id, namespace)

credentials = AwsStorageAccountCredentials(
access_key_id=access_key_id,
secret_access_key=secret_access_key,
region=region
region=region,
bucket=bucket
)

storage_account = StorageAccount(
Expand Down Expand Up @@ -302,6 +317,15 @@ def add_platform(context: Optional[str],
storage_id: str,
platform_type: str,
path: str):
# Validate path format
if re.match(r'^s3://', path):
click.echo(style("Error: Path must not start with 's3://'", fg='red'), err=True, color=True)
exit(1)

# Prefix path with a forward slash if not already present
if path and not path.startswith('/'):
path = '/' + path

"""Create a new platform"""
client = get_storage_client(context, endpoint_id, namespace)
platform = Platform(
Expand Down
1 change: 1 addition & 0 deletions dnastack/client/workbench/storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AwsStorageAccountCredentials(BaseModel):
access_key_id: Optional[str]
secret_access_key: Optional[str]
region: Optional[str]
bucket: Optional[str]
type: str = 'AWS_ACCESS_KEY'


Expand Down
13 changes: 10 additions & 3 deletions tests/exam_helper_for_workbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ def _create_storage_account(self, id=None) -> StorageAccount:
'--name', 'Test Storage Account',
'--access-key-id', env('E2E_AWS_ACCESS_KEY_ID', required=True),
'--secret-access-key', env('E2E_AWS_SECRET_ACCESS_KEY', required=True),
'--region', env('E2E_AWS_REGION', default='ca-central-1')
'--region', env('E2E_AWS_REGION', default='ca-central-1'),
'--bucket', env('E2E_AWS_BUCKET', default='s3://dnastack-workbench-sample-service-e2e-test')
))

def _get_or_create_storage_account(self) -> StorageAccount:
Expand All @@ -382,14 +383,20 @@ def _get_or_create_storage_account(self) -> StorageAccount:
def _create_platform(self, created_storage_account: StorageAccount, id=None) -> Platform:
if not id:
id = f'test-storage-account-{random.randint(0, 100000)}'

path = env('E2E_AWS_BUCKET', required=False, default='/dnastack-workbench-sample-service-e2e-test')

# Replace s3:// with /
if path.startswith('s3://'):
path = path.replace('s3://', '/')

return Platform(**self.simple_invoke(
'workbench', 'storage', 'platforms', 'add',
id,
'--name', 'Test Platform',
'--storage-id', created_storage_account.id,
'--platform', 'PACBIO',
'--path',
env('E2E_AWS_BUCKET', required=False, default='s3://dnastack-workbench-sample-service-e2e-test')
'--path', path
))

def _get_or_create_platform(self) -> Platform:
Expand Down

0 comments on commit e3d4da3

Please sign in to comment.