def check_device(data, options, prefix=""): try: if options.render: data = options.env.from_string(data).render() data = yaml.safe_load(data) except jinja2.TemplateNotFound as exc: print("%sinvalide device template:" % prefix) print("%smissing template: %s" % (prefix, exc)) return 1 except jinja2.TemplateSyntaxError as exc: print("%sinvalide device template:" % prefix) print("%serror: %s" % (prefix, exc)) print("%sline: %d" % (prefix, exc.lineno)) return 1 except jinja2.TemplateError as exc: print("%sinvalide device template:" % prefix) print("%serror: %s" % (prefix, exc)) print("%serror at: %d" % (prefix, exc.lineno)) return 1 except yaml.YAMLError as exc: print("%sinvalid device definition:" % prefix) print("%sinvalid yaml" % prefix) return 1 try: validate_device(data) except v.Invalid as exc: print("%sinvalid device definition:" % prefix) print("%skey: %s" % (prefix, exc.path)) print("%smgs: %s" % (prefix, exc.msg)) return 1 return 0
def check_device(data, options, prefix=""): try: data = yaml.safe_load(data) except yaml.YAMLError as exc: print("%sinvalid device definition:" % prefix) print("%sinvalid yaml" % prefix) return 1 try: validate_device(data) except v.Invalid as exc: print("%sinvalid device definition:" % prefix) print("%skey: %s" % (prefix, exc.path)) print("%smgs: %s" % (prefix, exc.msg)) return 1 return 0
def _handle_check(self, device): hostname = device.hostname self.stdout.write("* %s" % hostname) data = device.load_configuration() if data is None: self.stdout.write(" -> invalid or missing template") return 1 try: validate_device(data) except voluptuous.Invalid as exc: self.stdout.write(" -> invalid configuration") self.stdout.write(" -> %s" % exc.path) self.stdout.write(" -> %s" % exc.msg) return 1 return 0
def validate_data(self, hostname, data, job_ctx=None): """ Needs to be passed a device dictionary (jinja2 format) """ rendered = self.render_device_dictionary(hostname, data, job_ctx) try: ret = validate_device(yaml_safe_load(rendered)) except (voluptuous.Invalid, ConfigurationError) as exc: print("#######") print(rendered) print("#######") raise exc return ret