Skip to content

Commit

Permalink
fix voxpupuli#256 consider type of attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
lbetz authored and n00by committed Apr 26, 2018
1 parent e681738 commit b14166a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
14 changes: 10 additions & 4 deletions lib/puppet/parser/functions/icinga2_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Puppet::Parser::Functions
newfunction(:icinga2_attributes, :type => :rvalue) do |args|
raise Puppet::ParseError, 'Must provide at least one argument.' if args.length > 3
raise Puppet::ParseError, 'Must provide at least one argument.' if args.length > 4

if args[1]
indent = args[1]
Expand All @@ -11,11 +11,17 @@ module Puppet::Parser::Functions
end

if args[2]
constants = args[2]
globals = args[2].concat(lookupvar('::icinga2::params::globals'))
else
constants = lookupvar('::icinga2::_constants').keys.concat(lookupvar('::icinga2::params::globals'))
globals = lookupvar('::icinga2::params::globals')
end

Puppet::Icinga2::Utils.attributes(args[0], constants, indent)
if args[3]
constants = args[3].merge(lookupvar('::icinga2::_constants'))
else
constants = lookupvar('::icinga2::_constants')
end

Puppet::Icinga2::Utils.attributes(args[0], globals, constants, indent)
end
end
11 changes: 7 additions & 4 deletions lib/puppet_x/icinga2/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ module Puppet
module Icinga2
module Utils

def self.attributes(attrs, consts, indent=2)
def self.attributes(attrs, globals, consts, indent=2)

def self.value_types(value)

