diff options
author | Ken Barber <ken@bob.sh> | 2011-07-01 21:09:02 +0200 |
---|---|---|
committer | Ken Barber <ken@bob.sh> | 2011-07-01 21:09:02 +0200 |
commit | 07ee33455439d960b9996e8110e011437181b42a (patch) | |
tree | a5123f487a551a2608971b3ede13417139719b32 /lib/puppet/parser/functions | |
parent | 1abf4b62fc8e97c7096c9da3d350e3e6268c5c72 (diff) | |
download | puppet-stdlib-07ee33455439d960b9996e8110e011437181b42a.tar.gz puppet-stdlib-07ee33455439d960b9996e8110e011437181b42a.tar.bz2 |
Added validate_resource function and examples on how to use it (and kwalify as well)
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/validate_resource.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb new file mode 100644 index 0000000..bbb5a54 --- /dev/null +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -0,0 +1,36 @@ +# +# validate_resource +# + +module Puppet::Parser::Functions + newfunction(:validate_resource, :type => :statement, :doc => <<-EOS + EOS + ) do |arguments| + + require 'kwalify' + + if (arguments.size != 0) then + raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+ + "given #{arguments.size} for 0") + end + + + classhash = to_hash(recursive=false) + sourcepath = source.file + schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema") + schema = Kwalify::Yaml.load_file(schemapath) + validator = Kwalify::Validator.new(schema) + errors = validator.validate(classhash) + + if errors && !errors.empty? + error_output = "Resource validation failed:\n" + for e in errors + error_output += "[#{e.path}] #{e.message}\n" + end + raise(Puppet::ParseError, error_output) + end + + end +end + +# vim: set ts=2 sw=2 et : |