Class: Puppet::Confine::Variable

Inherits:
Puppet::Confine show all
Defined in:
lib/puppet/confine/variable.rb

Overview

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.

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::RFC_3986_URI_REGEX

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Instance Attribute Summary collapse

Attributes inherited from Puppet::Confine

#for_binary, #label, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Puppet::Confine

#for_binary?, inherited, #result, test

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

#initialize(values) ⇒ Variable


24
25
26
27
# File 'lib/puppet/confine/variable.rb', line 24

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

Instance Attribute Details

#nameObject

This is set by ConfineCollection.


17
18
19
# File 'lib/puppet/confine/variable.rb', line 17

def name
  @name
end

Class Method Details

.summarize(confines) ⇒ Object

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.


11
12
13
14
# File 'lib/puppet/confine/variable.rb', line 11

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

Instance Method Details

#facter_valueObject

Retrieve the value from facter


20
21
22
# File 'lib/puppet/confine/variable.rb', line 20

def facter_value
  @facter_value ||= ::Facter.value(name).to_s.downcase
end

#message(value) ⇒ Object


29
30
31
# File 'lib/puppet/confine/variable.rb', line 29

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

#pass?(value) ⇒ Boolean

Compare the passed-in value to the retrieved value.


34
35
36
# File 'lib/puppet/confine/variable.rb', line 34

def pass?(value)
  test_value.downcase.to_s == value.to_s.downcase
end

#resetObject


38
39
40
41
42
# File 'lib/puppet/confine/variable.rb', line 38

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

#valid?Boolean


44
45
46
47
48
# File 'lib/puppet/confine/variable.rb', line 44

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