<commit_before>require 'puppet/face'
Puppet::Parser::Functions.newfunction(:query_resource, :type => :rvalue, :doc => <<-EOT

  accepts two arguments, a query used to discover nodes, and a filter used to
  specify the resources that should be returned from those hosts.

  The query specified should conform to the following format:
    (Type[title] and fact_name<operator>fact_value) or ...
    Package[mysql-server] and cluster_id=my_first_cluster

  The filter provided should be a comma delimited list of resource references.

EOT
) do |args|
  filter, query = args
  query ||= ''
  # check that the filter is only one resource
  raise(Puppet::Error, 'Must specify at least one argument (resource to query for)') unless filter
  result = Puppet::Face[:query, :current].resource(:query => query, :filter => filter)
end<commit_msg>Fail if query_resource returns multiple resources<commit_after>require 'puppet/face'
Puppet::Parser::Functions.newfunction(:query_resource, :type => :rvalue, :doc => <<-EOT

  accepts two arguments, a query used to discover nodes, and a filter used to
  specify the resources that should be returned from those hosts.

  The query specified should conform to the following format:
    (Type[title] and fact_name<operator>fact_value) or ...
    Package[mysql-server] and cluster_id=my_first_cluster

  The filter provided should be a comma delimited list of resource references.

EOT
) do |args|
  filter, query = args
  query ||= ''
  # check that the filter is only one resource
  raise(Puppet::Error, 'Must specify at least one argument (resource to query for)') unless filter
  result = Puppet::Face[:query, :current].resource(:query => query, :filter => filter)
  raise(Puppet::Error, 'Function should only return one resource') if result.size > 1
  {result.keys.first => result.values.first.first}
end
{'fraction_non_alphanumeric': 0.09787928221859707, 'fraction_numerical': 0.000543773790103317, 'mean_word_length': 4.786163522012578, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6192944', 'n_tokens_mistral': 522, 'n_tokens_neox': 514, 'n_words': 204}