Skip to content

Commit 9bc3b12

Browse files
committed
(maint) make transports register more robust
Lifting all `@transports` accesses into a single method simplifies the code's internal structure (see Law of Demeter).
1 parent 42a393e commit 9bc3b12

File tree

2 files changed

+13
-38
lines changed

2 files changed

+13
-38
lines changed

lib/puppet/resource_api/transport.rb

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@ def register(schema)
99
raise Puppet::DevError, 'requires `:connection_info`' unless schema.key? :connection_info
1010
raise Puppet::DevError, '`:connection_info` must be a hash, not `%{other_type}`' % { other_type: schema[:connection_info].class } unless schema[:connection_info].is_a?(Hash)
1111

12-
init_transports
13-
unless @transports[current_environment][schema[:name]].nil?
12+
unless transports[schema[:name]].nil?
1413
raise Puppet::DevError, 'Transport `%{name}` is already registered for `%{environment}`' % {
1514
name: schema[:name],
1615
environment: current_environment,
1716
}
1817
end
19-
@transports[current_environment][schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema)
18+
transports[schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema)
2019
end
2120
module_function :register # rubocop:disable Style/AccessModifierDeclarations
2221

2322
# retrieve a Hash of transport schemas, keyed by their name.
2423
def list
25-
init_transports
26-
Marshal.load(Marshal.dump(@transports[current_environment]))
24+
Marshal.load(Marshal.dump(transports))
2725
end
2826
module_function :list # rubocop:disable Style/AccessModifierDeclarations
2927

@@ -47,9 +45,8 @@ def inject_device(name, transport)
4745
module_function :inject_device # rubocop:disable Style/AccessModifierDeclarations
4846

4947
def self.validate(name, connection_info)
50-
init_transports
51-
require "puppet/transport/schema/#{name}" unless @transports[current_environment].key? name
52-
transport_schema = @transports[current_environment][name]
48+
require "puppet/transport/schema/#{name}" unless transports.key? name
49+
transport_schema = transports[name]
5350
if transport_schema.nil?
5451
raise Puppet::DevError, 'Transport for `%{target}` not registered with `%{environment}`' % {
5552
target: name,
@@ -64,18 +61,12 @@ def self.validate(name, connection_info)
6461

6562
def self.get_context(name)
6663
require 'puppet/resource_api/puppet_context'
67-
Puppet::ResourceApi::PuppetContext.new(@transports[current_environment][name])
64+
Puppet::ResourceApi::PuppetContext.new(transports[name])
6865
end
6966
private_class_method :get_context
7067

71-
def self.init_transports
72-
@transports ||= {}
73-
@transports[current_environment] ||= {}
74-
end
75-
private_class_method :init_transports
76-
7768
def self.wrap_sensitive(name, connection_info)
78-
transport_schema = @transports[current_environment][name]
69+
transport_schema = transports[name]
7970
if transport_schema
8071
transport_schema.definition[:connection_info].each do |attr_name, options|
8172
if options.key?(:sensitive) && (options[:sensitive] == true) && connection_info.key?(attr_name)
@@ -87,6 +78,12 @@ def self.wrap_sensitive(name, connection_info)
8778
end
8879
private_class_method :wrap_sensitive
8980

81+
def self.transports
82+
@transports ||= {}
83+
@transports[current_environment] ||= {}
84+
end
85+
private_class_method :transports
86+
9087
def self.current_environment
9188
if Puppet.respond_to? :lookup
9289
env = Puppet.lookup(:current_environment)

spec/puppet/resource_api/transport_spec.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,28 +312,6 @@ class Wibble; end
312312
end
313313
end
314314

315-
describe '#init_transports' do
316-
context 'when there is not a current_environment' do
317-
it 'will return the default transport environment name' do
318-
change_environment
319-
320-
described_class.send :init_transports
321-
322-
expect(described_class.instance_variable_get(:@environment)).to eq(:transports_default)
323-
end
324-
end
325-
326-
context 'when there is a current_environment' do
327-
it 'will return the set environment name' do
328-
change_environment(:something)
329-
330-
described_class.send :init_transports
331-
332-
expect(described_class.instance_variable_get(:@environment)).to eq(:something)
333-
end
334-
end
335-
end
336-
337315
describe '#wrap_sensitive(name, connection_info)' do
338316
let(:schema) do
339317
{

0 commit comments

Comments
 (0)