X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Floadyaml.rb;h=f84f5300d33935a06f1572118e48287a9f5c37b0;hb=30caaa85aed7015ca0d77216bff175eebd917eb7;hp=9696362461283d67666fbdecdf46a08f2f40ff01;hpb=6963202b4b62c2816655ac9532521b018fdf83bd;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb index 969636246..f84f5300d 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb @@ -1,34 +1,61 @@ +# +# loadyaml.rb +# module Puppet::Parser::Functions - newfunction(:loadyaml, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args| -Load a YAML file containing an array, string, or hash, and return the data -in the corresponding native data type. -The second parameter is the default value. It will be returned if the file -was not found or could not be parsed. + newfunction(:loadyaml, :type => :rvalue, :arity => -2, :doc => <<-'DOC') do |args| + @summary + Load a YAML file containing an array, string, or hash, and return the data + in the corresponding native data type. -For example: + 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. - $myhash = loadyaml('/etc/puppet/data/myhash.yaml') - $myhash = loadyaml('no-file.yaml', {'default' => 'value'}) - ENDHEREDOC + @return [Array|String|Hash] + The data stored in the YAML file, the type depending on the type of data that was stored. + + @example Example Usage: + $myhash = loadyaml('/etc/puppet/data/myhash.yaml') + $myhash = loadyaml('https://example.local/my_hash.yaml') + $myhash = loadyaml('https://username:password@example.local/my_hash.yaml') + $myhash = loadyaml('no-file.yaml', {'default' => 'value'}) + DOC raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1 require 'yaml' - - if File.exists?(args[0]) - begin - YAML::load_file(args[0]) || args[1] - rescue Exception => e - if args[1] - args[1] + require 'open-uri' + begin + if args[0].start_with?('http://', 'https://') + username = '' + password = '' + if (match = args[0].match(%r{(http\://|https\://)(.*):(.*)@(.*)})) + # If URL is in the format of https://username:password@example.local/my_hash.yaml + protocol, username, password, path = match.captures + url = "#{protocol}#{path}" + elsif (match = args[0].match(%r{(http\:\/\/|https\:\/\/)(.*)@(.*)})) + # If URL is in the format of https://username@example.local/my_hash.yaml + protocol, username, path = match.captures + url = "#{protocol}#{path}" else - raise e + url = args[0] 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]}'") + args[1] + end + YAML.safe_load(contents) || args[1] + elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code + YAML.load_file(args[0]) || args[1] + else + warning("Can't load '#{args[0]}' File does not exist!") + args[1] end - else - warning("Can't load '#{args[0]}' File does not exist!") + rescue StandardError => e + raise e unless args[1] args[1] end - end - end