13
13
import os
14
14
import time
15
15
import requests
16
+ import datetime
17
+ from report import report
16
18
from credential import token
17
19
from conf import conf
18
20
@@ -34,6 +36,21 @@ def connection_request(url):
34
36
return response
35
37
36
38
39
+ def connection_request_delete (url ):
40
+ """
41
+ Definition responsible to receive the url, call it and send back the
42
+ response, updating the token whenever necessary.
43
+ """
44
+
45
+ access_token = token .get_token ()
46
+ response = requests .delete (
47
+ url , headers = {"Authorization" : "Bearer {}" .format (access_token )}
48
+ )
49
+
50
+ return response
51
+
52
+
53
+
37
54
def check_authentication (response = None ):
38
55
"""
39
56
Check if the current credential is valid and authenticating, if not, will
@@ -182,6 +199,63 @@ def inventory_list_all():
182
199
return inventory_full_detail
183
200
184
201
202
+ def inventory_list_all_no_system_profile ():
203
+ """
204
+ This def will collect all the HBI entries. This call will be pretty quick
205
+ once we are not consulting the system_profile endpoint, just the host one.
206
+ """
207
+
208
+ url = "https://console.redhat.com/api/inventory/v1/hosts?per_page=1"
209
+ response = connection_request (url )
210
+ check_authentication (response )
211
+
212
+ # Here we are checking the total number of objects and setting the correct
213
+ # number of pages based on that.
214
+ # check_response = divmod(response.json()['total'], 50)
215
+ # ITEMS_PER_PAGE = 10
216
+ check_response = divmod (response .json ()["total" ], conf .ITEMS_PER_PAGE )
217
+
218
+ if check_response [1 ] == 0 :
219
+ num_of_pages = check_response [0 ] + 1
220
+ else :
221
+ num_of_pages = check_response [0 ] + 2
222
+
223
+ list_of_servers = []
224
+ inventory_full_detail = {"results" : "" , "total" : response .json ()["total" ]}
225
+ inventory_full_detail ["results" ] = list_of_servers
226
+
227
+ stage_list = []
228
+ stage_dic = {"server" : stage_list }
229
+
230
+ # For debugin purposes
231
+ # num_of_pages = 2
232
+
233
+ for page in range (1 , num_of_pages ):
234
+ url = (
235
+ "https://console.redhat.com/api/inventory/v1/hosts?per_page="
236
+ + str (conf .ITEMS_PER_PAGE )
237
+ + "&page="
238
+ + str (page )
239
+ + "&order_by=display_name"
240
+ )
241
+ response = connection_request (url )
242
+
243
+ # debug
244
+ # print("page # {}".format(page))
245
+
246
+ for server in response .json ()["results" ]:
247
+
248
+ try :
249
+ stage_dic ["server" ] = server
250
+ except json .decoder .JSONDecodeError :
251
+ stage_dic ["server" ] = {}
252
+
253
+ list_of_servers .append (stage_dic )
254
+ stage_dic = {}
255
+
256
+ return inventory_full_detail
257
+
258
+
185
259
def inventory_list_search_by_name (fqdn ):
186
260
"""
187
261
This def will search the HBI entries by keyword
@@ -230,6 +304,73 @@ def inventory_list_search_by_name(fqdn):
230
304
return inventory_full_detail
231
305
232
306
307
+ def inventory_remove_stale (num_of_days ):
308
+ """
309
+ Def responsible to receive the # of days and check
310
+ all the servers currently in console.redhat.com, check the last
311
+ update and compare with the current date - num_of_days passed
312
+ by the customer.
313
+ """
314
+
315
+ print ("The file with the server list to be removed will be created here: {}" .format (conf .STALE_FILE ))
316
+ # Getting the current date and time
317
+ current_date = datetime .datetime .now ()
318
+ # Calculating the date to be tested and removed before that
319
+ date_to_test_and_remove = current_date - datetime .timedelta (days = int (num_of_days ))
320
+
321
+ print ("Inside remove stale, # of days: {}" .format (num_of_days ))
322
+ print ("Please, wait while checking the information ..." )
323
+ list_server_with_no_system_profile = inventory_list_all_no_system_profile ()
324
+
325
+ # print("ready to go")
326
+ stage_lst = []
327
+
328
+ for srv in list_server_with_no_system_profile ['results' ]:
329
+ srv_last_update = srv ['server' ]['updated' ].split ("T" )[0 ]
330
+ # To convert from string to format type
331
+ srv_last_update_time_format = datetime .datetime .strptime (srv_last_update , "%Y-%m-%d" )
332
+ if srv_last_update_time_format < date_to_test_and_remove :
333
+ stage_lst .append (srv )
334
+
335
+ report .json_stale_systems (stage_lst )
336
+
337
+ count = len (stage_lst )
338
+ print ("We got {} entries with date equal or less than {}." .format (count , date_to_test_and_remove .strftime ("%Y-%m-%d" )))
339
+ print ("if you wish, you can open a second terminal and double-check the file {} with the complete list of servers that will be removed, before you proceed." .format (conf .STALE_FILE ))
340
+ opt = input ("Would you like to proceed and remove then? (y|Y|n|N): " )
341
+
342
+ if (opt == "y" ) or (opt == "Y" ):
343
+ # print("proceed")
344
+ inventory_remove_entry (stage_lst )
345
+ elif (opt == "n" ) or (opt == "N" ):
346
+ print ("Canceling" )
347
+ else :
348
+ print ("Invalid option" )
349
+
350
+ # print("ready to go")
351
+
352
+
353
+ def inventory_remove_entry (srv_to_be_removed ):
354
+ """
355
+ Def responsible to remove the entry from console.redhat.com
356
+ """
357
+ # print("we are here to remove the entries")
358
+
359
+ if len (srv_to_be_removed ) == 0 :
360
+ print ("Nothing to remove" )
361
+ sys .exit ()
362
+
363
+ for srv in srv_to_be_removed :
364
+ print ("here to check" )
365
+ srv_uuid = srv ['server' ]['id' ]
366
+ last_update = srv ['server' ]['updated' ]
367
+ # srv_uuid = "1784587a-a502-4b98-97ea-634e92e882e2"
368
+ url = ("https://console.redhat.com/api/inventory/v1/hosts/" + srv_uuid )
369
+ print ("Removing .: {}, last update at: {}" .format (url , last_update ))
370
+ response = connection_request_delete (url )
371
+ check_authentication (response )
372
+
373
+
233
374
def swatch_list ():
234
375
"""
235
376
This def will collect the first 100 entries from Subscription Watch
0 commit comments