Skip to content

Commit 7f8e987

Browse files
committed
Allow multiple namevars with no title_patterns
Resources with multiple namevars can be specified by passing in values as properties rather than through the title. A valid design choice in some rare edge-cases. This commit allows it through the title-validation.
1 parent 9b85961 commit 7f8e987

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

lib/puppet/resource_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def strict_check(current_state)
355355
return if Puppet.settings[:strict] == :off
356356

357357
strict_check_canonicalize(current_state) if type_definition.feature?('canonicalize')
358-
strict_check_title_parameter(current_state) if type_definition.namevars.size > 1
358+
strict_check_title_parameter(current_state) if type_definition.namevars.size > 1 && !type_definition.title_patterns.empty?
359359

360360
nil
361361
end

lib/puppet/resource_api/type_definition.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def feature?(feature)
1818
(definition[:features] && definition[:features].include?(feature))
1919
end
2020

21+
def title_patterns
22+
definition[:title_patterns] ||= []
23+
end
24+
2125
def validate_schema(definition, attr_key)
2226
super(definition, attr_key)
2327
[:title, :provider, :alias, :audit, :before, :consume, :export, :loglevel, :noop, :notify, :require, :schedule, :stage, :subscribe, :tag].each do |name|

spec/puppet/resource_api_spec.rb

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,15 +1055,60 @@ def set(_context, changes) end
10551055
end
10561056

10571057
describe 'the registered type' do
1058-
subject(:type) { Puppet::Type.type(:with_patterns) }
1058+
subject(:type) { Puppet::Type.type(:multiple) }
10591059

10601060
it { is_expected.not_to be_nil }
10611061
it { expect(type.parameters).to eq [:package, :manager] }
10621062
end
10631063

1064+
describe "the type's class" do
1065+
let(:provider_class) do
1066+
Class.new do
1067+
def get(_context)
1068+
[{ package: 'php', manager: 'yum', ensure: 'present' }]
1069+
end
1070+
1071+
def set(_context, _changes); end
1072+
end
1073+
end
1074+
let(:type_class) { Puppet::Type.type(:multiple) }
1075+
1076+
before(:each) do
1077+
stub_const('Puppet::Provider::Multiple', Module.new)
1078+
stub_const('Puppet::Provider::Multiple::Multiple', provider_class)
1079+
end
1080+
1081+
describe '.instances' do
1082+
it 'uses the title provided by the provider' do
1083+
expect(type_class.instances[0].title).to eq(manager: 'yum', package: 'php')
1084+
end
1085+
end
1086+
1087+
context 'when flushing an instance' do
1088+
let(:provider_instance) { instance_double(provider_class, 'provider_instance') }
1089+
1090+
before(:each) do
1091+
allow(provider_class).to receive(:new).and_return(provider_instance)
1092+
end
1093+
1094+
after(:each) do
1095+
# reset cached provider between tests
1096+
type_class.instance_variable_set(:@my_provider, nil)
1097+
end
1098+
1099+
it 'uses the attributes when setting values' do
1100+
allow(provider_instance).to receive(:get).and_return([{ package: 'php', manager: 'yum', ensure: 'present' }])
1101+
expect(provider_instance).to receive(:set) { |_context, changes|
1102+
expect(changes.keys).to eq [{ package: 'php', manager: 'yum' }]
1103+
}
1104+
type_class.new(title: 'some title', package: 'php', manager: 'yum', ensure: :absent).flush
1105+
end
1106+
end
1107+
end
1108+
10641109
context 'with title_patterns' do
10651110
let(:name) { 'with_patterns' }
1066-
let(:title_patterns) {
1111+
let(:title_patterns) do
10671112
[
10681113
{
10691114
pattern: %r{^(?<package>.*[^/])/(?<manager>.*)$},
@@ -1074,7 +1119,7 @@ def set(_context, changes) end
10741119
desc: 'Where only the package is provided',
10751120
},
10761121
]
1077-
}
1122+
end
10781123

10791124
describe 'the registered type' do
10801125
subject(:type) { Puppet::Type.type(:with_patterns) }
@@ -1083,7 +1128,7 @@ def set(_context, changes) end
10831128
it { expect(type.parameters).to eq [:package, :manager] }
10841129
end
10851130

1086-
describe 'the type\'s class' do
1131+
describe "the type's class" do
10871132
let(:provider_class) do
10881133
Class.new do
10891134
def get(_context)

0 commit comments

Comments
 (0)