Skip to content

Commit

Permalink
error-report delete: allow 5d or 3d10h strings to be used for --to an…
Browse files Browse the repository at this point in the history
…d --since
  • Loading branch information
rp- committed Sep 25, 2024
1 parent 1cbe896 commit 0a03ca6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Added info text for SkipDisk scenarios
- error-report delete: allow 5d or 3d10h strings to be used for --to and --since

### Fixed
- parse_time_str/since argument: better wrong input handling
Expand Down
4 changes: 2 additions & 2 deletions linstor_client/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def parse_time_str(timestr):
:return: datetime of the timestr
:rtype: datetime
"""
m = re.match(r'(\d+\W*d)?(\d+\W*h?)?', timestr)
m = re.match(r'^(\d+\W*d)?(\d+\W*h?)?$', timestr)
try:
if m and any(m.groups()):
since_dt = datetime.now()
Expand All @@ -751,7 +751,7 @@ def parse_time_str(timestr):
raise ValueError()
except ValueError:
raise LinstorClientError(
"Unable to parse since time string: '{s_str}'. Format should be e.g.: '1d10h' or '3h'".format(
"Unable to parse time string: '{s_str}'. Format should be e.g.: '1d10h' or '3h'".format(
s_str=timestr),
ExitCode.ARGPARSE_ERROR
)
Expand Down
28 changes: 21 additions & 7 deletions linstor_client/commands/error_report_cmds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import linstor_client.argparse.argparse as argparse

from linstor_client.table import Table, TableHeader
from linstor_client.utils import Output
from linstor_client.utils import Output, LinstorClientError
from linstor_client.commands import Commands

from datetime import datetime
Expand Down Expand Up @@ -67,9 +67,11 @@ def setup_commands(self, parser):
)
c_del_err_report.add_argument("--nodes", nargs="+", help="Only delete error reports from the given nodes")
c_del_err_report.add_argument(
"--since", help="Datetime since when to delete error reports. Date format: 2020-08-30 13:40:00")
"--since",
help="Datetime since when to delete error reports. Date format: '2020-08-30 13:40:00' or '5d6h'")
c_del_err_report.add_argument(
"--to", type=str, help="Datetime until to delete error reports. Date format: 2020-08-30 13:40:00")
"--to",
help="Datetime until to delete error reports. Date format: '2020-08-30 13:40:00' or '5d6h'")
c_del_err_report.add_argument("--exception", help="Only delete error reports matching the exception")
c_del_err_report.add_argument("id", nargs="*", help="Delete error reports matching the given ids")
c_del_err_report.set_defaults(func=self.cmd_del_error_report)
Expand Down Expand Up @@ -148,12 +150,24 @@ def cmd_del_error_report(self, args):
def_dt_str = '0000-00-00 23:59:59'

if args.since:
since_str = self.fill_str_part(args.since, def_dt_str)
since_dt = datetime.strptime(since_str, dt_format)
try:
since_dt = self.parse_time_str(args.since)
except LinstorClientError:
since_str = self.fill_str_part(args.since, def_dt_str)
try:
since_dt = datetime.strptime(since_str, dt_format)
except ValueError as ve:
raise LinstorClientError("Unable to parse 'since' date: " + str(ve), exit_code=2)

if args.to:
to_str = self.fill_str_part(args.to, def_dt_str)
to_dt = datetime.strptime(to_str, dt_format)
try:
to_dt = self.parse_time_str(args.to)
except LinstorClientError:
to_str = self.fill_str_part(args.to, def_dt_str)
try:
to_dt = datetime.strptime(to_str, dt_format)
except ValueError as ve:
raise LinstorClientError("Unable to parse 'to' date: " + str(ve), exit_code=2)

replies = self.get_linstorapi().error_report_delete(
args.nodes,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_client_commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import unittest
from datetime import datetime, timedelta

import linstor_client_main
from linstor_client.commands import Commands
from linstor_client.utils import LinstorClientError


class TestClientCommands(unittest.TestCase):
def test_main_commands(self):
cli = linstor_client_main.LinStorCLI()
cli.check_parser_commands()

def _assert_parse_time_str(self, timestr, delta):
dt_now = datetime.now()
dt_now = dt_now.replace(microsecond=0)

dt = Commands.parse_time_str(timestr)
dt = dt.replace(microsecond=0)
dt_diff = dt_now - delta
self.assertEqual(dt_diff, dt)

def test_parse_time_str(self):
self._assert_parse_time_str("5d", timedelta(days=5))
self._assert_parse_time_str("3", timedelta(hours=3))
self._assert_parse_time_str("3h", timedelta(hours=3))

self.assertRaises(LinstorClientError, Commands.parse_time_str, "10m")
self.assertRaises(LinstorClientError, Commands.parse_time_str, "")


if __name__ == '__main__':
unittest.main()

0 comments on commit 0a03ca6

Please sign in to comment.