@@ -128,7 +128,7 @@ def inventory_list():
128
128
return inventory_full_detail
129
129
130
130
131
- def inventory_list_all ():
131
+ def inventory_list_all (current_only = False ):
132
132
"""
133
133
This def will collect all the HBI entries
134
134
"""
@@ -157,7 +157,8 @@ def inventory_list_all():
157
157
158
158
# For debugin purposes
159
159
# num_of_pages = 2
160
-
160
+
161
+
161
162
162
163
for page in range (1 , num_of_pages ):
163
164
url = (
@@ -169,50 +170,59 @@ def inventory_list_all():
169
170
)
170
171
response = connection_request (url )
171
172
173
+ inventory_batch = []
174
+ is_first_server = True
175
+ server_detail_url = "https://console.redhat.com/api/inventory/v1/hosts/"
172
176
inventory_batch = []
173
177
is_first_server = True
174
178
server_detail_url = "https://console.redhat.com/api/inventory/v1/hosts/"
175
179
for server in response .json ()["results" ]:
176
180
server_id = server ["id" ]
177
- inventory_batch .append (server_id )
178
- # if its the first entry
179
- if (len (inventory_batch ) == 1 ):
180
- server_detail_url = server_detail_url + server_id
181
- else :
182
- server_detail_url = server_detail_url + "," + server_id
183
-
184
- # now call the server details request with up to 50 ids
185
- url = (
186
- server_detail_url
187
- + "/system_profile"
188
- + FIELDS_TO_RETRIEVE
189
- )
190
- response_system_profile = connection_request (url )
191
-
192
-
193
- # now loop through the original server request
194
- for server in response .json ()["results" ]:
195
-
196
- try :
197
- stage_dic ["server" ] = server
198
- except json .decoder .JSONDecodeError :
199
- stage_dic ["server" ] = {}
200
-
201
- server_id = server ["id" ]
202
-
203
- try :
204
- server_details_list = response_system_profile .json ()["results" ]
205
- # loop through all the server details - finding the one that matches the id we're looping through
206
- for server_details in server_details_list :
207
- if (server_details ["id" ] == server_id ):
208
- stage_dic ["system_profile" ] = server_details ["system_profile" ]
209
- except json .decoder .JSONDecodeError :
210
- stage_dic ["system_profile" ] = {}
211
- except KeyError :
212
- stage_dic ["system_profile" ] = {}
213
-
214
- list_of_servers .append (stage_dic )
215
- stage_dic = {}
181
+ stale_timestamp = server ["stale_timestamp" ]
182
+ # if you want all systems, or just if you want current systems ,and thisone is current
183
+ if (not current_only or (current_only and is_fresh (stale_timestamp ))):
184
+ inventory_batch .append (server_id )
185
+ # if its the first entry
186
+ if (len (inventory_batch ) == 1 ):
187
+ server_detail_url = server_detail_url + server_id
188
+ else :
189
+ server_detail_url = server_detail_url + "," + server_id
190
+
191
+ # now call the server details request with up to 50 ids, assuming that we have some server ids in this batch
192
+ if (len (inventory_batch ) > 0 ):
193
+ url = (
194
+ server_detail_url
195
+ + "/system_profile"
196
+ + FIELDS_TO_RETRIEVE
197
+ )
198
+ response_system_profile = connection_request (url )
199
+
200
+
201
+ # now loop through the original server request
202
+ for server in response .json ()["results" ]:
203
+ # check whether we're getting everything - or whether the system is current or not
204
+ stale_timestamp = server ["stale_timestamp" ]
205
+ if (not current_only or (current_only and is_fresh (stale_timestamp ))):
206
+ try :
207
+ stage_dic ["server" ] = server
208
+ except json .decoder .JSONDecodeError :
209
+ stage_dic ["server" ] = {}
210
+
211
+ server_id = server ["id" ]
212
+
213
+ try :
214
+ server_details_list = response_system_profile .json ()["results" ]
215
+ # loop through all the server details - finding the one that matches the id we're looping through
216
+ for server_details in server_details_list :
217
+ if (server_details ["id" ] == server_id ):
218
+ stage_dic ["system_profile" ] = server_details ["system_profile" ]
219
+ except json .decoder .JSONDecodeError :
220
+ stage_dic ["system_profile" ] = {}
221
+ except KeyError :
222
+ stage_dic ["system_profile" ] = {}
223
+
224
+ list_of_servers .append (stage_dic )
225
+ stage_dic = {}
216
226
217
227
return inventory_full_detail
218
228
@@ -407,7 +417,7 @@ def swatch_list():
407
417
return response .json ()
408
418
409
419
410
- def swatch_list_all ():
420
+ def swatch_list_all (current_only = False ):
411
421
"""
412
422
This def will collect all the entries from Subscription Watch
413
423
"""
@@ -427,10 +437,7 @@ def swatch_list_all():
427
437
)
428
438
# num_of_pages = round(response.json()['meta']['count'] / 100 + 1)
429
439
430
- dic_full_list = {
431
- "data" : "" ,
432
- "meta" : {"count" : response .json ()["meta" ]["count" ]},
433
- }
440
+
434
441
full_list = []
435
442
dup_kvm_servers = []
436
443
server_with_no_dupes = []
@@ -449,7 +456,15 @@ def swatch_list_all():
449
456
# count = count + 100
450
457
451
458
for entry in response .json ()["data" ]:
452
- full_list .append (entry )
459
+ last_seen = entry .get ("last_seen" )
460
+ # either get all systems, or if getting current, check the last seen date
461
+ if (not current_only or (current_only and seen_recently (last_seen ))):
462
+ full_list .append (entry )
463
+
464
+ dic_full_list = {
465
+ "data" : "" ,
466
+ "meta" : {"count" : len (full_list )},
467
+ }
453
468
454
469
# The piece below is just to check/remove the duplicate entries
455
470
# caused by kvm/libvirt hypervisors. At this moment, swatch is
@@ -532,6 +547,39 @@ def swatch_list_all():
532
547
533
548
return dic_full_list
534
549
550
+ def is_fresh (stale_timestamp ):
551
+ stale_date_string = stale_timestamp
552
+ is_fresh = True
553
+ if (len (stale_date_string ) > 19 ):
554
+ stale_date_string = stale_timestamp [:19 ]
555
+ try :
556
+ stale_date = datetime .datetime .strptime (stale_date_string ,"%Y-%m-%dT%H:%M:%S" )
557
+ current_date = datetime .datetime .now ()
558
+ if (stale_date < current_date ):
559
+ is_fresh = False
560
+ except Exception as e :
561
+ is_fresh = True
562
+ print ("Exception in is_fresh : " + str (e ))
563
+
564
+ return is_fresh
565
+
566
+ def seen_recently (last_seen ):
567
+ stale_date_string = last_seen
568
+ seen_recently = True
569
+ if (len (stale_date_string ) > 19 ):
570
+ stale_date_string = last_seen [:19 ]
571
+ try :
572
+ last_seen_date = datetime .datetime .strptime (stale_date_string ,"%Y-%m-%dT%H:%M:%S" )
573
+ stale_date = last_seen_date + datetime .timedelta (days = 1 )
574
+ current_date = datetime .datetime .now ()
575
+ if (stale_date < current_date ):
576
+ seen_recently = False
577
+ except Exception as e :
578
+ seen_recently = True
579
+ print ("Exception in seen_recently : " + str (e ))
580
+
581
+ return seen_recently
582
+
535
583
536
584
def swatch_socket_summary ():
537
585
"""
0 commit comments