diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb index cc2b90a43..42775849d 100644 --- a/lib/puppet/parser/functions/defined_with_params.rb +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -48,8 +48,13 @@ title = nil end - resource = findresource(type, title) - if resource + resources = if title.empty? + catalog.resources.select { |r| r.type == type } + else + [findresource(type, title)] + end + + resources.compact.each do |resource| matches = params.map do |key, value| # eql? avoids bugs caused by monkeypatching in puppet resource_is_undef = resource[key].eql?(:undef) || resource[key].nil? @@ -57,7 +62,11 @@ (resource_is_undef && value_is_undef) || (resource[key] == value) end ret = params.empty? || !matches.include?(false) + + break if ret end - Puppet.debug("Resource #{reference} was not determined to be defined") + + Puppet.debug("Resource #{reference} was not determined to be defined") unless ret + ret end diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb index 74f9a7ccc..cc191a455 100644 --- a/spec/functions/defined_with_params_spec.rb +++ b/spec/functions/defined_with_params_spec.rb @@ -63,13 +63,35 @@ describe 'when passed a defined type' do let :pre_condition do - 'define test::deftype() { } test::deftype { "foo": }' + <<-PRECOND + define test::deftype( + Optional $port = undef + ) { } + + test::deftype { "foo": } + test::deftype { "baz": port => 100 } + test::deftype { "adv": port => 200 } + test::deftype { "adv2": port => 200 } + + # Unsure how to stub this out below properly + if defined_with_params(Test::Deftype, { 'port' => 200 }) { + notify { 'Duplicate found somewhere': } + } + if defined_with_params(Test::Deftype, { 'port' => 'nope' }) { + notify { 'Should not find me': } + } + PRECOND end it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) } it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) } it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) } - it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) } + it { + is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) + + expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil + expect(catalogue.resource('Notify[Should not find me]')).to be_nil + } end describe 'when passed a class' do