mreg-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. mreg_cli/__about__.py +7 -0
  2. mreg_cli/__init__.py +5 -0
  3. mreg_cli/__main__.py +8 -0
  4. mreg_cli/_version.py +16 -0
  5. mreg_cli/api/__init__.py +7 -0
  6. mreg_cli/api/abstracts.py +486 -0
  7. mreg_cli/api/endpoints.py +130 -0
  8. mreg_cli/api/fields.py +104 -0
  9. mreg_cli/api/history.py +152 -0
  10. mreg_cli/api/models.py +3416 -0
  11. mreg_cli/cli.py +382 -0
  12. mreg_cli/commands/__init__.py +1 -0
  13. mreg_cli/commands/base.py +62 -0
  14. mreg_cli/commands/dhcp.py +136 -0
  15. mreg_cli/commands/group.py +326 -0
  16. mreg_cli/commands/help.py +77 -0
  17. mreg_cli/commands/host.py +54 -0
  18. mreg_cli/commands/host_submodules/__init__.py +28 -0
  19. mreg_cli/commands/host_submodules/a_aaaa.py +452 -0
  20. mreg_cli/commands/host_submodules/bacnet.py +126 -0
  21. mreg_cli/commands/host_submodules/cname.py +166 -0
  22. mreg_cli/commands/host_submodules/core.py +507 -0
  23. mreg_cli/commands/host_submodules/rr.py +973 -0
  24. mreg_cli/commands/label.py +146 -0
  25. mreg_cli/commands/logging.py +112 -0
  26. mreg_cli/commands/network.py +516 -0
  27. mreg_cli/commands/permission.py +202 -0
  28. mreg_cli/commands/policy.py +519 -0
  29. mreg_cli/commands/recording.py +59 -0
  30. mreg_cli/commands/registry.py +56 -0
  31. mreg_cli/commands/root.py +58 -0
  32. mreg_cli/commands/zone.py +288 -0
  33. mreg_cli/config.py +253 -0
  34. mreg_cli/errorbuilder.py +193 -0
  35. mreg_cli/exceptions.py +237 -0
  36. mreg_cli/help_formatter.py +38 -0
  37. mreg_cli/main.py +238 -0
  38. mreg_cli/outputmanager.py +466 -0
  39. mreg_cli/py.typed +0 -0
  40. mreg_cli/tags.txt +55 -0
  41. mreg_cli/tokenfile.py +89 -0
  42. mreg_cli/types.py +160 -0
  43. mreg_cli/utilities/__init__.py +5 -0
  44. mreg_cli/utilities/api.py +595 -0
  45. mreg_cli/utilities/shared.py +65 -0
  46. mreg_cli/utilities/validators.py +19 -0
  47. mreg_cli-1.0.0.dist-info/AUTHORS +12 -0
  48. mreg_cli-1.0.0.dist-info/LICENSE +674 -0
  49. mreg_cli-1.0.0.dist-info/METADATA +1079 -0
  50. mreg_cli-1.0.0.dist-info/RECORD +53 -0
  51. mreg_cli-1.0.0.dist-info/WHEEL +5 -0
  52. mreg_cli-1.0.0.dist-info/entry_points.txt +2 -0
  53. mreg_cli-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,146 @@
