Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FM-7726) cleanups for the transport #153

Merged
merged 2 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/puppet/resource_api/transport/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
9 changes: 3 additions & 6 deletions lib/puppet/resource_api/type_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/puppet/resource_api/transport/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down