Merge "Support loading introspection rules from YAML files"
This commit is contained in:
commit
d0575910d8
|
@ -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
|
||||
|
|
|
@ -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': []},
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
features:
|
||||
- |
|
||||
Supports importing introspection rules from YAML files (in addition to
|
||||
already supported JSON format).
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue