diff --git a/cluster_api.py b/cluster_api.py index f6bcf43..c241272 100644 --- a/cluster_api.py +++ b/cluster_api.py @@ -34,4 +34,16 @@ def cluster_info(base_url, api_key): # output short clusters overview else: console.print(f"[red]Failed to retrieve data {response.status_code}[/]") Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:") - os.system('cls' if os.name == 'nt' else 'clear') \ No newline at end of file + os.system('cls' if os.name == 'nt' else 'clear') + +def check_api_key(base_url, api_key): # test api key and show spaceVM version + url = f"http://{base_url}/api/controllers/base-version/" + response = requests.get(url, headers={'Authorization': api_key}) + console = Console() + if response.status_code == 200: + cluster_info = response.json() + version = cluster_info['version'] + console.print(f"[bold green]Successfully conected to SpaceVM version {version}") + else: + console.print(f"[bold red]{response.status_code}[/]") + return response.status_code \ No newline at end of file diff --git a/config_data_import.py b/config_data_import.py index 137e912..0b6e45f 100644 --- a/config_data_import.py +++ b/config_data_import.py @@ -1,5 +1,8 @@ import os +import subprocess +from cluster_api import * +from data_pools_api import * from rich import print from rich.panel import Panel from rich.console import Console , Align @@ -55,7 +58,12 @@ def config_edit(config_relative_path): menu_choice=str(read_input) if menu_choice == "Y" or menu_choice == "y": base_url = input("Type SpaceVM Controller IP: ") + while ping(base_url) != True: + base_url = console.input("[bold red]No response.\nCheck and type SpaceVM Controller IP again: [/]") api_key = input("Type your API Key: ") + while check_api_key(base_url, "jwt " + api_key) != 200: + api_key = console.input("[bold red]Check and type SpaceVM Controller API Key again: [/]") + data_pools(base_url,"jwt " + api_key) data_pool_uuid = input("Type Data Pool UUID you wish to use: ") lines = [base_url, api_key, data_pool_uuid] with open(config_relative_path, "w+") as file: @@ -74,4 +82,16 @@ def config_edit(config_relative_path): cls() def cls(): - os.system('cls' if os.name=='nt' else 'clear') \ No newline at end of file + os.system('cls' if os.name=='nt' else 'clear') + +def ping(base_url): + DNULL = open(os.devnull, 'w') + if os.name == 'nt': + status = subprocess.call(["ping","-n","1",base_url],stdout = DNULL) + else: + status = subprocess.call(["ping","-c","1",base_url],stdout = DNULL) + + if status == 0: + return True + else: + return False \ No newline at end of file diff --git a/data_pools_api.py b/data_pools_api.py index 422c29e..df2d2f3 100644 --- a/data_pools_api.py +++ b/data_pools_api.py @@ -12,7 +12,7 @@ def data_pools(base_url, api_key): # output data pool info if response.status_code == 200: data_pools = response.json() results_data_pools_info = data_pools['results'] - os.system('cls' if os.name == 'nt' else 'clear') + #os.system('cls' if os.name == 'nt' else 'clear') console.rule("[bold cyan]Data Pools Overview") console.print(f"[bold]Data pools total:[/] {data_pools['count']}\n") panels = [] @@ -32,5 +32,4 @@ def data_pools(base_url, api_key): # output data pool info console.print(*panels, sep="\n") else: console.print(f"[red]Failed to retrieve data {response.status_code}[/]") - Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:") - os.system('cls' if os.name == 'nt' else 'clear') \ No newline at end of file + Prompt.ask("[green_yellow bold]ENTER - return to Main Menu.. :right_arrow_curving_down:") \ No newline at end of file diff --git a/main.py b/main.py index 0ad5154..6c85f3d 100644 --- a/main.py +++ b/main.py @@ -51,5 +51,7 @@ title="[bold magenta]SpaceVM Utility - Main Menu" , subtitle = menu_subtitle, su data_pools(base_url , api_key) if menu_choice == "6": vm_info_short(base_url , api_key) + if menu_choice == "7": + check_api_key(base_url, api_key) os.system('cls' if os.name=='nt' else 'clear') #clears screen before looping back to main menu -console.print("[red bold]Exiting Utility ") \ No newline at end of file +console.print("[red bold]Exiting Utility ")