1
+ """Label-related commands for mreg_cli."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ from typing import Any
7
+
8
+ from mreg_cli.api.models import Label
9
+ from mreg_cli.commands.base import BaseCommand
10
+ from mreg_cli.commands.registry import CommandRegistry
11
+ from mreg_cli.exceptions import EntityNotFound, InputFailure
12
+ from mreg_cli.outputmanager import OutputManager
13
+ from mreg_cli.types import Flag
14
+
15
+ command_registry = CommandRegistry()
16
+
17
+
18
+ class LabelCommands(BaseCommand):
19
+ """Label commands for the CLI."""
20
+
21
+ def __init__(self, cli: Any) -> None:
22
+ """Initialize the label commands."""
23
+ super().__init__(cli, command_registry, "label", "Manage host labels.", "Manage labels")
24
+
25
+
26
+ @command_registry.register_command(
27
+ prog="add",
28
+ description="Add a label",
29
+ short_desc="Add a label",
30
+ flags=[
31
+ Flag("name", short_desc="Label name", description="The name of the new label"),
32
+ Flag("description", description="The purpose of the label"),
33
+ ],
34
+ )
35
+ def label_add(args: argparse.Namespace) -> None:
36
+ """Add a label.
37
+
38
+ :param args: argparse.Namespace (name, description)
39
+ """
40
+ if " " in args.name:
41
+ raise InputFailure("The label name can't contain spaces.")
42
+
43
+ # We can't do a fetch_after_create here because the API is... broken.
44
+ # https://github.com/unioslo/mreg/blob/eed5c154bcc47b1dea474feabad46125ebde0aec/mreg/api/v1/views_labels.py#L30
45
+ # https://github.com/unioslo/mreg/blob/eed5c154bcc47b1dea474feabad46125ebde0aec/mreg/api/v1/views.py#L187
46
+ Label.create({"name": args.name, "description": args.description}, fetch_after_create=False)
47
+ OutputManager().add_ok(f'Added label "{args.name}"')
48
+
49
+
50
+ @command_registry.register_command(
51
+ prog="list", description="List labels", short_desc="List labels", flags=[]
52
+ )
53
+ def label_list(_: argparse.Namespace) -> None:
54
+ """List labels."""
55
+ labels = Label.get_all()
56
+ if not labels:
57
+ OutputManager().add_line("No labels")
58
+ return
59
+ OutputManager().add_formatted_table(("Name", "Description"), ("name", "description"), labels)
60
+
61
+
62
+ @command_registry.register_command(
63
+ prog="remove",
64
+ description="Remove a label",
65
+ short_desc="Remove a label",
66
+ flags=[
67
+ Flag(
68
+ "name",
69
+ short_desc="Label name",
70
+ description="The name of the label to remove",
71
+ )
72
+ ],
73
+ )
74
+ def label_delete(args: argparse.Namespace) -> None:
75
+ """Delete a label.
76
+
77
+ :param args: argparse.Namespace (name)
78
+ """
79
+ label = Label.get_by_name_or_raise(args.name)
80
+ if not label:
81
+ raise EntityNotFound(f'Label "{args.name}" does not exist.')
82
+
83
+ label.delete()
84
+ OutputManager().add_ok(f'Removed label "{args.name}"')
85
+
86
+
87
+ @command_registry.register_command(
88
+ prog="info",
89
+ description="Show details about a label",
90
+ short_desc="Label details",
91
+ flags=[Flag("name", short_desc="Label name", description="The name of the label")],
92
+ )
93
+ def label_info(args: argparse.Namespace) -> None:
94
+ """Show details about a label.
95
+
96
+ :param args: argparse.Namespace (name)
97
+ """
98
+ Label.get_by_name_or_raise(args.name).output()
99
+
100
+
101
+ @command_registry.register_command(
102
+ prog="rename",
103
+ description="Rename a label and/or change the description",
104
+ short_desc="Rename a label",
105
+ flags=[
106
+ Flag(
107
+ "oldname",
108
+ short_desc="Old name",
109
+ description="The old (current) name of the label",
110
+ ),
111
+ Flag("newname", short_desc="New name", description="The new name of the label"),
112
+ ],
113
+ )
114
+ def label_rename(args: argparse.Namespace) -> None:
115
+ """Rename a label.
116
+
117
+ :param args: argparse.Namespace (oldname, newname)
118
+ """
119
+ Label.get_by_name_or_raise(args.oldname).rename(args.newname)
120
+ OutputManager().add_ok(f'Renamed label "{args.oldname}" to "{args.newname}"')
121
+
122
+
123
+ @command_registry.register_command(
124
+ prog="set_description",
125
+ description="Set the description for the label",
126
+ short_desc="Describe a label",
127
+ flags=[
128
+ Flag(
129
+ "name",
130
+ short_desc="name",
131
+ description="The name of the label",
132
+ ),
133
+ Flag(
134
+ "desc",
135
+ short_desc="New description",
136
+ description="The new description of the label",
137
+ ),
138
+ ],
139
+ )
140
+ def label_redesc(args: argparse.Namespace) -> None:
141
+ """Change the description of a label.
142
+
143
+ :param args: argparse.Namespace (name, desc)
144
+ """
145
+ Label.get_by_name_or_raise(args.name).set_description(args.desc)
146
+ OutputManager().add_ok(f'Set description for label "{args.name}" to "{args.desc}"')
@@ -0,0 +1,112 @@
1
+ """Logging commands for the CLI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import os
7
+ from typing import Any
8
+
9
+ from mreg_cli.commands.base import BaseCommand
10
+ from mreg_cli.commands.registry import CommandRegistry
11
+ from mreg_cli.config import MregCliConfig
12
+ from mreg_cli.exceptions import InputFailure
13
+ from mreg_cli.outputmanager import OutputManager
14
+ from mreg_cli.types import Flag, LogLevel
15
+ from mreg_cli.utilities.shared import sizeof_fmt
16
+
17
+ command_registry = CommandRegistry()
18
+
19
+
20
+ class LoggingCommmands(BaseCommand):
21
+ """Logging commands for the CLI."""
22
+
23
+ def __init__(self, cli: Any) -> None:
24
+ """Initialize the logging commands."""
25
+ super().__init__(
26
+ cli,
27
+ command_registry,
28
+ "logging",
29
+ "Logging commands for the CLI.",
30
+ "Manage logging.",
31
+ )
32
+
33
+
34
+ @command_registry.register_command(
35
+ prog="start",
36
+ description="Start logging commands to a given file.",
37
+ short_desc="Start logging",
38
+ flags=[
39
+ Flag(
40
+ "filename",
41
+ description="The filename to log to.",
42
+ short_desc="Filename",
43
+ metavar="filename",
44
+ ),
45
+ Flag(
46
+ "level",
47
+ description="The logging level to use.",
48
+ short_desc="Level",
49
+ metavar="level",
50
+ choices=LogLevel.choices(),
51
+ default="INFO",
52
+ ),
53
+ ],
54
+ )
55
+ def start_logging(args: argparse.Namespace) -> None:
56
+ """Start logging to the given file."""
57
+ if not args.filename:
58
+ raise InputFailure("No filename given.")
59
+
60
+ config = MregCliConfig()
61
+ config.stop_logging()
62
+ config.start_logging(args.filename, args.level)
63
+
64
+
65
+ @command_registry.register_command(
66
+ prog="stop",
67
+ description="Stop logging.",
68
+ short_desc="Stop logging",
69
+ )
70
+ def stop_logging(_: argparse.Namespace):
71
+ """Stop logging."""
72
+ MregCliConfig().stop_logging()
73
+
74
+
75
+ @command_registry.register_command(
76
+ prog="level",
77
+ description="Set the logging level.",
78
+ short_desc="Set logging level",
79
+ flags=[
80
+ Flag(
81
+ "level",
82
+ description="The logging level to use.",
83
+ short_desc="Level",
84
+ metavar="level",
85
+ choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
86
+ ),
87
+ ],
88
+ )
89
+ def set_logging_level(args: argparse.Namespace) -> None:
90
+ """Set the logging level."""
91
+ config = MregCliConfig()
92
+ config.stop_logging()
93
+ config.start_logging(config.get_default_logfile(), args.level)
94
+
95
+
96
+ @command_registry.register_command(
97
+ prog="status",
98
+ description="Print the logging status.",
99
+ short_desc="Logging status",
100
+ )
101
+ def logging_status(_: argparse.Namespace):
102
+ """Print the logging status."""
103
+ level = MregCliConfig().get_logging_level()
104
+ file = MregCliConfig().get_default_logfile()
105
+
106
+ lines_in_logfile = 0
107
+ with open(file, "r") as f:
108
+ lines_in_logfile = sum(1 for _ in f)
109
+
110
+ filesize = sizeof_fmt(os.path.getsize(file))
111
+
112
+ OutputManager().add_line(f"{level} > {file} ({lines_in_logfile} lines, {filesize})")