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
This commit is contained in:
Dmitry Tantsur 2018-09-25 12:44:18 +02:00
parent 87251a3499
commit 30fb16c924
4 changed files with 37 additions and 4 deletions

View File

@ -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

View File

@ -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': []},

View File

@ -0,0 +1,5 @@
---
features:
- |
Supports importing introspection rules from YAML files (in addition to
already supported JSON format).

View File

@ -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