diff --git a/lib/puppet/resource_api/transport.rb b/lib/puppet/resource_api/transport.rb index 2bed256e..3c22a2c7 100644 --- a/lib/puppet/resource_api/transport.rb +++ b/lib/puppet/resource_api/transport.rb @@ -9,21 +9,19 @@ def register(schema) raise Puppet::DevError, 'requires `:connection_info`' unless schema.key? :connection_info 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) - init_transports - unless @transports[@environment][schema[:name]].nil? + unless transports[schema[:name]].nil? raise Puppet::DevError, 'Transport `%{name}` is already registered for `%{environment}`' % { name: schema[:name], - environment: @environment, + environment: current_environment, } end - @transports[@environment][schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema) + transports[schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema) end module_function :register # rubocop:disable Style/AccessModifierDeclarations # retrieve a Hash of transport schemas, keyed by their name. def list - init_transports - Marshal.load(Marshal.dump(@transports[@environment])) + Marshal.load(Marshal.dump(transports)) end module_function :list # rubocop:disable Style/AccessModifierDeclarations @@ -47,13 +45,12 @@ def inject_device(name, transport) module_function :inject_device # rubocop:disable Style/AccessModifierDeclarations def self.validate(name, connection_info) - init_transports - require "puppet/transport/schema/#{name}" unless @transports[@environment].key? name - transport_schema = @transports[@environment][name] + require "puppet/transport/schema/#{name}" unless transports.key? name + transport_schema = transports[name] if transport_schema.nil? raise Puppet::DevError, 'Transport for `%{target}` not registered with `%{environment}`' % { target: name, - environment: @environment, + environment: current_environment, } end message_prefix = 'The connection info provided does not match the Transport Schema' @@ -64,24 +61,12 @@ def self.validate(name, connection_info) def self.get_context(name) require 'puppet/resource_api/puppet_context' - Puppet::ResourceApi::PuppetContext.new(@transports[@environment][name]) + Puppet::ResourceApi::PuppetContext.new(transports[name]) end private_class_method :get_context - def self.init_transports - lookup = Puppet.lookup(:current_environment) if Puppet.respond_to? :lookup - @environment = if lookup.nil? - :transports_default - else - lookup.name - end - @transports ||= {} - @transports[@environment] ||= {} - end - private_class_method :init_transports - def self.wrap_sensitive(name, connection_info) - transport_schema = @transports[@environment][name] + transport_schema = transports[name] if transport_schema transport_schema.definition[:connection_info].each do |attr_name, options| if options.key?(:sensitive) && (options[:sensitive] == true) && connection_info.key?(attr_name) @@ -92,4 +77,20 @@ def self.wrap_sensitive(name, connection_info) connection_info end private_class_method :wrap_sensitive + + def self.transports + @transports ||= {} + @transports[current_environment] ||= {} + end + private_class_method :transports + + def self.current_environment + if Puppet.respond_to? :lookup + env = Puppet.lookup(:current_environment) + env.nil? ? :transports_default : env.name + else + :transports_default + end + end + private_class_method :current_environment end diff --git a/spec/puppet/resource_api/transport/wrapper_spec.rb b/spec/puppet/resource_api/transport/wrapper_spec.rb index 1b56c20d..744c3a0a 100644 --- a/spec/puppet/resource_api/transport/wrapper_spec.rb +++ b/spec/puppet/resource_api/transport/wrapper_spec.rb @@ -109,7 +109,7 @@ class SomethingSomethingDarkside; end context 'when using method?' do it 'will return false' do - expect { instance.method :wibble }.to raise_error + expect { instance.method :wibble }.to raise_error NameError, %r{undefined method `wibble'} end end end diff --git a/spec/puppet/resource_api/transport_spec.rb b/spec/puppet/resource_api/transport_spec.rb index c3547477..ccb02997 100644 --- a/spec/puppet/resource_api/transport_spec.rb +++ b/spec/puppet/resource_api/transport_spec.rb @@ -113,27 +113,29 @@ def change_environment(name = nil) end context 'when a transports are added to multiple environments' do + let(:transports) { described_class.instance_variable_get(:@transports) } + it 'will record the schemas in the correct structure' do - change_environment(:wibble) + change_environment(nil) described_class.register(schema) - expect(described_class.instance_variable_get(:@transports)).to be_key(:wibble) - expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) - expect(described_class.instance_variable_get(:@transports)[:wibble][schema[:name]].definition).to eq(schema) + expect(transports).to have_key(:transports_default) + expect(transports[:transports_default][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) + expect(transports[:transports_default][schema[:name]].definition).to eq(schema) - change_environment(:foo) + change_environment('foo') described_class.register(schema) described_class.register(schema2) - expect(described_class.instance_variable_get(:@transports)).to be_key(:foo) - expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) - expect(described_class.instance_variable_get(:@transports)[:foo][schema[:name]].definition).to eq(schema) - expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) - expect(described_class.instance_variable_get(:@transports)[:foo][schema2[:name]].definition).to eq(schema2) + expect(transports).to have_key('foo') + expect(transports['foo'][schema[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) + expect(transports['foo'][schema[:name]].definition).to eq(schema) + expect(transports['foo'][schema2[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) + expect(transports['foo'][schema2[:name]].definition).to eq(schema2) change_environment(:bar) described_class.register(schema3) - expect(described_class.instance_variable_get(:@transports)).to be_key(:bar) - expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) - expect(described_class.instance_variable_get(:@transports)[:bar][schema3[:name]].definition).to eq(schema3) + expect(transports).to have_key(:bar) + expect(transports[:bar][schema3[:name]]).to be_a_kind_of(Puppet::ResourceApi::TransportSchemaDef) + expect(transports[:bar][schema3[:name]].definition).to eq(schema3) end end end @@ -310,28 +312,6 @@ class Wibble; end end end - describe '#init_transports' do - context 'when there is not a current_environment' do - it 'will return the default transport environment name' do - change_environment - - described_class.send :init_transports - - expect(described_class.instance_variable_get(:@environment)).to eq(:transports_default) - end - end - - context 'when there is a current_environment' do - it 'will return the set environment name' do - change_environment(:something) - - described_class.send :init_transports - - expect(described_class.instance_variable_get(:@environment)).to eq(:something) - end - end - end - describe '#wrap_sensitive(name, connection_info)' do let(:schema) do {