-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite loadjson to use the modern function API
This also resolves a bug where JSON.parse returned a StringIO. To properly catch this, the tests are rewritten to avoid mocking where possible. Exceptions are with external URLs and where a failure is expected.
- Loading branch information
Showing
4 changed files
with
130 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary DEPRECATED. Use the namespaced function [`stdlib::loadjson`](#stdlibloadjson) instead. | ||
Puppet::Functions.create_function(:loadjson, Puppet::Functions::InternalFunction) do | ||
dispatch :deprecation_gen do | ||
scope_param | ||
repeated_param 'Any', :args | ||
end | ||
def deprecation_gen(scope, *args) | ||
call_function('deprecation', 'loadjson', 'This function is deprecated, please use stdlib::loadjson instead.', false) | ||
scope.call_function('stdlib::loadjson', args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary | ||
# Load a JSON file containing an array, string, or hash, and return the data | ||
# in the corresponding native data type. | ||
# | ||
# @example Example Usage: | ||
# $myhash = loadjson('/etc/puppet/data/myhash.json') | ||
# $myhash = loadjson('https://example.local/my_hash.json') | ||
# $myhash = loadjson('https://username:password@example.local/my_hash.json') | ||
# $myhash = loadjson('no-file.json', {'default' => 'value'}) | ||
Puppet::Functions.create_function(:'stdlib::loadjson') do | ||
# @param path | ||
# A file path or a URL. | ||
# @param default | ||
# The default value to be returned if the file was not found or could not | ||
# be parsed. | ||
# | ||
# @return | ||
# The data stored in the JSON file, the type depending on the type of data | ||
# that was stored. | ||
dispatch :loadjson do | ||
param 'String[1]', :path | ||
optional_param 'Data', :default | ||
return_type 'Data' | ||
end | ||
|
||
def loadjson(path, default = nil) | ||
if path.start_with?('http://', 'https://') | ||
require 'uri' | ||
require 'open-uri' | ||
uri = URI.parse(path) | ||
options = {} | ||
if uri.user | ||
options[:http_basic_authentication] = [uri.user, uri.password] | ||
uri.user = nil | ||
end | ||
|
||
begin | ||
content = uri.open(**options) { |f| load_json_source(f) } | ||
rescue OpenURI::HTTPError => e | ||
Puppet.warn("Can't load '#{url}' HTTP Error Code: '#{e.io.status[0]}'") | ||
return default | ||
end | ||
elsif File.exist?(path) | ||
content = File.open(path) { |f| load_json_source(f) } | ||
else | ||
Puppet.warn("Can't load '#{path}' File does not exist!") | ||
return default | ||
end | ||
|
||
content || default | ||
rescue StandardError => e | ||
raise e unless default | ||
|
||
default | ||
end | ||
|
||
def load_json_source(source) | ||
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? | ||
PSON.load(source) | ||
else | ||
JSON.load(source) | ||
Check failure on line 63 in lib/puppet/functions/stdlib/loadjson.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
Check failure on line 63 in lib/puppet/functions/stdlib/loadjson.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters