-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathloadjson.rb
More file actions
66 lines (60 loc) · 1.85 KB
/
loadjson.rb
File metadata and controls
66 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)
end
end
end