Added validate_resource function and examples on how to use it (and kwalify as well)
This commit is contained in:
parent
1abf4b62fc
commit
07ee334554
7 changed files with 174 additions and 0 deletions
9
examples/kwalify-1.pp
Normal file
9
examples/kwalify-1.pp
Normal file
|
@ -0,0 +1,9 @@
|
|||
$schema = {
|
||||
'type' => 'seq',
|
||||
'sequence' => [
|
||||
{ 'type' => 'str', 'enum' => ['asdf','fdsa'] }
|
||||
]
|
||||
}
|
||||
$document = ['a', 'b', 'c']
|
||||
|
||||
kwalify($schema, $document)
|
24
examples/kwalify-2.pp
Normal file
24
examples/kwalify-2.pp
Normal file
|
@ -0,0 +1,24 @@
|
|||
$schema = {
|
||||
'type' => 'map',
|
||||
'mapping' => {
|
||||
'name' => {
|
||||
'type' => 'str',
|
||||
'required' => true,
|
||||
},
|
||||
'email' => {
|
||||
'type' => 'str',
|
||||
'pattern' => '/@/',
|
||||
},
|
||||
'age' => {
|
||||
'type' => 'str',
|
||||
'pattern' => '/^\d+$/',
|
||||
},
|
||||
}
|
||||
}
|
||||
$document = {
|
||||
'name' => 'foo',
|
||||
'email' => 'foo@mail.com',
|
||||
'age' => 20,
|
||||
}
|
||||
|
||||
kwalify($schema, $document)
|
23
examples/validate_resource-1.pp
Normal file
23
examples/validate_resource-1.pp
Normal file
|
@ -0,0 +1,23 @@
|
|||
define fooresource(
|
||||
$color,
|
||||
$type,
|
||||
$somenumber,
|
||||
$map
|
||||
) {
|
||||
|
||||
validate_resource()
|
||||
|
||||
# ... do something ...
|
||||
|
||||
}
|
||||
|
||||
fooresource { "example1":
|
||||
color => "blue",
|
||||
type => "circle",
|
||||
somenumber => 5,
|
||||
map => {
|
||||
a => 1,
|
||||
b => 2,
|
||||
c => 3,
|
||||
}
|
||||
}
|
39
examples/validate_resource-1.schema
Normal file
39
examples/validate_resource-1.schema
Normal file
|
@ -0,0 +1,39 @@
|
|||
type: map
|
||||
mapping:
|
||||
"title":
|
||||
type: str
|
||||
required: yes
|
||||
"name":
|
||||
type: str
|
||||
required: yes
|
||||
"caller_module_name":
|
||||
type: str
|
||||
required: yes
|
||||
"module_name":
|
||||
type: str
|
||||
required: yes
|
||||
"color":
|
||||
type: str
|
||||
required: yes
|
||||
"type":
|
||||
type: str
|
||||
required: yes
|
||||
"somenumber":
|
||||
type: str
|
||||
required: yes
|
||||
pattern: /^\d+$/
|
||||
"map":
|
||||
type: map
|
||||
mapping:
|
||||
"a":
|
||||
type: str
|
||||
required: yes
|
||||
pattern: /^\d+$/
|
||||
"b":
|
||||
type: str
|
||||
required: yes
|
||||
pattern: /^\d+$/
|
||||
"c":
|
||||
type: str
|
||||
required: yes
|
||||
pattern: /^\d+$/
|
17
examples/validate_resource-2.pp
Normal file
17
examples/validate_resource-2.pp
Normal file
|
@ -0,0 +1,17 @@
|
|||
class foo (
|
||||
$a,
|
||||
$b,
|
||||
$c
|
||||
) {
|
||||
|
||||
validate_resource()
|
||||
|
||||
# ... do something ...
|
||||
|
||||
}
|
||||
|
||||
class { "foo":
|
||||
a => "1",
|
||||
b => "foobaz",
|
||||
c => ['a','b','c']
|
||||
}
|
26
examples/validate_resource-2.schema
Normal file
26
examples/validate_resource-2.schema
Normal file
|
@ -0,0 +1,26 @@
|
|||
type: map
|
||||
mapping:
|
||||
"title":
|
||||
type: str
|
||||
required: yes
|
||||
"name":
|
||||
type: str
|
||||
required: yes
|
||||
"caller_module_name":
|
||||
type: str
|
||||
required: yes
|
||||
"module_name":
|
||||
type: str
|
||||
required: yes
|
||||
"a":
|
||||
type: str
|
||||
required: yes
|
||||
"b":
|
||||
type: str
|
||||
required: yes
|
||||
pattern: /^foo/
|
||||
"c":
|
||||
type: seq
|
||||
required: yes
|
||||
sequence:
|
||||
- type: str
|
36
lib/puppet/parser/functions/validate_resource.rb
Normal file
36
lib/puppet/parser/functions/validate_resource.rb
Normal file
|
@ -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 :
|
Loading…
Reference in a new issue