if value =~ /^\d+\.?\d*[dhms]?$/ || value =~ /^(true|false)$/
if value =~ /^\d+\.?\d*[dhms]?$/ || value =~ /^(true|false)$/ || value =~ /^!?(host|service|user)\./ || value =~ /^\{{2}.*\}{2}$/
result = value
else
if $constants.index { |x| value =~ /^!?(#{x})(\..+$|$)/ } || value =~ /^!?(host|service|user)\./ || value =~ /^\{{2}.*\}{2}$/
if $constants.index { |x| if $hash_attrs.include?(x) then value =~ /^!?(#{x})(\..+$|$)/ else value =~ /^!?#{x}$/ end }
result = value
else
result = "\"#{value}\""
Expand Down Expand Up @@ -197,7 +197,10 @@ def self.process_hash(attrs, indent=2, level=3, prefix=' '*indent)


# globals (params.pp) and all keys of attrs hash itselfs must not quoted
$constants = consts.concat(attrs.keys) << "name"
$constants = globals.concat(consts.keys) << "name"

# select all attributes and constants if there value is a hash
$hash_attrs = attrs.merge(consts).select { |x,y| y.is_a?(Hash) }.keys

# initialize returned configuration
config = ''
Expand Down
1 change: 0 additions & 1 deletion manifests/object.pp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
fail('The object type must be different from the apply target')
}

$_constants = concat(keys($::icinga2::_constants), $::icinga2::params::globals, $attrs_list)
$_attrs = merge($attrs, {
'assign where' => $assign,
'ignore where' => $ignore,
Expand Down
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
$ca_dir = 'C:/ProgramData/icinga2/var/lib/icinga2/ca'
$ido_pgsql_package = undef
$ido_mysql_package = undef
$service_reload = undef

$constants = {
'PluginDir' => 'C:/Program Files/ICINGA2/sbin',
Expand Down
50 changes: 25 additions & 25 deletions spec/defines/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@


context "#{os} with import => [bar, baz], apply => desc in host.vars.bar, assign => [desc.bar]" do
let(:params) { {:import => ['bar', 'baz'], :apply => 'desc in host.vars.bar', :apply_target => 'Host', :assign => ['desc.bar'], :attrs => {'vars' => 'vars + desc'}, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:import => ['bar', 'baz'], :apply => 'desc in host.vars.bar', :apply_target => 'Host', :assign => ['desc.bar'], :attrs => {'vars' => 'vars + desc'}, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars'] } }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/import "bar"/)
Expand All @@ -92,7 +92,7 @@


context "#{os} with apply => desc => config in host.vars.bar" do
let(:params) { {:apply => 'desc => config in host.vars.bar', :apply_target => 'Host', :attrs => {'vars' => 'vars + desc + !config.bar'}, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:apply => 'desc => config in host.vars.bar', :apply_target => 'Host', :attrs => {'vars' => 'vars + desc + !config.bar'}, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars = vars \+ desc \+ !config.bar\n/)
Expand Down Expand Up @@ -182,31 +182,31 @@


context "#{os} with attrs => { vars => { bar => unparsed string } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => '-:"unparsed string"' } }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => '-:"unparsed string"' } }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = "unparsed string"/) }
end


context "#{os} with attrs => { vars => { bar => {} } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \{\}/) }
end


context "#{os} with attrs => { vars => { bar => [] } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \[\s+\]/) }
end


context "#{os} with attrs => { vars => {key1 => 4247, key2 => value2} }" do
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.key1 = 4247\n/)
Expand All @@ -215,7 +215,7 @@


context "#{os} with attrs => { vars => {foo => {key1 => 4247, key2 => value2}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => {'key1' => '4247', 'key2' => 'value2'}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => {'key1' => '4247', 'key2' => 'value2'}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["key1"\] = 4247\n/)
Expand All @@ -224,39 +224,39 @@


context "#{os} with attrs => { vars => {foo => {bar => {key => 4247, key2 => value2}}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => {'key1' => '4247', 'key2' => 'value2'}}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => {'key1' => '4247', 'key2' => 'value2'}}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \{\n\s+key1 = 4247\n\s+key2 = "value2"\n\s+\}\n/) }
end


context "#{os} with attrs => { vars => {foo => {bar => [4247, value2]}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \[ 4247, "value2", \]/) }
end


context "#{os} with attrs => { foo => {{ 0.8 * macro($bar$) }} }" do
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = \{{2} 0.8 \* macro\(\$bar\$\) \}{2}/) }
end


context "#{os} with attrs => { foo => 6 + {{ 0.8 * macro($bar$) }} }" do
let(:params) { {:attrs => { 'foo' => '6 + {{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '6 + {{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = 6 \+ \{{2} 0.8 \* macro\(\$bar\$\) \}{2}/) }
end


context "#{os} with attrs => { foo => {{ 0.8 * macro($bar$) }} / 2 }" do
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }} / 2' }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }} / 2' }, :object_type => 'foo', :target => '/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = \{{2} 0.8 \* macro\(\$bar\$\) \}{2} \/ 2/) }
Expand Down Expand Up @@ -354,8 +354,8 @@
end


context "Windows with import => [bar, baz], apply => desc in host.vars.bar, assign => [desc.bar]" do
let(:params) { {:import => ['bar', 'baz'], :apply => 'desc in host.vars.bar', :apply_target => 'Host', :assign => ['desc.bar'], :attrs => {'vars' => 'vars + desc'}, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
context "Windows 2012 R2 with import => [bar, baz], apply => desc in host.vars.bar, assign => [desc.bar]" do
let(:params) { {:import => ['bar', 'baz'], :apply => 'desc in host.vars.bar', :apply_target => 'Host', :assign => ['desc.bar'], :attrs => {'vars' => 'vars + desc'}, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/import "bar"/)
Expand All @@ -367,7 +367,7 @@


context "Windows 2012 R2 with apply => desc => config in host.vars.bar" do
let(:params) { {:apply => 'desc => config in host.vars.bar', :apply_target => 'Host', :attrs => {'vars' => 'vars + desc + !config.bar'}, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:apply => 'desc => config in host.vars.bar', :apply_target => 'Host', :attrs => {'vars' => 'vars + desc + !config.bar'}, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars = vars \+ desc \+ !config.bar\r\n/)
Expand Down Expand Up @@ -456,31 +456,31 @@


context "Windows 2012 R2 with attrs => { vars => { bar => unparsed string } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => '-:"unparsed string"' } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => '-:"unparsed string"' } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = "unparsed string"/) }
end


context "Windows 2012 R2 with attrs => { vars => { bar => {} } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \{\}/) }
end


context "Windows 2012 R2 with attrs => { vars => { bar => [] } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \[\s+\]/) }
end


context "Windows 2012 R2 with attrs => { vars => {key1 => 4247, key2 => value2} }" do
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.key1 = 4247\r\n/)
Expand All @@ -489,7 +489,7 @@


context "Windows 2012 R2 with attrs => { vars => {foo => {key1 => 4247, key2 => value2}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => {'key1' => '4247', 'key2' => 'value2'}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => {'key1' => '4247', 'key2' => 'value2'}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["key1"\] = 4247\r\n/)
Expand All @@ -498,39 +498,39 @@


context "Windows 2012 R2 with attrs => { vars => {foo => {bar => {key => 4247, key2 => value2}}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => {'key1' => '4247', 'key2' => 'value2'}}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => {'key1' => '4247', 'key2' => 'value2'}}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \{\r\n\s+key1 = 4247\r\n\s+key2 = "value2"\r\n\s+\}\r\n/) }
end


context "Windows 2012 R2 with attrs => { vars => {foo => {bar => [4247, value2]}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['vars']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \[ 4247, "value2", \]/) }
end


context "Windows 2012 R2 with attrs => { foo => {{ 0.8 * macro($bar$) }} }" do
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = \{{2} 0.8 \* macro\(\$bar\$\) \}{2}/) }
end


context "Windows 2012 R2 with attrs => { foo => 6 + {{ 0.8 * macro($bar$) }} }" do
let(:params) { {:attrs => { 'foo' => '6 + {{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '6 + {{ 0.8 * macro($bar$) }}' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = 6 \+ \{{2} 0.8 \* macro\(\$bar\$\) \}{2}/) }
end


context "Windows 2012 R2 with attrs => { foo => {{ 0.8 * macro($bar$) }} / 2 }" do
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }} / 2' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
let(:params) { {:attrs => { 'foo' => '{{ 0.8 * macro($bar$) }} / 2' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10', :attrs_list => ['foo']} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/foo = \{{2} 0.8 \* macro\(\$bar\$\) \}{2} \/ 2/) }
Expand Down
6 changes: 3 additions & 3 deletions templates/object.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
<% end -%>
<% unless @import.empty? %><%= "\n" %><% end -%>
<% if @apply.is_a?(String) and @apply =~ /^([A-Za-z_]+)\s+in\s+.+$/ -%>
<%= scope.function_icinga2_attributes([@_attrs,2,@_constants.concat([$1])]) -%>
<%= scope.function_icinga2_attributes([@_attrs,2,@attrs_list,{$1=>{}}]) -%>
<% elsif @apply.is_a?(String) and @apply =~ /^([A-Za-z_]+)\s+=>\s+([A-Za-z_]+)\s+in\s+.+$/ -%>
<%= scope.function_icinga2_attributes([@_attrs,2,@_constants.concat([$1,$2])]) -%>
<%= scope.function_icinga2_attributes([@_attrs,2,@attrs_list.concat([$1]),{$2=>{}}]) -%>
<% else -%>
<%= scope.function_icinga2_attributes([@_attrs,2]) -%>
<%= scope.function_icinga2_attributes([@_attrs,2,@attrs_list]) -%>
<% end -%>
}

0 comments on commit b14166a

Please sign in to comment.