Skip to content

Commit

Permalink
first use command + profile fixes + login responsibility moved
Browse files Browse the repository at this point in the history
  • Loading branch information
agustinhydrolix committed Aug 9, 2024
1 parent ab79b8f commit 2594b31
Show file tree
Hide file tree
Showing 28 changed files with 628 additions and 481 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def basic_show(profile,


def basic_transform(ctx: click.Context):
profile_info: ProfileUserContext = ctx.obj['usercontext']
profile_info: ProfileUserContext = ctx.parent.obj['usercontext']
project_name, table_name = profile_info.projectname, profile_info.tablename
if not project_name or not table_name:
raise HdxCliException(f"No project/table parameters provided and "
Expand Down
29 changes: 17 additions & 12 deletions src/hdx_cli/cli_interface/dictionary/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ..common.migration import migrate_a_dictionary
from ...library_api.common.generic_resource import access_resource
from ...library_api.utility.decorators import report_error_and_exit
from ...library_api.utility.decorators import report_error_and_exit, ensure_logged_in
from ...library_api.common.exceptions import (ResourceNotFoundException,
MissingSettingsException,
InvalidFormatFileException)
Expand All @@ -29,23 +29,28 @@
metavar='DICTIONARYNAME', default=None)
@click.pass_context
@report_error_and_exit(exctype=Exception)
@ensure_logged_in
def dictionary(ctx: click.Context,
project_name,
dictionary_name):
project_name: str,
dictionary_name: str):
user_profile = ctx.parent.obj['usercontext']
org_id = user_profile.org_id
ProfileUserContext.update_context(user_profile,
projectname=project_name,
dictionaryname=dictionary_name)

project = user_profile.projectname
if not project:
ProfileUserContext.update_context(
user_profile,
projectname=project_name,
dictionaryname=dictionary_name
)

project_name = user_profile.projectname
if not project_name:
raise ResourceNotFoundException(
f"No project parameter provided and "
f"no project set in profile '{user_profile.profilename}'")
project_body = access_resource(user_profile, [('projects', project_name)])
if not project_body:
raise ResourceNotFoundException(f"Project '{project_name}' not found.")

project_id = access_resource(user_profile,
[('projects', project)])['uuid']
project_id = project_body.get('uuid')
org_id = user_profile.org_id
resource_path = f'/config/v1/orgs/{org_id}/projects/{project_id}/dictionaries/'
ctx.obj = {'resource_path': resource_path,
'usercontext': user_profile}
Expand Down
63 changes: 32 additions & 31 deletions src/hdx_cli/cli_interface/function/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import click

from ..common.migration import migrate_a_function
from ...library_api.common.generic_resource import access_resource
from ...library_api.userdata.token import AuthInfo
from ...library_api.common import rest_operations as rest_ops
from ...library_api.common.context import ProfileUserContext
from ...library_api.utility.decorators import report_error_and_exit
from ...library_api.common.exceptions import LogicException
from ...library_api.utility.decorators import report_error_and_exit, ensure_logged_in
from ...library_api.common.exceptions import LogicException, ResourceNotFoundException
from ...library_api.common.logging import get_logger
from ..common.rest_operations import (delete as command_delete,
list_ as command_list,
Expand All @@ -25,31 +26,29 @@
metavar='FUNCTIONNAME', default=None)
@click.pass_context
@report_error_and_exit(exctype=Exception)
@ensure_logged_in
def function(ctx: click.Context,
project_name,
function_name):
project_name: str,
function_name: str):
user_profile = ctx.parent.obj['usercontext']
ProfileUserContext.update_context(user_profile,
projectname=project_name,
functionname=function_name)
ProfileUserContext.update_context(
user_profile,
projectname=project_name,
functionname=function_name
)

hostname = user_profile.hostname
project = user_profile.projectname
if not project:
project_name = user_profile.projectname
if not project_name:
raise LogicException(f"No project parameter provided and "
f"no project set in profile '{user_profile.profilename}'")

project_body = access_resource(user_profile, [('projects', project_name)])
if not project_body:
raise ResourceNotFoundException(f"Project '{project_name}' not found.")

