Sha256: 9582ec657b81130c5d59c4b3b8aa97ecf0c575f9f4a5f353f9d0f3fbf5c8cbbe

Contents?: true

Size: 1.62 KB

Versions: 24

Compression:

Stored size: 1.62 KB

Contents

require 'puppet/confine'

# Require a specific value for a variable, either a Puppet setting
# or a Facter value.  This class is a bit weird because the name
# is set explicitly by the ConfineCollection class -- from this class,
# it's not obvious how the name would ever get set.
class Puppet::Confine::Variable < Puppet::Confine
  # Provide a hash summary of failing confines -- the key of the hash
  # is the name of the confine, and the value is the missing yet required values.
  # Only returns failed values, not all required values.
  def self.summarize(confines)
    result = Hash.new { |hash, key| hash[key] = [] }
    confines.inject(result) { |total, confine| total[confine.name] += confine.values unless confine.valid?; total }
  end

  # This is set by ConfineCollection.
  attr_accessor :name

  # Retrieve the value from facter
  def facter_value
    @facter_value ||= Puppet.runtime[:facter].value(name).to_s.downcase
  end

  def initialize(values)
    super
    @values = @values.collect { |v| v.to_s.downcase }
  end

  def message(value)
    "facter value '#{test_value}' for '#{self.name}' not in required list '#{values.join(",")}'"
  end

  # Compare the passed-in value to the retrieved value.
  def pass?(value)
    test_value.downcase.to_s == value.to_s.downcase
  end

  def reset
    # Reset the cache.  We want to cache it during a given
    # run, but not across runs.
    @facter_value = nil
  end

  def valid?
    @values.include?(test_value.to_s.downcase)
  ensure
    reset
  end

  private

  def setting?
    Puppet.settings.valid?(name)
  end

  def test_value
    setting? ? Puppet.settings[name] : facter_value
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
puppet-6.29.0 lib/puppet/confine/variable.rb
puppet-6.29.0-x86-mingw32 lib/puppet/confine/variable.rb
puppet-6.29.0-x64-mingw32 lib/puppet/confine/variable.rb
puppet-6.29.0-universal-darwin lib/puppet/confine/variable.rb
puppet-6.28.0 lib/puppet/confine/variable.rb
puppet-6.28.0-x86-mingw32 lib/puppet/confine/variable.rb
puppet-6.28.0-x64-mingw32 lib/puppet/confine/variable.rb
puppet-6.28.0-universal-darwin lib/puppet/confine/variable.rb
puppet-6.27.0 lib/puppet/confine/variable.rb
puppet-6.27.0-x86-mingw32 lib/puppet/confine/variable.rb
puppet-6.27.0-x64-mingw32 lib/puppet/confine/variable.rb
puppet-6.27.0-universal-darwin lib/puppet/confine/variable.rb
puppet-6.26.0 lib/puppet/confine/variable.rb
puppet-6.26.0-x86-mingw32 lib/puppet/confine/variable.rb
puppet-6.26.0-x64-mingw32 lib/puppet/confine/variable.rb
puppet-6.26.0-universal-darwin lib/puppet/confine/variable.rb
puppet-6.25.1 lib/puppet/confine/variable.rb
puppet-6.25.1-x86-mingw32 lib/puppet/confine/variable.rb
puppet-6.25.1-x64-mingw32 lib/puppet/confine/variable.rb
puppet-6.25.1-universal-darwin lib/puppet/confine/variable.rb