Skip to content

Commit

Permalink
Handle resource alias not found error (#29)
Browse files Browse the repository at this point in the history
* Handle resource alias not found error

* Move read alias file to utils
  • Loading branch information
jpeimer authored Nov 14, 2024
1 parent ec8715c commit 9855168
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 43 deletions.
9 changes: 9 additions & 0 deletions app/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
from rich.console import Console

CONSOLE = Console()

ALIASES_FILE_PATH = "app/manifests/aliases.json"

HOW_TO_UPDATE_ALIASES_MESSAGE = (
"How to update the resource aliases file: "
"https://github.com/RedHatQE/must-gather-explorer?tab=readme-ov-file#update-cluster-resources-aliases\n"
)
14 changes: 14 additions & 0 deletions app/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class MissingResourceKindAliasError(Exception):
def __init__(self, requested_kind: str) -> None:
self.requested_kind = requested_kind

def __repr__(self) -> str:
return f"Resource kind alias '{self.requested_kind}' not found"


class FailToReadJSONFileError(Exception):
def __init__(self, file_name: str) -> None:
self.file_name = file_name

def __repr__(self) -> str:
return f"Can't read file '{self.file_name}'"
56 changes: 22 additions & 34 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import json
import os
import sys
from functools import lru_cache
from typing import Any, Dict, List

import click
import yaml
from rich.console import Console
from rich.prompt import Prompt
from rich.table import Table

from app.constants import ALIASES_FILE_PATH

CONSOLE = Console()

NAMESPACE_FLAG = "-n"
from app.constants import HOW_TO_UPDATE_ALIASES_MESSAGE, CONSOLE
from app.exceptions import MissingResourceKindAliasError, FailToReadJSONFileError
from app.utils import read_aliases_file


@click.command("must-gather-explorer")
Expand Down Expand Up @@ -115,11 +111,12 @@ def main(
print_help()
continue

namespace_flag = "-n"
namespace_name = ""
if NAMESPACE_FLAG in commands_list:
namespace_index = commands_list.index(NAMESPACE_FLAG)
if namespace_flag in commands_list:
namespace_index = commands_list.index(namespace_flag)
namespace_name = commands_list[namespace_index + 1]
commands_list.remove(NAMESPACE_FLAG)
commands_list.remove(namespace_flag)
commands_list.remove(namespace_name)

if not commands_list:
Expand Down Expand Up @@ -149,14 +146,19 @@ def main(
continue
resource_name = commands_list[0]

resources_raw_data = get_cluster_resources_raw_data(
all_resources=all_resources, kind=resource_kind, name=resource_name, namespace=namespace_name
)
if not resources_raw_data:
CONSOLE.print(f"No resources found for {resource_kind} {resource_name} {namespace_name}")
continue
try:
resources_raw_data = get_cluster_resources_raw_data(
all_resources=all_resources, kind=resource_kind, name=resource_name, namespace=namespace_name
)
if not resources_raw_data:
CONSOLE.print(f"No resources found for {resource_kind} {resource_name} {namespace_name}")
continue
actions_dict[action_name](resources_raw_data, print_yaml)

actions_dict[action_name](resources_raw_data, print_yaml)
except FailToReadJSONFileError:
sys.exit(1)
except MissingResourceKindAliasError:
continue


def get_resources(resources_raw_data: List[Dict[str, Any]], print_yaml: bool = False, **kwargs: Dict[Any, Any]) -> None:
Expand Down Expand Up @@ -238,21 +240,7 @@ def get_cluster_resources_raw_data(
def get_resource_kind_by_alias(requested_kind: str) -> str:
kind_lower = requested_kind.lower()

how_to_update_aliases_message = (
"How to update the resource aliases file: "
"https://github.com/RedHatQE/must-gather-explorer?tab=readme-ov-file#update-cluster-resources-aliases\n"
)

try:
with open(ALIASES_FILE_PATH) as aliases_file:
resources_aliases = json.load(aliases_file)
except Exception as exp:
CONSOLE.print(
f"[bold red]Error:[/bold red] Can't read the aliases_file\n"
f"Error details: {exp}\n"
f"{how_to_update_aliases_message}"
)
sys.exit(1)
resources_aliases = read_aliases_file()

for kind, aliases in resources_aliases.items():
if kind == kind_lower or kind_lower in aliases:
Expand All @@ -261,9 +249,9 @@ def get_resource_kind_by_alias(requested_kind: str) -> str:
CONSOLE.print(
f"[bold red]Error:[/bold red] Not valid resource kind '{kind_lower}', "
f"please make sure it was typed correctly and alias file is up to date\n"
f"{how_to_update_aliases_message}"
f"{HOW_TO_UPDATE_ALIASES_MESSAGE}"
)
sys.exit(2)
raise MissingResourceKindAliasError(requested_kind=requested_kind)


if __name__ == "__main__":
Expand Down
12 changes: 3 additions & 9 deletions app/resource_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import click
from pyhelper_utils.shell import run_command
from rich.console import Console

from app.constants import ALIASES_FILE_PATH

CONSOLE = Console()
from app.constants import ALIASES_FILE_PATH, CONSOLE
from app.utils import read_aliases_file


@click.command("update-resources-aliases")
Expand All @@ -22,11 +20,7 @@ def fill_api_resources_aliases() -> None:
CONSOLE.print(f"Error message: {err}")
sys.exit(1)

try:
with open(ALIASES_FILE_PATH) as aliases_file:
resources_aliases = json.load(aliases_file)
except Exception:
resources_aliases = {}
resources_aliases = read_aliases_file(raise_on_error=False)

cli_resource_alias: Dict[str, List[str]] = {}

Expand Down
19 changes: 19 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

from app.constants import ALIASES_FILE_PATH, HOW_TO_UPDATE_ALIASES_MESSAGE, CONSOLE
from app.exceptions import FailToReadJSONFileError


def read_aliases_file(raise_on_error: bool = True) -> dict[str, list[str]]:
try:
with open(ALIASES_FILE_PATH) as aliases_file:
return json.load(aliases_file)
except (FileNotFoundError, json.JSONDecodeError) as exp:
if raise_on_error:
CONSOLE.print(
f"[bold red]Error:[/bold red] Can't read the aliases_file\n"
f"Error details: {exp}\n"
f"{HOW_TO_UPDATE_ALIASES_MESSAGE}"
)
raise FailToReadJSONFileError(file_name=ALIASES_FILE_PATH)
return {}

0 comments on commit 9855168

Please sign in to comment.