Skip to content

Commit cf31d9c

Browse files
committed
node: fix get_monitor result type and cast explicitly
1 parent d87a401 commit cf31d9c

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

jenkinsapi/node.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import logging
99

10-
from typing import TYPE_CHECKING, Optional
10+
from typing import TYPE_CHECKING, Optional, Union
1111
import xml.etree.ElementTree as ET
1212

1313
import time
@@ -496,7 +496,7 @@ def set_config_element(self, el_name: str, value: str) -> None:
496496
xml_str = ET.tostring(self._et)
497497
self.upload_config(xml_str)
498498

499-
def get_monitor(self, monitor_name: str, poll_monitor=True) -> str:
499+
def get_monitor(self, monitor_name: str, poll_monitor=True) -> object:
500500
"""
501501
Polls the node returning one of the monitors in the monitorData
502502
branch of the returned node api tree.
@@ -514,68 +514,85 @@ def get_monitor(self, monitor_name: str, poll_monitor=True) -> str:
514514

515515
return monitor_data[full_monitor_name]
516516

517+
def get_monitor_dict(
518+
self,
519+
monitor_name: str,
520+
poll_monitor: bool = True,
521+
) -> dict:
522+
value = self.get_monitor(monitor_name, poll_monitor)
523+
if not isinstance(value, dict):
524+
raise JenkinsAPIException(
525+
f"Monitor {monitor_name!r} did not return a dictionary"
526+
)
527+
return value
528+
517529
def get_available_physical_memory(self) -> int:
518530
"""
519531
Returns the node's available physical memory in bytes.
520532
"""
521-
monitor_data = self.get_monitor("SwapSpaceMonitor")
533+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
522534
return monitor_data["availablePhysicalMemory"]
523535

524536
def get_available_swap_space(self) -> int:
525537
"""
526538
Returns the node's available swap space in bytes.
527539
"""
528-
monitor_data = self.get_monitor("SwapSpaceMonitor")
540+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
529541
return monitor_data["availableSwapSpace"]
530542

531543
def get_total_physical_memory(self) -> int:
532544
"""
533545
Returns the node's total physical memory in bytes.
534546
"""
535-
monitor_data = self.get_monitor("SwapSpaceMonitor")
547+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
536548
return monitor_data["totalPhysicalMemory"]
537549

538550
def get_total_swap_space(self) -> int:
539551
"""
540552
Returns the node's total swap space in bytes.
541553
"""
542-
monitor_data = self.get_monitor("SwapSpaceMonitor")
554+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
543555
return monitor_data["totalSwapSpace"]
544556

545557
def get_workspace_path(self) -> str:
546558
"""
547559
Returns the local path to the node's Jenkins workspace directory.
548560
"""
549-
monitor_data = self.get_monitor("DiskSpaceMonitor")
561+
monitor_data = self.get_monitor_dict("DiskSpaceMonitor")
550562
return monitor_data["path"]
551563

552564
def get_workspace_size(self) -> int:
553565
"""
554566
Returns the size in bytes of the node's Jenkins workspace directory.
555567
"""
556-
monitor_data = self.get_monitor("DiskSpaceMonitor")
568+
monitor_data = self.get_monitor_dict("DiskSpaceMonitor")
557569
return monitor_data["size"]
558570

559571
def get_temp_path(self) -> str:
560572
"""
561573
Returns the local path to the node's temp directory.
562574
"""
563-
monitor_data = self.get_monitor("TemporarySpaceMonitor")
575+
monitor_data = self.get_monitor_dict("TemporarySpaceMonitor")
564576
return monitor_data["path"]
565577

566578
def get_temp_size(self) -> int:
567579
"""
568580
Returns the size in bytes of the node's temp directory.
569581
"""
570-
monitor_data = self.get_monitor("TemporarySpaceMonitor")
582+
monitor_data = self.get_monitor_dict("TemporarySpaceMonitor")
571583
return monitor_data["size"]
572584

573585
def get_architecture(self) -> str:
574586
"""
575587
Returns the system architecture of the node eg. "Linux (amd64)".
576588
"""
577589
# no need to poll as the architecture will never change
578-
return self.get_monitor("ArchitectureMonitor", poll_monitor=False)
590+
value = self.get_monitor("ArchitectureMonitor", poll_monitor=False)
591+
if not isinstance(value, str):
592+
raise JenkinsAPIException(
593+
"Monitor ArchitectureMonitor did not return a string"
594+
)
595+
return value
579596

580597
def block_until_idle(self, timeout: int, poll_time: int = 5) -> None:
581598
"""
@@ -603,7 +620,7 @@ def get_response_time(self) -> int:
603620
"""
604621
Returns the node's average response time.
605622
"""
606-
monitor_data = self.get_monitor("ResponseTimeMonitor")
623+
monitor_data = self.get_monitor_dict("ResponseTimeMonitor")
607624
return monitor_data["average"]
608625

609626
def get_clock_difference(self) -> int:
@@ -612,5 +629,5 @@ def get_clock_difference(self) -> int:
612629
the master Jenkins clock.
613630
Used to detect out of sync clocks.
614631
"""
615-
monitor_data = self.get_monitor("ClockMonitor")
632+
monitor_data = self.get_monitor_dict("ClockMonitor")
616633
return monitor_data["diff"]

0 commit comments

Comments
 (0)