Skip to content

Commit

Permalink
added the feature to list all the stale and stale_warning
Browse files Browse the repository at this point in the history
  • Loading branch information
waldirio committed Mar 15, 2024
1 parent ac7247b commit 92fd896
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
98 changes: 98 additions & 0 deletions crhc_cli/execution/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,104 @@ 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.
"""

url = "https://console.redhat.com/api/inventory/v1/hosts?staleness=stale&staleness=stale_warning&per_page=1"
response = connection_request(url)
check_authentication(response)

# Here we are checking the total number of objects and setting the correct
# number of pages based on that.
# check_response = divmod(response.json()['total'], 50)
# ITEMS_PER_PAGE = 10
# check_response = divmod(response.json()["total"], conf.ITEMS_PER_PAGE)

num_of_pages = return_num_of_pages(response.json()["total"], type="inventory")

# if check_response[1] == 0:
# num_of_pages = check_response[0] + 1
# else:
# num_of_pages = check_response[0] + 2

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
Expand Down
1 change: 1 addition & 0 deletions crhc_cli/help/help_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down
46 changes: 46 additions & 0 deletions crhc_cli/parse/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ 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"):
# execution.inventory_list()
# response = execution.inventory_list()
# print(response)
# print(json.dumps(response, indent=4))
# num_of_days = input("Please, type the # of days with no updates. 0 for today, 1 for yesteday, 2 for 2 days ago ...: ")
# if num_of_days.isdigit():
# pass
# else:
# print("invalid entry, exiting")
# sys.exit()
response = execution.inventory_list_stale()
print(json.dumps(response, indent=4))
# sys.exit()
# print("end here")
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"):
Expand Down Expand Up @@ -194,6 +219,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():
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down

0 comments on commit 92fd896

Please sign in to comment.