lib/puppet/resource_api/transport.rb in puppet-resource_api-1.8.8 vs lib/puppet/resource_api/transport.rb in puppet-resource_api-1.8.9

- old
+ new

@@ -6,10 +6,15 @@ raise Puppet::DevError, 'requires a hash as schema, not `%{other_type}`' % { other_type: schema.class } unless schema.is_a? Hash raise Puppet::DevError, 'requires a `:name`' unless schema.key? :name raise Puppet::DevError, 'requires `:desc`' unless schema.key? :desc 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) + if schema[:connection_info_order].nil? + schema[:connection_info_order] = schema[:connection_info].keys + else + raise Puppet::DevError, '`:connection_info_order` must be an array, not `%{other_type}`' % { other_type: schema[:connection_info_order].class } unless schema[:connection_info_order].is_a?(Array) + end unless transports[schema[:name]].nil? raise Puppet::DevError, 'Transport `%{name}` is already registered for `%{environment}`' % { name: schema[:name], environment: current_environment_name, @@ -19,10 +24,12 @@ end module_function :register # rubocop:disable Style/AccessModifierDeclarations # retrieve a Hash of transport schemas, keyed by their name. # Only already loaded transports are returned. + # use Puppet::Util::Autoload.new(self, 'puppet/transport/schema').loadall(current_environment) to load all transport schemas + # note that loadall uses `require` and thus does not re-load changed files, nor does it re-populate the internal cache def list Marshal.load(Marshal.dump(transports)) end module_function :list # rubocop:disable Style/AccessModifierDeclarations @@ -37,17 +44,29 @@ Marshal.load(Marshal.dump(transports)) end end module_function :list_all_transports # rubocop:disable Style/AccessModifierDeclarations - # Loads all schemas using the Puppet Autoloader. + # Loads all schemas using the Puppet Autoloader. This method is clearing the cache and forcing `Kernel.load`, so that the cache is up-to-date. def self.load_all_schemas require 'puppet' require 'puppet/settings' require 'puppet/util/autoload' - @autoloader ||= Puppet::Util::Autoload.new(self, 'puppet/transport/schema') - @autoloader.loadall(current_environment) + + # Since we're force-loading all schemas below, we can replace the cache here completely + @transports = {} + + loader = Puppet::Util::Autoload + # from puppetlabs/puppet:lib/puppet/util/autoload.rb + # Puppet::Util::Autoload.loadall('puppet/transport/schema', current_environment) + path = 'puppet/transport/schema' + env = current_environment + # Load every instance of everything we can find. + loader.files_to_load(path, env).each do |file| + name = file.chomp('.rb') + loader.load_file(name, env) + end end private_class_method :load_all_schemas def connect(name, connection_info) validate(name, connection_info) @@ -106,16 +125,11 @@ connection_info end private_class_method :wrap_sensitive def self.transports - env = current_environment - if env - ObjectIdCacheAdapter.adapt(env).retrieve(:rsapi_transport_cache) - else - @transports_default ||= {} - end + @transports ||= {} end private_class_method :transports def self.current_environment Puppet.lookup(:current_environment) if Puppet.respond_to? :lookup @@ -157,21 +171,6 @@ # don't return a value as we've already modified the hash nil end private_class_method :clean_bolt_attributes - - # copy from https://github.com/puppetlabs/puppet/blob/8cae8a17dbac08d2db0238d5bce2f1e4d1898d65/lib/puppet/pops/adapters.rb#L6-L17 - # to keep backwards compatibility with puppet4 and 5, which don't have this yet. - class ObjectIdCacheAdapter < Puppet::Pops::Adaptable::Adapter - attr_accessor :cache - - def initialize - @cache = {} - end - - # Retrieves a mutable hash with all stored values - def retrieve(obj) - @cache[obj.__id__] ||= {} - end - end end