Skip to content

Commit 1105f1f

Browse files
committed
(wip) use json, not pson, steal Bens work to make pup4
1 parent ade894a commit 1105f1f

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# @summary
3+
# This function loads the metadata of a given module.
4+
#
5+
# @example Example Usage:
6+
# $metadata = load_module_metadata('archive')
7+
# notify { $metadata['author']: }
8+
#
9+
# @return
10+
# The modules metadata
11+
#
12+
Puppet::Functions.create_function(:'stdlib::load_module_metadata') do
13+
# @param args
14+
# The original array of arguments. Port this to individually managed params
15+
# to get the full benefit of the modern function API.
16+
#
17+
# @return [Data type]
18+
# Describe what the function returns here
19+
#
20+
dispatch :default_impl do
21+
param 'String', :module_name
22+
optional_param 'Bool', :allow_empty_metadata
23+
end
24+
25+
def default_impl(module_name, allow_empty_metadata)
26+
module_path = function_get_module_path([module_name])
27+
metadata_json = File.join(module_path, 'metadata.json')
28+
29+
if File.exists?(metadata_json)
30+
return Puppet::Util::Json.load(File.read(metadata_json))
31+
32+
else
33+
if !allow_empty_metadata
34+
raise(Puppet::ParseError,
35+
"load_module_metadata(): No metadata.json file for module #{module_name}")
36+
end
37+
38+
return Hash.new
39+
end
40+
end
41+
end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# @summary
3+
# Load a JSON file containing an array, string, or hash, and return the data
4+
# in the corresponding native data type.
5+
#
6+
# The first parameter can be a file path or a URL.
7+
# The second parameter is the default value. It will be returned if the file
8+
# was not found or could not be parsed.
9+
#
10+
# @return [Array|String|Hash]
11+
# The data stored in the JSON file, the type depending on the type of data that was stored.
12+
#
13+
# @example Example Usage:
14+
# $myhash = loadjson('/etc/puppet/data/myhash.json')
15+
# $myhash = loadjson('https://example.local/my_hash.json')
16+
# $myhash = loadjson('https://username:password@example.local/my_hash.json')
17+
# $myhash = loadjson('no-file.json', {'default' => 'value'})
18+
#
19+
#
20+
Puppet::Functions.create_function(:'stdlib::loadjson') do
21+
require 'puppet/util/json'
22+
23+
# If URL is in the format of https://username:password@example.local/my_info.json
24+
URI_WITH_NAME_AND_PASS_PATTERN = %r{(http\://|https\://)(.*):(.*)@(.*)}
25+
26+
# If URL is in the format of https://username@example.local/my_info.json
27+
URI_WITH_NAME_PATTERN = %r{(http\://|https\://)(.*)@(.*)}
28+
29+
dispatch :default_impl do
30+
param 'String', :path_or_uri
31+
optional_param Any, :default
32+
end
33+
34+
def default_impl(path_or_uri, default = nil)
35+
begin
36+
if path_or_uri.start_with?('http://', 'https://')
37+
load_from_uri(path_or_uri, default)
38+
39+
elsif File.exists?(path_or_uri)
40+
content = File.read(path_or_uri)
41+
42+
Puppet::Util::Json.load(content) || default
43+
else
44+
warning("Can't load '#{path_or_uri}' File does not exist!")
45+
46+
default
47+
end
48+
rescue StandardError => err
49+
if default
50+
default
51+
else
52+
raise err
53+
end
54+
end
55+
end
56+
57+
private
58+
def load_from_uri(uri, default)
59+
require 'open-uri'
60+
61+
username = ''
62+
password = ''
63+
if (match = uri.match(URI_WITH_NAME_AND_PASS_PATTERN))
64+
protocol, username, password, path = match.captures
65+
url = "#{protocol}#{path}"
66+
elsif (match = uri.match(URI_WITH_NAME_PATTERN))
67+
protocol, username, path = match.captures
68+
url = "#{protocol}#{path}"
69+
else
70+
url = uri
71+
end
72+
73+
begin
74+
contents = OpenURI.open_uri(url, :http_basic_authentication => [username, password])
75+
rescue OpenURI::HTTPError => err
76+
res = err.io
77+
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
78+
79+
default
80+
end
81+
82+
Puppet::Util::Json.load(contents) || default
83+
end
84+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# @summary
3+
# This function accepts JSON as a string and converts it into the correct
4+
# Puppet structure.
5+
#
6+
# @return
7+
# convert JSON into Puppet structure
8+
#
9+
# > *Note:*
10+
# The optional second argument can be used to pass a default value that will
11+
# be returned if the parsing of JSON string have failed.
12+
#
13+
#
14+
Puppet::Functions.create_function(:'stdlib::parsejson') do
15+
16+
dispatch :default_impl do
17+
param 'String', :json_string
18+
optional_param 'Any', :default
19+
end
20+
21+
def default_impl(json_string, default = nil)
22+
require 'puppet/util/json'
23+
24+
begin
25+
Puppet::Util::Json.load(json_string) || default
26+
rescue StandardError => err
27+
if default
28+
default
29+
else
30+
raise err
31+
end
32+
end
33+
end
34+
end

lib/puppet/parser/functions/load_module_metadata.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Puppet::Parser::Functions
1515
DOC
1616
) do |args|
1717
raise(Puppet::ParseError, 'load_module_metadata(): Wrong number of arguments, expects one or two') unless [1, 2].include?(args.size)
18+
19+
Puppet.deprecation_warning("`load_module_metadata' is deprecated, please use `stdlib::load_module_data' instead")
1820
mod = args[0]
1921
allow_empty_metadata = args[1]
2022
module_path = function_get_module_path([mod])

lib/puppet/parser/functions/loadjson.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module Puppet::Parser::Functions
2323
DOC
2424

2525
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1
26+
27+
Puppet.deprecation_warning("`loadjson' is deprecated, please use `stdlib::loadjson' instead")
2628
require 'open-uri'
2729
begin
2830
if args[0].start_with?('http://', 'https://')

lib/puppet/parser/functions/parsejson.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Puppet::Parser::Functions
1818
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
1919

2020
begin
21+
Puppet.deprecation_warning("`parsejson' is deprecated, please use `stdlib::parsejson' instead")
2122
PSON.load(arguments[0]) || arguments[1]
2223
rescue StandardError => e
2324
raise e unless arguments[1]

0 commit comments

Comments
 (0)