From 63c56f9a4e6f4f26437de5e12b23220becfe125e Mon Sep 17 00:00:00 2001 From: Katja Hellgren <96579188+KatHellg@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:19:22 +0100 Subject: [PATCH] Fix/SK-1127 | Add session_id flag to CLI resources (#742) --- fedn/cli/client_cmd.py | 36 ++++++++++++++++++++++++++--- fedn/cli/combiner_cmd.py | 37 +++++++++++++++++++++++++++--- fedn/cli/model_cmd.py | 47 ++++++++++++++++++++++++++++++++++---- fedn/cli/package_cmd.py | 37 +++++++++++++++++++++++++++--- fedn/cli/round_cmd.py | 47 ++++++++++++++++++++++++++++++++++---- fedn/cli/session_cmd.py | 36 ++++++++++++++++++++++++++--- fedn/cli/shared.py | 26 ++++++++++++++------- fedn/cli/status_cmd.py | 43 ++++++++++++++++++++++++++++++---- fedn/cli/validation_cmd.py | 42 ++++++++++++++++++++++++++++++---- 9 files changed, 315 insertions(+), 36 deletions(-) diff --git a/fedn/cli/client_cmd.py b/fedn/cli/client_cmd.py index 666bc6545..7c9ffc1e7 100644 --- a/fedn/cli/client_cmd.py +++ b/fedn/cli/client_cmd.py @@ -60,12 +60,42 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_ if _token: headers["Authorization"] = _token - click.echo(f"\nListing clients: {url}\n") - click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - print_response(response, "clients") + print_response(response, "clients", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Client ID") +@client_cmd.command("get") +@click.pass_context +def get_client(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: client with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients") + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "client", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/combiner_cmd.py b/fedn/cli/combiner_cmd.py index 3e7753e80..dd45fbd1b 100644 --- a/fedn/cli/combiner_cmd.py +++ b/fedn/cli/combiner_cmd.py @@ -88,11 +88,42 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None, if _token: headers["Authorization"] = _token - click.echo(f"\nListing combiners: {url}\n") - click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - print_response(response, "combiners") + print_response(response, "combiners", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Combiner ID") +@combiner_cmd.command("get") +@click.pass_context +def get_combiner(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: combiner with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners") + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "combiner", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/model_cmd.py b/fedn/cli/model_cmd.py index 80a8f795e..2e522e5a1 100644 --- a/fedn/cli/model_cmd.py +++ b/fedn/cli/model_cmd.py @@ -17,10 +17,11 @@ def model_cmd(ctx): @click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") @click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") @click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-session_id", "--session_id", required=False, help="models in session with given session id") @click.option("--n_max", required=False, help="Number of items to list") @model_cmd.command("list") @click.pass_context -def list_models(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): +def list_models(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of models @@ -28,6 +29,8 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, n_m """ url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models") + + headers = {} if n_max: @@ -38,11 +41,47 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, n_m if _token: headers["Authorization"] = _token - click.echo(f"\nListing models: {url}\n") - click.echo(f"Headers: {headers}") + if session_id: + url = f"{url}?session_id={session_id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "models", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Model ID") +@model_cmd.command("get") +@click.pass_context +def get_model(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: model with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models") + + + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + try: response = requests.get(url, headers=headers) - print_response(response, "models") + print_response(response, "model", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/package_cmd.py b/fedn/cli/package_cmd.py index 3c78d9944..b8a130f68 100644 --- a/fedn/cli/package_cmd.py +++ b/fedn/cli/package_cmd.py @@ -66,11 +66,42 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, n if _token: headers["Authorization"] = _token - click.echo(f"\nListing packages: {url}\n") - click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - print_response(response, "packages") + print_response(response, "packages", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Package ID") +@package_cmd.command("get") +@click.pass_context +def get_package(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: package with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages") + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "package", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/round_cmd.py b/fedn/cli/round_cmd.py index ac42f43ef..2f889fef3 100644 --- a/fedn/cli/round_cmd.py +++ b/fedn/cli/round_cmd.py @@ -16,11 +16,12 @@ def round_cmd(ctx): @click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") @click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") @click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-session_id", "--session_id", required=False, help="Rounds in session with given session id") @click.option("-t", "--token", required=False, help="Authentication token") @click.option("--n_max", required=False, help="Number of items to list") @round_cmd.command("list") @click.pass_context -def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): +def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of rounds @@ -28,6 +29,7 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, n_m """ url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds") + headers = {} if n_max: @@ -38,11 +40,48 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, n_m if _token: headers["Authorization"] = _token - click.echo(f"\nListing rounds: {url}\n") - click.echo(f"Headers: {headers}") + if session_id: + url = f"{url}?round_config.session_id={session_id}" + try: response = requests.get(url, headers=headers) - print_response(response, "rounds") + print_response(response, "rounds", None) + + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-id", "--id", required=True, help="Round ID") +@click.option("-t", "--token", required=False, help="Authentication token") +@round_cmd.command("get") +@click.pass_context +def get_round(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: round with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds") + + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "round", id) + except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/session_cmd.py b/fedn/cli/session_cmd.py index 65db98c69..a0f1e64c3 100644 --- a/fedn/cli/session_cmd.py +++ b/fedn/cli/session_cmd.py @@ -38,11 +38,41 @@ def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, n if _token: headers["Authorization"] = _token - click.echo(f"\nListing sessions: {url}\n") - click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - print_response(response, "sessions") + print_response(response, "sessions", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Session ID") +@session_cmd.command("get") +@click.pass_context +def get_session(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: session with given session id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="sessions") + headers = {} + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "session", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/shared.py b/fedn/cli/shared.py index d32f4ff43..21fa2b072 100644 --- a/fedn/cli/shared.py +++ b/fedn/cli/shared.py @@ -64,7 +64,7 @@ def get_client_package_dir(path: str) -> str: # Print response from api (list of entities) -def print_response(response, entity_name: str): +def print_response(response, entity_name: str, so): """Prints the api response to the cli. :param response: type: array @@ -72,18 +72,28 @@ def print_response(response, entity_name: str): :param entity_name: type: string description: name of entity + :param so: + type: boolean + desriptions: single output format return: None """ if response.status_code == 200: json_data = response.json() - count, result = json_data.values() - click.echo(f"Found {count} {entity_name}") - click.echo("\n---------------------------------\n") - for obj in result: - click.echo("{") - for k, v in obj.items(): + if so: + click.echo(f"Found {entity_name}") + click.echo("\n---------------------------------\n") + for k, v in json_data.items(): click.echo(f"\t{k}: {v}") - click.echo("}") + else: + count, result = json_data.values() + click.echo(f"Found {count} {entity_name}") + click.echo("\n---------------------------------\n") + for obj in result: + print(obj.get("session_id")) + click.echo("{") + for k, v in obj.items(): + click.echo(f"\t{k}: {v}") + click.echo("}") elif response.status_code == 500: json_data = response.json() click.echo(f'Error: {json_data["message"]}') diff --git a/fedn/cli/status_cmd.py b/fedn/cli/status_cmd.py index c879ca1ef..9b751f65b 100644 --- a/fedn/cli/status_cmd.py +++ b/fedn/cli/status_cmd.py @@ -16,10 +16,11 @@ def status_cmd(ctx): @click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") @click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") @click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-session_id", "--session_id", required=False, help="statuses with given session id") @click.option("--n_max", required=False, help="Number of items to list") @status_cmd.command("list") @click.pass_context -def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): +def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of statuses @@ -37,11 +38,45 @@ def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, n if _token: headers["Authorization"] = _token - click.echo(f"\nListing statuses: {url}\n") - click.echo(f"Headers: {headers}") + if session_id: + url = f"{url}?sessionId={session_id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "statuses", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="Status ID") +@status_cmd.command("get") +@click.pass_context +def get_status(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: status with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="statuses") + headers = {} + + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + try: response = requests.get(url, headers=headers) - print_response(response, "statuses") + print_response(response, "status", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/validation_cmd.py b/fedn/cli/validation_cmd.py index 4bf4e63fa..b7417af5e 100644 --- a/fedn/cli/validation_cmd.py +++ b/fedn/cli/validation_cmd.py @@ -17,10 +17,11 @@ def validation_cmd(ctx): @click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") @click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") @click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-session_id", "--session_id", required=False, help="validations in session with given session id") @click.option("--n_max", required=False, help="Number of items to list") @validation_cmd.command("list") @click.pass_context -def list_validations(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): +def list_validations(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of validations @@ -38,11 +39,44 @@ def list_validations(ctx, protocol: str, host: str, port: str, token: str = None if _token: headers["Authorization"] = _token - click.echo(f"\nListing validations: {url}\n") - click.echo(f"Headers: {headers}") + if session_id: + url = f"{url}?sessionId={session_id}" + + + try: + response = requests.get(url, headers=headers) + print_response(response, "validations", None) + except requests.exceptions.ConnectionError: + click.echo(f"Error: Could not connect to {url}") + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-id", "--id", required=True, help="validation ID") +@validation_cmd.command("get") +@click.pass_context +def get_validation(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: validation with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="validations") + headers = {} + + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if id: + url = f"{url}{id}" + try: response = requests.get(url, headers=headers) - print_response(response, "validations") + print_response(response, "validation", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}")