Skip to content

Commit 18bf960

Browse files
committed
node: fix get_monitor result type and cast explicitly
1 parent 720586d commit 18bf960

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
@@ -447,7 +447,7 @@ def set_config_element(self, el_name: str, value: str) -> None:
447447
xml_str = ET.tostring(self._et)
448448
self.upload_config(xml_str)
449449

450-
def get_monitor(self, monitor_name: str, poll_monitor=True) -> str:
450+
def get_monitor(self, monitor_name: str, poll_monitor=True) -> object:
451451
"""
452452
Polls the node returning one of the monitors in the monitorData
453453
branch of the returned node api tree.
@@ -465,68 +465,85 @@ def get_monitor(self, monitor_name: str, poll_monitor=True) -> str:
465465

466466
return monitor_data[full_monitor_name]
467467

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+
468480
def get_available_physical_memory(self) -> int:
469481
"""
470482
Returns the node's available physical memory in bytes.
471483
"""
472-
monitor_data = self.get_monitor("SwapSpaceMonitor")
484+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
473485
return monitor_data["availablePhysicalMemory"]
474486

475487
def get_available_swap_space(self) -> int:
476488
"""
477489
Returns the node's available swap space in bytes.
478490
"""
479-
monitor_data = self.get_monitor("SwapSpaceMonitor")
491+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
480492
return monitor_data["availableSwapSpace"]
481493

482494
def get_total_physical_memory(self) -> int:
483495
"""
484496
Returns the node's total physical memory in bytes.
485497
"""
486-
monitor_data = self.get_monitor("SwapSpaceMonitor")
498+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
487499
return monitor_data["totalPhysicalMemory"]
488500

489501
def get_total_swap_space(self) -> int:
490502
"""
491503
Returns the node's total swap space in bytes.
492504
"""
493-
monitor_data = self.get_monitor("SwapSpaceMonitor")
505+
monitor_data = self.get_monitor_dict("SwapSpaceMonitor")
494506
return monitor_data["totalSwapSpace"]
495507

496508
def get_workspace_path(self) -> str:
497509
"""
498510
Returns the local path to the node's Jenkins workspace directory.
499511
"""
500-
monitor_data = self.get_monitor("DiskSpaceMonitor")
512+
monitor_data = self.get_monitor_dict("DiskSpaceMonitor")
501513
return monitor_data["path"]
502514

503515
def get_workspace_size(self) -> int:
504516
"""
505517
Returns the size in bytes of the node's Jenkins workspace directory.
506518
"""
507-
monitor_data = self.get_monitor("DiskSpaceMonitor")
519+
monitor_data = self.get_monitor_dict("DiskSpaceMonitor")
508520
return monitor_data["size"]
509521

510522
def get_temp_path(self) -> str:
511523
"""
512524
Returns the local path to the node's temp directory.
513525
"""
514-
monitor_data = self.get_monitor("TemporarySpaceMonitor")
526+
monitor_data = self.get_monitor_dict("TemporarySpaceMonitor")
515527
return monitor_data["path"]
516528

517529
def get_temp_size(self) -> int:
518530
"""
519531
Returns the size in bytes of the node's temp directory.
520532
"""
521-
monitor_data = self.get_monitor("TemporarySpaceMonitor")
533+
monitor_data = self.get_monitor_dict("TemporarySpaceMonitor")
522534
return monitor_data["size"]
523535

524536
def get_architecture(self) -> str:
525537
"""
526538
Returns the system architecture of the node eg. "Linux (amd64)".
527539
"""
528540
# 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
530547

531548
def block_until_idle(self, timeout: int, poll_time: int = 5) -> None:
532549
"""
@@ -554,7 +571,7 @@ def get_response_time(self) -> int:
554571
"""
555572
Returns the node's average response time.
556573
"""
557-
monitor_data = self.get_monitor("ResponseTimeMonitor")
574+
monitor_data = self.get_monitor_dict("ResponseTimeMonitor")
558575
return monitor_data["average"]
559576

560577
def get_clock_difference(self) -> int:
@@ -563,5 +580,5 @@ def get_clock_difference(self) -> int:
563580
the master Jenkins clock.
564581
Used to detect out of sync clocks.
565582
"""
566-
monitor_data = self.get_monitor("ClockMonitor")
583+
monitor_data = self.get_monitor_dict("ClockMonitor")
567584
return monitor_data["diff"]

0 commit comments

Comments
 (0)