Skip to content

Commit

Permalink
options added to the storage create command
Browse files Browse the repository at this point in the history
  • Loading branch information
agustinhydrolix committed Aug 22, 2024
1 parent e9bb478 commit 0fa6c14
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/hdx_cli/cli_interface/storage/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@
dynamic_confirmation_prompt, ensure_logged_in)
from ...library_api.common.context import ProfileUserContext
from ...library_api.common.logging import get_logger
from ..common.undecorated_click_commands import basic_create_with_body_from_string, basic_create_from_dict_body
from ..common.undecorated_click_commands import basic_create_from_dict_body, get_resource_list
from ..common.undecorated_click_commands import basic_delete, basic_settings
from ..common.rest_operations import (list_ as command_list,
show as command_show)

logger = get_logger()

@report_error_and_exit(exctype=Exception)
def get_credential_id(ctx, param, value):
if value is None:
return value

user_profile = ctx.parent.obj.get('usercontext')
org_id = user_profile.org_id
resource_path = f'/config/v1/orgs/{org_id}/credentials/'
credentials_list = get_resource_list(user_profile, resource_path)
credential_id = [c.get('uuid') for c in credentials_list if c.get('name') == value]
if not credential_id or credential_id[0] is None:
raise click.BadParameter(f"Credential name '{value}' not found.")
return credential_id[0]


@click.group(help="Storage-related operations")
@click.option('--storage', 'storage_name', metavar='STORAGENAME', default=None,
Expand Down Expand Up @@ -43,6 +57,14 @@ def storage(ctx: click.Context, storage_name: str):
help='Region for the storage bucket.')
@click.option('-c', '--cloud', default=None, required=False,
help='Type of cloud storage (e.g., aws, gcp).')
@click.option('-E', '--endpoint', default=None, required=False,
help='Endpoint for the storage bucket.')
@click.option('-C', '--credential-name', 'credential_id', default=None, required=False,
callback=get_credential_id,
help='Name of the credential to use for the storage bucket.')
@click.option('-M', '--io-perf-mode', default=None, required=False,
type=click.Choice(['aggressive', 'moderate', 'relaxed']),
help='I/O performance mode for the storage bucket.')
@click.pass_context
@report_error_and_exit(exctype=Exception)
def create(ctx: click.Context,
Expand All @@ -51,30 +73,35 @@ def create(ctx: click.Context,
bucket_path: str,
bucket_name: str,
region: str,
cloud: str):
cloud: str,
endpoint: str,
credential_id: str,
io_perf_mode: str):
if not settings_filename and not all((bucket_path, bucket_name, region, cloud)):
raise click.BadParameter("You must specify either a settings file or the bucket path, name, region, and cloud.")

user_profile = ctx.parent.obj.get('usercontext')
resource_path = ctx.parent.obj.get('resource_path')

if settings_filename:
with open(settings_filename, "r", encoding="utf-8") as file:
basic_create_with_body_from_string(user_profile,
resource_path,
storage_name,
file.read())
else:
storage_settings_list = [('bucket_path', bucket_path),
('bucket_name', bucket_name),
('region', region),
('cloud', cloud)]
if not settings_filename:
storage_settings = {
'bucket_path': bucket_path,
'bucket_name': bucket_name,
'region': region,
'cloud': cloud,
'endpoint': endpoint,
'credential_id': credential_id,
'io_perf_mode': io_perf_mode
}
body = {
'name': storage_name,
'settings': {key: value for key, value in storage_settings_list}
'settings': {key: value for key, value in storage_settings.items() if value is not None},
}
basic_create_from_dict_body(user_profile, resource_path, body)
else:
with open(settings_filename, "r", encoding="utf-8") as file:
body = json.load(file)

body['name'] = storage_name
basic_create_from_dict_body(user_profile, resource_path, body)
logger.info(f'Created storage {storage_name}')


Expand Down

0 comments on commit 0fa6c14

Please sign in to comment.