From 065daeeaf6c2eb8a41a1cd5d8fe59654473c14eb Mon Sep 17 00:00:00 2001 From: Waldirio M Pinheiro Date: Thu, 14 Mar 2024 23:40:24 -0700 Subject: [PATCH] [ENHANCEMENT] added the feature to list all the stale and stale_warning * added the feature to list all the stale and stale_warning * clearing some left over --- crhc_cli/execution/execution.py | 88 +++++++++++++++++++++++++++++++++ crhc_cli/help/help_opt.py | 1 + crhc_cli/parse/parse.py | 34 +++++++++++++ tests/test_help.py | 1 + 4 files changed, 124 insertions(+) diff --git a/crhc_cli/execution/execution.py b/crhc_cli/execution/execution.py index 4adaaaa..cafb6b6 100644 --- a/crhc_cli/execution/execution.py +++ b/crhc_cli/execution/execution.py @@ -369,6 +369,94 @@ def inventory_list_search_by_name(fqdn): return inventory_full_detail +def inventory_list_stale(current_only=False): + """ + Def resposible to retrieve the list of entries in stale and + stale_warning status. + """ + + # Adding the stale and stale_warning to return the correct # of elements + url = "https://console.redhat.com/api/inventory/v1/hosts?staleness=stale&staleness=stale_warning&per_page=1" + response = connection_request(url) + check_authentication(response) + + num_of_pages = return_num_of_pages(response.json()["total"], type="inventory") + + list_of_servers = [] + inventory_full_detail = {"results": "", "total": response.json()["total"]} + inventory_full_detail["results"] = list_of_servers + + stage_list = [] + stage_dic = {"server": stage_list} + + # For debugin purposes + # num_of_pages = 2 + + for page in range(1, num_of_pages): + url = ( + "https://console.redhat.com/api/inventory/v1/hosts?staleness=stale&staleness=stale_warning&per_page=" + + str(conf.ITEMS_PER_PAGE) + + "&page=" + + str(page) + + "&order_by=display_name" + ) + response = connection_request(url) + + inventory_batch = [] + # is_first_server = True + server_detail_url = "https://console.redhat.com/api/inventory/v1/hosts/" + for server in response.json()["results"]: + server_id = server["id"] + stale_timestamp = server["stale_timestamp"] + # if you want all systems, or just if you want current systems ,and thisone is current + if (not current_only or (current_only and is_fresh(stale_timestamp))): + inventory_batch.append(server_id) + # if its the first entry + if (len(inventory_batch) == 1): + server_detail_url = server_detail_url + server_id + else: + server_detail_url = server_detail_url + "," + server_id + + # now call the server details request with up to 50 ids, assuming that we have some server ids in this batch + if (len(inventory_batch) > 0): + url = ( + server_detail_url + + "/system_profile" + + FIELDS_TO_RETRIEVE + ) + response_system_profile = connection_request(url) + + # now loop through the original server request + for server in response.json()["results"]: + # check whether we're getting everything - or whether the system is current or not + stale_timestamp = server["stale_timestamp"] + if (not current_only or (current_only and is_fresh(stale_timestamp))): + try: + stage_dic["server"] = server + except json.decoder.JSONDecodeError: + stage_dic["server"] = {} + + server_id = server["id"] + + try: + server_details_list = response_system_profile.json()["results"] + # loop through all the server details - finding the one that matches the id we're looping through + for server_details in server_details_list: + if (server_details["id"] == server_id): + stage_dic["system_profile"] = server_details["system_profile"] + except json.decoder.JSONDecodeError: + stage_dic["system_profile"] = {} + except KeyError: + stage_dic["system_profile"] = {} + + list_of_servers.append(stage_dic) + stage_dic = {} + + return inventory_full_detail + + + + def inventory_remove_stale(num_of_days): """ Def responsible to receive the # of days and check diff --git a/crhc_cli/help/help_opt.py b/crhc_cli/help/help_opt.py index ed2de1a..062b9b7 100644 --- a/crhc_cli/help/help_opt.py +++ b/crhc_cli/help/help_opt.py @@ -51,6 +51,7 @@ def help_inventory_menu(): list List the inventory entries, first 50\n\ list_all List all the inventory entries\n\ display_name Please, type the FQDN or Partial Hostname\n\ + list_stale List all the machines in stale and stale_warning status\n\ remove_stale Remove all the stale entries based on the # of days\n\ \n\ Flags: \n\ diff --git a/crhc_cli/parse/parse.py b/crhc_cli/parse/parse.py index fecf30b..ac09276 100644 --- a/crhc_cli/parse/parse.py +++ b/crhc_cli/parse/parse.py @@ -72,6 +72,19 @@ def inventory_sub_menu(): # print("Error: {}".format(e)) ... + # To print in JSON format + try: + if (sys.argv[1] == "inventory") and (sys.argv[2] == "list_stale"): + response = execution.inventory_list_stale() + print(json.dumps(response, indent=4)) + sys.exit() + except IndexError as e: + # print("Error: {}".format(e)) + ... + + + + # To print in JSON format try: if (sys.argv[1] == "inventory") and (sys.argv[2] == "remove_stale"): @@ -194,6 +207,27 @@ def inventory_sub_menu(): # print("Error: {}".format(e)) ... + # To print in CSV format + try: + if ( + (sys.argv[1] == "inventory") + and (sys.argv[2] == "list_stale") + and (sys.argv[3] == "--csv") + ): + # Checking if the connection still alive before + # printing sometihng + if execution.check_authentication(): + print( + "This process can spend some minutes according to \ +the number of servers in your account." + ) + response = execution.inventory_list_stale() + report.csv_report_inventory(response) + sys.exit() + except IndexError as e: + # print("Error: {}".format(e)) + ... + def swatch_sub_menu(): """ diff --git a/tests/test_help.py b/tests/test_help.py index 490eb81..31d786a 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -53,6 +53,7 @@ def test_check_inventory_help_menu(): list List the inventory entries, first 50\n\ list_all List all the inventory entries\n\ display_name Please, type the FQDN or Partial Hostname\n\ + list_stale List all the machines in stale and stale_warning status\n\ remove_stale Remove all the stale entries based on the # of days\n\ \n\ Flags: \n\