From 4122f08454f0c493543c9d94b5fbfa8a2b0dc727 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 29 Jan 2019 19:39:23 +0000 Subject: [PATCH 1/2] (maint) lift duplicate assignment --- lib/puppet/resource_api/type_definition.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/puppet/resource_api/type_definition.rb b/lib/puppet/resource_api/type_definition.rb index 130bf524..3e515aed 100644 --- a/lib/puppet/resource_api/type_definition.rb +++ b/lib/puppet/resource_api/type_definition.rb @@ -39,9 +39,6 @@ def validate_schema(definition, attr_key) supported_features = %w[supports_noop canonicalize remote_resource simple_get_filter].freeze unknown_features = definition[:features] - supported_features Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty? - - # store the validated definition - @definition = definition end end @@ -75,10 +72,12 @@ class BaseTypeDefinition def initialize(definition, attr_key) @data_type_cache = {} validate_schema(definition, attr_key) + # store the validated definition + @definition = definition end def name - @definition[:name] + definition[:name] end def namevars @@ -118,8 +117,6 @@ def validate_schema(definition, attr_key) attr[:behaviour] = attr[:behavior] attr.delete(:behavior) end - # store the validated definition - @definition = definition end # validates a resource hash against its type schema From 3a642760158cf1645494a7ce48a0479a55677a34 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Fri, 1 Feb 2019 15:34:32 +0000 Subject: [PATCH 2/2] (FM-7726) deep symbolize all keys when loading credentials --- lib/puppet/resource_api/transport/wrapper.rb | 9 ++++++++- spec/puppet/resource_api/transport/wrapper_spec.rb | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/puppet/resource_api/transport/wrapper.rb b/lib/puppet/resource_api/transport/wrapper.rb index a8b102d2..c1ac50a0 100644 --- a/lib/puppet/resource_api/transport/wrapper.rb +++ b/lib/puppet/resource_api/transport/wrapper.rb @@ -11,7 +11,7 @@ def initialize(name, url_or_config) url = URI.parse(url_or_config) raise "Unexpected url '#{url_or_config}' found. Only file:/// URLs for configuration supported at the moment." unless url.scheme == 'file' raise "Trying to load config from '#{url.path}, but file does not exist." if url && !File.exist?(url.path) - config = (Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {}).map { |k, v| [k.to_sym, v] }.to_h + config = self.class.deep_symbolize(Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {}) else config = url_or_config end @@ -38,4 +38,11 @@ def method_missing(method_name, *args, &block) super end end + + # From https://stackoverflow.com/a/11788082/4918 + def self.deep_symbolize(obj) + return obj.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = deep_symbolize(v); } if obj.is_a? Hash + return obj.each_with_object([]) { |v, memo| memo << deep_symbolize(v); } if obj.is_a? Array + obj + end end diff --git a/spec/puppet/resource_api/transport/wrapper_spec.rb b/spec/puppet/resource_api/transport/wrapper_spec.rb index b6652424..ccb7c093 100644 --- a/spec/puppet/resource_api/transport/wrapper_spec.rb +++ b/spec/puppet/resource_api/transport/wrapper_spec.rb @@ -11,7 +11,7 @@ it 'will not throw an error' do allow(File).to receive(:exist?).and_return(true) expect(Puppet::ResourceApi::Transport).to receive(:connect) - expect(Hocon).to receive(:load) + expect(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return('foo' => %w[a b], 'bar' => 2) expect { described_class.new('wibble', url) }.not_to raise_error end end