Puppet Function: :'wmflib::regex_data'

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

Overview

:'wmflib::regex_data'(String[1] $key, Hash $options, Puppet::LookupContext $context)Any

Parameters:

  • key (String[1])
  • options (Hash)
  • context (Puppet::LookupContext)

Returns:

  • (Any)


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'modules/wmflib/lib/puppet/functions/wmflib/regex_data.rb', line 3

Puppet::Functions.create_function(:'wmflib::regex_data') do
  dispatch :regex_data do
    param 'String[1]', :key
    param 'Hash', :options
    param 'Puppet::LookupContext', :context
  end

  def regex_data(key, options, context)
    return context.cached_value(key) if context.cache_has_key(key)
    path = options['path']
    node = options['node']
    data = load_data_hash(path, context)
    regex_lookup(key, node, data, context)
  end

  def load_data_hash(path, context)
    context.cached_file_data(path) do |content|
      begin
        # TODO: Use YAML.safe_load and manuly create our Regex objects
        data = YAML.load(content, path)
        if data.is_a?(Hash)
          Puppet::Pops::Lookup::HieraConfig.symkeys_to_string(data)
        else
          msg = format(_("%{path}: file does not contain a valid yaml hash"), path: path)
          raise Puppet::DataBinding::LookupError, msg if Puppet[:strict] == :error && data != false
          Puppet.warning(msg)
          {}
        end
      rescue YAML::SyntaxError => ex
        # YamlLoadErrors include the absolute path to the file, so no need to add that
        raise Puppet::DataBinding::LookupError, format(_("Unable to parse %{message}"), message: ex.message)
      end
    end
  end

  def regex_lookup(key, matchon, data, context)
    data.each do |label, datahash|
      r = datahash["__regex"]
      Puppet.debug("Scanning label #{label} for matches to '#{r}' in '#{matchon}' ")
      next unless r.match(matchon)
      Puppet.debug("Label #{label} matches; searching within it")
      next unless datahash.include?(key)
      return context.cache(key, context.interpolate(datahash[key]))
    end
    return context.not_found
  rescue => detail
    Puppet.debug(detail)
    return nil
  end
end