project_id = project_body.get('uuid')
org_id = user_profile.org_id
scheme = user_profile.scheme
timeout = user_profile.timeout
list_projects_url = f'{scheme}://{hostname}/config/v1/orgs/{org_id}/projects/'
auth_token: AuthInfo = user_profile.auth
headers = {'Authorization': f'{auth_token.token_type} {auth_token.token}',
'Accept': 'application/json'}
projects_list = rest_ops.list(list_projects_url,
headers=headers,
timeout=timeout)
project_id = [p['uuid'] for p in projects_list if p['name'] == project]
ctx.obj = {'resource_path': f'/config/v1/orgs/{org_id}/projects/{project_id[0]}/functions/',
ctx.obj = {'resource_path': f'/config/v1/orgs/{org_id}/projects/{project_id}/functions/',
'usercontext': user_profile}


Expand All @@ -72,8 +71,8 @@ def function(ctx: click.Context,
@report_error_and_exit(exctype=Exception)
def create(ctx: click.Context,
function_name: str,
sql_from_file,
inline_sql):
sql_from_file: str,
inline_sql: str):
if inline_sql and sql_from_file:
raise LogicException(
'Only one of the options --inline-sql and --sql-from-file can be used.')
Expand Down Expand Up @@ -125,14 +124,16 @@ def migrate(ctx: click.Context,
raise click.BadParameter('Either provide a --target-profile or all four target cluster options.')

user_profile = ctx.parent.obj['usercontext']
migrate_a_function(user_profile,
function_name,
target_profile,
target_cluster_hostname,
target_cluster_username,
target_cluster_password,
target_cluster_uri_scheme,
target_project_name)
migrate_a_function(
user_profile,
function_name,
target_profile,
target_cluster_hostname,
target_cluster_username,
target_cluster_password,
target_cluster_uri_scheme,
target_project_name
)
logger.info(f'Migrated function {function_name}')


Expand Down
47 changes: 30 additions & 17 deletions src/hdx_cli/cli_interface/job/alter/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
help="Perform operation on the passed job name.")
@click.pass_context
@report_error_and_exit(exctype=Exception)
def alter(ctx: click.Context, project_name, table_name, alter_name):
def alter(ctx: click.Context, project_name: str, table_name: str, alter_name: str):
user_profile = ctx.parent.obj['usercontext']
ProfileUserContext.update_context(
user_profile,
projectname=project_name,
tablename=table_name,
altername=alter_name
)
alter_path = ctx.parent.obj['resource_path'] + 'alter/'
ctx.obj = {'resource_path': alter_path,
'usercontext': user_profile}
ProfileUserContext.update_context(user_profile,
projectname=project_name,
tablename=table_name,
altername=alter_name)


@alter.group(help="Create a new alter job.")
Expand Down Expand Up @@ -90,18 +92,20 @@ def list_(ctx: click.Context, status: str, project_name: str, table_name: str):
@click.option('-i', '--indent', is_flag=True, default=False, help='Indent the output.')
@click.pass_context
@report_error_and_exit(exctype=Exception)
def show(ctx: click.Context, job_name, indent: bool,):
def show(ctx: click.Context, job_name: str, indent: bool,):
profile = ctx.parent.obj.get('usercontext')
resource_path = ctx.parent.obj.get('resource_path')
job = _get_alter_job(profile, resource_path, job_name)
indentation = DEFAULT_INDENTATION if indent else None
logger.info(json.dumps(job, indent=indentation))


_confirmation_prompt = partial(dynamic_confirmation_prompt,
prompt="Please type 'delete this resource' to delete: ",
confirmation_message='delete this resource',
fail_message='Incorrect prompt input: resource was not deleted')
_confirmation_prompt = partial(
dynamic_confirmation_prompt,
prompt="Please type 'delete this resource' to delete: ",
confirmation_message='delete this resource',
fail_message='Incorrect prompt input: resource was not deleted'
)


@alter.command(help='Delete an existing alter job.')
Expand All @@ -110,7 +114,7 @@ def show(ctx: click.Context, job_name, indent: bool,):
@click.argument('resource_name')
@click.pass_context
@report_error_and_exit(exctype=Exception)
def delete(ctx: click.Context, resource_name: str, disable_confirmation_prompt):
def delete(ctx: click.Context, resource_name: str, disable_confirmation_prompt: bool):
_confirmation_prompt(prompt_active=not disable_confirmation_prompt)
resource_path = ctx.parent.obj.get('resource_path')
profile = ctx.parent.obj.get('usercontext')
Expand Down Expand Up @@ -165,7 +169,7 @@ def verify(ctx: click.Context, job_name: str, indent: bool):
logger.info(f'{verify_result}')


def delete_alter(profile, resource_path, job_name) -> bool:
def delete_alter(profile: ProfileUserContext, resource_path: str, job_name: str) -> bool:
job_id = _get_alter_job(profile, resource_path, job_name).get('uuid')

hostname = profile.hostname
Expand All @@ -179,7 +183,7 @@ def delete_alter(profile, resource_path, job_name) -> bool:
return True


def perform_alter_job(profile, resource_path, job_name, action):
def perform_alter_job(profile: ProfileUserContext, resource_path: str, job_name: str, action: str):
job_id = _get_alter_job(profile, resource_path, job_name).get('uuid')

hostname = profile.hostname
Expand All @@ -192,7 +196,11 @@ def perform_alter_job(profile, resource_path, job_name, action):
rest_ops.create(url, headers=headers, timeout=timeout)


def verify_alter(profile, resource_path, job_name, indentation=False) -> str:
def verify_alter(profile: ProfileUserContext,
resource_path: str,
job_name: str,
indentation: bool = False
) -> str:
job_id = _get_alter_job(profile, resource_path, job_name).get('uuid')

hostname = profile.hostname
Expand All @@ -208,7 +216,7 @@ def verify_alter(profile, resource_path, job_name, indentation=False) -> str:
return json.dumps(results[0], indent=indentation)


def _create_alter_job(profile, resource_path, alter_job):
def _create_alter_job(profile: ProfileUserContext, resource_path: str, alter_job) -> None:
scheme = profile.scheme
hostname = profile.hostname
url = f'{scheme}://{hostname}{resource_path}'
Expand All @@ -219,7 +227,7 @@ def _create_alter_job(profile, resource_path, alter_job):
rest_ops.create(url, headers=headers, timeout=timeout, body=alter_job, body_type='csv')


def _get_alter_job(profile, resource_path, job_name) -> dict:
def _get_alter_job(profile: ProfileUserContext, resource_path: str, job_name: str) -> dict:
if not job_name:
_, resource_kind = heuristically_get_resource_kind(resource_path)
if not (job_name := getattr(profile, resource_kind + 'name')):
Expand All @@ -233,7 +241,12 @@ def _get_alter_job(profile, resource_path, job_name) -> dict:
raise ResourceNotFoundException('Cannot find resource.')


def list_alter_jobs(profile, resource_path, status_to_filter, project_to_filter, table_to_filter):
def list_alter_jobs(profile: ProfileUserContext,
resource_path: str,
status_to_filter: str,
project_to_filter: str,
table_to_filter: str
) -> None:
default_alter_job_list = get_resource_list(profile, resource_path).get('results')

if status_to_filter is not None:
Expand Down
15 changes: 9 additions & 6 deletions src/hdx_cli/cli_interface/job/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .alter.commands import alter as alter_command
from .batch.commands import batch as batch_command

from ...library_api.utility.decorators import report_error_and_exit, confirmation_prompt
from ...library_api.utility.decorators import report_error_and_exit, confirmation_prompt, ensure_logged_in
from ...library_api.common import rest_operations as rest_ops
from ...library_api.common.logging import get_logger

Expand All @@ -13,6 +13,8 @@

@click.group(help="Job-related operations")
@click.pass_context
@report_error_and_exit(exctype=Exception)
@ensure_logged_in
def job(ctx: click.Context):
user_profile = ctx.parent.obj['usercontext']
org_id = user_profile.org_id
Expand All @@ -21,11 +23,7 @@ def job(ctx: click.Context):
'usercontext': user_profile}


