@@ -5,27 +5,38 @@ module Puppet::Parser::Functions
55 newfunction ( :loadyaml , :type => :rvalue , :arity => -2 , :doc => <<-'DOC' ) do |args |
66 Load a YAML file containing an array, string, or hash, and return the data
77 in the corresponding native data type.
8+ The first parameter can be a file path or a URL.
89 The second parameter is the default value. It will be returned if the file
910 was not found or could not be parsed.
1011
1112 For example:
1213
1314 $myhash = loadyaml('/etc/puppet/data/myhash.yaml')
15+ $myhash = loadyaml('https://example.local/my_hash.yaml')
1416 $myhash = loadyaml('no-file.yaml', {'default' => 'value'})
1517 DOC
1618
1719 raise ArgumentError , 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args . length >= 1
1820 require 'yaml'
19-
20- if File . exists? ( args [ 0 ] ) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
21- begin
21+ require 'open-uri'
22+ begin
23+ if args [ 0 ] . start_with? ( 'http://' , 'https://' )
24+ begin
25+ contents = OpenURI . open_uri ( args [ 0 ] )
26+ rescue OpenURI ::HTTPError => err
27+ res = err . io
28+ warning ( "Can't load '#{ args [ 0 ] } ' HTTP Error Code: '#{ res . status [ 0 ] } '" )
29+ args [ 1 ]
30+ end
31+ YAML . safe_load ( contents ) || args [ 1 ]
32+ elsif File . exists? ( args [ 0 ] ) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
2233 YAML . load_file ( args [ 0 ] ) || args [ 1 ]
23- rescue StandardError => e
24- raise e unless args [ 1 ]
34+ else
35+ warning ( "Can't load ' #{ args [ 0 ] } ' File does not exist!" )
2536 args [ 1 ]
2637 end
27- else
28- warning ( "Can't load ' #{ args [ 0 ] } ' File does not exist!" )
38+ rescue StandardError => e
39+ raise e unless args [ 1 ]
2940 args [ 1 ]
3041 end
3142 end
0 commit comments