Skip to content

Commit 33ff524

Browse files
Merge pull request #1115 from trevor-vaughan/MODULES-10729-defined_with_params
[MODULES-10729] defined_with_params - unnamed type
2 parents 88ffa24 + 733320e commit 33ff524

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/puppet/parser/functions/defined_with_params.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,25 @@
4848
title = nil
4949
end
5050

51-
resource = findresource(type, title)
52-
if resource
51+
resources = if title.empty?
52+
catalog.resources.select { |r| r.type == type }
53+
else
54+
[findresource(type, title)]
55+
end
56+
57+
resources.compact.each do |resource|
5358
matches = params.map do |key, value|
5459
# eql? avoids bugs caused by monkeypatching in puppet
5560
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
5661
value_is_undef = value.eql?(:undef) || value.nil?
5762
(resource_is_undef && value_is_undef) || (resource[key] == value)
5863
end
5964
ret = params.empty? || !matches.include?(false)
65+
66+
break if ret
6067
end
61-
Puppet.debug("Resource #{reference} was not determined to be defined")
68+
69+
Puppet.debug("Resource #{reference} was not determined to be defined") unless ret
70+
6271
ret
6372
end

spec/functions/defined_with_params_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,35 @@
6363

6464
describe 'when passed a defined type' do
6565
let :pre_condition do
66-
'define test::deftype() { } test::deftype { "foo": }'
66+
<<-PRECOND
67+
define test::deftype(
68+
Optional $port = undef
69+
) { }
70+
71+
test::deftype { "foo": }
72+
test::deftype { "baz": port => 100 }
73+
test::deftype { "adv": port => 200 }
74+
test::deftype { "adv2": port => 200 }
75+
76+
# Unsure how to stub this out below properly
77+
if defined_with_params(Test::Deftype, { 'port' => 200 }) {
78+
notify { 'Duplicate found somewhere': }
79+
}
80+
if defined_with_params(Test::Deftype, { 'port' => 'nope' }) {
81+
notify { 'Should not find me': }
82+
}
83+
PRECOND
6784
end
6885

6986
it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) }
7087
it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) }
7188
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) }
72-
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) }
89+
it {
90+
is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false)
91+
92+
expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil
93+
expect(catalogue.resource('Notify[Should not find me]')).to be_nil
94+
}
7395
end
7496

7597
describe 'when passed a class' do

0 commit comments

Comments
 (0)