require 'puppet/util/node_groups'

module Puppet::Parser::Functions
  newfunction(:node_groups, :type => :rvalue) do |args|
    node_name = args[0]
    raise ArgumentError, 'Function accepts a single String' unless (
      args.length == 0 or
      ( args.length == 1 and node_name.is_a?(String) )
    )

    ng     = Puppet::Util::Node_groups.new
    groups = ng.groups.get_groups

    # When querying a specific group
    if args.length == 1
      # Assuming there is only one group by the name
      Puppet::Util::Node_groups.hashify_group_array(
        groups.select { |g| g['name'] == node_name }
      )
    else
      Puppet::Util::Node_groups.hashify_group_array(groups)
    end
  end
end

begin
  require 'puppet/util/node_groups'
rescue LoadError
  mod = Puppet::Module.find('node_manager', Puppet[:environment].to_s)
  require File.join mod.path, 'lib/puppet/util/node_groups'
end

module Puppet::Parser::Functions
  newfunction(:node_groups, :type => :rvalue) do |args|
    node_name = args[0]
    raise ArgumentError, 'Function accepts a single String' unless (
      args.length == 0 or
      ( args.length == 1 and node_name.is_a?(String) )
    )

    ng     = Puppet::Util::Node_groups.new
    groups = ng.groups.get_groups

    # When querying a specific group
    if args.length == 1
      # Assuming there is only one group by the name
      Puppet::Util::Node_groups.hashify_group_array(
        groups.select { |g| g['name'] == node_name }
      )
    else
      Puppet::Util::Node_groups.hashify_group_array(groups)
    end
  end
end

Make library require statement more robust
Make library require statement more robust

Previously, there seemed to be some weird edge conditions under which
puppet would return a load error, e.g. if running in `apply` mode, while
at the same time would work correctly when running under puppetserver.
This commit implements a begin/rescue pattern that is used in several
other Puppet modules on the Forge to successfully load the library in
all known circumstances.
