Skip to content

(wip) use json, not pson, steal Bens work to make pup4 #1087

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

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 41 additions & 0 deletions lib/puppet/functions/stdlib/load_module_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# @summary
# This function loads the metadata of a given module.
#
# @example Example Usage:
# $metadata = load_module_metadata('archive')
# notify { $metadata['author']: }
#
# @return
# The modules metadata
#
Puppet::Functions.create_function(:'stdlib::load_module_metadata') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
param 'String', :module_name
optional_param 'Bool', :allow_empty_metadata
end

def default_impl(module_name, allow_empty_metadata)
module_path = function_get_module_path([module_name])
metadata_json = File.join(module_path, 'metadata.json')

if File.exists?(metadata_json)
return Puppet::Util::Json.load(File.read(metadata_json))

else
if !allow_empty_metadata
raise(Puppet::ParseError,
"load_module_metadata(): No metadata.json file for module #{module_name}")
end

return Hash.new
end
end
end
93 changes: 93 additions & 0 deletions lib/puppet/functions/stdlib/loadjson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# @summary
# Load a JSON file containing an array, string, or hash, and return the data
# in the corresponding native data type.
#
# The first parameter can be a file path or a URL.
# The second parameter is the default value. It will be returned if the file
# was not found or could not be parsed.
#
# @return [Array|String|Hash]
# The data stored in the JSON file, the type depending on the type of data that was stored.
#
# @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
require 'puppet/util/json'

# If URL is in the format of https://username:password@example.local/my_info.json
URI_WITH_NAME_AND_PASS_PATTERN = %r{(http\://|https\://)(.*):(.*)@(.*)}

# If URL is in the format of https://username@example.local/my_info.json
URI_WITH_NAME_PATTERN = %r{(http\://|https\://)(.*)@(.*)}

dispatch :load_from_url do
param 'Variant[Stdlib::HttpUrl, Stdlib::HttpsUrl]', :url
optional_param Any, :default
end

dispatch :load_local do
param 'Stdlib::Absolutepath', :path
optional_param Any, :default
end

def with_error_or_default(default = nil, &block)
begin
yield
rescue StandardError => err
if default
default
else
raise err
end
end
end

def load_local(path, default)
with_error_or_default(default) do
if File.exists?(path)
content = File.read(path)

Puppet::Util::Json.load(content) || default
else
warning("Can't load '#{path}' File does not exist!")

default
end
end
end

def load_from_url(url, default)
with_error_or_default(default) do
require 'open-uri'

username = ''
password = ''
if (match = url.match(URI_WITH_NAME_AND_PASS_PATTERN))
protocol, username, password, path = match.captures
url = "#{protocol}#{path}"
elsif (match = url.match(URI_WITH_NAME_PATTERN))
protocol, username, path = match.captures
url = "#{protocol}#{path}"
else
url = url
end

begin
contents = OpenURI.open_uri(url, :http_basic_authentication => [username, password])
rescue OpenURI::HTTPError => err
res = err.io
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")

default
end

Puppet::Util::Json.load(contents) || default
end
end
end
34 changes: 34 additions & 0 deletions lib/puppet/functions/stdlib/parsejson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# @summary
# This function accepts JSON as a string and converts it into the correct
# Puppet structure.
#
# @return
# convert JSON into Puppet structure
#
# > *Note:*
# The optional second argument can be used to pass a default value that will
# be returned if the parsing of JSON string have failed.
#
#
Puppet::Functions.create_function(:'stdlib::parsejson') do

dispatch :default_impl do
param 'String', :json_string
optional_param 'Any', :default
end

def default_impl(json_string, default = nil)
require 'puppet/util/json'

begin
Puppet::Util::Json.load(json_string) || default
rescue StandardError => err
if default
default
else
raise err
end
end
end
end
2 changes: 2 additions & 0 deletions lib/puppet/parser/functions/load_module_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Puppet::Parser::Functions
DOC
) do |args|
raise(Puppet::ParseError, 'load_module_metadata(): Wrong number of arguments, expects one or two') unless [1, 2].include?(args.size)

Puppet.deprecation_warning("`load_module_metadata' is deprecated, please use `stdlib::load_module_data' instead")
mod = args[0]
allow_empty_metadata = args[1]
module_path = function_get_module_path([mod])
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/parser/functions/loadjson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Puppet::Parser::Functions
DOC

raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1

Puppet.deprecation_warning("`loadjson' is deprecated, please use `stdlib::loadjson' instead")
require 'open-uri'
begin
if args[0].start_with?('http://', 'https://')
Expand Down
1 change: 1 addition & 0 deletions lib/puppet/parser/functions/parsejson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Puppet::Parser::Functions
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1

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