Opportunity was added validate a versioned schemas.
* It is needed for data model validation of
openstack services which should be versioned.
* Some mistakes in commentaries were fixed.
Change-Id: I4bd765255f8b327b6aa7167102fa7eceb1c8e02b
Related-PROD: PROD-26805
diff --git a/_modules/modelschema.py b/_modules/modelschema.py
index 5e319ae..451f3c1 100644
--- a/_modules/modelschema.py
+++ b/_modules/modelschema.py
@@ -65,40 +65,54 @@
.. code-block:: bash
- salt-call modelutils.schema_list
-
-
+ salt-call modelschema.schema_list
"""
+
output = {}
- schemas = glob.glob('{}/*/schemas/*.yaml'.format(_get_base_dir()))
- for schema in schemas:
- if os.path.exists(schema):
- role_name = schema.split('/')[-1].replace('.yaml', '')
- service_name = schema.split('/')[-3]
- print role_name, service_name
- name = '{}-{}'.format(service_name, role_name)
- output[name] = {
- 'service': service_name,
- 'role': role_name,
- 'path': schema,
- 'valid': schema_validate(service_name, role_name)[name]
- }
+
+ def _parse_schema(pattern, helper, output):
+ schemas = glob.glob(pattern.format(_get_base_dir()))
+ for schema in schemas:
+ if os.path.exists(schema):
+ sc_splited = schema.split('/')
+ parsed = helper(sc_splited)
+ name = '{}-{}'.format(*parsed[:2])
+ output[name] = {
+ 'service': parsed[0],
+ 'role': parsed[1],
+ 'path': schema,
+ 'valid': schema_validate(*parsed)[name]
+ }
+
+ versioned_schemas_path = '{}/*/schemas/*/*.yaml'
+ _parse_schema(versioned_schemas_path, lambda sc: (sc[-4], sc[-1].replace('.yaml', ''), sc[-2]), output)
+
+ common_schemas_path = '{}/*/schemas/*.yaml'
+ _parse_schema(common_schemas_path, lambda sc: (sc[-3], sc[-1].replace('.yaml', '')), output)
+
return output
-def schema_get(service, role):
+def schema_get(service, role, version=None):
"""
- Returns pillar schema for given service and role. If no service and role
- is specified, method will return all known schemas.
+ Returns pillar schema for given service and role.
CLI Examples:
.. code-block:: bash
- salt-call modelutils.schema_get ntp server
+ salt-call modelschema.schema_get ntp server
+ .. or ..
+
+ salt-call modelschema.schema_get keystone server pike
"""
- schema_path = 'salt://{}/schemas/{}.yaml'.format(service, role)
+
+ if version:
+ schema_path = 'salt://{}/schemas/{}/{}.yaml'.format(service, version, role)
+ else:
+ schema_path = 'salt://{}/schemas/{}.yaml'.format(service, role)
+
schema = __salt__['cp.get_file_str'](schema_path)
if schema:
try:
@@ -111,7 +125,7 @@
return {'{}-{}'.format(service, role): data}
-def schema_validate(service, role):
+def schema_validate(service, role, version=None):
"""
Validates pillar schema itself of given service and role.
@@ -119,11 +133,14 @@
.. code-block:: bash
- salt-call modelutils.schema_validate ntp server
+ salt-call modelschema.schema_validate ntp server
+ .. or ..
+
+ salt-call modelschema.schema_validate keystone server pike
"""
- schema = schema_get(service, role)['{}-{}'.format(service, role)]
+ schema = schema_get(service, role, version)['{}-{}'.format(service, role)]
cls = _validator_for(schema)
LOG.debug("Validating schema..")
try:
@@ -136,18 +153,21 @@
return {'{}-{}'.format(service, role): data}
-def model_validate(service=None, role=None):
+def model_validate(service, role, version=None):
"""
- Validates pillar metadata by schema for given service and role. If
- no service and role is specified, method will validate all defined
- services.
+ Validates pillar metadata by schema for given service and role.
CLI Example:
- .. code-block:: bash
- salt-run modelschema.model_validate keystone server
+ .. code-block:: bash
+
+ salt-call modelschema.model_validate ntp server
+
+ .. or ..
+
+ salt-call modelschema.model_validate keystone server pike
"""
- schema = schema_get(service, role)['{}-{}'.format(service, role)]
+ schema = schema_get(service, role, version)['{}-{}'.format(service, role)]
model = __salt__['pillar.get']('{}:{}'.format(service, role))
try:
_validate(model, schema)
@@ -195,7 +215,7 @@
return data
-def schema_from_tests(service):
+def schema_from_tests(service, version=None):
"""
Generate pillar schema skeleton for given service. Method iterates throught
test pillars and generates schema scaffold structure in JSON format that
@@ -206,10 +226,18 @@
.. code-block:: bash
- salt-call modelutils.schema_from_tests keystone
+ salt-call modelschema.schema_from_tests keystone pike
+
+ .. or ..
+
+ salt-call modelschema.schema_from_tests ntp
"""
- pillars = glob.glob(
- '{}/{}/tests/pillar/*.sls'.format(_get_base_dir(), service))
+ if version:
+ pillars = glob.glob(
+ '{}/{}/tests/pillar/{}/*.sls'.format(_get_base_dir(), service, version))
+ else:
+ pillars = glob.glob(
+ '{}/{}/tests/pillar/*.sls'.format(_get_base_dir(), service))
raw_data = {}
for pillar in pillars:
if os.path.exists(pillar):