|
1 | 1 | import argparse
|
2 | 2 | import sys
|
3 |
| -from typing import Any, Dict, List, Tuple, Type |
| 3 | +from enum import Enum |
| 4 | +from typing import Any, Dict, List, Optional, Tuple, Type |
4 | 5 |
|
5 | 6 | from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
|
6 | 7 | from knot_resolver_manager.datamodel.cache_schema import CacheClearRPCSchema
|
|
9 | 10 | from knot_resolver_manager.utils.requests import request
|
10 | 11 |
|
11 | 12 |
|
| 13 | +class CacheOperations(Enum): |
| 14 | + CLEAR = 0 |
| 15 | + |
| 16 | + |
12 | 17 | @register_command
|
13 |
| -class CacheClearCommand(Command): |
| 18 | +class CacheCommand(Command): |
14 | 19 | def __init__(self, namespace: argparse.Namespace) -> None:
|
15 | 20 | super().__init__(namespace)
|
| 21 | + self.operation: Optional[CacheOperations] = namespace.operation if hasattr(namespace, "operation") else None |
16 | 22 |
|
17 |
| - config_dict: Dict[str, Any] = {"exact-name": namespace.exact_name} |
18 |
| - |
| 23 | + # CLEAR operation |
| 24 | + self.clear_dict: Dict[str, Any] = {} |
| 25 | + if hasattr(namespace, "exact_name"): |
| 26 | + self.clear_dict["exact-name"] = namespace.exact_name |
19 | 27 | if hasattr(namespace, "name"):
|
20 |
| - config_dict["name"] = namespace.name |
| 28 | + self.clear_dict["name"] = namespace.name |
21 | 29 | if hasattr(namespace, "rr_type"):
|
22 |
| - config_dict["rr-type"] = namespace.rr_type |
| 30 | + self.clear_dict["rr-type"] = namespace.rr_type |
23 | 31 | if hasattr(namespace, "chunk_size"):
|
24 |
| - config_dict["chunk-size"] = namespace.chunk_size |
25 |
| - |
26 |
| - try: |
27 |
| - self.config = CacheClearRPCSchema(config_dict) |
28 |
| - except (AggregateDataValidationError, DataValidationError) as e: |
29 |
| - print(e, file=sys.stderr) |
30 |
| - sys.exit(1) |
| 32 | + self.clear_dict["chunk-size"] = namespace.chunk_size |
31 | 33 |
|
32 | 34 | @staticmethod
|
33 | 35 | def register_args_subparser(
|
34 | 36 | subparser: "argparse._SubParsersAction[argparse.ArgumentParser]",
|
35 | 37 | ) -> Tuple[argparse.ArgumentParser, "Type[Command]"]:
|
36 |
| - cache_clear = subparser.add_parser("cache-clear", help="Purge cache records matching specified criteria.") |
37 |
| - cache_clear.set_defaults(exact_name=False) |
38 |
| - cache_clear.add_argument( |
| 38 | + cache_parser = subparser.add_parser("cache", help="Performs operations on the running resolver's cache.") |
| 39 | + |
| 40 | + config_subparsers = cache_parser.add_subparsers(help="operation type") |
| 41 | + |
| 42 | + # CLEAR operation |
| 43 | + clear_subparser = config_subparsers.add_parser("clear", help="Purge cache records matching specified criteria.") |
| 44 | + clear_subparser.set_defaults(operation=CacheOperations.CLEAR, exact_name=False) |
| 45 | + clear_subparser.add_argument( |
39 | 46 | "--exact-name",
|
40 | 47 | help="If set, only records with the same name are removed.",
|
41 | 48 | action="store_true",
|
42 | 49 | dest="exact_name",
|
43 | 50 | )
|
44 |
| - cache_clear.add_argument( |
| 51 | + clear_subparser.add_argument( |
45 | 52 | "--rr-type",
|
46 | 53 | help="Optional, you may additionally specify the type to remove, but that is only supported with '--exact-name' flag set.",
|
47 | 54 | action="store",
|
48 | 55 | type=str,
|
49 | 56 | )
|
50 |
| - cache_clear.add_argument( |
| 57 | + clear_subparser.add_argument( |
51 | 58 | "--chunk-size",
|
52 | 59 | help="Optional, the number of records to remove in one round; default: 100."
|
53 | 60 | " The purpose is not to block the resolver for long. The resolver repeats the command after one millisecond until all matching data are cleared.",
|
54 | 61 | action="store",
|
55 | 62 | type=int,
|
56 | 63 | default=100,
|
57 | 64 | )
|
58 |
| - cache_clear.add_argument( |
| 65 | + clear_subparser.add_argument( |
59 | 66 | "name",
|
60 | 67 | type=str,
|
61 | 68 | nargs="?",
|
62 | 69 | help="Optional, subtree to purge; if the name isn't provided, whole cache is purged (and any other parameters are disregarded).",
|
63 | 70 | default=None,
|
64 | 71 | )
|
65 | 72 |
|
66 |
| - return cache_clear, CacheClearCommand |
| 73 | + return cache_parser, CacheCommand |
67 | 74 |
|
68 | 75 | @staticmethod
|
69 | 76 | def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords:
|
70 | 77 | return {}
|
71 | 78 |
|
72 | 79 | def run(self, args: CommandArgs) -> None:
|
73 |
| - body: str = DataFormat.JSON.dict_dump(self.config.get_unparsed_data()) |
74 |
| - response = request(args.socket, "POST", "cache-clear", body) |
| 80 | + if not self.operation: |
| 81 | + args.subparser.print_help() |
| 82 | + sys.exit() |
| 83 | + |
| 84 | + if self.operation == CacheOperations.CLEAR: |
| 85 | + try: |
| 86 | + validated = CacheClearRPCSchema(self.clear_dict) |
| 87 | + except (AggregateDataValidationError, DataValidationError) as e: |
| 88 | + print(e, file=sys.stderr) |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + body: str = DataFormat.JSON.dict_dump(validated.get_unparsed_data()) |
| 92 | + response = request(args.socket, "POST", "cache/clear", body) |
75 | 93 |
|
76 | 94 | if response.status != 200:
|
77 | 95 | print(response, file=sys.stderr)
|
|
0 commit comments