7
7
import json
8
8
import logging
9
9
10
- from typing import TYPE_CHECKING , Optional
10
+ from typing import TYPE_CHECKING , Optional , Union
11
11
import xml .etree .ElementTree as ET
12
12
13
13
import time
@@ -447,7 +447,7 @@ def set_config_element(self, el_name: str, value: str) -> None:
447
447
xml_str = ET .tostring (self ._et )
448
448
self .upload_config (xml_str )
449
449
450
- def get_monitor (self , monitor_name : str , poll_monitor = True ) -> str :
450
+ def get_monitor (self , monitor_name : str , poll_monitor = True ) -> object :
451
451
"""
452
452
Polls the node returning one of the monitors in the monitorData
453
453
branch of the returned node api tree.
@@ -465,68 +465,85 @@ def get_monitor(self, monitor_name: str, poll_monitor=True) -> str:
465
465
466
466
return monitor_data [full_monitor_name ]
467
467
468
+ def get_monitor_dict (
469
+ self ,
470
+ monitor_name : str ,
471
+ poll_monitor : bool = True ,
472
+ ) -> dict :
473
+ value = self .get_monitor (monitor_name , poll_monitor )
474
+ if not isinstance (value , dict ):
475
+ raise JenkinsAPIException (
476
+ f"Monitor { monitor_name !r} did not return a dictionary"
477
+ )
478
+ return value
479
+
468
480
def get_available_physical_memory (self ) -> int :
469
481
"""
470
482
Returns the node's available physical memory in bytes.
471
483
"""
472
- monitor_data = self .get_monitor ("SwapSpaceMonitor" )
484
+ monitor_data = self .get_monitor_dict ("SwapSpaceMonitor" )
473
485
return monitor_data ["availablePhysicalMemory" ]
474
486
475
487
def get_available_swap_space (self ) -> int :
476
488
"""
477
489
Returns the node's available swap space in bytes.
478
490
"""
479
- monitor_data = self .get_monitor ("SwapSpaceMonitor" )
491
+ monitor_data = self .get_monitor_dict ("SwapSpaceMonitor" )
480
492
return monitor_data ["availableSwapSpace" ]
481
493
482
494
def get_total_physical_memory (self ) -> int :
483
495
"""
484
496
Returns the node's total physical memory in bytes.
485
497
"""
486
- monitor_data = self .get_monitor ("SwapSpaceMonitor" )
498
+ monitor_data = self .get_monitor_dict ("SwapSpaceMonitor" )
487
499
return monitor_data ["totalPhysicalMemory" ]
488
500
489
501
def get_total_swap_space (self ) -> int :
490
502
"""
491
503
Returns the node's total swap space in bytes.
492
504
"""
493
- monitor_data = self .get_monitor ("SwapSpaceMonitor" )
505
+ monitor_data = self .get_monitor_dict ("SwapSpaceMonitor" )
494
506
return monitor_data ["totalSwapSpace" ]
495
507
496
508
def get_workspace_path (self ) -> str :
497
509
"""
498
510
Returns the local path to the node's Jenkins workspace directory.
499
511
"""
500
- monitor_data = self .get_monitor ("DiskSpaceMonitor" )
512
+ monitor_data = self .get_monitor_dict ("DiskSpaceMonitor" )
501
513
return monitor_data ["path" ]
502
514
503
515
def get_workspace_size (self ) -> int :
504
516
"""
505
517
Returns the size in bytes of the node's Jenkins workspace directory.
506
518
"""
507
- monitor_data = self .get_monitor ("DiskSpaceMonitor" )
519
+ monitor_data = self .get_monitor_dict ("DiskSpaceMonitor" )
508
520
return monitor_data ["size" ]
509
521
510
522
def get_temp_path (self ) -> str :
511
523
"""
512
524
Returns the local path to the node's temp directory.
513
525
"""
514
- monitor_data = self .get_monitor ("TemporarySpaceMonitor" )
526
+ monitor_data = self .get_monitor_dict ("TemporarySpaceMonitor" )
515
527
return monitor_data ["path" ]
516
528
517
529
def get_temp_size (self ) -> int :
518
530
"""
519
531
Returns the size in bytes of the node's temp directory.
520
532
"""
521
- monitor_data = self .get_monitor ("TemporarySpaceMonitor" )
533
+ monitor_data = self .get_monitor_dict ("TemporarySpaceMonitor" )
522
534
return monitor_data ["size" ]
523
535
524
536
def get_architecture (self ) -> str :
525
537
"""
526
538
Returns the system architecture of the node eg. "Linux (amd64)".
527
539
"""
528
540
# no need to poll as the architecture will never change
529
- return self .get_monitor ("ArchitectureMonitor" , poll_monitor = False )
541
+ value = self .get_monitor ("ArchitectureMonitor" , poll_monitor = False )
542
+ if not isinstance (value , str ):
543
+ raise JenkinsAPIException (
544
+ "Monitor ArchitectureMonitor did not return a string"
545
+ )
546
+ return value
530
547
531
548
def block_until_idle (self , timeout : int , poll_time : int = 5 ) -> None :
532
549
"""
@@ -554,7 +571,7 @@ def get_response_time(self) -> int:
554
571
"""
555
572
Returns the node's average response time.
556
573
"""
557
- monitor_data = self .get_monitor ("ResponseTimeMonitor" )
574
+ monitor_data = self .get_monitor_dict ("ResponseTimeMonitor" )
558
575
return monitor_data ["average" ]
559
576
560
577
def get_clock_difference (self ) -> int :
@@ -563,5 +580,5 @@ def get_clock_difference(self) -> int:
563
580
the master Jenkins clock.
564
581
Used to detect out of sync clocks.
565
582
"""
566
- monitor_data = self .get_monitor ("ClockMonitor" )
583
+ monitor_data = self .get_monitor_dict ("ClockMonitor" )
567
584
return monitor_data ["diff" ]
0 commit comments