job.add_command(alter_command)
job.add_command(batch_command)


@click.command(help='Purge all batch jobs in your org')
@click.command(help='Purge all batch jobs in your org.')
@click.pass_context
@report_error_and_exit(exctype=Exception)
@confirmation_prompt(prompt="Please type 'purge all jobs' to proceed: ",
Expand All @@ -45,3 +43,8 @@ def purgejobs(ctx: click.Context):
'Accept': 'application/json'}
rest_ops.create(purgejobs_url, headers=headers, timeout=timeout)
logger.info('All jobs purged')


job.add_command(alter_command)
job.add_command(batch_command)
job.add_command(purgejobs)
28 changes: 16 additions & 12 deletions src/hdx_cli/cli_interface/pool/commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
import json

from ...library_api.utility.decorators import report_error_and_exit
from ...library_api.utility.decorators import report_error_and_exit, ensure_logged_in
from ...library_api.common.logging import get_logger
from ...library_api.common.context import ProfileUserContext
from ..common.rest_operations import delete as command_delete
Expand All @@ -17,16 +17,21 @@
@click.option('--pool', 'pool_name', help="Perform operation on the passed pool.",
metavar='POOLNAME', default=None)
@click.pass_context
def pool(ctx: click.Context,
pool_name):
@report_error_and_exit(exctype=Exception)
@ensure_logged_in
def pool(ctx: click.Context, pool_name: str):
user_profile = ctx.parent.obj['usercontext']
ProfileUserContext.update_context(user_profile,
poolname=pool_name)
ProfileUserContext.update_context(user_profile, poolname=pool_name)
ctx.obj = {'resource_path': f'/config/v1/pools/',
'usercontext': user_profile}


def build_request_body(replicas, cpu, memory, storage, pool_service):
def build_request_body(replicas: int,
cpu: float,
memory: int,
storage: int,
pool_service: str):
"""Build the request body for creating a new pool"""
return {
'description': 'Created with hdxcli tool',
'settings': {
Expand All @@ -41,7 +46,7 @@ def build_request_body(replicas, cpu, memory, storage, pool_service):
}


@click.command(help='Create new workload isolation resources')
@click.command(help='Create a new pool.')
@click.option('--replicas', '-r',
type=int,
help='Number of replicas for the workload (default: 1)',
Expand Down Expand Up @@ -73,11 +78,10 @@ def create(ctx: click.Context,
storage: int,
pool_service: str,
pool_name: str):
user_profile = ctx.parent.obj.get('usercontext')
resource_path = ctx.parent.obj.get('resource_path')

body = build_request_body(replicas, cpu, memory, storage, pool_service)
basic_create_with_body_from_string(user_profile, resource_path, pool_name, json.dumps(body))
user_profile = ctx.parent.obj['usercontext']
resource_path = ctx.parent.obj['resource_path']
pool_body = build_request_body(replicas, cpu, memory, storage, pool_service)
basic_create_with_body_from_string(user_profile, resource_path, pool_name, json.dumps(pool_body))
logger.info(f'Created pool {pool_name}')


Expand Down
Loading

0 comments on commit 2594b31

Please sign in to comment.