From 4ed6f2f3345bdf2fab2dd74017e95727103fbbc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Mon, 29 Jun 2015 13:29:51 +0000 Subject: [PATCH] Move validation to seperate function and cli command. Add support for ResourceProviders --- solar/solar/cli.py | 6 +++++ solar/solar/core/virtual_resource.py | 38 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/solar/solar/cli.py b/solar/solar/cli.py index d47c3d42..9a82cd17 100644 --- a/solar/solar/cli.py +++ b/solar/solar/cli.py @@ -327,6 +327,12 @@ def init_cli_resource(): r = all[name] r.update(args) + @resource.command() + def validate(): + errors = vr.validate_resources() + for r, error in errors: + print 'ERROR: %s: %s' % (r.name, error) + @resource.command() @click.argument('path') def get_inputs(path): diff --git a/solar/solar/core/virtual_resource.py b/solar/solar/core/virtual_resource.py index ad17ae7b..f642c603 100644 --- a/solar/solar/core/virtual_resource.py +++ b/solar/solar/core/virtual_resource.py @@ -9,6 +9,7 @@ from jinja2 import Template, Environment, meta from solar import utils from solar.core import validation from solar.core import resource as resource_module +from solar.core import resource_provider from solar.core import signals @@ -60,31 +61,38 @@ def create_virtual_resource(vr_name, template): reciver = db[reciver] signals.connect(emitter, reciver, mapping) - for r in db.values(): - if not isinstance(r, resource_module.Resource): - continue - - print 'Validating {}'.format(r.name) - errors = validation.validate_resource(r) - if errors: - print 'ERROR: %s: %s' % (r.name, errors) - #import sys;sys.exit() return created_resources -def create(name, path, kwargs, virtual_resource=None): - if not os.path.exists(path): - raise Exception('Base resource does not exist: {0}'.format(path)) +def create(name, base_path, kwargs, virtual_resource=None): + if isinstance(base_path, resource_provider.BaseProvider): + base_path = base_path.directory + if not os.path.exists(base_path): + raise Exception( + 'Base resource does not exist: {0}'.format(base_path) + ) - if is_virtual(path): - template = _compile_file(name, path, kwargs) + if is_virtual(base_path): + template = _compile_file(name, base_path, kwargs) yaml_template = yaml.load(StringIO(template)) resources = create_virtual_resource(name, yaml_template) else: - resource = create_resource(name, path, kwargs, virtual_resource) + resource = create_resource(name, base_path, kwargs, virtual_resource) resources = [resource] return resources +def validate_resources(): + db = resource_module.load_all() + all_errors = [] + for r in db.values(): + if not isinstance(r, resource_module.Resource): + continue + + errors = validation.validate_resource(r) + if errors: + all_errors.append((r, errors)) + return all_errors + def _compile_file(name, path, kwargs): with open(path) as f: content = f.read()