diff --git a/docs/user_guide/source/command_ref.rst b/docs/user_guide/source/command_ref.rst index 3e349e8..c5e1807 100644 --- a/docs/user_guide/source/command_ref.rst +++ b/docs/user_guide/source/command_ref.rst @@ -23,6 +23,7 @@ Commands: | ``put Put a single file.`` | ``putlist Put a number of files specified in a list.`` | ``stat List transactions.`` + ``quota Get the tape quota for the group.`` Each command has its own specific options. The argument is generally the file or filelist that the user wishes to operate on. The full command listing is diff --git a/nlds_client/clientlib/transactions.py b/nlds_client/clientlib/transactions.py index a2188bb..4c30bab 100644 --- a/nlds_client/clientlib/transactions.py +++ b/nlds_client/clientlib/transactions.py @@ -939,6 +939,56 @@ def change_metadata(user: str, return response_dict +def get_quota(user: str, + group: str, + ): + """Make a request to get the quota for a particular group in the NLDS + + :param user: the username to get the quota + :type user: string + + :param group: the group to get the quota for + :type group: string + + :raises requests.exceptions.ConnectionError: if the server cannot be + reached + + :return: A Dictionary of the response + :rtype: Dict + """ + # get the config, user and group + config = load_config() + token = load_token(config) + user = get_user(config, user) + group = get_group(config, group) + url = construct_server_url(config, "catalog/quota") + + # build the parameters. holdings->get requires + # user: str + # group: str + # token: str + input_params = {"user" : user, + "group" : group, + "token": token,} + + response_dict = main_loop( + url=url, + input_params=input_params, + method=requests.get + ) + + if not response_dict: + response_dict = { + "msg" : f"GET QUOTA for user {user} and group {group} failed", + "success" : False + } + # mark as failed in RPC call + elif "details" in response_dict and "failure" in response_dict["details"]: + response_dict["success"] = False + + return response_dict + + def init_client( url: str = None, verify_certificates: bool = True, diff --git a/nlds_client/nlds_client.py b/nlds_client/nlds_client.py index b07eb48..0320354 100755 --- a/nlds_client/nlds_client.py +++ b/nlds_client/nlds_client.py @@ -16,7 +16,7 @@ list_holding, find_file, monitor_transactions, get_transaction_state, - change_metadata, + change_metadata, get_quota, init_client) from nlds_client.clientlib.exceptions import (ConnectionError, RequestError, @@ -357,6 +357,56 @@ def print_meta(response:dict, req_details:str): click.echo(f"{'':<12}{'label':<8}: {h['new_meta']['label']}") click.echo(f"{'':<12}{'tags':<8}: {h['new_meta']['tags']}") +def print_quota(response: dict, req_details:str): + """Print out the quota response from the quota command""" + try: + group = response['details']['group'] + quota = pretty_size(response['data']['quota']) + if quota == 0: + quota_string = f" There is no allocated quota for the group {group}." + else: + quota_string = f" The allocated quota for the group {group} is {quota}." + click.echo(f"Checking quota for {req_details}:") + click.echo(quota_string) + except KeyError as e: + click.echo(f"Error: Missing key in response data - {e}") + +def print_diskspace(response: dict, req_details:str): + """Print out the diskspace response from the quota command""" + try: + group = response['details']['group'] + diskspace = pretty_size(response['data']['diskspace']) + if diskspace == 0: + diskspace_string = f" There is no used diskspace for the group {group}." + else: + diskspace_string = f" The used diskspace for the group {group} is {diskspace}." + click.echo(f"Checking diskspace for {req_details}:") + click.echo(diskspace_string) + except KeyError as e: + click.echo(f"Error: Missing key in response data - {e}") + +def print_remaining_quota(response: dict, req_details:str): + """Print out the diskspace response from the quota command""" + try: + group = response['details']['group'] + quota = response['data']['quota'] + diskspace = response['data']['diskspace'] + + # Determine the appropriate message based on quota and diskspace + if quota == 0 and diskspace == 0: + return # Print nothing if there is no quota and no diskspace + elif quota == 0 and diskspace > 0: + message = f"{pretty_size(diskspace)} over quota." + elif quota >= diskspace: + message = f"{pretty_size(quota - diskspace)} remaining." + else: + message = f"{pretty_size(diskspace-quota)} over quota." + + click.echo(" ") + click.echo(message) + except KeyError as e: + click.echo(f"Error: Missing key in response data - {e}") + def print_response(tr:dict): if 'msg' in tr and len(tr['msg']) > 0: @@ -750,6 +800,41 @@ def find(user, group, groupall, label, holding_id, transaction_id, path, tag, except RequestError as re: raise click.UsageError(re) +"""Quota command""" +@nlds_client.command("quota", + help=f"Get the quota for a particular service.{user_help_text}") +@click.option("-u", "--user", default=None, type=str, + help="The username of the user getting the quota.") +@click.option("-g", "--group", default=None, type=str, + help="The group to get the quota for.") + +def quota(user, group, ): + try: + response = get_quota(user, group, ) + req_details = format_request_details( + user, group, + ) + if response['success']: + if json: + click.echo(json_dumps(response)) + else: + print_quota(response, req_details) + print_diskspace(response, req_details) + print_remaining_quota(response, req_details) + else: + fail_string = "Failed to get quota with " + fail_string += req_details + if response['details']['failure']: + fail_string += "\n" + response['details']['failure'] + raise click.UsageError(fail_string) + + except ConnectionError as ce: + raise click.UsageError(ce) + except AuthenticationError as ae: + raise click.UsageError(ae) + except RequestError as re: + raise click.UsageError(re) + """Meta command""" @nlds_client.command("meta", help=f"Alter metadata for a holding.{user_help_text}")