Puppet Function: :'cloudlib::httpyaml'

Defined in:
modules/cloudlib/lib/puppet/functions/cloudlib/httpyaml.rb
Function type:
Ruby 4.x API

Overview

:'cloudlib::httpyaml'(Hash $options, Puppet::LookupContext $context)Any

Parameters:

  • options (Hash)
  • context (Puppet::LookupContext)

Returns:

  • (Any)


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
# File 'modules/cloudlib/lib/puppet/functions/cloudlib/httpyaml.rb', line 1

Puppet::Functions.create_function(:'cloudlib::httpyaml') do
  require 'net/http'
  require 'uri'
  require 'yaml'
  dispatch :data_hash do
    param 'Hash', :options
    param 'Puppet::LookupContext', :context
  end
  def data_hash(options, context)
    uri = URI.parse(options['uri'])
    path = URI.escape(context.interpolate(uri.request_uri))
    if context.cache_has_key(path)
      context.explain { "Returning cached value for #{path}" }
      value = context.cached_value(path)
      return value
    else
      context.explain { "Querying #{uri}" }
      if context.cache_has_key('__http_handler')
        http_handler = context.cached_value('__http_handler')
      else
        http_handler = Net::HTTP.new(uri.host, uri.port)
        context.cache('__http_handler', http_handler)
      end
      request = Net::HTTP::Get.new(path)
      begin
        response = http_handler.request(request)
        body = YAML.safe_load(response.body)
        value = body.fetch('hiera', {})
        context.cache(path, value)
        return value
      rescue StandardError => e
        raise Puppet::DataBinding::LookupError, "cloudlib::httpyaml failed #{e.message}"
      end
    end
  end
end