summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/validate_resource.rb
blob: bbb5a546a5d41ba6d7bafea2c2635e0ca5c689fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 :