diff --git a/lib/puppet/parser/functions/icinga2_attributes.rb b/lib/puppet/parser/functions/icinga2_attributes.rb index 496882fa9..b5dcadbe9 100644 --- a/lib/puppet/parser/functions/icinga2_attributes.rb +++ b/lib/puppet/parser/functions/icinga2_attributes.rb @@ -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] @@ -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 diff --git a/lib/puppet_x/icinga2/utils.rb b/lib/puppet_x/icinga2/utils.rb index 2e1ba7cf6..bfa34611e 100644 --- a/lib/puppet_x/icinga2/utils.rb +++ b/lib/puppet_x/icinga2/utils.rb @@ -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}\"" @@ -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 = '' diff --git a/manifests/object.pp b/manifests/object.pp index 169f97e46..d91ecefcc 100644 --- a/manifests/object.pp +++ b/manifests/object.pp @@ -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, diff --git a/manifests/params.pp b/manifests/params.pp index b150f6c38..120110b6a 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -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', diff --git a/spec/defines/object_spec.rb b/spec/defines/object_spec.rb index 22afed836..71dab5182 100644 --- a/spec/defines/object_spec.rb +++ b/spec/defines/object_spec.rb @@ -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"/) @@ -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/) @@ -182,7 +182,7 @@ 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"/) } @@ -190,7 +190,7 @@ 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 = \{\}/) } @@ -198,7 +198,7 @@ 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+\]/) } @@ -206,7 +206,7 @@ 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/) @@ -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/) @@ -224,7 +224,7 @@ 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/) } @@ -232,7 +232,7 @@ 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", \]/) } @@ -240,7 +240,7 @@ 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}/) } @@ -248,7 +248,7 @@ 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}/) } @@ -256,7 +256,7 @@ 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/) } @@ -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"/) @@ -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/) @@ -456,7 +456,7 @@ 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"/) } @@ -464,7 +464,7 @@ 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 = \{\}/) } @@ -472,7 +472,7 @@ 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+\]/) } @@ -480,7 +480,7 @@ 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/) @@ -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/) @@ -498,7 +498,7 @@ 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/) } @@ -506,7 +506,7 @@ 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", \]/) } @@ -514,7 +514,7 @@ 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}/) } @@ -522,7 +522,7 @@ 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}/) } @@ -530,7 +530,7 @@ 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/) } diff --git a/templates/object.conf.erb b/templates/object.conf.erb index c210d947f..3a0c77269 100644 --- a/templates/object.conf.erb +++ b/templates/object.conf.erb @@ -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 -%> }