From d7f033030e066b7f28060e513ed12c0bd6fcb664 Mon Sep 17 00:00:00 2001 From: lucasheld Date: Fri, 7 Apr 2023 21:03:33 +0200 Subject: [PATCH] feat: add support for uptime kuma 1.21.2 --- README.md | 2 +- run_tests.sh | 2 +- tests/test_maintenance.py | 62 +++---- uptime_kuma_api/api.py | 217 +++++++++++++----------- uptime_kuma_api/docstrings.py | 5 +- uptime_kuma_api/maintenance_strategy.py | 3 + 6 files changed, 152 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index 0cee7af..84f57c8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This package was developed to configure Uptime Kuma with Ansible. The Ansible co Python version 3.6+ is required. -Supported Uptime Kuma versions: 1.17.0 - 1.21.1 +Supported Uptime Kuma versions: 1.17.0 - 1.21.2 Installation --- diff --git a/run_tests.sh b/run_tests.sh index 2a573bb..af7ff36 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -5,7 +5,7 @@ if [ $version ] then versions=("$version") else - versions=(1.21.1 1.20.2 1.19.6 1.18.5 1.17.1) + versions=(1.21.2 1.21.1 1.20.2 1.19.6 1.18.5 1.17.1) fi for version in ${versions[*]} diff --git a/tests/test_maintenance.py b/tests/test_maintenance.py index a298443..e5cf40e 100644 --- a/tests/test_maintenance.py +++ b/tests/test_maintenance.py @@ -24,20 +24,15 @@ class TestMaintenance(UptimeKumaTestCase): "2022-12-27 22:36:00", "2022-12-29 22:36:00" ], - "timeRange": [ - { - "hours": 2, - "minutes": 0 - }, - { - "hours": 3, - "minutes": 0 - } - ], "weekdays": [], "daysOfMonth": [] } + if parse_version(self.api.version) >= parse_version("1.21.2"): + expected_maintenance.update({ + "timezone": "Europe/Berlin" + }) + # add maintenance r = self.api.add_maintenance(**expected_maintenance) self.assertEqual(r["msg"], "Added Successfully.") @@ -119,18 +114,6 @@ class TestMaintenance(UptimeKumaTestCase): "dateRange": [ "2022-12-27 00:00:00" ], - "timeRange": [ - { - "hours": 2, - "minutes": 0, - "seconds": 0 - }, - { - "hours": 3, - "minutes": 0, - "seconds": 0 - } - ], "weekdays": [], "daysOfMonth": [] } @@ -147,16 +130,6 @@ class TestMaintenance(UptimeKumaTestCase): "2022-12-27 22:36:00", "2022-12-29 22:36:00" ], - "timeRange": [ - { - "hours": 2, - "minutes": 0 - }, - { - "hours": 3, - "minutes": 0 - } - ], "weekdays": [], "daysOfMonth": [] } @@ -246,12 +219,33 @@ class TestMaintenance(UptimeKumaTestCase): 10, 20, 30, - "lastDay4", - "lastDay2" + "lastDay1" ] } self.do_test_maintenance_strategy(expected_maintenance) + def test_maintenance_strategy_cron(self): + if parse_version(self.api.version) < parse_version("1.21.2"): + self.skipTest("Unsupported in this Uptime Kuma version") + + expected_maintenance = { + "title": "test", + "description": "test", + "strategy": MaintenanceStrategy.CRON, + "active": True, + "intervalDay": 1, + "dateRange": [ + "2022-12-27 22:37:00", + "2022-12-31 22:37:00" + ], + "weekdays": [], + "daysOfMonth": [], + "cron": "50 5 * * *", + "durationMinutes": 120, + "timezone": "Europe/Berlin" + } + self.do_test_maintenance_strategy(expected_maintenance) + def do_test_maintenance_strategy(self, expected_maintenance): # add maintenance r = self.api.add_maintenance(**expected_maintenance) diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index 9d76d0d..93f2d5c 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -184,49 +184,6 @@ def _build_docker_host_data( return data -def _build_maintenance_data( - title: str, - strategy: MaintenanceStrategy, - active: bool = True, - description: str = "", - dateRange: list = None, - intervalDay: int = 1, - weekdays: list = None, - daysOfMonth: list = None, - timeRange: list = None -) -> dict: - if not dateRange: - dateRange = [ - datetime.date.today().strftime("%Y-%m-%d 00:00:00") - ] - if not timeRange: - timeRange = [ - { - "hours": 2, - "minutes": 0, - }, { - "hours": 3, - "minutes": 0, - } - ] - if not weekdays: - weekdays = [] - if not daysOfMonth: - daysOfMonth = [] - data = { - "title": title, - "active": active, - "intervalDay": intervalDay, - "dateRange": dateRange, - "description": description, - "strategy": strategy, - "weekdays": weekdays, - "daysOfMonth": daysOfMonth, - "timeRange": timeRange - } - return data - - def _build_tag_data( name: str, color: str @@ -611,7 +568,7 @@ class UptimeKumaApi(object): interval: int = 60, retryInterval: int = 60, resendInterval: int = 0, - maxretries: int = 0, + maxretries: int = 1, upsideDown: bool = False, notificationIDList: list = None, httpBodyEncoding: str = "json", @@ -686,6 +643,10 @@ class UptimeKumaApi(object): # GAMEDIG game: str = None ) -> dict: + # https://github.com/louislam/uptime-kuma/compare/1.21.1...1.21.2#diff-f672603317047f3e6f27b0d7a44f6f244b7dbb5d0d0a85f1059a6b0bc2cb9aa0L910 + if parse_version(self.version) < parse_version("1.21.2"): + maxretries = 0 + data = { "type": type, "name": name, @@ -828,6 +789,58 @@ class UptimeKumaApi(object): return data + def _build_maintenance_data( + self, + title: str, + strategy: MaintenanceStrategy, + active: bool = True, + description: str = "", + dateRange: list = None, + intervalDay: int = 1, + weekdays: list = None, + daysOfMonth: list = None, + timeRange: list = None, + cron: str = "30 3 * * *", + durationMinutes: int = 60, + timezone: str = None + ) -> dict: + if not dateRange: + dateRange = [ + datetime.date.today().strftime("%Y-%m-%d 00:00:00") + ] + if not timeRange: + timeRange = [ + { + "hours": 2, + "minutes": 0, + }, { + "hours": 3, + "minutes": 0, + } + ] + if not weekdays: + weekdays = [] + if not daysOfMonth: + daysOfMonth = [] + data = { + "title": title, + "active": active, + "intervalDay": intervalDay, + "dateRange": dateRange, + "description": description, + "strategy": strategy, + "weekdays": weekdays, + "daysOfMonth": daysOfMonth, + "timeRange": timeRange + } + if parse_version(self.version) >= parse_version("1.21.2"): + data.update({ + "cron": cron, + "durationMinutes": durationMinutes, + "timezone": timezone, + }) + return data + # monitor def get_monitors(self) -> list: @@ -875,7 +888,7 @@ class UptimeKumaApi(object): 'keyword': None, 'maintenance': False, 'maxredirects': 10, - 'maxretries': 0, + 'maxretries': 1, 'method': 'GET', 'mqttPassword': None, 'mqttSuccessMessage': None, @@ -957,7 +970,7 @@ class UptimeKumaApi(object): 'keyword': None, 'maintenance': False, 'maxredirects': 10, - 'maxretries': 0, + 'maxretries': 1, 'method': 'GET', 'mqttPassword': None, 'mqttSuccessMessage': None, @@ -2838,29 +2851,27 @@ class UptimeKumaApi(object): ], "timeRange": [ { - "hours": 2, - "minutes": 0, - "seconds": 0 + "hours": 0, + "minutes": 0 }, { - "hours": 3, - "minutes": 0, - "seconds": 0 + "hours": 0, + "minutes": 0 } ], "weekdays": [], "daysOfMonth": [], "timeslotList": [ { - "id": 1, - "startDate": "2022-12-27 14:39:00", - "endDate": "2022-12-30 14:39:00", - "startDateServerTimezone": "2022-12-27 15:39", - "endDateServerTimezone": "2022-12-30 15:39", - "serverTimezoneOffset": "+01:00" + "startDate": "2022-12-27 22:36:00", + "endDate": "2022-12-29 22:36:00" } ], - "status": "under-maintenance" + "cron": "", + "durationMinutes": null, + "timezone": "Europe/Berlin", + "timezoneOffset": "+02:00", + "status": "ended" } ] """ @@ -2891,29 +2902,28 @@ class UptimeKumaApi(object): ], "timeRange": [ { - "hours": 2, - "minutes": 0, - "seconds": 0 + "hours": 0, + "minutes": 0 }, { - "hours": 3, - "minutes": 0, - "seconds": 0 + "hours": 0, + "minutes": 0 } ], "weekdays": [], "daysOfMonth": [], "timeslotList": [ { - "id": 1, - "startDate": "2022-12-27 14:39:00", - "endDate": "2022-12-30 14:39:00", - "startDateServerTimezone": "2022-12-27 15:39", - "endDateServerTimezone": "2022-12-30 15:39", - "serverTimezoneOffset": "+01:00" + "startDate": "2022-12-27 22:36:00", + "endDate": "2022-12-29 22:36:00" } ], - "status": "under-maintenance" + "cron": null, + "duration": null, + "durationMinutes": 0, + "timezone": "Europe/Berlin", + "timezoneOffset": "+02:00", + "status": "ended" } """ return self._call('getMaintenance', id_)["maintenance"] @@ -2938,18 +2948,6 @@ class UptimeKumaApi(object): ... dateRange=[ ... "2022-12-27 00:00:00" ... ], - ... timeRange=[ - ... { - ... "hours": 2, - ... "minutes": 0, - ... "seconds": 0 - ... }, - ... { - ... "hours": 3, - ... "minutes": 0, - ... "seconds": 0 - ... } - ... ], ... weekdays=[], ... daysOfMonth=[] ... ) @@ -2970,20 +2968,9 @@ class UptimeKumaApi(object): ... "2022-12-27 22:36:00", ... "2022-12-29 22:36:00" ... ], - ... timeRange=[ - ... { - ... "hours": 2, - ... "minutes": 0, - ... "seconds": 0 - ... }, - ... { - ... "hours": 3, - ... "minutes": 0, - ... "seconds": 0 - ... } - ... ], ... weekdays=[], - ... daysOfMonth=[] + ... daysOfMonth=[], + ... timezone="Europe/Berlin" ... ) { "msg": "Added Successfully.", @@ -3015,7 +3002,8 @@ class UptimeKumaApi(object): ... } ... ], ... weekdays=[], - ... daysOfMonth=[] + ... daysOfMonth=[], + ... timezone="Europe/Berlin" ... ) { "msg": "Added Successfully.", @@ -3052,7 +3040,8 @@ class UptimeKumaApi(object): ... 5, ... 0 ... ], - ... daysOfMonth=[] + ... daysOfMonth=[], + ... timezone="Europe/Berlin" ... ) { "msg": "Added Successfully.", @@ -3089,15 +3078,39 @@ class UptimeKumaApi(object): ... 10, ... 20, ... 30, - ... "lastDay2" - ... ] + ... "lastDay1" + ... ], + ... timezone="Europe/Berlin" + ... ) + { + "msg": "Added Successfully.", + "maintenanceID": 1 + } + + Example (strategy: :attr:`~.MaintenanceStrategy.CRON`):: + + >>> api.add_maintenance( + ... title="test", + ... description="test", + ... strategy=MaintenanceStrategy.CRON, + ... active=True, + ... intervalDay=1, + ... dateRange=[ + ... "2022-12-27 22:39:00", + ... "2022-12-31 22:39:00" + ... ], + ... weekdays=[], + ... daysOfMonth=[], + ... cron="50 5 * * *", + ... durationMinutes=120, + ... timezone="Europe/Berlin" ... ) { "msg": "Added Successfully.", "maintenanceID": 1 } """ - data = _build_maintenance_data(**kwargs) + data = self._build_maintenance_data(**kwargs) _check_arguments_maintenance(data) return self._call('addMaintenance', data) diff --git a/uptime_kuma_api/docstrings.py b/uptime_kuma_api/docstrings.py index 8d4d1bf..f320735 100644 --- a/uptime_kuma_api/docstrings.py +++ b/uptime_kuma_api/docstrings.py @@ -319,8 +319,11 @@ def maintenance_docstring(mode) -> str: :param list, optional dateRange: DateTime Range, defaults to ``[""]`` :param int, optional intervalDay: Interval (Run once every day), defaults to ``1`` :param list, optional weekdays: List that contains the days of the week on which the maintenance is enabled (Sun = ``0``, Mon = ``1``, ..., Sat = ``6``). Required for ``strategy`` :attr:`~.MaintenanceStrategy.RECURRING_WEEKDAY`., defaults to ``[]``. - :param list, optional daysOfMonth: List that contains the days of the month on which the maintenance is enabled (Day 1 = ``1``, Day 2 = ``2``, ..., Day 31 = ``31``) and the last day of the month (Last Day of Month = ``"lastDay1"``, 2nd Last Day of Month = ``"lastDay2"``, 3rd Last Day of Month = ``"lastDay3"``, 4th Last Day of Month = ``"lastDay4"``). Required for ``strategy`` :attr:`~.MaintenanceStrategy.RECURRING_DAY_OF_MONTH`., defaults to ``[]``. + :param list, optional daysOfMonth: List that contains the days of the month on which the maintenance is enabled (Day 1 = ``1``, Day 2 = ``2``, ..., Day 31 = ``31``) and the last day of the month (``"lastDay1"``). Required for ``strategy`` :attr:`~.MaintenanceStrategy.RECURRING_DAY_OF_MONTH`., defaults to ``[]``. :param list, optional timeRange: Maintenance Time Window of a Day, defaults to ``[{{"hours": 2, "minutes": 0}}, {{"hours": 3, "minutes": 0}}]``. + :param str, optional cron: Cron Schedule. Required for ``strategy`` :attr:`~.MaintenanceStrategy.CRON`., defaults to ``"30 3 * * *"`` + :param int, optional durationMinutes: Duration (Minutes). Required for ``strategy`` :attr:`~.MaintenanceStrategy.CRON`., defaults to ``60`` + :param str, optional timezone: Timezone, defaults to ``None`` (Server Timezone) """ diff --git a/uptime_kuma_api/maintenance_strategy.py b/uptime_kuma_api/maintenance_strategy.py index 45af153..fe484c4 100644 --- a/uptime_kuma_api/maintenance_strategy.py +++ b/uptime_kuma_api/maintenance_strategy.py @@ -18,3 +18,6 @@ class MaintenanceStrategy(str, Enum): RECURRING_DAY_OF_MONTH = "recurring-day-of-month" """Recurring - Day of Month""" + + CRON = "cron" + """Cron Expression"""