Added support modules for schema operations
* Added support for backup
EME-13
Change-Id: I11cabb77bb66f3e9f1e35c9a00c9c196ded0ceb3
diff --git a/_runners/modelschema.py b/_runners/modelschema.py
new file mode 100644
index 0000000..1a82179
--- /dev/null
+++ b/_runners/modelschema.py
@@ -0,0 +1,72 @@
+
+# Import python libs
+import logging
+import glob
+import os
+import yaml
+
+# Import salt modules
+import salt.client
+
+# Import third party libs
+from jsonschema import validate
+from jsonschema.validators import validator_for
+from jsonschema.exceptions import SchemaError
+
+
+__virtualname__ = 'modelschema'
+
+LOG = logging.getLogger(__name__)
+
+BASE_DIR = '/usr/share/salt-formulas/env/_formulas'
+
+
+def _get_schemas():
+ '''
+ Method will return all known schemas.
+ '''
+ output = {}
+ schemas = glob.glob('{}/*/schemas/*.yaml'.format(BASE_DIR))
+ for schema in schemas:
+ if os.path.exists(schema):
+ filename = schema.split('/')[-1].replace('.yaml', '')
+ service_name, role_name = filename.split('-')
+ if service_name not in output:
+ output[service_name] = {}
+ with open(schema, 'r') as stream:
+ try:
+ data = yaml.load(stream)
+ except yaml.YAMLError as exc:
+ data = None
+ LOG.error(exc)
+ output[service_name][role_name] = data
+ return output
+
+
+def validate_node_model(target, service, role):
+ '''
+ Validates pillar by schema for given given minion, service and role.
+ If no service and role is specified, method will validate all
+ defined services on minion.
+
+ CLI Example:
+ .. code-block:: bash
+ salt-run modelschema.validate_node_model
+ '''
+ client = salt.client.LocalClient(__opts__['conf_file'])
+ schema = _get_schemas()[service][role]
+ result = {}
+
+ validator = validator_for(schema)
+ try:
+ validator.check_schema(schema)
+ except SchemaError as exception:
+ LOG.error(exception)
+ return result
+
+ minions = client.cmd(target, 'pillar.data', timeout=1)
+ for minion, pillar in minions.items():
+ model = pillar[service][role]
+ validation_result = validator(schema).validate(model)
+ result[minion] = validation_result
+ return result