validate static configuration

Check for validity of configuration with static yaml schema

Change-Id: I2241076d11d25730d08decee4a5fc049133984fc
This commit is contained in:
hewei 2019-02-19 09:56:16 +08:00
parent a1d829009d
commit ea33464513
4 changed files with 83 additions and 7 deletions

View File

@ -145,3 +145,4 @@ python-zaqarclient==1.2.0
tooz==1.58.0
zake==0.1.6
psutil==5.4.3
jsonschema==2.6.0

View File

@ -54,3 +54,4 @@ pytz>=2013.6 # MIT
tenacity>=4.9.0
tooz>=1.58.0 # Apache-2.0
psutil>=5.4.3 # BSD
jsonschema>=2.6.0 # MIT

View File

@ -45,6 +45,75 @@ OPTS = [
help='static data sources configuration directory')]
STATIC_SCHEMA = {
"type": "object",
"required": ["definitions"],
"properties": {
"metadata": {
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
},
"description": {
"type": "string",
},
}
},
"definitions": {
"type": "object",
"required": ["entities", "relationships"],
"properties": {
"entities": {
"type": "array",
"items": {
"type": "object",
"required": ["static_id"],
"properties": {
"static_id": {
"type": "string",
},
"type": {
"type": "string",
},
"name": {
"type": "string",
},
"id": {
"type": "string",
},
"state": {
"type": "string",
},
},
},
},
"relationships": {
"type": "array",
"items": {
"type": "object",
"required": ["source", "target",
"relationship_type"],
"properties": {
"source": {
"type": "string",
},
"target": {
"type": "string",
},
"relationship_type": {
"type": "string",
},
},
},
},
}
},
},
}
class StaticFields(object):
"""yaml fields for static definitions"""
METADATA = 'metadata'

View File

@ -13,6 +13,7 @@
# under the License.
from itertools import chain
from jsonschema import validate
from six.moves import reduce
from oslo_log import log
@ -21,6 +22,7 @@ from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import GraphAction
from vitrage.datasources.driver_base import DriverBase
from vitrage.datasources.static import STATIC_DATASOURCE
from vitrage.datasources.static import STATIC_SCHEMA
from vitrage.datasources.static import StaticFields
from vitrage.utils import file as file_utils
@ -39,10 +41,15 @@ class StaticDriver(DriverBase):
self.entities_cache = []
@staticmethod
def _is_valid_config(config):
def _is_valid_config(config, path):
"""check for validity of configuration"""
# TODO(yujunz) check with yaml schema or reuse template validation
return StaticFields.DEFINITIONS in config
try:
validate(config, STATIC_SCHEMA)
return True
except Exception:
msg = "Invalid config file: %s" % path
LOG.exception(msg)
return False
@staticmethod
def get_event_types():
@ -103,9 +110,7 @@ class StaticDriver(DriverBase):
def _get_entities_from_file(cls, path):
config = file_utils.load_yaml_file(path)
if not cls._is_valid_config(config):
LOG.warning("Skipped invalid config (possible obsoleted): {}"
.format(path))
if not cls._is_valid_config(config, path):
return []
definitions = config[StaticFields.DEFINITIONS]