Skip to content

Commit

Permalink
Refactor 'type':
Browse files Browse the repository at this point in the history
Using puppet4 type validation
Allow to define multple types
  • Loading branch information
jkroepke committed Feb 12, 2017
1 parent c5404ff commit 52a921a
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions manifests/type.pp
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
define collectd::type (
$target,
$ds = $title,
$ds_type = undef,
$types = [],
$min = 'U',
$max = 'U',
$ds_name = 'value',
String $target,
String $ds = $title,
Optional[Variant['ABSOLUTE', 'COUNTER', 'DERIVE', 'GAUGE']] $ds_type = undef,
Variant[Numeric, 'U'] $min = 'U',
Variant[Numeric, 'U'] $max = 'U',
String $ds_name = 'value',
Array[Struct[{
min => Variant[Numeric, 'U'],
max => Variant[Numeric, 'U'],
ds_type => Variant['ABSOLUTE', 'COUNTER', 'DERIVE', 'GAUGE'],
ds_name => String,
}]] $types = [],
) {
validate_string($ds_name)
validate_array($types)

if empty($types) {
validate_string($ds_type)

$upper_ds_type = upcase($ds_type)

validate_re($upper_ds_type,
['^ABSOLUTE$', '^COUNTER$', '^DERIVE$', '^GAUGE$'])

if $min != 'U' {
validate_numeric($min)
}

if $max != 'U' {
validate_numeric($max)
}

$_types = [{
min => $min,
max => $max,
ds_type => $upper_ds_type,
ds_type => $ds_type,
ds_name => $ds_name,
}]
} else {
# validation should be done with puppet4 types..
$_types = $types
}

$content = inline_template("<%= @ds %>\t<%= @_types.map{ |type| type['ds_name']+':'+type['ds_type']+':'+type['min'].to_s+':'+type['max'].to_s }.join(', ') -%>")
$content = $_types.map |$type| {
"${type['ds_name']}:${type['ds_type']}:${type['min']}:${type['max']}"
}.join(', ')

concat::fragment { "${target}/${ds}":
content => $content,
content => "${ds}\t${content}",
target => $target,
order => $ds,
}
Expand Down

0 comments on commit 52a921a

Please sign in to comment.