Add data validation tool

Simple command line tool to check data intended to bareon. It uses data
validation API provided by data-drivers in same manner as data-drivers
do during "regular" call of bareon.

Change-Id: I2a8950178839c13d8ebcc72cacba203b4bfe37c5
This commit is contained in:
Dmitry Bogun 2016-12-21 13:29:12 +02:00 committed by Andrii Ostapenko
parent 0ad1deb43b
commit a00d1239bc
4 changed files with 91 additions and 2 deletions

83
bareon/cmd/validator.py Normal file
View File

@ -0,0 +1,83 @@
# Copyright 2016 Mirantis, Inc.
# Copyright 2016 Cray Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import sys
from oslo.config import cfg
from bareon import errors
from bareon.utils import utils
from bareon import version
cli_opts = [
cfg.StrOpt(
'data_driver', default='ironic',
help='Data driver'
),
cfg.StrOpt(
'input_data_file', positional=True, default='-',
help='Path to deployment config'
)
]
CONF = cfg.ConfigOpts()
def worker():
CONF.register_cli_opts(cli_opts)
CONF(sys.argv[1:], project='fuel-agent',
version=version.version_info.release_string())
stream = None
try:
if CONF.input_data_file == '-':
stream = sys.stdin
else:
stream = open(CONF.input_data_file, 'rt')
data = json.load(stream)
stream.close()
except IOError as e:
raise OperationFailed('Unable to read input data {!r}: {} - {}'.format(
stream, e.filename, e))
except (TypeError, ValueError) as e:
raise OperationFailed('Unable to decode input data: {}'.format(e))
cls = utils.get_data_driver(CONF.data_driver)
try:
cls.validate_data(data)
except errors.WrongInputDataError as e:
message = [
'Validation failure\n\n',
e.message]
raise OperationFailed(''.join(message))
def main():
try:
worker()
except OperationFailed as e:
sys.stderr.write('{}'.format(e))
sys.stderr.write('\n')
return 1
class OperationFailed(Exception):
pass
if __name__ == '__main__':
sys.exit(main())

View File

@ -20,6 +20,7 @@ import pkg_resources
import six
import bareon.drivers.data
import bareon.errors
@six.add_metaclass(abc.ABCMeta)
@ -30,6 +31,10 @@ class BaseDataDriver(object):
methods for getting object schemes, etc.
"""
# Can be used by clients outside of bareon package. Client who loads
# data-driver via stevedore
exc = bareon.errors
data_validation_schema = None
def __init__(self, data):

View File

@ -31,10 +31,10 @@ class InputDataSchemaValidationError(WrongInputDataError):
def __init__(self, defects):
human_readable_defects = []
for idx, d in enumerate(defects):
path = [''] + list(d.path)
path = list(d.path)
path = '/'.join((str(x) for x in path))
human_readable_defects.append(
'#{} ({}): {}'.format(idx, path, d.message))
'{:>2} (/{}): {}'.format('#{}'.format(idx), path, d.message))
indent = ' ' * 4
separator = '\n{}'.format(indent)

View File

@ -25,6 +25,7 @@ console_scripts =
bareon-build-image = bareon.cmd.agent:build_image
bareon-ironic-callback = bareon.cmd.ironic_callback:main
bareon-mkbootstrap = bareon.cmd.agent:mkbootstrap
bareon-data-validator = bareon.cmd.validator:main
bareon.drivers.data =
nailgun = bareon.drivers.data.nailgun:Nailgun