From 30fb16c924592e6c41864bf789ed38ad8e10ffd9 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Tue, 25 Sep 2018 12:44:18 +0200 Subject: [PATCH] Support loading introspection rules from YAML files Writing introspection rules can be annoying, let's support YAML to make operator's life simpler. Change-Id: Ie11797d34fef6be6ecbdd30cc3ed43e92294b55c Story: #2003870 Task: #26707 --- ironic_inspector_client/shell.py | 9 ++++--- ironic_inspector_client/test/test_shell.py | 26 +++++++++++++++++++ .../rules-import-yaml-815ebc6ca6fe28b9.yaml | 5 ++++ requirements.txt | 1 + 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/rules-import-yaml-815ebc6ca6fe28b9.yaml diff --git a/ironic_inspector_client/shell.py b/ironic_inspector_client/shell.py index 9973f05..bd208e1 100644 --- a/ironic_inspector_client/shell.py +++ b/ironic_inspector_client/shell.py @@ -20,6 +20,7 @@ import sys from osc_lib.command import command from osc_lib import utils +import yaml import ironic_inspector_client from ironic_inspector_client import resource as res @@ -176,19 +177,19 @@ class AbortCommand(command.Command): class RuleImportCommand(command.Lister): - """Import one or several introspection rules from a json file.""" + """Import one or several introspection rules from a JSON/YAML file.""" COLUMNS = ("UUID", "Description") def get_parser(self, prog_name): parser = super(RuleImportCommand, self).get_parser(prog_name) - parser.add_argument('file', help='JSON file to import, may contain ' - 'one or several rules') + parser.add_argument('file', help='JSON or YAML file to import, may ' + 'contain one or several rules') return parser def take_action(self, parsed_args): with open(parsed_args.file, 'r') as fp: - rules = json.load(fp) + rules = yaml.safe_load(fp) if not isinstance(rules, list): rules = [rules] client = self.app.client_manager.baremetal_introspection diff --git a/ironic_inspector_client/test/test_shell.py b/ironic_inspector_client/test/test_shell.py index 07cea76..e31cf06 100644 --- a/ironic_inspector_client/test/test_shell.py +++ b/ironic_inspector_client/test/test_shell.py @@ -230,6 +230,32 @@ class TestRules(BaseTest): self.rules_api.from_json.assert_any_call({'foo': 'bar'}) self.rules_api.from_json.assert_any_call({'answer': 42}) + def test_import_yaml(self): + f = tempfile.NamedTemporaryFile() + self.addCleanup(f.close) + f.write(b"""--- +- foo: bar +- answer: 42 +""") + f.flush() + + arglist = [f.name] + verifylist = [('file', f.name)] + + self.rules_api.from_json.side_effect = iter([ + {'uuid': '1', 'description': 'd1', 'links': []}, + {'uuid': '2', 'description': 'd2', 'links': []} + ]) + + cmd = shell.RuleImportCommand(self.app, None) + parsed_args = self.check_parser(cmd, arglist, verifylist) + cols, values = cmd.take_action(parsed_args) + + self.assertEqual(('UUID', 'Description'), cols) + self.assertEqual([('1', 'd1'), ('2', 'd2')], values) + self.rules_api.from_json.assert_any_call({'foo': 'bar'}) + self.rules_api.from_json.assert_any_call({'answer': 42}) + def test_list(self): self.rules_api.get_all.return_value = [ {'uuid': '1', 'description': 'd1', 'links': []}, diff --git a/releasenotes/notes/rules-import-yaml-815ebc6ca6fe28b9.yaml b/releasenotes/notes/rules-import-yaml-815ebc6ca6fe28b9.yaml new file mode 100644 index 0000000..500c155 --- /dev/null +++ b/releasenotes/notes/rules-import-yaml-815ebc6ca6fe28b9.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Supports importing introspection rules from YAML files (in addition to + already supported JSON format). diff --git a/requirements.txt b/requirements.txt index 84d8ebe..a9d78a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +6,6 @@ osc-lib>=1.8.0 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0 oslo.utils>=3.33.0 # Apache-2.0 pbr!=2.1.0,>=2.0.0 # Apache-2.0 +PyYAML>=3.12 # MIT requests>=2.14.2 # Apache-2.0 six>=1.10.0 # MIT